diff --git a/tree/src/rm.rs b/tree/src/rm.rs index 7d9d7574..8caa9667 100644 --- a/tree/src/rm.rs +++ b/tree/src/rm.rs @@ -349,21 +349,7 @@ fn rm_directory(cfg: &RmConfig, filepath: &Path, metadata: &fs::Metadata) -> io: }; for entry in read_dir_iterator { - let entry = match entry { - Ok(n) => n, - Err(e) => { - eprintln!( - "rm: {}", - gettext!( - "cannot remove '{}': {}", - display_cleaned(¤t_path), - error_string(&e) - ) - ); - success = false; - continue; - } - }; + let entry = entry?; let subname = entry.file_name(); // Need to manually keep track of visited files. The alternative of @@ -435,7 +421,7 @@ fn rm_directory(cfg: &RmConfig, filepath: &Path, metadata: &fs::Metadata) -> io: // Ascend up a level no matter the return value of the previous // `fs::symlink_metadata` - std::env::set_current_dir("..")?; + std::env::set_current_dir("..").expect("msg"); match dir_metadata { Ok(md) => { diff --git a/tree/tests/rm/mod.rs b/tree/tests/rm/mod.rs index 8921996d..cbacca8a 100644 --- a/tree/tests/rm/mod.rs +++ b/tree/tests/rm/mod.rs @@ -708,88 +708,118 @@ fn test_rm_r_root() { // ln -sr / rootlink3 unix::fs::symlink(relpath_to_root, rootlink3).unwrap(); + for arg in [ + String::from("/"), + String::from("//"), + String::from("///"), + String::from("////"), + format!("{rootlink}/"), + format!("{rootlink2}/"), + format!("{rootlink3}/"), + ] { + rm_test( + &["-r", &arg], + "", + "rm: it is dangerous to operate recursively on '/'\n", + 1, + ); + } + + for arg in [ + String::from("//."), + String::from("/./"), + String::from("/.//"), + String::from("/../"), + String::from("/.././"), + format!("{rootlink}/.."), + format!("{rootlink2}/."), + format!("{rootlink3}/./"), + ] { + let cleaned = if arg.ends_with("//") { + arg.strip_suffix("/").unwrap() + } else { + &arg + }; + rm_test( + &["-r", &arg], + "", + &format!("rm: refusing to remove '.' or '..' directory: skipping '{cleaned}'\n"), + 1, + ); + } + rm_test( - &["-r", "/"], + &["-r", "//."], "", - "rm: it is dangerous to operate recursively on '/'\n", + &format!( + "rm: refusing to remove '.' or '..' directory: skipping '{}'\n", + "//." + ), 1, ); rm_test( - &["-r", "//"], + &["-r", "/./"], "", - "rm: it is dangerous to operate recursively on '/'\n", + &format!( + "rm: refusing to remove '.' or '..' directory: skipping '{}'\n", + "/./" + ), 1, ); rm_test( - &["-r", "///"], + &["-r", "/.//"], "", - "rm: it is dangerous to operate recursively on '/'\n", + &format!( + "rm: refusing to remove '.' or '..' directory: skipping '{}'\n", + "/./" + ), 1, ); rm_test( - &["-r", "////"], + &["-r", "/../"], "", - "rm: it is dangerous to operate recursively on '/'\n", + &format!( + "rm: refusing to remove '.' or '..' directory: skipping '{}'\n", + "/../" + ), 1, ); rm_test( - &["-r", &format!("{rootlink}/")], + &["-r", "/.././"], "", - "rm: it is dangerous to operate recursively on '/'\n", + &format!( + "rm: refusing to remove '.' or '..' directory: skipping '{}'\n", + "/.././" + ), 1, ); rm_test( - &["-r", &format!("{rootlink2}/")], + &["-r", &format!("{rootlink}/..")], "", - "rm: it is dangerous to operate recursively on '/'\n", + &format!( + "rm: refusing to remove '.' or '..' directory: skipping '{}'\n", + format!("{rootlink}/..") + ), 1, ); rm_test( - &["-r", &format!("{rootlink3}/")], + &["-r", &format!("{rootlink2}/.")], "", - "rm: it is dangerous to operate recursively on '/'\n", + &format!( + "rm: refusing to remove '.' or '..' directory: skipping '{}'\n", + format!("{rootlink2}/.") + ), + 1, + ); + rm_test( + &["-r", &format!("{rootlink3}/./")], + "", + &format!( + "rm: refusing to remove '.' or '..' directory: skipping '{}'\n", + format!("{rootlink3}/./") + ), 1, ); - - // for arg in [ - // String::from("/"), - // String::from("//"), - // String::from("///"), - // String::from("////"), - // format!("{rootlink}/"), - // format!("{rootlink2}/"), - // format!("{rootlink3}/"), - // ] { - // rm_test( - // &["-r", &arg], - // "", - // "rm: it is dangerous to operate recursively on '/'\n", - // 1, - // ); - // } - - // for arg in [ - // String::from("//."), - // String::from("/./"), - // String::from("/.//"), - // String::from("/../"), - // String::from("/.././"), - // format!("{rootlink}/.."), - // format!("{rootlink2}/."), - // format!("{rootlink3}/./"), - // ] { - // let cleaned = if arg.ends_with("//") { - // arg.strip_suffix("/").unwrap() - // } else { - // &arg - // }; - // rm_test( - // &["-r", &arg], - // "", - // &format!("rm: refusing to remove '.' or '..' directory: skipping '{cleaned}'\n"), - // 1, - // ); - // } fs::remove_dir_all(test_dir).unwrap(); }