Skip to content

Commit bd5a75f

Browse files
committed
rm: refactor prompt_file create prompt_file_permission_read_only
1 parent de72217 commit bd5a75f

File tree

1 file changed

+24
-26
lines changed

1 file changed

+24
-26
lines changed

src/uu/rm/src/rm.rs

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -500,50 +500,48 @@ fn prompt_file(path: &Path, options: &Options) -> bool {
500500
}
501501
}
502502
}
503+
503504
// 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
504505
match File::options().read(true).write(true).open(path) {
505506
Ok(file) => {
506-
if file.metadata().is_err() {
507+
let Ok(metadata) = file.metadata() else {
507508
return true;
508-
}
509+
};
509510

510-
let metadata = file.metadata().unwrap();
511-
if metadata.permissions().readonly() {
512-
if metadata.len() == 0 {
513-
prompt_yes!(
514-
"remove write-protected regular empty file {}?",
515-
path.quote()
516-
)
517-
} else {
518-
prompt_yes!("remove write-protected regular file {}?", path.quote())
519-
}
520-
} else if options.interactive == InteractiveMode::Always {
521-
if metadata.len() == 0 {
511+
if options.interactive == InteractiveMode::Always && !metadata.permissions().readonly()
512+
{
513+
return if metadata.len() == 0 {
522514
prompt_yes!("remove regular empty file {}?", path.quote())
523515
} else {
524516
prompt_yes!("remove file {}?", path.quote())
525-
}
526-
} else {
527-
true
517+
};
528518
}
519+
prompt_file_permission_readonly(path, Ok(metadata))
529520
}
530521
Err(err) => {
531522
if err.kind() != ErrorKind::PermissionDenied {
532523
return true;
533524
}
534-
match fs::metadata(path) {
535-
Ok(metadata) if metadata.len() == 0 => {
536-
prompt_yes!(
537-
"remove write-protected regular empty file {}?",
538-
path.quote()
539-
)
540-
}
541-
_ => prompt_yes!("remove write-protected regular file {}?", path.quote()),
542-
}
525+
prompt_file_permission_readonly(path, fs::metadata(path))
543526
}
544527
}
545528
}
546529

530+
#[allow(clippy::cognitive_complexity)]
531+
fn prompt_file_permission_readonly(
532+
path: &Path,
533+
metadata_or_err: Result<Metadata, std::io::Error>,
534+
) -> bool {
535+
match metadata_or_err {
536+
Ok(metadata) if !metadata.permissions().readonly() => true,
537+
Ok(metadata) if metadata.len() == 0 => prompt_yes!(
538+
"remove write-protected regular empty file {}?",
539+
path.quote()
540+
),
541+
_ => prompt_yes!("remove write-protected regular file {}?", path.quote()),
542+
}
543+
}
544+
547545
// For directories finding if they are writable or not is a hassle. In Unix we can use the built-in rust crate to to check mode bits. But other os don't have something similar afaik
548546
// Most cases are covered by keep eye out for edge cases
549547
#[cfg(unix)]

0 commit comments

Comments
 (0)