Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
78 changes: 49 additions & 29 deletions src/uu/chmod/src/chmod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,40 +370,60 @@ impl Chmoder {

for filename in files {
let file = Path::new(filename);
if !file.exists() {
if file.is_symlink() {
if !self.dereference && !self.recursive {
// The file is a symlink and we should not follow it
// Don't try to change the mode of the symlink itself
continue;
}
if self.recursive && self.traverse_symlinks == TraverseSymlinks::None {
continue;
}
// `Path::exists()` returns `false` when the path's metadata cannot be
// read at all (for example a parent directory without search
// permission), which made chmod misreport an existing but
// inaccessible file as "No such file or directory". Use
// `try_exists()` so a permission error is surfaced as such, matching
// GNU (issue #9789).
match file.try_exists() {
Ok(false) => {
if file.is_symlink() {
if !self.dereference && !self.recursive {
// The file is a symlink and we should not follow it
// Don't try to change the mode of the symlink itself
continue;
}
if self.recursive && self.traverse_symlinks == TraverseSymlinks::None {
continue;
}

if !self.quiet {
show!(ChmodError::DanglingSymlink(filename.into()));
set_exit_code(1);
}

if self.verbose {
println!(
"{}",
translate!("chmod-verbose-failed-dangling", "file" => filename.quote())
);
}
} else if !self.quiet {
show!(ChmodError::NoSuchFile(filename.into()));
}
// GNU exits with exit code 1 even if -q or --quiet are passed
// So we set the exit code, because it hasn't been set yet if `self.quiet` is true.
set_exit_code(1);
continue;
}
Err(err) if err.kind() == std::io::ErrorKind::PermissionDenied => {
if !self.quiet {
show!(ChmodError::DanglingSymlink(filename.into()));
set_exit_code(1);
show!(ChmodError::PermissionDenied(filename.into()));
}

if self.verbose {
println!(
"{}",
translate!("chmod-verbose-failed-dangling", "file" => filename.quote())
);
set_exit_code(1);
continue;
}
// Present, or an unexpected error that the chmod attempt below
// will surface with a precise message.
Ok(true) | Err(_) => {
if !self.dereference && file.is_symlink() {
// The file is a symlink and we should not follow it
// chmod 755 --no-dereference a/link
// should not change the permissions in this case
continue;
}
} else if !self.quiet {
show!(ChmodError::NoSuchFile(filename.into()));
}
// GNU exits with exit code 1 even if -q or --quiet are passed
// So we set the exit code, because it hasn't been set yet if `self.quiet` is true.
set_exit_code(1);
continue;
} else if !self.dereference && file.is_symlink() {
// The file is a symlink and we should not follow it
// chmod 755 --no-dereference a/link
// should not change the permissions in this case
continue;
}
if self.recursive && self.preserve_root && Self::is_root(file) {
return Err(ChmodError::PreserveRoot("/".into()).into());
Expand Down
24 changes: 24 additions & 0 deletions tests/by-util/test_chmod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,30 @@
.stderr_is("chmod: cannot access 'd/no-x/y': Permission denied\n");
}

#[test]
fn test_chmod_inaccessible_file_reports_permission_denied() {
// Non-recursive chmod of a file whose metadata cannot be read because its
// parent directory lacks search permission must report "Permission denied",
// not "No such file or directory" (issue #9789). `Path::exists()` collapses
// the permission error into `false`, so the fix relies on `try_exists()`.
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;

at.mkdir("locked");
make_file(&at.plus_as_string("locked/file"), 0o100644);

Check failure on line 418 in tests/by-util/test_chmod.rs

View workflow job for this annotation

GitHub Actions / Style and Lint (unix)

ERROR: `cargo clippy`: long literal lacking separators (file:'tests/by-util/test_chmod.rs', line:418)
set_permissions(at.plus_as_string("locked"), Permissions::from_mode(0o000)).unwrap();

scene
.ucmd()
.arg("644")
.arg("locked/file")
.fails()
.stderr_is("chmod: cannot access 'locked/file': Permission denied\n");

// Restore search permission so the fixture directory can be cleaned up.
set_permissions(at.plus_as_string("locked"), Permissions::from_mode(0o755)).unwrap();
}

#[test]
#[allow(clippy::unreadable_literal)]
fn test_chmod_recursive_correct_exit_code() {
Expand Down
Loading