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

cp: fix backup of destination symlink

This commit is contained in:
Daniel Hofstetter 2023-12-27 15:39:54 +01:00
parent bf26eda786
commit 0701f535ac
2 changed files with 26 additions and 1 deletions

View file

@ -1496,7 +1496,11 @@ fn context_for(src: &Path, dest: &Path) -> String {
/// Implements a simple backup copy for the destination file. /// Implements a simple backup copy for the destination file.
/// TODO: for the backup, should this function be replaced by `copy_file(...)`? /// TODO: for the backup, should this function be replaced by `copy_file(...)`?
fn backup_dest(dest: &Path, backup_path: &Path) -> CopyResult<PathBuf> { fn backup_dest(dest: &Path, backup_path: &Path) -> CopyResult<PathBuf> {
fs::copy(dest, backup_path)?; if dest.is_symlink() {
fs::rename(dest, backup_path)?;
} else {
fs::copy(dest, backup_path)?;
}
Ok(backup_path.into()) Ok(backup_path.into())
} }

View file

@ -678,6 +678,27 @@ fn test_cp_arg_backup() {
); );
} }
#[test]
fn test_cp_arg_backup_with_dest_a_symlink() {
let (at, mut ucmd) = at_and_ucmd!();
let source = "source";
let source_content = "content";
let symlink = "symlink";
let original = "original";
let backup = "symlink~";
at.write(source, source_content);
at.write(original, "original");
at.symlink_file(original, symlink);
ucmd.arg("-b").arg(source).arg(symlink).succeeds();
assert!(!at.symlink_exists(symlink));
assert_eq!(source_content, at.read(symlink));
assert!(at.symlink_exists(backup));
assert_eq!(original, at.resolve_link(backup));
}
#[test] #[test]
fn test_cp_arg_backup_with_other_args() { fn test_cp_arg_backup_with_other_args() {
let (at, mut ucmd) = at_and_ucmd!(); let (at, mut ucmd) = at_and_ucmd!();