mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-27 11:07:44 +00:00
Merge pull request #6825 from matrixhead/dup-source
cp: normalize path when checking for duplicate source
This commit is contained in:
commit
d1f2534811
2 changed files with 67 additions and 5 deletions
|
@ -29,7 +29,7 @@ use platform::copy_on_write;
|
||||||
use uucore::display::Quotable;
|
use uucore::display::Quotable;
|
||||||
use uucore::error::{set_exit_code, UClapError, UError, UResult, UUsageError};
|
use uucore::error::{set_exit_code, UClapError, UError, UResult, UUsageError};
|
||||||
use uucore::fs::{
|
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,
|
path_ends_with_terminator, paths_refer_to_same_file, FileInformation, MissingHandling,
|
||||||
ResolveMode,
|
ResolveMode,
|
||||||
};
|
};
|
||||||
|
@ -1264,9 +1264,17 @@ pub fn copy(sources: &[PathBuf], target: &Path, options: &Options) -> CopyResult
|
||||||
};
|
};
|
||||||
|
|
||||||
for source in sources {
|
for source in sources {
|
||||||
if seen_sources.contains(source) {
|
let normalized_source = normalize_path(source);
|
||||||
// FIXME: compare sources by the actual file they point to, not their path. (e.g. dir/file == dir/../dir/file in most cases)
|
if options.backup == BackupMode::NoBackup && seen_sources.contains(&normalized_source) {
|
||||||
show_warning!("source file {} specified more than once", source.quote());
|
let file_type = if source.symlink_metadata()?.file_type().is_dir() {
|
||||||
|
"directory"
|
||||||
|
} else {
|
||||||
|
"file"
|
||||||
|
};
|
||||||
|
show_warning!(
|
||||||
|
"source {file_type} {} specified more than once",
|
||||||
|
source.quote()
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
let dest = construct_dest_path(source, target, target_type, options)
|
let dest = construct_dest_path(source, target, target_type, options)
|
||||||
.unwrap_or_else(|_| target.to_path_buf());
|
.unwrap_or_else(|_| target.to_path_buf());
|
||||||
|
@ -1309,7 +1317,7 @@ pub fn copy(sources: &[PathBuf], target: &Path, options: &Options) -> CopyResult
|
||||||
copied_destinations.insert(dest.clone());
|
copied_destinations.insert(dest.clone());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
seen_sources.insert(source);
|
seen_sources.insert(normalized_source);
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(pb) = progress_bar {
|
if let Some(pb) = progress_bar {
|
||||||
|
|
|
@ -121,6 +121,60 @@ fn test_cp_duplicate_files() {
|
||||||
assert_eq!(at.read(TEST_COPY_TO_FOLDER_FILE), "Hello, World!\n");
|
assert_eq!(at.read(TEST_COPY_TO_FOLDER_FILE), "Hello, World!\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_cp_duplicate_folder() {
|
||||||
|
let (at, mut ucmd) = at_and_ucmd!();
|
||||||
|
ucmd.arg("-r")
|
||||||
|
.arg(TEST_COPY_FROM_FOLDER)
|
||||||
|
.arg(TEST_COPY_FROM_FOLDER)
|
||||||
|
.arg(TEST_COPY_TO_FOLDER)
|
||||||
|
.succeeds()
|
||||||
|
.stderr_contains(format!(
|
||||||
|
"source directory '{TEST_COPY_FROM_FOLDER}' specified more than once"
|
||||||
|
));
|
||||||
|
assert!(at.dir_exists(format!("{TEST_COPY_TO_FOLDER}/{TEST_COPY_FROM_FOLDER}").as_str()));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[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_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]
|
#[test]
|
||||||
fn test_cp_same_file() {
|
fn test_cp_same_file() {
|
||||||
let (at, mut ucmd) = at_and_ucmd!();
|
let (at, mut ucmd) = at_and_ucmd!();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue