Skip to content

Commit 3f4e52a

Browse files
committed
fix(rm): safely reopen path for parent fd in recursive dir removal
Reopen the current directory path fresh to obtain the parent file descriptor, avoiding the use of potentially dropped `current_fd` after recursive removal steps. This ensures robust handling of file descriptors during safe directory deletion on Linux.
1 parent 0816ae3 commit 3f4e52a

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,6 @@ pub fn safe_remove_dir_recursive_impl(
391391
};
392392

393393
// Release current dir fd to keep FD usage low
394-
let restore_fd = current_fd.open_subdir(OsStr::new("..")).ok();
395394
drop(current_fd);
396395

397396
let (child_error, returned_fd) =
@@ -438,6 +437,9 @@ pub fn safe_remove_dir_recursive_impl(
438437
}
439438
}
440439

441-
let parent_fd = current_fd.open_subdir(OsStr::new("..")).ok();
440+
// Reopen current path fresh to obtain parent; avoids using possibly dropped fd
441+
let parent_fd = DirFd::open(path)
442+
.ok()
443+
.and_then(|fd| fd.open_subdir(OsStr::new("..")).ok());
442444
(error, parent_fd)
443445
}

0 commit comments

Comments
 (0)