mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-29 03:57:44 +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 {
|
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 {
|
||||||
|
@ -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
|
// 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
|
|
||||||
}
|
}
|
||||||
|
prompt_file_permission_readonly(path, Ok(metadata))
|
||||||
}
|
}
|
||||||
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, 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
|
// 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
|
// Most cases are covered by keep eye out for edge cases
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue