mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 19:47:45 +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:
commit
36039a819d
2 changed files with 22 additions and 2 deletions
|
@ -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());
|
||||
}
|
||||
|
||||
|
|
|
@ -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!();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue