Skip to content

Commit b847aeb

Browse files
committed
rm: skip prompt when stdin is not interactive; Fix #7326
1 parent fc46a04 commit b847aeb

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/uu/rm/src/rm.rs

+13
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use clap::{builder::ValueParser, parser::ValueSource, Arg, ArgAction, Command};
99
use std::ffi::{OsStr, OsString};
1010
use std::fs::{self, Metadata};
11+
use std::io::{stdin, IsTerminal};
1112
use std::ops::BitOr;
1213
#[cfg(not(windows))]
1314
use std::os::unix::ffi::OsStrExt;
@@ -576,6 +577,10 @@ fn prompt_dir(path: &Path, options: &Options) -> bool {
576577
if options.interactive == InteractiveMode::Never {
577578
return true;
578579
}
580+
// Silently proceed if stdin is not interactive terminal
581+
if !stdin().is_terminal() {
582+
return true;
583+
}
579584

580585
// We can't use metadata.permissions.readonly for directories because it only works on files
581586
// So we have to handle whether a directory is writable manually
@@ -591,6 +596,10 @@ fn prompt_file(path: &Path, options: &Options) -> bool {
591596
if options.interactive == InteractiveMode::Never {
592597
return true;
593598
}
599+
// Silently proceed if stdin is not interactive terminal
600+
if !stdin().is_terminal() {
601+
return true;
602+
}
594603
// If interactive is Always we want to check if the file is symlink to prompt the right message
595604
if options.interactive == InteractiveMode::Always {
596605
if let Ok(metadata) = fs::symlink_metadata(path) {
@@ -615,6 +624,10 @@ fn prompt_file(path: &Path, options: &Options) -> bool {
615624
}
616625

617626
fn prompt_file_permission_readonly(path: &Path) -> bool {
627+
// Silently proceed if stdin is not interactive terminal
628+
if !stdin().is_terminal() {
629+
return true;
630+
}
618631
match fs::metadata(path) {
619632
Ok(_) if is_writable(path) => true,
620633
Ok(metadata) if metadata.len() == 0 => prompt_yes!(

0 commit comments

Comments
 (0)