Skip to content

Commit 92832f2

Browse files
cerdelensylvestre
authored andcommitted
chmod: fix error handling if multiple files are handled (uutils#9793)
* chmod: fix error handling if multiple files are handled * chmod: add regression test for correct exit codes * chmod: fix test expected error msg --------- Co-authored-by: Sylvestre Ledru <sylvestre@debian.org>
1 parent 27bc745 commit 92832f2

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

src/uu/chmod/src/chmod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ impl Chmoder {
411411
return Err(ChmodError::PreserveRoot("/".into()).into());
412412
}
413413
if self.recursive {
414-
r = self.walk_dir_with_context(file, true);
414+
r = self.walk_dir_with_context(file, true).and(r);
415415
} else {
416416
r = self.chmod_file(file).and(r);
417417
}

tests/by-util/test_chmod.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,38 @@ fn test_permission_denied() {
375375
.stderr_is("chmod: cannot access 'd/no-x/y': Permission denied\n");
376376
}
377377

378+
#[test]
379+
#[allow(clippy::unreadable_literal)]
380+
fn test_chmod_recursive_correct_exit_code() {
381+
let (at, mut ucmd) = at_and_ucmd!();
382+
383+
// create 3 folders to test on
384+
at.mkdir("a");
385+
at.mkdir("a/b");
386+
at.mkdir("z");
387+
388+
// remove read permissions for folder a so the chmod command for a/b fails
389+
let mut perms = at.metadata("a").permissions();
390+
perms.set_mode(0o000);
391+
set_permissions(at.plus_as_string("a"), perms).unwrap();
392+
393+
#[cfg(not(target_os = "linux"))]
394+
let err_msg = "chmod: Permission denied\n";
395+
#[cfg(target_os = "linux")]
396+
let err_msg = "chmod: cannot access 'a': Permission denied\n";
397+
398+
// order of command is a, a/b then c
399+
// command is expected to fail and not just take the last exit code
400+
ucmd.arg("-R")
401+
.arg("--verbose")
402+
.arg("a+w")
403+
.arg("a")
404+
.arg("z")
405+
.umask(0)
406+
.fails()
407+
.stderr_is(err_msg);
408+
}
409+
378410
#[test]
379411
#[allow(clippy::unreadable_literal)]
380412
fn test_chmod_recursive() {

0 commit comments

Comments
 (0)