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

cp: --rem don't fail if dest is symlink to source

This commit is contained in:
Daniel Hofstetter 2023-10-19 17:05:29 +02:00
parent f971a69d69
commit 772892e2e4
2 changed files with 24 additions and 0 deletions

View file

@ -1647,6 +1647,14 @@ fn copy_file(
dest.display()
)));
}
if paths_refer_to_same_file(source, dest, true)
&& matches!(
options.overwrite,
OverwriteMode::Clobber(ClobberMode::RemoveDestination)
)
{
fs::remove_file(dest)?;
}
}
if file_or_link_exists(dest) {

View file

@ -2827,6 +2827,22 @@ fn test_cp_mode_hardlink_no_dereference() {
assert_eq!(at.read_symlink("z"), "slink");
}
#[test]
fn test_remove_destination_with_destination_being_a_symlink_to_source() {
let (at, mut ucmd) = at_and_ucmd!();
let file = "file";
let symlink = "symlink";
at.touch(file);
at.symlink_file(file, symlink);
ucmd.args(&["--remove-destination", file, symlink])
.succeeds();
assert!(!at.symlink_exists(symlink));
assert!(at.file_exists(file));
assert!(at.file_exists(symlink));
}
#[test]
fn test_remove_destination_symbolic_link_loop() {
let (at, mut ucmd) = at_and_ucmd!();