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

cp: restrict copy through dangling symlink with -f

Change `cp` to terminate with an error when attempting to copy through
a dangling symbolic link with the `--force` option. Before this
commit,

    touch src
    ln -s no-such-file dest
    cp -f src dest

would incorrectly replace `dest` with the contents of `src`. After
this commit, it correctly fails with the error message

    cp: not writing through dangling symlink 'dest'
This commit is contained in:
Jeffrey Finkelstein 2022-10-23 17:40:30 -04:00
parent 418518a443
commit d48a650966
2 changed files with 24 additions and 5 deletions

View file

@ -1827,6 +1827,19 @@ fn test_copy_through_dangling_symlink_no_dereference_2() {
.stderr_only("cp: not writing through dangling symlink 'target'");
}
/// Test that copy through a dangling symbolic link fails, even with --force.
#[test]
#[cfg(not(windows))]
fn test_copy_through_dangling_symlink_force() {
let (at, mut ucmd) = at_and_ucmd!();
at.touch("src");
at.symlink_file("no-such-file", "dest");
ucmd.args(&["--force", "src", "dest"])
.fails()
.stderr_only("cp: not writing through dangling symlink 'dest'");
assert!(!at.file_exists("dest"));
}
#[test]
#[cfg(unix)]
fn test_cp_archive_on_nonexistent_file() {