1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

cp: don't fail when --backup=numbered is passed

This commit is contained in:
Sylvestre Ledru 2023-12-23 12:48:38 +01:00
parent 837640bc02
commit 06c98fbdd3
2 changed files with 16 additions and 3 deletions

View file

@ -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() 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) // 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 // If the target file was already created in this cp call, do not overwrite
return Err(Error::Error(format!( return Err(Error::Error(format!(
"will not overwrite just-created '{}' with '{}'", "will not overwrite just-created '{}' with '{}'",

View file

@ -3562,7 +3562,8 @@ fn test_cp_attributes_only() {
#[test] #[test]
fn test_cp_seen_file() { 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("a");
at.mkdir("b"); at.mkdir("b");
@ -3570,11 +3571,21 @@ fn test_cp_seen_file() {
at.write("a/f", "a"); at.write("a/f", "a");
at.write("b/f", "b"); at.write("b/f", "b");
ucmd.arg("a/f") ts.ucmd()
.arg("a/f")
.arg("b/f") .arg("b/f")
.arg("c") .arg("c")
.fails() .fails()
.stderr_contains("will not overwrite just-created 'c/f' with 'b/f'"); .stderr_contains("will not overwrite just-created 'c/f' with 'b/f'");
assert!(at.plus("c").join("f").exists()); 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());
} }