touch: -h should not fail when running on broken symlink

Fixes more of tests/touch/no-dereference.sh
This commit is contained in:
Sylvestre Ledru 2023-04-22 08:56:01 +02:00
parent c3656e561c
commit aaea758b6a
2 changed files with 20 additions and 1 deletions

View file

@ -139,15 +139,25 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let path = pathbuf.as_path();
if let Err(e) = path.metadata() {
// Check the metadata of the path
let metadata_result = if matches.get_flag(options::NO_DEREF) {
path.symlink_metadata()
} else {
path.metadata()
};
if let Err(e) = metadata_result {
// If the error is not a NotFound error, return the error with context
if e.kind() != std::io::ErrorKind::NotFound {
return Err(e.map_err_context(|| format!("setting times of {}", filename.quote())));
}
// If the NO_CREATE flag is set, skip this iteration
if matches.get_flag(options::NO_CREATE) {
continue;
}
// If the NO_DEREF flag is set, show an error and skip this iteration
if matches.get_flag(options::NO_DEREF) {
show!(USimpleError::new(
1,
@ -159,6 +169,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
continue;
}
// Create the file, and handle errors by showing the error and skipping this iteration
if let Err(e) = File::create(path) {
show!(e.map_err_context(|| format!("cannot touch {}", path.quote())));
continue;

View file

@ -825,3 +825,11 @@ fn test_touch_no_dereference_ref_dangling() {
ucmd.args(&["-h", "-r", "dangling", "file"]).succeeds();
}
#[test]
fn test_touch_no_dereference_dangling() {
let (at, mut ucmd) = at_and_ucmd!();
at.relative_symlink_file("nowhere", "dangling");
ucmd.args(&["-h", "dangling"]).succeeds();
}