Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat(chmod): use dirfd for recursive subdirectory traversal
- Update chmod recursive logic to use directory file descriptors instead of full paths for subdirectories
- Improves performance, avoids path length issues, and ensures dirfd-relative openat calls
- Add test to verify strace output shows no AT_FDCWD with multi-component paths
  • Loading branch information
mattsu2020 committed Dec 3, 2025
commit f56412c324af5a60e1547a4dc23d440cc46f67f4
19 changes: 17 additions & 2 deletions src/uu/chmod/src/chmod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -522,9 +522,24 @@ impl Chmoder {
.safe_chmod_file(&entry_path, dir_fd, &entry_name, meta.mode() & 0o7777)
.and(r);

// Recurse into subdirectories
// Recurse into subdirectories using the existing directory fd
if meta.is_dir() {
r = self.walk_dir_with_context(&entry_path, false).and(r);
match dir_fd.open_subdir(&entry_name) {
Ok(child_dir_fd) => {
r = self.safe_traverse_dir(&child_dir_fd, &entry_path).and(r);
}
Err(err) => {
let error = if err.kind() == std::io::ErrorKind::PermissionDenied {
ChmodError::PermissionDenied(
entry_path.to_string_lossy().to_string(),
)
.into()
} else {
err.into()
};
r = r.and(Err(error));
}
}
}
}
}
Expand Down
40 changes: 40 additions & 0 deletions tests/by-util/test_chmod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1280,6 +1280,46 @@ fn test_chmod_non_utf8_paths() {
);
}

#[cfg(all(target_os = "linux", feature = "chmod"))]
#[test]
fn test_chmod_recursive_uses_dirfd_for_subdirs() {
use std::process::Command;
use uutests::get_tests_binary;

// Skip test if strace is not available
if Command::new("strace").arg("-V").output().is_err() {
eprintln!("strace not found; skipping test_chmod_recursive_uses_dirfd_for_subdirs");
return;
}

let (at, _ucmd) = at_and_ucmd!();
at.mkdir("x");
at.mkdir("x/y");
at.mkdir("x/y/z");

let log_path = at.plus_as_string("strace.log");

let status = Command::new("strace")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whether to guarantee the detailed condition of “safely traversing while holding directory FD”
Otherwise, using scripts is not a problem.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the issue with the current test is that we aren't never certain that it will fail :)
as it is skipped easily


    // Skip test if strace is not available
    if Command::new("strace").arg("-V").output().is_err() {
        eprintln!("strace not found; skipping test_chmod_recursive_uses_dirfd_for_subdirs");
        return;
    }

.arg("-e")
.arg("openat")
.arg("-o")
.arg(&log_path)
.arg(get_tests_binary!())
.args(["chmod", "-R", "+x", "x"])
.current_dir(&at.subdir)
.status()
.expect("failed to run strace");
assert!(status.success(), "strace run failed");

let log = at.read("strace.log");

// Regression guard: ensure recursion uses dirfd-relative openat instead of AT_FDCWD with a multi-component path
assert!(
!log.contains("openat(AT_FDCWD, \"x/y"),
"chmod recursed using AT_FDCWD with a multi-component path; expected dirfd-relative openat"
);
}

#[test]
fn test_chmod_colored_output() {
// Test colored help message
Expand Down