Skip to content
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
18 changes: 13 additions & 5 deletions src/uu/touch/src/touch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@ pub fn uu_app() -> Command {
/// Possible causes:
/// - The user doesn't have permission to access the file
/// - One of the directory components of the file path doesn't exist.
/// - Dangling symlink is given and -r/--reference is used.
///
/// It will return an `Err` on the first error. However, for any of the files,
/// if all of the following are true, it will print the error and continue touching
Expand Down Expand Up @@ -573,14 +574,21 @@ fn update_times(
}

/// Get metadata of the provided path
/// If `follow` is `true`, the function will try to follow symlinks
/// If `follow` is `false` or the symlink is broken, the function will return metadata of the symlink itself
/// If `follow` is `true`, the function will try to follow symlinks. Errors if the symlink is dangling, otherwise defaults to symlink metadata.
/// If `follow` is `false`, the function will return metadata of the symlink itself
fn stat(path: &Path, follow: bool) -> std::io::Result<(FileTime, FileTime)> {
let metadata = if follow {
fs::metadata(path).or_else(|_| fs::symlink_metadata(path))
match fs::metadata(path) {
// Successfully followed symlink
Ok(meta) => meta,
// Dangling symlink
Err(e) if e.kind() == ErrorKind::NotFound => return Err(e),
// Other error (?), try to get the symlink metadata
Err(_) => fs::symlink_metadata(path)?,
}
} else {
fs::symlink_metadata(path)
}?;
fs::symlink_metadata(path)?
};

Ok((
FileTime::from_last_access_time(&metadata),
Expand Down
25 changes: 25 additions & 0 deletions tests/by-util/test_touch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,31 @@ fn test_touch_reference() {
}
}

#[test]
Copy link
Collaborator

Choose a reason for hiding this comment

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

So this test will run on many platforms and looking at std::os::unix::fs::symlink this will probably fail on non unix systems, ideally if you can find a way to also run this on windows or otherwise just gate it with a cfg[] for unix

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah right thanks, I'll look around for something cross-platform here

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 copied the approach in symlink_file in cp.rs, hopefully that's good enough

fn test_touch_reference_dangling() {
let temp_dir = tempfile::tempdir().unwrap();
let nonexistent_target = temp_dir.path().join("nonexistent_target");
let dangling_symlink = temp_dir.path().join("test_touch_reference_dangling");

#[cfg(not(windows))]
{
std::os::unix::fs::symlink(&nonexistent_target, &dangling_symlink).unwrap();
}
#[cfg(windows)]
{
std::os::windows::fs::symlink_file(&nonexistent_target, &dangling_symlink).unwrap();
}

new_ucmd!()
.args(&[
"--reference",
dangling_symlink.to_str().unwrap(),
"some_file",
])
.fails()
.stderr_contains("touch: failed to get attributes of");
}

#[test]
fn test_touch_set_date() {
let (at, mut ucmd) = at_and_ucmd!();
Expand Down
Loading