Skip to content

Commit

Permalink
rm: added write-protected check for files
Browse files Browse the repository at this point in the history
Signed-off-by: Stefin <[email protected]>
  • Loading branch information
stefins committed Aug 20, 2022
1 parent aef9608 commit 9ffb00c
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/uu/rm/src/rm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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 {
Expand All @@ -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())))
Expand Down

0 comments on commit 9ffb00c

Please sign in to comment.