Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/uu/rm/src/rm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -851,7 +851,9 @@ fn path_is_current_or_parent_directory(path: &Path) -> bool {
let dir_separator = MAIN_SEPARATOR as u8;
if let Ok(path_bytes) = path_str {
return path_bytes == ([b'.'])
|| path_bytes == ([b'.', dir_separator])
|| path_bytes == ([b'.', b'.'])
|| path_bytes == ([b'.', b'.', dir_separator])
|| path_bytes.ends_with(&[dir_separator, b'.'])
|| path_bytes.ends_with(&[dir_separator, b'.', b'.'])
|| path_bytes.ends_with(&[dir_separator, b'.', dir_separator])
Expand Down
37 changes: 37 additions & 0 deletions tests/by-util/test_rm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -767,12 +767,22 @@ fn test_current_or_parent_dir_rm4() {

at.mkdir("d");

let file_1 = "file1";
let file_2 = "d/file2";

at.touch(file_1);
at.touch(file_2);

let answers = [
"rm: refusing to remove '.' or '..' directory: skipping 'd/.'",
"rm: refusing to remove '.' or '..' directory: skipping 'd/./'",
"rm: refusing to remove '.' or '..' directory: skipping 'd/./'",
"rm: refusing to remove '.' or '..' directory: skipping 'd/..'",
"rm: refusing to remove '.' or '..' directory: skipping 'd/../'",
"rm: refusing to remove '.' or '..' directory: skipping '.'",
"rm: refusing to remove '.' or '..' directory: skipping './'",
"rm: refusing to remove '.' or '..' directory: skipping '../'",
"rm: refusing to remove '.' or '..' directory: skipping '..'",
];
let std_err_str = ts
.ucmd()
Expand All @@ -782,12 +792,20 @@ fn test_current_or_parent_dir_rm4() {
.arg("d/.////")
.arg("d/..")
.arg("d/../")
.arg(".")
.arg("./")
.arg("../")
.arg("..")
.fails()
.stderr_move_str();

for (idx, line) in std_err_str.lines().enumerate() {
assert_eq!(line, answers[idx]);
}
// checks that no file was silently removed
assert!(at.dir_exists("d"));
assert!(at.file_exists(file_1));
assert!(at.file_exists(file_2));
}

#[test]
Expand All @@ -798,12 +816,22 @@ fn test_current_or_parent_dir_rm4_windows() {

at.mkdir("d");

let file_1 = "file1";
let file_2 = "d/file2";

at.touch(file_1);
at.touch(file_2);

let answers = [
"rm: refusing to remove '.' or '..' directory: skipping 'd\\.'",
"rm: refusing to remove '.' or '..' directory: skipping 'd\\.\\'",
"rm: refusing to remove '.' or '..' directory: skipping 'd\\.\\'",
"rm: refusing to remove '.' or '..' directory: skipping 'd\\..'",
"rm: refusing to remove '.' or '..' directory: skipping 'd\\..\\'",
"rm: refusing to remove '.' or '..' directory: skipping '.'",
"rm: refusing to remove '.' or '..' directory: skipping '.\\'",
"rm: refusing to remove '.' or '..' directory: skipping '..'",
"rm: refusing to remove '.' or '..' directory: skipping '..\\'",
];
let std_err_str = ts
.ucmd()
Expand All @@ -813,12 +841,21 @@ fn test_current_or_parent_dir_rm4_windows() {
.arg("d\\.\\\\\\\\")
.arg("d\\..")
.arg("d\\..\\")
.arg(".")
.arg(".\\")
.arg("..")
.arg("..\\")
.fails()
.stderr_move_str();

for (idx, line) in std_err_str.lines().enumerate() {
assert_eq!(line, answers[idx]);
}

// checks that no file was silently removed
assert!(at.dir_exists("d"));
assert!(at.file_exists(file_1));
assert!(at.file_exists(file_2));
}

#[test]
Expand Down
Loading