1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-27 11:07:44 +00:00

cp: skip duplicate source check when --backup is given

This commit is contained in:
mhead 2024-10-27 14:18:20 +05:30
parent 74cd797c8a
commit 4d5bf4205f
2 changed files with 28 additions and 1 deletions

View file

@ -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)

View file

@ -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!();