Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

du: Return non zero error code when dealing with permissions errors #3382

Merged
merged 1 commit into from
Apr 10, 2022
Merged
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
9 changes: 7 additions & 2 deletions src/uu/du/src/du.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use std::str::FromStr;
use std::time::{Duration, UNIX_EPOCH};
use std::{error::Error, fmt::Display};
use uucore::display::{print_verbatim, Quotable};
use uucore::error::{UError, UResult};
use uucore::error::{set_exit_code, UError, UResult};
use uucore::format_usage;
use uucore::parse_size::{parse_size, ParseSizeError};
use uucore::InvalidEncodingHandling;
Expand Down Expand Up @@ -301,6 +301,7 @@ fn du(
my_stat.path.quote(),
e
);
set_exit_code(1);
return Box::new(iter::once(my_stat));
}
};
Expand Down Expand Up @@ -340,8 +341,12 @@ fn du(
let description = format!("cannot access {}", entry.path().quote());
let error_message = "Permission denied";
show_error_custom_description!(description, "{}", error_message);
set_exit_code(1);
Copy link
Member

Choose a reason for hiding this comment

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

I think this can be cleaned up some more with UResult, which will print the right error message automatically when converting from an io::Error. However, that is a more involved refactor, so I'll approve and leave it up to you whether you want to do this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK, I will land this and open an new issue and maybe work on it

}
_ => {
set_exit_code(1);
show_error!("cannot access {}: {}", entry.path().quote(), error);
}
_ => show_error!("cannot access {}: {}", entry.path().quote(), error),
},
},
Err(error) => show_error!("{}", error),
Expand Down
17 changes: 16 additions & 1 deletion tests/by-util/test_du.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ fn test_du_no_permission() {

ts.ccmd("chmod").arg("-r").arg(SUB_DIR_LINKS).succeeds();

let result = ts.ucmd().arg(SUB_DIR_LINKS).run(); // TODO: replace with ".fails()" once `du` is fixed
let result = ts.ucmd().arg(SUB_DIR_LINKS).fails();
result.stderr_contains(
"du: cannot read directory 'subdir/links': Permission denied (os error 13)",
);
Expand All @@ -449,6 +449,21 @@ fn test_du_no_permission() {
_du_no_permission(result.stdout_str());
}

#[cfg(not(target_os = "windows"))]
#[cfg(feature = "chmod")]
#[test]
fn test_du_no_exec_permission() {
let ts = TestScenario::new(util_name!());
let at = &ts.fixtures;

at.mkdir_all("d/no-x/y");

ts.ccmd("chmod").arg("u=rw").arg("d/no-x").succeeds();

let result = ts.ucmd().arg("d/no-x").fails();
result.stderr_contains("du: cannot access 'd/no-x/y': Permission denied");
}

#[cfg(target_vendor = "apple")]
fn _du_no_permission(s: &str) {
assert_eq!(s, "0\tsubdir/links\n");
Expand Down