Skip to content

Commit e0c53ce

Browse files
committed
rm: add is_dir_empty() helper function
1 parent 28ae796 commit e0c53ce

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/uu/rm/src/rm.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,17 @@ pub fn remove(files: &[&OsStr], options: &Options) -> bool {
330330
had_err
331331
}
332332

333+
/// Whether the given directory is empty.
334+
///
335+
/// `path` must be a directory. If there is an error reading the
336+
/// contents of the directory, this returns `false`.
337+
fn is_dir_empty(path: &Path) -> bool {
338+
match std::fs::read_dir(path) {
339+
Err(_) => false,
340+
Ok(iter) => iter.count() == 0,
341+
}
342+
}
343+
333344
/// Whether the given file or directory is writable.
334345
#[cfg(unix)]
335346
fn is_writable(path: &Path) -> bool {
@@ -403,7 +414,7 @@ fn handle_dir(path: &Path, options: &Options) -> bool {
403414
if file_type.is_dir() {
404415
// If we are in Interactive Mode Always and the directory isn't empty we ask if we should descend else we push this directory onto dirs vector
405416
if options.interactive == InteractiveMode::Always
406-
&& fs::read_dir(entry.path()).unwrap().count() != 0
417+
&& !is_dir_empty(entry.path())
407418
{
408419
// If we don't descend we push this directory onto our not_descended vector else we push this directory onto dirs vector
409420
if prompt_descend(entry.path()) {

0 commit comments

Comments
 (0)