cp: normalize path when checking for duplicate source

This commit is contained in:
mhead 2024-10-26 12:09:38 +05:30
parent 971da040b5
commit 74cd797c8a
2 changed files with 17 additions and 4 deletions

View file

@ -29,7 +29,7 @@ use platform::copy_on_write;
use uucore::display::Quotable;
use uucore::error::{set_exit_code, UClapError, UError, UResult, UUsageError};
use uucore::fs::{
are_hardlinks_to_same_file, canonicalize, get_filename, is_symlink_loop,
are_hardlinks_to_same_file, canonicalize, get_filename, is_symlink_loop, normalize_path,
path_ends_with_terminator, paths_refer_to_same_file, FileInformation, MissingHandling,
ResolveMode,
};
@ -1264,8 +1264,8 @@ pub fn copy(sources: &[PathBuf], target: &Path, options: &Options) -> CopyResult
};
for source in sources {
if seen_sources.contains(source) {
// FIXME: compare sources by the actual file they point to, not their path. (e.g. dir/file == dir/../dir/file in most cases)
let normalized_source = normalize_path(source);
if 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)
@ -1309,7 +1309,7 @@ pub fn copy(sources: &[PathBuf], target: &Path, options: &Options) -> CopyResult
copied_destinations.insert(dest.clone());
}
}
seen_sources.insert(source);
seen_sources.insert(normalized_source);
}
if let Some(pb) = progress_bar {

View file

@ -121,6 +121,19 @@ fn test_cp_duplicate_files() {
assert_eq!(at.read(TEST_COPY_TO_FOLDER_FILE), "Hello, World!\n");
}
#[test]
fn test_cp_duplicate_files_normalized_path() {
let (at, mut ucmd) = at_and_ucmd!();
ucmd.arg(TEST_HELLO_WORLD_SOURCE)
.arg(format!("./{TEST_HELLO_WORLD_SOURCE}"))
.arg(TEST_COPY_TO_FOLDER)
.succeeds()
.stderr_contains(format!(
"source file './{TEST_HELLO_WORLD_SOURCE}' specified more than once"
));
assert_eq!(at.read(TEST_COPY_TO_FOLDER_FILE), "Hello, World!\n");
}
#[test]
fn test_cp_same_file() {
let (at, mut ucmd) = at_and_ucmd!();