Skip to content

Commit d1f2534

Browse files
authored
Merge pull request #6825 from matrixhead/dup-source
cp: normalize path when checking for duplicate source
2 parents c808faf + e947c71 commit d1f2534

File tree

2 files changed

+67
-5
lines changed

2 files changed

+67
-5
lines changed

src/uu/cp/src/cp.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use platform::copy_on_write;
2929
use uucore::display::Quotable;
3030
use uucore::error::{set_exit_code, UClapError, UError, UResult, UUsageError};
3131
use uucore::fs::{
32-
are_hardlinks_to_same_file, canonicalize, get_filename, is_symlink_loop,
32+
are_hardlinks_to_same_file, canonicalize, get_filename, is_symlink_loop, normalize_path,
3333
path_ends_with_terminator, paths_refer_to_same_file, FileInformation, MissingHandling,
3434
ResolveMode,
3535
};
@@ -1264,9 +1264,17 @@ pub fn copy(sources: &[PathBuf], target: &Path, options: &Options) -> CopyResult
12641264
};
12651265

12661266
for source in sources {
1267-
if seen_sources.contains(source) {
1268-
// FIXME: compare sources by the actual file they point to, not their path. (e.g. dir/file == dir/../dir/file in most cases)
1269-
show_warning!("source file {} specified more than once", source.quote());
1267+
let normalized_source = normalize_path(source);
1268+
if options.backup == BackupMode::NoBackup && seen_sources.contains(&normalized_source) {
1269+
let file_type = if source.symlink_metadata()?.file_type().is_dir() {
1270+
"directory"
1271+
} else {
1272+
"file"
1273+
};
1274+
show_warning!(
1275+
"source {file_type} {} specified more than once",
1276+
source.quote()
1277+
);
12701278
} else {
12711279
let dest = construct_dest_path(source, target, target_type, options)
12721280
.unwrap_or_else(|_| target.to_path_buf());
@@ -1309,7 +1317,7 @@ pub fn copy(sources: &[PathBuf], target: &Path, options: &Options) -> CopyResult
13091317
copied_destinations.insert(dest.clone());
13101318
}
13111319
}
1312-
seen_sources.insert(source);
1320+
seen_sources.insert(normalized_source);
13131321
}
13141322

13151323
if let Some(pb) = progress_bar {

tests/by-util/test_cp.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,60 @@ fn test_cp_duplicate_files() {
121121
assert_eq!(at.read(TEST_COPY_TO_FOLDER_FILE), "Hello, World!\n");
122122
}
123123

124+
#[test]
125+
fn test_cp_duplicate_folder() {
126+
let (at, mut ucmd) = at_and_ucmd!();
127+
ucmd.arg("-r")
128+
.arg(TEST_COPY_FROM_FOLDER)
129+
.arg(TEST_COPY_FROM_FOLDER)
130+
.arg(TEST_COPY_TO_FOLDER)
131+
.succeeds()
132+
.stderr_contains(format!(
133+
"source directory '{TEST_COPY_FROM_FOLDER}' specified more than once"
134+
));
135+
assert!(at.dir_exists(format!("{TEST_COPY_TO_FOLDER}/{TEST_COPY_FROM_FOLDER}").as_str()));
136+
}
137+
138+
#[test]
139+
fn test_cp_duplicate_files_normalized_path() {
140+
let (at, mut ucmd) = at_and_ucmd!();
141+
ucmd.arg(TEST_HELLO_WORLD_SOURCE)
142+
.arg(format!("./{TEST_HELLO_WORLD_SOURCE}"))
143+
.arg(TEST_COPY_TO_FOLDER)
144+
.succeeds()
145+
.stderr_contains(format!(
146+
"source file './{TEST_HELLO_WORLD_SOURCE}' specified more than once"
147+
));
148+
assert_eq!(at.read(TEST_COPY_TO_FOLDER_FILE), "Hello, World!\n");
149+
}
150+
151+
#[test]
152+
fn test_cp_duplicate_files_with_plain_backup() {
153+
let (_, mut ucmd) = at_and_ucmd!();
154+
ucmd.arg(TEST_HELLO_WORLD_SOURCE)
155+
.arg(TEST_HELLO_WORLD_SOURCE)
156+
.arg(TEST_COPY_TO_FOLDER)
157+
.arg("--backup")
158+
.fails()
159+
// cp would skip duplicate src check and fail when it tries to overwrite the "just-created" file.
160+
.stderr_contains(
161+
"will not overwrite just-created 'hello_dir/hello_world.txt' with 'hello_world.txt",
162+
);
163+
}
164+
165+
#[test]
166+
fn test_cp_duplicate_files_with_numbered_backup() {
167+
let (at, mut ucmd) = at_and_ucmd!();
168+
// cp would skip duplicate src check and succeeds
169+
ucmd.arg(TEST_HELLO_WORLD_SOURCE)
170+
.arg(TEST_HELLO_WORLD_SOURCE)
171+
.arg(TEST_COPY_TO_FOLDER)
172+
.arg("--backup=numbered")
173+
.succeeds();
174+
at.file_exists(TEST_COPY_TO_FOLDER_FILE);
175+
at.file_exists(format!("{TEST_COPY_TO_FOLDER}.~1~"));
176+
}
177+
124178
#[test]
125179
fn test_cp_same_file() {
126180
let (at, mut ucmd) = at_and_ucmd!();

0 commit comments

Comments
 (0)