Skip to content

Commit

Permalink
macOS debug rustcoreutils#1
Browse files Browse the repository at this point in the history
  • Loading branch information
Joseph Rafael Ferrer committed May 25, 2024
1 parent 14f85cd commit e354618
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 39 deletions.
16 changes: 15 additions & 1 deletion tree/src/rm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,21 @@ fn rm_directory(cfg: &RmConfig, filepath: &Path, metadata: &fs::Metadata) -> io:
};

for entry in read_dir_iterator {
let entry = entry?;
let entry = match entry {
Ok(n) => n,
Err(e) => {
eprintln!(
"rm: {}",
gettext!(
"cannot remove '{}': {}",
display_cleaned(&current_path),
error_string(&e)
)
);
success = false;
continue;
}
};
let subname = entry.file_name();

// Need to manually keep track of visited files. The alternative of
Expand Down
119 changes: 81 additions & 38 deletions tree/tests/rm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -708,45 +708,88 @@ 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,
);
}
rm_test(
&["-r", "/"],
"",
"rm: it is dangerous to operate recursively on '/'\n",
1,
);
rm_test(
&["-r", "//"],
"",
"rm: it is dangerous to operate recursively on '/'\n",
1,
);
rm_test(
&["-r", "///"],
"",
"rm: it is dangerous to operate recursively on '/'\n",
1,
);
rm_test(
&["-r", "////"],
"",
"rm: it is dangerous to operate recursively on '/'\n",
1,
);
rm_test(
&["-r", &format!("{rootlink}/")],
"",
"rm: it is dangerous to operate recursively on '/'\n",
1,
);
rm_test(
&["-r", &format!("{rootlink2}/")],
"",
"rm: it is dangerous to operate recursively on '/'\n",
1,
);
rm_test(
&["-r", &format!("{rootlink3}/")],
"",
"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,
);
}
// 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();
}
Expand Down

0 comments on commit e354618

Please sign in to comment.