mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 19:47:45 +00:00
rm: Refactor prompt_file, lower nesting depth
Addressing issue #5345. Introduce new helper function: prompt_file_permission_read_only
This commit is contained in:
parent
3c2e327a34
commit
f18e8983b1
1 changed files with 28 additions and 33 deletions
|
@ -486,7 +486,6 @@ fn prompt_dir(path: &Path, options: &Options) -> bool {
|
|||
}
|
||||
}
|
||||
|
||||
#[allow(clippy::cognitive_complexity)]
|
||||
fn prompt_file(path: &Path, options: &Options) -> bool {
|
||||
// If interactive is Never we never want to send prompts
|
||||
if options.interactive == InteractiveMode::Never {
|
||||
|
@ -503,47 +502,43 @@ 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
|
||||
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())
|
||||
}
|
||||
let Ok(metadata) = file.metadata() else {
|
||||
return true;
|
||||
};
|
||||
|
||||
if options.interactive == InteractiveMode::Always && !metadata.permissions().readonly()
|
||||
{
|
||||
return if metadata.len() == 0 {
|
||||
prompt_yes!("remove regular empty file {}?", path.quote())
|
||||
} else {
|
||||
true
|
||||
}
|
||||
} else {
|
||||
true
|
||||
prompt_yes!("remove file {}?", path.quote())
|
||||
};
|
||||
}
|
||||
prompt_file_permission_readonly(path, Ok(metadata))
|
||||
}
|
||||
Err(err) => {
|
||||
if err.kind() == ErrorKind::PermissionDenied {
|
||||
match fs::metadata(path) {
|
||||
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
|
||||
if err.kind() != ErrorKind::PermissionDenied {
|
||||
return true;
|
||||
}
|
||||
prompt_file_permission_readonly(path, fs::metadata(path))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn prompt_file_permission_readonly(
|
||||
path: &Path,
|
||||
metadata_or_err: Result<Metadata, std::io::Error>,
|
||||
) -> bool {
|
||||
match metadata_or_err {
|
||||
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
|
||||
// Most cases are covered by keep eye out for edge cases
|
||||
#[cfg(unix)]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue