1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-08-01 05:27:45 +00:00

rm: split prompt_file() into two functions

This commit is contained in:
Daniel Hofstetter 2023-08-10 15:27:20 +02:00
parent a8b6e500d4
commit bdd25c37cf

View file

@ -370,7 +370,7 @@ fn handle_dir(path: &Path, options: &Options) -> bool {
} }
fn remove_dir(path: &Path, options: &Options) -> bool { fn remove_dir(path: &Path, options: &Options) -> bool {
if prompt_file(path, options, true) { if prompt_dir(path, options) {
if let Ok(mut read_dir) = fs::read_dir(path) { if let Ok(mut read_dir) = fs::read_dir(path) {
if options.dir || options.recursive { if options.dir || options.recursive {
if read_dir.next().is_none() { if read_dir.next().is_none() {
@ -415,7 +415,7 @@ fn remove_dir(path: &Path, options: &Options) -> bool {
} }
fn remove_file(path: &Path, options: &Options) -> bool { fn remove_file(path: &Path, options: &Options) -> bool {
if prompt_file(path, options, false) { if prompt_file(path, options) {
match fs::remove_file(path) { match fs::remove_file(path) {
Ok(_) => { Ok(_) => {
if options.verbose { if options.verbose {
@ -437,8 +437,23 @@ fn remove_file(path: &Path, options: &Options) -> bool {
false false
} }
fn prompt_dir(path: &Path, options: &Options) -> bool {
// If interactive is Never we never want to send prompts
if options.interactive == InteractiveMode::Never {
return true;
}
// We can't use metadata.permissions.readonly for directories because it only works on files
// So we have to handle whether a directory is writable manually
if let Ok(metadata) = fs::metadata(path) {
handle_writable_directory(path, options, &metadata)
} else {
true
}
}
#[allow(clippy::cognitive_complexity)] #[allow(clippy::cognitive_complexity)]
fn prompt_file(path: &Path, options: &Options, is_dir: bool) -> bool { fn prompt_file(path: &Path, options: &Options) -> bool {
// If interactive is Never we never want to send prompts // If interactive is Never we never want to send prompts
if options.interactive == InteractiveMode::Never { if options.interactive == InteractiveMode::Never {
return true; return true;
@ -451,58 +466,48 @@ fn prompt_file(path: &Path, options: &Options, is_dir: bool) -> bool {
} }
} }
} }
if is_dir { // File::open(path) doesn't open the file in write mode so we need to use file options to open it in also write mode to check if it can written too
// We can't use metadata.permissions.readonly for directories because it only works on files match File::options().read(true).write(true).open(path) {
// So we have to handle whether a directory is writable on not manually Ok(file) => {
if let Ok(metadata) = fs::metadata(path) { if let Ok(metadata) = file.metadata() {
handle_writable_directory(path, options, &metadata) if metadata.permissions().readonly() {
} else { if metadata.len() == 0 {
true prompt_yes!(
} "remove write-protected regular empty file {}?",
} else { path.quote()
// File::open(path) doesn't open the file in write mode so we need to use file options to open it in also write mode to check if it can written too )
match File::options().read(true).write(true).open(path) {
Ok(file) => {
if let Ok(metadata) = file.metadata() {
if metadata.permissions().readonly() {
if metadata.len() == 0 {
prompt_yes!(
"remove write-protected regular empty file {}?",
path.quote()
)
} else {
prompt_yes!("remove write-protected regular file {}?", path.quote())
}
} else if options.interactive == InteractiveMode::Always {
if metadata.len() == 0 {
prompt_yes!("remove regular empty file {}?", path.quote())
} else {
prompt_yes!("remove file {}?", path.quote())
}
} else { } else {
true prompt_yes!("remove write-protected regular file {}?", path.quote())
}
} else if options.interactive == InteractiveMode::Always {
if metadata.len() == 0 {
prompt_yes!("remove regular empty file {}?", path.quote())
} else {
prompt_yes!("remove file {}?", path.quote())
} }
} else { } else {
true true
} }
} else {
true
} }
Err(err) => { }
if err.kind() == ErrorKind::PermissionDenied { Err(err) => {
if let Ok(metadata) = fs::metadata(path) { if err.kind() == ErrorKind::PermissionDenied {
if metadata.len() == 0 { if let Ok(metadata) = fs::metadata(path) {
prompt_yes!( if metadata.len() == 0 {
"remove write-protected regular empty file {}?", prompt_yes!(
path.quote() "remove write-protected regular empty file {}?",
) path.quote()
} else { )
prompt_yes!("remove write-protected regular file {}?", path.quote())
}
} else { } else {
prompt_yes!("remove write-protected regular file {}?", path.quote()) prompt_yes!("remove write-protected regular file {}?", path.quote())
} }
} else { } else {
true prompt_yes!("remove write-protected regular file {}?", path.quote())
} }
} else {
true
} }
} }
} }