Skip to content

Commit 99d4fbd

Browse files
authored
Merge pull request #7325 from jfinkels/rm-prompt-message-inaccessible
rm: correct prompt for removing inaccessible dir
2 parents 447b087 + 2b531b7 commit 99d4fbd

File tree

2 files changed

+44
-20
lines changed

2 files changed

+44
-20
lines changed

src/uu/rm/src/rm.rs

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -339,15 +339,18 @@ fn is_dir_empty(path: &Path) -> bool {
339339
}
340340
}
341341

342+
#[cfg(unix)]
343+
fn is_readable_metadata(metadata: &Metadata) -> bool {
344+
let mode = metadata.permissions().mode();
345+
(mode & 0o400) > 0
346+
}
347+
342348
/// Whether the given file or directory is readable.
343349
#[cfg(unix)]
344350
fn is_readable(path: &Path) -> bool {
345351
match std::fs::metadata(path) {
346352
Err(_) => false,
347-
Ok(metadata) => {
348-
let mode = metadata.permissions().mode();
349-
(mode & 0o400) > 0
350-
}
353+
Ok(metadata) => is_readable_metadata(&metadata),
351354
}
352355
}
353356

@@ -357,15 +360,18 @@ fn is_readable(_path: &Path) -> bool {
357360
true
358361
}
359362

363+
#[cfg(unix)]
364+
fn is_writable_metadata(metadata: &Metadata) -> bool {
365+
let mode = metadata.permissions().mode();
366+
(mode & 0o200) > 0
367+
}
368+
360369
/// Whether the given file or directory is writable.
361370
#[cfg(unix)]
362371
fn is_writable(path: &Path) -> bool {
363372
match std::fs::metadata(path) {
364373
Err(_) => false,
365-
Ok(metadata) => {
366-
let mode = metadata.permissions().mode();
367-
(mode & 0o200) > 0
368-
}
374+
Ok(metadata) => is_writable_metadata(&metadata),
369375
}
370376
}
371377

@@ -623,20 +629,25 @@ fn prompt_file_permission_readonly(path: &Path) -> bool {
623629
// Most cases are covered by keep eye out for edge cases
624630
#[cfg(unix)]
625631
fn handle_writable_directory(path: &Path, options: &Options, metadata: &Metadata) -> bool {
626-
use std::os::unix::fs::PermissionsExt;
627-
let mode = metadata.permissions().mode();
628-
// Check if directory has user write permissions
629-
// Why is S_IWUSR showing up as a u16 on macos?
630-
#[allow(clippy::unnecessary_cast)]
631-
let user_writable = (mode & (libc::S_IWUSR as u32)) != 0;
632-
if !user_writable {
633-
prompt_yes!("remove write-protected directory {}?", path.quote())
634-
} else if options.interactive == InteractiveMode::Always {
635-
prompt_yes!("remove directory {}?", path.quote())
636-
} else {
637-
true
632+
match (
633+
is_readable_metadata(metadata),
634+
is_writable_metadata(metadata),
635+
options.interactive,
636+
) {
637+
(false, false, _) => prompt_yes!(
638+
"attempt removal of inaccessible directory {}?",
639+
path.quote()
640+
),
641+
(false, true, InteractiveMode::Always) => prompt_yes!(
642+
"attempt removal of inaccessible directory {}?",
643+
path.quote()
644+
),
645+
(true, false, _) => prompt_yes!("remove write-protected directory {}?", path.quote()),
646+
(_, _, InteractiveMode::Always) => prompt_yes!("remove directory {}?", path.quote()),
647+
(_, _, _) => true,
638648
}
639649
}
650+
640651
/// Checks if the path is referring to current or parent directory , if it is referring to current or any parent directory in the file tree e.g '/../..' , '../..'
641652
fn path_is_current_or_parent_directory(path: &Path) -> bool {
642653
let path_str = os_str_as_bytes(path.as_os_str());

tests/by-util/test_rm.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -874,6 +874,19 @@ fn test_inaccessible_dir_nonempty() {
874874
assert!(at.dir_exists("dir"));
875875
}
876876

877+
#[cfg(not(windows))]
878+
#[test]
879+
fn test_inaccessible_dir_interactive() {
880+
let (at, mut ucmd) = at_and_ucmd!();
881+
at.mkdir("dir");
882+
at.set_mode("dir", 0);
883+
ucmd.args(&["-i", "-d", "dir"])
884+
.pipe_in("y\n")
885+
.succeeds()
886+
.stderr_only("rm: attempt removal of inaccessible directory 'dir'? ");
887+
assert!(!at.dir_exists("dir"));
888+
}
889+
877890
#[cfg(not(windows))]
878891
#[test]
879892
fn test_inaccessible_dir_recursive() {

0 commit comments

Comments
 (0)