Skip to content

Commit

Permalink
Fix windows build
Browse files Browse the repository at this point in the history
  • Loading branch information
AnirbanHalder654322 committed Sep 9, 2024
1 parent b3d348c commit 1ba9c88
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
18 changes: 14 additions & 4 deletions src/uu/rm/src/rm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,11 +413,16 @@ fn handle_dir(path: &Path, options: &Options) -> bool {
had_err = remove_dir(path, options).bitor(had_err);
} else if options.recursive {
if is_root || !options.preserve_root {
show_error!("it is dangerous to operate recursively on '/'");
show_error!(
"it is dangerous to operate recursively on '{}'",
MAIN_SEPARATOR
);
show_error!("use --no-preserve-root to override this failsafe");
had_err = true;
} else {
show_error!("could not remove directory {}", path.quote());
had_err = true;

Check warning on line 424 in src/uu/rm/src/rm.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/rm/src/rm.rs#L423-L424

Added lines #L423 - L424 were not covered by tests
}
show_error!("could not remove directory {}", path.quote());
had_err = true;
} else {
show_error!(
"cannot remove {}: Is a directory", // GNU's rm error message does not include help
Expand Down Expand Up @@ -623,7 +628,6 @@ fn handle_writable_directory(path: &Path, options: &Options, metadata: &Metadata
/// Removes trailing slashes, for example 'd/../////' yield 'd/../' required to fix rm-r4 GNU test
fn clean_trailing_slashes(path: &Path) -> &Path {
let path_str = os_str_as_bytes(path.as_os_str());

let dir_separator = MAIN_SEPARATOR as u8;

if let Ok(path_bytes) = path_str {
Expand All @@ -643,7 +647,13 @@ fn clean_trailing_slashes(path: &Path) -> &Path {
break;
}
}
#[cfg(unix)]
return Path::new(OsStr::from_bytes(&path_bytes[0..=idx]));

#[cfg(not(unix))]
// Unwrapping is fine here as os_str_as_bytes() would return an error on non unix
// systems with non utf-8 characters and thus bypass the if let Ok branch
return Path::new(std::str::from_utf8(&path_bytes[0..=idx]).unwrap());
}
}
path
Expand Down
6 changes: 4 additions & 2 deletions tests/by-util/test_rm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,12 +323,14 @@ fn test_rm_no_operand() {
fn test_rm_only_slashes_acting_as_root() {
// We are testing the path '//////' which will be cleaned to be '/'.

let str_slashes = std::str::from_utf8(&[std::path::MAIN_SEPARATOR as u8; 6]).unwrap();

let ts = TestScenario::new(util_name!());
ts.ucmd()
.arg("-r")
.arg("//////")
.arg(str_slashes)
.fails()
.stderr_contains("it is dangerous to operate recursively on '/'");
.stderr_contains("it is dangerous to operate recursively on");
}

#[test]
Expand Down

0 comments on commit 1ba9c88

Please sign in to comment.