1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

Merge pull request #5356 from terade/refactoring_prompt_file

`rm`: refactor `prompt_file`, issue #5345
This commit is contained in:
Daniel Hofstetter 2023-10-18 07:04:22 +02:00 committed by GitHub
commit 7a608196dc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -492,7 +492,6 @@ fn prompt_dir(path: &Path, options: &Options) -> bool {
} }
} }
#[allow(clippy::cognitive_complexity)]
fn prompt_file(path: &Path, options: &Options) -> 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 {
@ -509,45 +508,37 @@ fn prompt_file(path: &Path, options: &Options) -> bool {
// 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 // 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) { match File::options().read(true).write(true).open(path) {
Ok(file) => { Ok(file) => {
if let Ok(metadata) = file.metadata() { let Ok(metadata) = file.metadata() else {
if metadata.permissions().readonly() { return true;
if metadata.len() == 0 { };
prompt_yes!(
"remove write-protected regular empty file {}?", if options.interactive == InteractiveMode::Always && !metadata.permissions().readonly()
path.quote() {
) return if metadata.len() == 0 {
} else { prompt_yes!("remove regular empty file {}?", path.quote())
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 file {}?", path.quote())
} };
} else {
true
} }
} }
Err(err) => { Err(err) => {
if err.kind() == ErrorKind::PermissionDenied { if err.kind() != ErrorKind::PermissionDenied {
match fs::metadata(path) { return true;
Ok(metadata) if metadata.len() == 0 => {
prompt_yes!(
"remove write-protected regular empty file {}?",
path.quote()
)
}
_ => prompt_yes!("remove write-protected regular file {}?", path.quote()),
}
} else {
true
} }
} }
} }
prompt_file_permission_readonly(path)
}
fn prompt_file_permission_readonly(path: &Path) -> bool {
match fs::metadata(path) {
Ok(metadata) if !metadata.permissions().readonly() => true,
Ok(metadata) if metadata.len() == 0 => prompt_yes!(
"remove write-protected regular empty file {}?",
path.quote()
),
_ => prompt_yes!("remove write-protected regular file {}?", path.quote()),
}
} }
// 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 // 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