File tree 2 files changed +26
-1
lines changed 2 files changed +26
-1
lines changed Original file line number Diff line number Diff line change @@ -1496,7 +1496,11 @@ fn context_for(src: &Path, dest: &Path) -> String {
1496
1496
/// Implements a simple backup copy for the destination file.
1497
1497
/// TODO: for the backup, should this function be replaced by `copy_file(...)`?
1498
1498
fn backup_dest ( dest : & Path , backup_path : & Path ) -> CopyResult < PathBuf > {
1499
- fs:: copy ( dest, backup_path) ?;
1499
+ if dest. is_symlink ( ) {
1500
+ fs:: rename ( dest, backup_path) ?;
1501
+ } else {
1502
+ fs:: copy ( dest, backup_path) ?;
1503
+ }
1500
1504
Ok ( backup_path. into ( ) )
1501
1505
}
1502
1506
Original file line number Diff line number Diff line change @@ -678,6 +678,27 @@ fn test_cp_arg_backup() {
678
678
) ;
679
679
}
680
680
681
+ #[ test]
682
+ fn test_cp_arg_backup_with_dest_a_symlink ( ) {
683
+ let ( at, mut ucmd) = at_and_ucmd ! ( ) ;
684
+ let source = "source" ;
685
+ let source_content = "content" ;
686
+ let symlink = "symlink" ;
687
+ let original = "original" ;
688
+ let backup = "symlink~" ;
689
+
690
+ at. write ( source, source_content) ;
691
+ at. write ( original, "original" ) ;
692
+ at. symlink_file ( original, symlink) ;
693
+
694
+ ucmd. arg ( "-b" ) . arg ( source) . arg ( symlink) . succeeds ( ) ;
695
+
696
+ assert ! ( !at. symlink_exists( symlink) ) ;
697
+ assert_eq ! ( source_content, at. read( symlink) ) ;
698
+ assert ! ( at. symlink_exists( backup) ) ;
699
+ assert_eq ! ( original, at. resolve_link( backup) ) ;
700
+ }
701
+
681
702
#[ test]
682
703
fn test_cp_arg_backup_with_other_args ( ) {
683
704
let ( at, mut ucmd) = at_and_ucmd ! ( ) ;
You can’t perform that action at this time.
0 commit comments