Skip to content

Commit 0649270

Browse files
committed
fix(linux/rm): correct prompting logic for write-protected files in Interactive::Always mode
Refactor the prompt_file_with_stat function in src/uu/rm/src/platform/linux.rs to fix inconsistent prompting for Interactive::Always. Previously, it always used non-protected wording regardless of file writability. Now, it checks if the file is writable and uses appropriate messaging (simple for writable, protected for non-writable). The match logic for Interactive::Once and PromptProtected is also simplified using a triple condition for better readability and to ensure empty vs non-empty protected files are distinguished correctly, matching expected rm behavior.
1 parent 5cca390 commit 0649270

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

src/uu/rm/src/platform/linux.rs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,28 +48,27 @@ fn prompt_file_with_stat(path: &Path, stat: &libc::stat, options: &Options) -> b
4848
let stdin_ok = options.__presume_input_tty.unwrap_or(false) || stdin().is_terminal();
4949

5050
// Match original behaviour:
51-
// - Interactive::Always: always prompt (with symlink/file specific message)
52-
// - Otherwise: prompt only when write-protected
51+
// - Interactive::Always: always prompt; use non-protected wording when writable,
52+
// otherwise fall through to protected wording.
5353
if options.interactive == InteractiveMode::Always {
5454
if is_symlink {
5555
return prompt_yes!("remove symbolic link {}?", path.quote());
5656
}
57-
if len == 0 {
58-
return prompt_yes!("remove regular empty file {}?", path.quote());
57+
if writable {
58+
return if len == 0 {
59+
prompt_yes!("remove regular empty file {}?", path.quote())
60+
} else {
61+
prompt_yes!("remove file {}?", path.quote())
62+
};
5963
}
60-
return prompt_yes!("remove file {}?", path.quote());
64+
// Not writable: use protected wording below
6165
}
6266

63-
// Interactive::Once or ::PromptProtected paths
64-
match (stdin_ok, writable) {
65-
(false, _) if options.interactive == InteractiveMode::PromptProtected => true,
66-
(_, true) => true,
67-
(_, false) if len == 0 => {
68-
prompt_yes!(
69-
"remove write-protected regular empty file {}?",
70-
path.quote()
71-
)
72-
}
67+
// Interactive::Once or ::PromptProtected (and non-writable Always) paths
68+
match (stdin_ok, writable, len == 0) {
69+
(false, _, _) if options.interactive == InteractiveMode::PromptProtected => true,
70+
(_, true, _) => true,
71+
(_, false, true) => prompt_yes!("remove write-protected regular empty file {}?", path.quote()),
7372
_ => prompt_yes!("remove write-protected regular file {}?", path.quote()),
7473
}
7574
}

0 commit comments

Comments
 (0)