From 84e061048753656b3f4eaf627e8d6efef1034190 Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Sat, 22 Apr 2023 18:54:43 +0200 Subject: [PATCH] touch: don't generate an error on 'touch -h -' --- src/uu/touch/src/touch.rs | 18 ++++++++++++++---- tests/by-util/test_touch.rs | 7 +++++++ 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/uu/touch/src/touch.rs b/src/uu/touch/src/touch.rs index de1924134..45a51f02d 100644 --- a/src/uu/touch/src/touch.rs +++ b/src/uu/touch/src/touch.rs @@ -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(()) } diff --git a/tests/by-util/test_touch.rs b/tests/by-util/test_touch.rs index a2e378777..7d2b8ad3f 100644 --- a/tests/by-util/test_touch.rs +++ b/tests/by-util/test_touch.rs @@ -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(); +}