Merge pull request #5430 from cakebaker/cp_remove_destination_shouldnt_fail

cp --remove-destination: don't fail if destination is symlink to source
This commit is contained in:
Sylvestre Ledru 2023-10-22 22:17:32 +02:00 committed by GitHub
commit 03d598d08b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
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!();