diff --git a/src/uu/rm/src/rm.rs b/src/uu/rm/src/rm.rs index a55d3f2d1..32d50ee93 100644 --- a/src/uu/rm/src/rm.rs +++ b/src/uu/rm/src/rm.rs @@ -14,6 +14,8 @@ use clap::{crate_version, Arg, Command}; use remove_dir_all::remove_dir_all; use std::collections::VecDeque; use std::fs; +use std::fs::File; +use std::io::ErrorKind; use std::io::{stderr, stdin, BufRead, Write}; use std::ops::BitOr; use std::path::{Path, PathBuf}; @@ -384,7 +386,7 @@ fn remove_file(path: &Path, options: &Options) -> bool { } else { true }; - if response { + if response && prompt_write_protected(path, false) { match fs::remove_file(path) { Ok(_) => { if options.verbose { @@ -406,6 +408,23 @@ fn remove_file(path: &Path, options: &Options) -> bool { false } +fn prompt_write_protected(path: &Path, is_dir: bool) -> bool { + match File::open(path) { + Ok(_) => true, + Err(err) => { + if err.kind() == ErrorKind::PermissionDenied { + if is_dir { + prompt(&(format!("rm: remove write-protected directory {}? ", path.quote()))) + } else { + prompt(&(format!("rm: remove write-protected file {}? ", path.quote()))) + } + } else { + true + } + } + } +} + fn prompt_file(path: &Path, is_dir: bool) -> bool { if is_dir { prompt(&(format!("rm: remove directory {}? ", path.quote())))