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

Merge pull request #5739 from cakebaker/cp_backup_with_symlink_to_source

cp: backup dest symlink linking to source
This commit is contained in:
Sylvestre Ledru 2024-01-14 23:30:06 +01:00 committed by GitHub
commit 36039a819d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 2 deletions

View file

@ -1510,7 +1510,7 @@ fn backup_dest(dest: &Path, backup_path: &Path) -> CopyResult<PathBuf> {
///
/// Copying to the same file is only allowed if both `--backup` and
/// `--force` are specified and the file is a regular file.
fn is_forbidden_copy_to_same_file(
fn is_forbidden_to_copy_to_same_file(
source: &Path,
dest: &Path,
options: &Options,
@ -1522,6 +1522,7 @@ fn is_forbidden_copy_to_same_file(
options.dereference(source_in_command_line) || !source.is_symlink();
paths_refer_to_same_file(source, dest, dereference_to_compare)
&& !(options.force() && options.backup != BackupMode::NoBackup)
&& !(dest.is_symlink() && options.backup != BackupMode::NoBackup)
}
/// Back up, remove, or leave intact the destination file, depending on the options.
@ -1533,7 +1534,7 @@ fn handle_existing_dest(
) -> CopyResult<()> {
// Disallow copying a file to itself, unless `--force` and
// `--backup` are both specified.
if is_forbidden_copy_to_same_file(source, dest, options, source_in_command_line) {
if is_forbidden_to_copy_to_same_file(source, dest, options, source_in_command_line) {
return Err(format!("{} and {} are the same file", source.quote(), dest.quote()).into());
}

View file

@ -731,6 +731,25 @@ fn test_cp_arg_backup_with_dest_a_symlink() {
assert_eq!(original, at.resolve_link(backup));
}
#[test]
fn test_cp_arg_backup_with_dest_a_symlink_to_source() {
let (at, mut ucmd) = at_and_ucmd!();
let source = "source";
let source_content = "content";
let symlink = "symlink";
let backup = "symlink~";
at.write(source, source_content);
at.symlink_file(source, 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!(source, at.resolve_link(backup));
}
#[test]
fn test_cp_arg_backup_with_other_args() {
let (at, mut ucmd) = at_and_ucmd!();