1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 19:47:45 +00:00

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

This commit is contained in:
Daniel Hofstetter 2023-10-20 09:40:09 +02:00 committed by Sylvestre Ledru
parent 03d598d08b
commit 94492c98a5
2 changed files with 29 additions and 2 deletions

View file

@ -33,8 +33,8 @@ use platform::copy_on_write;
use uucore::display::Quotable;
use uucore::error::{set_exit_code, UClapError, UError, UResult, UUsageError};
use uucore::fs::{
canonicalize, is_symlink_loop, paths_refer_to_same_file, FileInformation, MissingHandling,
ResolveMode,
are_hardlinks_to_same_file, canonicalize, is_symlink_loop, paths_refer_to_same_file,
FileInformation, MissingHandling, ResolveMode,
};
use uucore::{backup_control, update_control};
// These are exposed for projects (e.g. nushell) that want to create an `Options` value, which
@ -1657,6 +1657,15 @@ fn copy_file(
}
}
if are_hardlinks_to_same_file(source, dest)
&& matches!(
options.overwrite,
OverwriteMode::Clobber(ClobberMode::RemoveDestination)
)
{
fs::remove_file(dest)?;
}
if file_or_link_exists(dest) {
handle_existing_dest(source, dest, options, source_in_command_line)?;
}

View file

@ -2827,6 +2827,24 @@ fn test_cp_mode_hardlink_no_dereference() {
assert_eq!(at.read_symlink("z"), "slink");
}
#[cfg(not(any(windows, target_os = "android")))]
#[test]
fn test_remove_destination_with_destination_being_a_hardlink_to_source() {
let (at, mut ucmd) = at_and_ucmd!();
let file = "file";
let hardlink = "hardlink";
at.touch(file);
at.hard_link(file, hardlink);
ucmd.args(&["--remove-destination", file, hardlink])
.succeeds();
assert_eq!("", at.resolve_link(hardlink));
assert!(at.file_exists(file));
assert!(at.file_exists(hardlink));
}
#[test]
fn test_remove_destination_with_destination_being_a_symlink_to_source() {
let (at, mut ucmd) = at_and_ucmd!();