Skip to content

Commit 74cd797

Browse files
committed
cp: normalize path when checking for duplicate source
1 parent 971da04 commit 74cd797

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

src/uu/cp/src/cp.rs

Lines changed: 4 additions & 4 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,8 +1264,8 @@ 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)
1267+
let normalized_source = normalize_path(source);
1268+
if 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)
@@ -1309,7 +1309,7 @@ pub fn copy(sources: &[PathBuf], target: &Path, options: &Options) -> CopyResult
13091309
copied_destinations.insert(dest.clone());
13101310
}
13111311
}
1312-
seen_sources.insert(source);
1312+
seen_sources.insert(normalized_source);
13131313
}
13141314

13151315
if let Some(pb) = progress_bar {

tests/by-util/test_cp.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,19 @@ 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_files_normalized_path() {
126+
let (at, mut ucmd) = at_and_ucmd!();
127+
ucmd.arg(TEST_HELLO_WORLD_SOURCE)
128+
.arg(format!("./{TEST_HELLO_WORLD_SOURCE}"))
129+
.arg(TEST_COPY_TO_FOLDER)
130+
.succeeds()
131+
.stderr_contains(format!(
132+
"source file './{TEST_HELLO_WORLD_SOURCE}' specified more than once"
133+
));
134+
assert_eq!(at.read(TEST_COPY_TO_FOLDER_FILE), "Hello, World!\n");
135+
}
136+
124137
#[test]
125138
fn test_cp_same_file() {
126139
let (at, mut ucmd) = at_and_ucmd!();

0 commit comments

Comments
 (0)