Skip to content

Commit

Permalink
macOS debug rustcoreutils#2
Browse files Browse the repository at this point in the history
  • Loading branch information
Joseph Rafael Ferrer committed May 25, 2024
1 parent e354618 commit 03cbda8
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 70 deletions.
18 changes: 2 additions & 16 deletions tree/src/rm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(&current_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
Expand Down Expand Up @@ -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) => {
Expand Down
138 changes: 84 additions & 54 deletions tree/tests/rm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down

0 comments on commit 03cbda8

Please sign in to comment.