1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-30 20:47:46 +00:00

touch: don't generate an error on 'touch -h -'

This commit is contained in:
Sylvestre Ledru 2023-04-22 18:54:43 +02:00
parent aaea758b6a
commit 84e0610487
2 changed files with 21 additions and 4 deletions

View file

@ -209,14 +209,24 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
}
}
if matches.get_flag(options::NO_DEREF) {
set_symlink_file_times(path, atime, mtime)
} else {
// sets the file access and modification times for a file or a symbolic link.
// The filename, access time (atime), and modification time (mtime) are provided as inputs.
// If the filename is not "-", indicating a special case for touch -h -,
// the code checks if the NO_DEREF flag is set, which means the user wants to
// set the times for a symbolic link itself, rather than the file it points to.
if filename == "-" {
filetime::set_file_times(path, atime, mtime)
} else {
let should_set_symlink_times = matches.get_flag(options::NO_DEREF);
if should_set_symlink_times {
set_symlink_file_times(path, atime, mtime)
} else {
filetime::set_file_times(path, atime, mtime)
}
}
.map_err_context(|| format!("setting times of {}", path.quote()))?;
}
Ok(())
}

View file

@ -833,3 +833,10 @@ fn test_touch_no_dereference_dangling() {
ucmd.args(&["-h", "dangling"]).succeeds();
}
#[test]
fn test_touch_dash() {
let (_, mut ucmd) = at_and_ucmd!();
ucmd.args(&["-h", "-"]).succeeds().no_stderr().no_stdout();
}