@@ -2261,7 +2261,8 @@ fn handle_existing_dest(
22612261 dest : & Path ,
22622262 options : & Options ,
22632263 source_in_command_line : bool ,
2264- copied_files : & HashMap < FileInformation , PathBuf > ,
2264+ dest_metadata : & Metadata ,
2265+ copied_files : & mut HashMap < FileInformation , PathBuf > ,
22652266) -> CopyResult < ( ) > {
22662267 // Disallow copying a file to itself, unless `--force` and
22672268 // `--backup` are both specified.
@@ -2283,6 +2284,36 @@ fn handle_existing_dest(
22832284 options. overwrite . verify ( dest, options. debug ) ?;
22842285 }
22852286
2287+ // Refuse the conflict before touching the directory or its backup. A
2288+ // skipped update still counts as a destination for hard-link preservation.
2289+ if dest_metadata. is_dir ( ) && !options. dereference ( source_in_command_line) && source. is_symlink ( )
2290+ {
2291+ if options. copy_mode == CopyMode :: Update
2292+ && !should_update ( & source. symlink_metadata ( ) ?, dest, options) ?
2293+ {
2294+ if options. preserve_hard_links ( ) {
2295+ copied_files. insert (
2296+ FileInformation :: from_path ( source, false ) ?,
2297+ dest. to_path_buf ( ) ,
2298+ ) ;
2299+ }
2300+ return Err ( CpError :: Skipped ( false ) ) ;
2301+ }
2302+ if options. backup != BackupMode :: None {
2303+ if options. copy_mode == CopyMode :: Link {
2304+ let context = translate ! ( "cp-error-cannot-create-hard-link" ,
2305+ "dest" => dest. quote( ) , "source" => source. quote( ) ) ;
2306+ return Err ( translate ! ( "cp-error-file-exists" , "path" => context) . into ( ) ) ;
2307+ }
2308+ return Err ( translate ! ( "cp-error-cannot-create-symbolic-link-file-exists" , "dest" => dest. quote( ) ) . into ( ) ) ;
2309+ }
2310+ return Err (
2311+ translate ! ( "cp-error-cannot-overwrite-directory-with-non-directory" ,
2312+ "dir" => dest. quote( ) , "source" => source. quote( ) )
2313+ . into ( ) ,
2314+ ) ;
2315+ }
2316+
22862317 let mut is_dest_removed = false ;
22872318 let backup_path = backup_control:: get_backup_path ( options. backup , dest, & options. backup_suffix ) ;
22882319 if let Some ( backup_path) = backup_path {
@@ -2551,6 +2582,29 @@ fn print_paths(parents: bool, source: &Path, dest: &Path) -> CopyResult<()> {
25512582 Ok ( ( ) )
25522583}
25532584
2585+ /// Decide whether an existing destination should be updated.
2586+ fn should_update ( source_metadata : & Metadata , dest : & Path , options : & Options ) -> CopyResult < bool > {
2587+ match options. update {
2588+ UpdateMode :: All => Ok ( true ) ,
2589+ UpdateMode :: None => {
2590+ if options. debug {
2591+ println ! ( "skipped {}" , dest. quote( ) ) ;
2592+ }
2593+ Ok ( false )
2594+ }
2595+ UpdateMode :: NoneFail => {
2596+ Err ( translate ! ( "cp-error-not-replacing" , "file" => dest. quote( ) ) . into ( ) )
2597+ }
2598+ UpdateMode :: IfOlder => {
2599+ if source_metadata. modified ( ) ? <= dest. symlink_metadata ( ) ?. modified ( ) ? {
2600+ return Ok ( false ) ;
2601+ }
2602+ options. overwrite . verify ( dest, options. debug ) ?;
2603+ Ok ( true )
2604+ }
2605+ }
2606+ }
2607+
25542608/// Handles the copy mode for a file copy operation.
25552609///
25562610/// This function determines how to copy a file based on the provided options.
@@ -2603,7 +2657,13 @@ fn handle_copy_mode(
26032657 )
26042658 } ) ?;
26052659 }
2606- CopyMode :: Copy => {
2660+ CopyMode :: Copy | CopyMode :: Update => {
2661+ if options. copy_mode == CopyMode :: Update
2662+ && dest. exists ( )
2663+ && !should_update ( source_metadata, dest, options) ?
2664+ {
2665+ return Ok ( PerformedAction :: Skipped ) ;
2666+ }
26072667 copy_helper (
26082668 source,
26092669 dest,
@@ -2624,69 +2684,6 @@ fn handle_copy_mode(
26242684 symlink_file ( source, dest, symlinked_files) ?;
26252685 }
26262686 }
2627- CopyMode :: Update => {
2628- if dest. exists ( ) {
2629- match options. update {
2630- UpdateMode :: All => {
2631- copy_helper (
2632- source,
2633- dest,
2634- options,
2635- context,
2636- source_metadata,
2637- symlinked_files,
2638- source_in_command_line,
2639- created_parent_dirs,
2640- ) ?;
2641- }
2642- UpdateMode :: None => {
2643- if options. debug {
2644- println ! ( "skipped {}" , dest. quote( ) ) ;
2645- }
2646-
2647- return Ok ( PerformedAction :: Skipped ) ;
2648- }
2649- UpdateMode :: NoneFail => {
2650- return Err ( CpError :: Error (
2651- translate ! ( "cp-error-not-replacing" , "file" => dest. quote( ) ) ,
2652- ) ) ;
2653- }
2654- UpdateMode :: IfOlder => {
2655- let dest_metadata = fs:: symlink_metadata ( dest) ?;
2656-
2657- let src_time = source_metadata. modified ( ) ?;
2658- let dest_time = dest_metadata. modified ( ) ?;
2659- if src_time <= dest_time {
2660- return Ok ( PerformedAction :: Skipped ) ;
2661- }
2662-
2663- options. overwrite . verify ( dest, options. debug ) ?;
2664-
2665- copy_helper (
2666- source,
2667- dest,
2668- options,
2669- context,
2670- source_metadata,
2671- symlinked_files,
2672- source_in_command_line,
2673- created_parent_dirs,
2674- ) ?;
2675- }
2676- }
2677- } else {
2678- copy_helper (
2679- source,
2680- dest,
2681- options,
2682- context,
2683- source_metadata,
2684- symlinked_files,
2685- source_in_command_line,
2686- created_parent_dirs,
2687- ) ?;
2688- }
2689- }
26902687 CopyMode :: AttrOnly => {
26912688 // The destination must have the source's type. Creating a regular
26922689 // file for a FIFO or a device both gets the type wrong and, for a
@@ -2854,7 +2851,7 @@ fn copy_file(
28542851 fs:: remove_file ( dest) ?;
28552852 }
28562853
2857- if initial_dest_metadata . is_some ( )
2854+ if let Some ( dest_metadata ) = & initial_dest_metadata
28582855 && ( !options. attributes_only
28592856 || matches ! (
28602857 options. overwrite,
@@ -2879,7 +2876,14 @@ fn copy_file(
28792876 }
28802877 }
28812878 }
2882- handle_existing_dest ( source, dest, options, source_in_command_line, copied_files) ?;
2879+ handle_existing_dest (
2880+ source,
2881+ dest,
2882+ options,
2883+ source_in_command_line,
2884+ dest_metadata,
2885+ copied_files,
2886+ ) ?;
28832887 if are_hardlinks_to_same_file ( source, dest) {
28842888 if options. copy_mode == CopyMode :: Copy {
28852889 return Ok ( ( ) ) ;
0 commit comments