Skip to content

Commit 4d5bf42

Browse files
committed
cp: skip duplicate source check when --backup is given
1 parent 74cd797 commit 4d5bf42

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/uu/cp/src/cp.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1265,7 +1265,7 @@ pub fn copy(sources: &[PathBuf], target: &Path, options: &Options) -> CopyResult
12651265

12661266
for source in sources {
12671267
let normalized_source = normalize_path(source);
1268-
if seen_sources.contains(&normalized_source) {
1268+
if options.backup == BackupMode::NoBackup && seen_sources.contains(&normalized_source) {
12691269
show_warning!("source file {} specified more than once", source.quote());
12701270
} else {
12711271
let dest = construct_dest_path(source, target, target_type, options)

tests/by-util/test_cp.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,33 @@ fn test_cp_duplicate_files_normalized_path() {
134134
assert_eq!(at.read(TEST_COPY_TO_FOLDER_FILE), "Hello, World!\n");
135135
}
136136

137+
#[test]
138+
fn test_cp_duplicate_files_with_plain_backup() {
139+
let (_, mut ucmd) = at_and_ucmd!();
140+
ucmd.arg(TEST_HELLO_WORLD_SOURCE)
141+
.arg(TEST_HELLO_WORLD_SOURCE)
142+
.arg(TEST_COPY_TO_FOLDER)
143+
.arg("--backup")
144+
.fails()
145+
// cp would skip duplicate src check and fail when it tries to overwrite the "just-created" file.
146+
.stderr_contains(
147+
"will not overwrite just-created 'hello_dir/hello_world.txt' with 'hello_world.txt",
148+
);
149+
}
150+
151+
#[test]
152+
fn test_cp_duplicate_files_with_numbered_backup() {
153+
let (at, mut ucmd) = at_and_ucmd!();
154+
// cp would skip duplicate src check and succeeds
155+
ucmd.arg(TEST_HELLO_WORLD_SOURCE)
156+
.arg(TEST_HELLO_WORLD_SOURCE)
157+
.arg(TEST_COPY_TO_FOLDER)
158+
.arg("--backup=numbered")
159+
.succeeds();
160+
at.file_exists(TEST_COPY_TO_FOLDER_FILE);
161+
at.file_exists(format!("{TEST_COPY_TO_FOLDER}.~1~"));
162+
}
163+
137164
#[test]
138165
fn test_cp_same_file() {
139166
let (at, mut ucmd) = at_and_ucmd!();

0 commit comments

Comments
 (0)