From 06c98fbdd33e28110e8442dd4e6b261c3b6a1e22 Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Sat, 23 Dec 2023 12:48:38 +0100 Subject: [PATCH] cp: don't fail when --backup=numbered is passed --- src/uu/cp/src/cp.rs | 4 +++- tests/by-util/test_cp.rs | 15 +++++++++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/uu/cp/src/cp.rs b/src/uu/cp/src/cp.rs index d7aeea1b9..4d002359f 100644 --- a/src/uu/cp/src/cp.rs +++ b/src/uu/cp/src/cp.rs @@ -1203,7 +1203,9 @@ pub fn copy(sources: &[PathBuf], target: &Path, options: &Options) -> CopyResult if fs::metadata(&dest).is_ok() && !fs::symlink_metadata(&dest)?.file_type().is_symlink() { // There is already a file and it isn't a symlink (managed in a different place) - if copied_destinations.contains(&dest) { + if copied_destinations.contains(&dest) + && options.backup != BackupMode::NumberedBackup + { // If the target file was already created in this cp call, do not overwrite return Err(Error::Error(format!( "will not overwrite just-created '{}' with '{}'", diff --git a/tests/by-util/test_cp.rs b/tests/by-util/test_cp.rs index 5227f0194..eda5dd4c6 100644 --- a/tests/by-util/test_cp.rs +++ b/tests/by-util/test_cp.rs @@ -3562,7 +3562,8 @@ fn test_cp_attributes_only() { #[test] fn test_cp_seen_file() { - let (at, mut ucmd) = at_and_ucmd!(); + let ts = TestScenario::new(util_name!()); + let at = &ts.fixtures; at.mkdir("a"); at.mkdir("b"); @@ -3570,11 +3571,21 @@ fn test_cp_seen_file() { at.write("a/f", "a"); at.write("b/f", "b"); - ucmd.arg("a/f") + ts.ucmd() + .arg("a/f") .arg("b/f") .arg("c") .fails() .stderr_contains("will not overwrite just-created 'c/f' with 'b/f'"); assert!(at.plus("c").join("f").exists()); + + ts.ucmd() + .arg("--backup=numbered") + .arg("a/f") + .arg("b/f") + .arg("c") + .succeeds(); + assert!(at.plus("c").join("f").exists()); + assert!(at.plus("c").join("f.~1~").exists()); }