mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 11:37:44 +00:00
rm: Remove invalid symlinks
Checking with file.exists() was not good for this purpose as Path::exists() returns false for invalid symlinks.
This commit is contained in:
parent
5f1cb07df8
commit
416c2b7f89
2 changed files with 21 additions and 13 deletions
24
src/rm/rm.rs
24
src/rm/rm.rs
|
@ -128,16 +128,8 @@ fn remove(files: Vec<String>, force: bool, interactive: InteractiveMode, one_fs:
|
||||||
for filename in &files {
|
for filename in &files {
|
||||||
let filename = &filename[..];
|
let filename = &filename[..];
|
||||||
let file = Path::new(filename);
|
let file = Path::new(filename);
|
||||||
if file.exists() {
|
match file.symlink_metadata() {
|
||||||
let is_dir = match file.symlink_metadata() {
|
Ok(metadata) => if metadata.is_dir() {
|
||||||
Ok(metadata) => metadata.is_dir(),
|
|
||||||
Err(e) => {
|
|
||||||
had_err = true;
|
|
||||||
show_error!("could not read metadata of '{}': {}", filename, e);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
if is_dir {
|
|
||||||
if recursive && (filename != "/" || !preserve_root) {
|
if recursive && (filename != "/" || !preserve_root) {
|
||||||
if interactive != InteractiveMode::InteractiveAlways {
|
if interactive != InteractiveMode::InteractiveAlways {
|
||||||
if let Err(e) = fs::remove_dir_all(file) {
|
if let Err(e) = fs::remove_dir_all(file) {
|
||||||
|
@ -214,10 +206,16 @@ fn remove(files: Vec<String>, force: bool, interactive: InteractiveMode, one_fs:
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
had_err = remove_file(&file, interactive, verbose).bitor(had_err);
|
had_err = remove_file(&file, interactive, verbose).bitor(had_err);
|
||||||
|
},
|
||||||
|
Err(e) => {
|
||||||
|
// TODO: When the error is not about missing files
|
||||||
|
// (e.g., permission), even rm -f should fail with
|
||||||
|
// outputting the error, but there's no easy eay.
|
||||||
|
if !force {
|
||||||
|
had_err = true;
|
||||||
|
show_error!("no such file or directory '{}'", filename);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else if !force {
|
|
||||||
show_error!("no such file or directory '{}'", filename);
|
|
||||||
had_err = true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -148,3 +148,13 @@ fn test_rm_dir_symlink() {
|
||||||
|
|
||||||
ucmd.arg(link).succeeds();
|
ucmd.arg(link).succeeds();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_rm_invalid_symlink() {
|
||||||
|
let (at, mut ucmd) = at_and_ucmd!();
|
||||||
|
let link = "test_rm_invalid_symlink";
|
||||||
|
|
||||||
|
at.symlink(link, link);
|
||||||
|
|
||||||
|
ucmd.arg(link).succeeds();
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue