From 74cd797c8a1205721c34d484b71fa32c47ae7939 Mon Sep 17 00:00:00 2001 From: mhead Date: Sat, 26 Oct 2024 12:09:38 +0530 Subject: [PATCH] cp: normalize path when checking for duplicate source --- src/uu/cp/src/cp.rs | 8 ++++---- tests/by-util/test_cp.rs | 13 +++++++++++++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/uu/cp/src/cp.rs b/src/uu/cp/src/cp.rs index 62509b756..3cd026335 100644 --- a/src/uu/cp/src/cp.rs +++ b/src/uu/cp/src/cp.rs @@ -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 { diff --git a/tests/by-util/test_cp.rs b/tests/by-util/test_cp.rs index 0fba59672..0ecb06bd8 100644 --- a/tests/by-util/test_cp.rs +++ b/tests/by-util/test_cp.rs @@ -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!();