touch/gnu compat: when touch fails because of a permission error, change the error message

+ return 1 as error code when having this error
This commit is contained in:
Sylvestre Ledru 2021-06-02 22:53:10 +02:00
parent e7f5916864
commit eb2c06c37e
2 changed files with 30 additions and 4 deletions

View file

@ -155,6 +155,8 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
(now, now)
};
let mut error_code = 0;
for filename in &files {
let path = &filename[..];
@ -202,14 +204,28 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
if matches.is_present(options::NO_DEREF) {
if let Err(e) = set_symlink_file_times(path, atime, mtime) {
show_warning!("cannot touch '{}': {}", path, e);
// we found an error, it should fail in any case
error_code = 1;
if e.kind() == std::io::ErrorKind::PermissionDenied {
// GNU compatibility (not-owner.sh)
show_error!("setting times of '{}': {}", path, "Permission denied");
} else {
show_error!("setting times of '{}': {}", path, e);
}
}
} else if let Err(e) = filetime::set_file_times(path, atime, mtime) {
show_warning!("cannot touch '{}': {}", path, e);
}
}
// we found an error, it should fail in any case
error_code = 1;
0
if e.kind() == std::io::ErrorKind::PermissionDenied {
// GNU compatibility (not-owner.sh)
show_error!("setting times of '{}': {}", path, "Permission denied");
} else {
show_error!("setting times of '{}': {}", path, e);
}
}
}
error_code
}
fn stat(path: &str, follow: bool) -> (FileTime, FileTime) {

View file

@ -449,3 +449,13 @@ fn test_touch_mtime_dst_fails() {
ucmd.args(&["-m", "-t", &s, file]).fails();
}
}
#[test]
#[cfg(unix)]
fn test_touch_system_fails() {
let (_at, mut ucmd) = at_and_ucmd!();
let file = "/";
ucmd.args(&[file])
.fails()
.stderr_contains("setting times of '/'");
}