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:
commit
7a608196dc
1 changed files with 24 additions and 33 deletions
|
@ -492,7 +492,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 {
|
||||
|
@ -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
|
||||
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 {
|
||||
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 {
|
||||
prompt_yes!("remove file {}?", path.quote())
|
||||
}
|
||||
} else {
|
||||
true
|
||||
}
|
||||
} else {
|
||||
true
|
||||
};
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
if err.kind() == ErrorKind::PermissionDenied {
|
||||
if err.kind() != ErrorKind::PermissionDenied {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
prompt_file_permission_readonly(path)
|
||||
}
|
||||
|
||||
fn prompt_file_permission_readonly(path: &Path) -> bool {
|
||||
match fs::metadata(path) {
|
||||
Ok(metadata) if metadata.len() == 0 => {
|
||||
prompt_yes!(
|
||||
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()),
|
||||
}
|
||||
} else {
|
||||
true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue