@@ -1646,12 +1646,24 @@ impl Build {
16461646 paths
16471647 }
16481648
1649+ /// Copies a file from `src` to `dst`
1650+ // TODO: Rename to cp_r or something like that after #122590 has been merged.
1651+ pub fn copy_but_like_really_copy ( & self , src : & Path , dst : & Path ) {
1652+ self . copy_internal ( src, dst, false , false ) ;
1653+ }
1654+
16491655 /// Copies a file from `src` to `dst`
16501656 pub fn copy ( & self , src : & Path , dst : & Path ) {
1651- self . copy_internal ( src, dst, false ) ;
1657+ self . copy_internal ( src, dst, false , true ) ;
16521658 }
16531659
1654- fn copy_internal ( & self , src : & Path , dst : & Path , dereference_symlinks : bool ) {
1660+ fn copy_internal (
1661+ & self ,
1662+ src : & Path ,
1663+ dst : & Path ,
1664+ dereference_symlinks : bool ,
1665+ link_if_possible : bool ,
1666+ ) {
16551667 if self . config . dry_run ( ) {
16561668 return ;
16571669 }
@@ -1671,7 +1683,7 @@ impl Build {
16711683 return ;
16721684 }
16731685 }
1674- if let Ok ( ( ) ) = fs:: hard_link ( & src, dst) {
1686+ if link_if_possible && fs:: hard_link ( & src, dst) . is_ok ( ) {
16751687 // Attempt to "easy copy" by creating a hard link
16761688 // (symlinks don't work on windows), but if that fails
16771689 // just fall back to a slow `copy` operation.
@@ -1706,6 +1718,25 @@ impl Build {
17061718 }
17071719 }
17081720
1721+ // TODO: Rename to cp_r or something like that after #122590 has been merged.
1722+ pub fn cp_r_but_like_really_copy ( & self , src : & Path , dst : & Path ) {
1723+ if self . config . dry_run ( ) {
1724+ return ;
1725+ }
1726+ for f in self . read_dir ( src) {
1727+ let path = f. path ( ) ;
1728+ let name = path. file_name ( ) . unwrap ( ) ;
1729+ let dst = dst. join ( name) ;
1730+ if t ! ( f. file_type( ) ) . is_dir ( ) {
1731+ t ! ( fs:: create_dir_all( & dst) ) ;
1732+ self . cp_r_but_like_really_copy ( & path, & dst) ;
1733+ } else {
1734+ let _ = fs:: remove_file ( & dst) ;
1735+ self . copy_but_like_really_copy ( & path, & dst) ;
1736+ }
1737+ }
1738+ }
1739+
17091740 /// Copies the `src` directory recursively to `dst`. Both are assumed to exist
17101741 /// when this function is called. Unwanted files or directories can be skipped
17111742 /// by returning `false` from the filter function.
@@ -1751,7 +1782,7 @@ impl Build {
17511782 if !src. exists ( ) {
17521783 panic ! ( "ERROR: File \" {}\" not found!" , src. display( ) ) ;
17531784 }
1754- self . copy_internal ( src, & dst, true ) ;
1785+ self . copy_internal ( src, & dst, true , true ) ;
17551786 chmod ( & dst, perms) ;
17561787 }
17571788
0 commit comments