Skip to content

Commit fd9df17

Browse files
Fix windows build
1 parent b3d348c commit fd9df17

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

src/uu/rm/src/rm.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -413,11 +413,15 @@ fn handle_dir(path: &Path, options: &Options) -> bool {
413413
had_err = remove_dir(path, options).bitor(had_err);
414414
} else if options.recursive {
415415
if is_root || !options.preserve_root {
416-
show_error!("it is dangerous to operate recursively on '/'");
416+
show_error!(
417+
"it is dangerous to operate recursively on '{}'",
418+
MAIN_SEPARATOR
419+
);
417420
show_error!("use --no-preserve-root to override this failsafe");
421+
} else {
422+
show_error!("could not remove directory {}", path.quote());
423+
had_err = true;
418424
}
419-
show_error!("could not remove directory {}", path.quote());
420-
had_err = true;
421425
} else {
422426
show_error!(
423427
"cannot remove {}: Is a directory", // GNU's rm error message does not include help
@@ -623,7 +627,6 @@ fn handle_writable_directory(path: &Path, options: &Options, metadata: &Metadata
623627
/// Removes trailing slashes, for example 'd/../////' yield 'd/../' required to fix rm-r4 GNU test
624628
fn clean_trailing_slashes(path: &Path) -> &Path {
625629
let path_str = os_str_as_bytes(path.as_os_str());
626-
627630
let dir_separator = MAIN_SEPARATOR as u8;
628631

629632
if let Ok(path_bytes) = path_str {
@@ -643,7 +646,13 @@ fn clean_trailing_slashes(path: &Path) -> &Path {
643646
break;
644647
}
645648
}
649+
#[cfg(unix)]
646650
return Path::new(OsStr::from_bytes(&path_bytes[0..=idx]));
651+
652+
#[cfg(not(unix))]
653+
// Unwrapping is fine here as os_str_as_bytes() would return an error on non unix
654+
// systems with non utf-8 characters and thus bypass the if let Ok branch
655+
return Path::new(std::str::from_utf8(&path_bytes[0..=idx]).unwrap());
647656
}
648657
}
649658
path

tests/by-util/test_rm.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,12 +323,14 @@ fn test_rm_no_operand() {
323323
fn test_rm_only_slashes_acting_as_root() {
324324
// We are testing the path '//////' which will be cleaned to be '/'.
325325

326+
let str_slashes = std::str::from_utf8(&[std::path::MAIN_SEPARATOR as u8; 6]).unwrap();
327+
326328
let ts = TestScenario::new(util_name!());
327329
ts.ucmd()
328330
.arg("-r")
329-
.arg("//////")
331+
.arg(str_slashes)
330332
.fails()
331-
.stderr_contains("it is dangerous to operate recursively on '/'");
333+
.stderr_contains("it is dangerous to operate recursively on");
332334
}
333335

334336
#[test]

0 commit comments

Comments
 (0)