Skip to content

Commit 7f35680

Browse files
committed
rm: update is_dir_empty function to return Result type for better error handling
1 parent af65557 commit 7f35680

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

src/uu/rm/src/platform/linux.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ pub fn safe_remove_dir_recursive(
239239

240240
// Before trying to remove the directory, check if it's actually empty
241241
// This handles the case where some children weren't removed due to user "no" responses
242-
if !is_dir_empty(path) {
242+
if !is_dir_empty(path).unwrap_or(false) {
243243
// Directory is not empty, so we can't/shouldn't remove it
244244
// In interactive mode, this might be expected if user said "no" to some children
245245
// In non-interactive mode, this indicates an error (some children couldn't be removed)
@@ -292,7 +292,7 @@ pub fn safe_remove_dir_recursive_impl(path: &Path, dir_fd: &DirFd, options: &Opt
292292
if is_dir {
293293
// Ask user if they want to descend into this directory
294294
if options.interactive == InteractiveMode::Always
295-
&& !is_dir_empty(&entry_path)
295+
&& !is_dir_empty(&entry_path).unwrap_or(false)
296296
&& !prompt_descend(&entry_path)
297297
{
298298
continue;

src/uu/rm/src/rm.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -527,8 +527,8 @@ pub fn remove(files: &[&OsStr], options: &Options) -> bool {
527527
///
528528
/// `path` must be a directory. If there is an error reading the
529529
/// contents of the directory, this returns `false`.
530-
fn is_dir_empty(path: &Path) -> bool {
531-
fs::read_dir(path).is_ok_and(|mut iter| iter.next().is_none())
530+
fn is_dir_empty(path: &Path) -> Result<bool, io::Error> {
531+
fs::read_dir(path).map(|mut iter| iter.next().is_none())
532532
}
533533

534534
#[cfg(unix)]
@@ -599,7 +599,7 @@ fn remove_dir_recursive(
599599
// Base case 2: this is a non-empty directory, but the user
600600
// doesn't want to descend into it.
601601
if options.interactive == InteractiveMode::Always
602-
&& !is_dir_empty(path)
602+
&& !is_dir_empty(path).unwrap_or(false)
603603
&& !prompt_descend(path)
604604
{
605605
return false;
@@ -699,7 +699,8 @@ fn handle_dir(path: &Path, options: &Options, progress_bar: Option<&ProgressBar>
699699
}
700700

701701
// Refuse to remove non-empty directory without -r/-R
702-
if !options.recursive && !is_dir_empty(path) {
702+
// If we can't read the directory, fall through to let remove_dir handle the error
703+
if !options.recursive && !is_dir_empty(path).unwrap_or(true) {
703704
show_error!(
704705
"{}: Directory not empty",
705706
translate!("rm-error-cannot-remove", "file" => path.quote())

0 commit comments

Comments
 (0)