Skip to content

Commit

Permalink
Merge pull request #5430 from cakebaker/cp_remove_destination_shouldn…
Browse files Browse the repository at this point in the history
…t_fail

cp --remove-destination: don't fail if destination is symlink to source
  • Loading branch information
sylvestre committed Oct 22, 2023
2 parents a0b17e4 + 772892e commit 03d598d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/uu/cp/src/cp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
16 changes: 16 additions & 0 deletions tests/by-util/test_cp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!();
Expand Down

0 comments on commit 03d598d

Please sign in to comment.