Skip to content

Commit 188d9cf

Browse files
committed
perf(linux/rm): optimize descriptor usage by keeping dir_fd open in safe_remove_dir_recursive_impl
Reuse the existing directory file descriptor instead of opening/closing new ones for each subdirectory or file during recursive removal. This simplifies the code, reduces overhead from descriptor management, and prevents potential deep descriptor stacks on systems with many nested directories. Removed unnecessary `drop(dir_fd)` call and adjusted progress bar increments accordingly.
1 parent 1f78fc6 commit 188d9cf

File tree

1 file changed

+9
-24
lines changed

1 file changed

+9
-24
lines changed

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

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ pub fn safe_remove_dir_recursive_impl(
343343
}
344344
};
345345

346-
// Collect names and stat data, then drop FD to avoid deep descriptor stacks
346+
// Collect names and stat data
347347
let mut collected = Vec::with_capacity(entries.len());
348348
for entry_name in entries {
349349
match dir_fd.stat_at(&entry_name, false) {
@@ -356,7 +356,6 @@ pub fn safe_remove_dir_recursive_impl(
356356
}
357357
}
358358
}
359-
drop(dir_fd); // free descriptor before descending
360359

361360
let mut error = false;
362361

@@ -383,32 +382,18 @@ pub fn safe_remove_dir_recursive_impl(
383382
}
384383

385384
if !child_error {
386-
match DirFd::open(path) {
387-
Ok(fd) => {
388-
if let Some(pb) = progress_bar {
389-
pb.inc(1);
390-
}
391-
error = handle_unlink(&fd, entry_name.as_ref(), &entry_path, true, options)
392-
|| error;
393-
}
394-
Err(e) => {
395-
error = handle_error_with_force(e, &entry_path, options) || error;
396-
}
385+
if let Some(pb) = progress_bar {
386+
pb.inc(1);
397387
}
388+
error = handle_unlink(&dir_fd, entry_name.as_ref(), &entry_path, true, options)
389+
|| error;
398390
}
399391
} else if prompt_file_with_stat(&entry_path, &entry_stat, options) {
400-
match DirFd::open(path) {
401-
Ok(fd) => {
402-
if let Some(pb) = progress_bar {
403-
pb.inc(1);
404-
}
405-
error = handle_unlink(&fd, entry_name.as_ref(), &entry_path, false, options)
406-
|| error;
407-
}
408-
Err(e) => {
409-
error = handle_error_with_force(e, &entry_path, options) || error;
410-
}
392+
if let Some(pb) = progress_bar {
393+
pb.inc(1);
411394
}
395+
error = handle_unlink(&dir_fd, entry_name.as_ref(), &entry_path, false, options)
396+
|| error;
412397
}
413398
}
414399

0 commit comments

Comments
 (0)