Skip to content

Commit

Permalink
cp: skip duplicate source check when --backup is given
Browse files Browse the repository at this point in the history
  • Loading branch information
matrixhead committed Oct 27, 2024
1 parent 74cd797 commit 4d5bf42
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/uu/cp/src/cp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1265,7 +1265,7 @@ pub fn copy(sources: &[PathBuf], target: &Path, options: &Options) -> CopyResult

for source in sources {
let normalized_source = normalize_path(source);
if seen_sources.contains(&normalized_source) {
if options.backup == BackupMode::NoBackup && seen_sources.contains(&normalized_source) {
show_warning!("source file {} specified more than once", source.quote());
} else {
let dest = construct_dest_path(source, target, target_type, options)
Expand Down
27 changes: 27 additions & 0 deletions tests/by-util/test_cp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,33 @@ fn test_cp_duplicate_files_normalized_path() {
assert_eq!(at.read(TEST_COPY_TO_FOLDER_FILE), "Hello, World!\n");
}

#[test]
fn test_cp_duplicate_files_with_plain_backup() {
let (_, mut ucmd) = at_and_ucmd!();
ucmd.arg(TEST_HELLO_WORLD_SOURCE)
.arg(TEST_HELLO_WORLD_SOURCE)
.arg(TEST_COPY_TO_FOLDER)
.arg("--backup")
.fails()
// cp would skip duplicate src check and fail when it tries to overwrite the "just-created" file.
.stderr_contains(
"will not overwrite just-created 'hello_dir/hello_world.txt' with 'hello_world.txt",
);
}

#[test]
fn test_cp_duplicate_files_with_numbered_backup() {
let (at, mut ucmd) = at_and_ucmd!();
// cp would skip duplicate src check and succeeds
ucmd.arg(TEST_HELLO_WORLD_SOURCE)
.arg(TEST_HELLO_WORLD_SOURCE)
.arg(TEST_COPY_TO_FOLDER)
.arg("--backup=numbered")
.succeeds();
at.file_exists(TEST_COPY_TO_FOLDER_FILE);
at.file_exists(format!("{TEST_COPY_TO_FOLDER}.~1~"));
}

#[test]
fn test_cp_same_file() {
let (at, mut ucmd) = at_and_ucmd!();
Expand Down

0 comments on commit 4d5bf42

Please sign in to comment.