1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

cp: copy attributes of dangling symbolic link

Fix a bug in which `cp` incorrectly exited with an error when
attempting to copy the attributes of a dangling symbolic link (that
is, when running `cp -P -p`).

Fixes #3531.
This commit is contained in:
Jeffrey Finkelstein 2022-07-03 13:01:23 -04:00
parent a7a9da9672
commit 4780690caa
2 changed files with 53 additions and 11 deletions

View file

@ -9,6 +9,8 @@ use std::os::unix::fs;
#[cfg(unix)]
use std::os::unix::fs::symlink as symlink_file;
#[cfg(unix)]
use std::os::unix::fs::MetadataExt;
#[cfg(all(unix, not(target_os = "freebsd")))]
use std::os::unix::fs::PermissionsExt;
#[cfg(windows)]
@ -1536,6 +1538,37 @@ fn test_copy_through_dangling_symlink_no_dereference() {
.no_stdout();
}
/// Test for copying a dangling symbolic link and its permissions.
#[test]
fn test_copy_through_dangling_symlink_no_dereference_permissions() {
let (at, mut ucmd) = at_and_ucmd!();
// target name link name
at.symlink_file("no-such-file", "dangle");
// don't dereference the link
// | copy permissions, too
// | | from the link
// | | | to new file d2
// | | | |
// V V V V
ucmd.args(&["-P", "-p", "dangle", "d2"])
.succeeds()
.no_stderr()
.no_stdout();
assert!(at.symlink_exists("d2"));
// `-p` means `--preserve=mode,ownership,timestamps`
#[cfg(unix)]
{
let metadata1 = at.symlink_metadata("dangle");
let metadata2 = at.symlink_metadata("d2");
assert_eq!(metadata1.mode(), metadata2.mode());
assert_eq!(metadata1.uid(), metadata2.uid());
assert_eq!(metadata1.atime(), metadata2.atime());
assert_eq!(metadata1.mtime(), metadata2.mtime());
assert_eq!(metadata1.ctime(), metadata2.ctime());
}
}
#[test]
#[cfg(unix)]
fn test_cp_archive_on_nonexistent_file() {