@@ -438,23 +438,34 @@ fn link(src: &Path, dst: &Path, settings: &Settings) -> UResult<()> {
438438 }
439439 }
440440
441- if settings. symbolic {
442- symlink ( & source, dst) ?;
443- } else {
444- let p = if settings. logical && source. is_symlink ( ) {
445- fs:: canonicalize ( & source)
446- . map_err_context ( || translate ! ( "ln-failed-to-access" , "file" => source. quote( ) ) ) ?
441+ let res = ( || -> UResult < ( ) > {
442+ if settings. symbolic {
443+ Ok ( symlink ( & source, dst) ?)
447444 } else {
448- source. to_path_buf ( )
449- } ;
450- if let Err ( e) = fs:: hard_link ( & p, dst) {
451- if p. is_dir ( ) {
452- return Err ( LnError :: FailedToCreateHardLinkDir ( source. to_path_buf ( ) ) . into ( ) ) ;
445+ let p = if settings. logical && source. is_symlink ( ) {
446+ fs:: canonicalize ( & source) . map_err_context (
447+ || translate ! ( "ln-failed-to-access" , "file" => source. quote( ) ) ,
448+ ) ?
449+ } else {
450+ source. to_path_buf ( )
451+ } ;
452+ if let Err ( e) = fs:: hard_link ( & p, dst) {
453+ if p. is_dir ( ) {
454+ return Err ( LnError :: FailedToCreateHardLinkDir ( source. to_path_buf ( ) ) . into ( ) ) ;
455+ }
456+ return Err ( e) . map_err_context ( || {
457+ translate ! ( "ln-failed-to-create-hard-link" , "source" => source. quote( ) , "dest" => dst. quote( ) )
458+ } ) ;
453459 }
454- return Err ( e) . map_err_context ( || {
455- translate ! ( "ln-failed-to-create-hard-link" , "source" => source. quote( ) , "dest" => dst. quote( ) )
456- } ) ;
460+ Ok ( ( ) )
461+ }
462+ } ) ( ) ;
463+ if res. is_err ( ) {
464+ if let Some ( ref p) = backup_path {
465+ fs:: rename ( p, dst)
466+ . map_err_context ( || translate ! ( "ln-cannot-backup" , "file" => dst. quote( ) ) ) ?;
457467 }
468+ res?;
458469 }
459470
460471 if settings. verbose {
0 commit comments