Skip to content

cp -P hardlink-to-symlink hardlink-to-same-symlink should no-op #7753

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/uu/cp/src/cp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2304,7 +2304,7 @@ fn copy_file(
}
handle_existing_dest(source, dest, options, source_in_command_line, copied_files)?;
if are_hardlinks_to_same_file(source, dest) {
if options.copy_mode == CopyMode::Copy && options.backup != BackupMode::NoBackup {
if options.copy_mode == CopyMode::Copy {
return Ok(());
}
if options.copy_mode == CopyMode::Link && (!source_is_symlink || !dest_is_symlink) {
Expand Down
21 changes: 21 additions & 0 deletions tests/by-util/test_cp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4675,6 +4675,7 @@ fn test_cp_no_dereference_attributes_only_with_symlink() {
/// contains the test for cp when the source and destination points to the same file
mod same_file {

use std::os::unix::fs::MetadataExt;
use uutests::util::TestScenario;
use uutests::util_name;

Expand Down Expand Up @@ -5594,6 +5595,26 @@ mod same_file {
assert_eq!(FILE_NAME, at.resolve_link(hardlink_to_symlink));
assert_eq!(at.read(FILE_NAME), CONTENTS);
}

#[test]
fn test_hardlink_of_symlink_to_hardlink_of_same_symlink_with_option_no_deref() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
let hardlink1 = "hardlink_to_symlink_1";
let hardlink2 = "hardlink_to_symlink_2";
at.write(FILE_NAME, CONTENTS);
at.symlink_file(FILE_NAME, SYMLINK_NAME);
at.hard_link(SYMLINK_NAME, hardlink1);
at.hard_link(SYMLINK_NAME, hardlink2);
let ino = at.symlink_metadata(hardlink1).ino();
assert_eq!(ino, at.symlink_metadata(hardlink2).ino()); // Sanity check
scene.ucmd().args(&["-P", hardlink1, hardlink2]).succeeds();
assert!(at.file_exists(FILE_NAME));
assert!(at.symlink_exists(SYMLINK_NAME));
// If hardlink a and b point to the same symlink, then cp a b doesn't create a new file
assert_eq!(ino, at.symlink_metadata(hardlink1).ino());
assert_eq!(ino, at.symlink_metadata(hardlink2).ino());
}
}

// the following tests are for how the cp should behave when the source is a symlink
Expand Down
Loading