mirror of
https://github.com/uutils/coreutils.git
synced 2026-06-10 15:48:22 -07:00
Merge pull request #5731 from cakebaker/cp_backup_with_dest_a_symlink
cp: fix backup of destination symlink
This commit is contained in:
+5
-1
@@ -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())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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!();
|
||||||
|
|||||||
Reference in New Issue
Block a user