@@ -1668,10 +1668,23 @@ impl Build {
1668
1668
/// You can neither rely on this being a copy nor it being a link,
1669
1669
/// so do not write to dst.
1670
1670
pub fn copy_link ( & self , src : & Path , dst : & Path ) {
1671
- self . copy_link_internal ( src, dst, false ) ;
1671
+ self . copy_internal ( src, dst, false , true ) ;
1672
1672
}
1673
1673
1674
- fn copy_link_internal ( & self , src : & Path , dst : & Path , dereference_symlinks : bool ) {
1674
+ /// Links a file from `src` to `dst`.
1675
+ /// Unlike, [`Build::copy_link`], this makes an actual copy, which is usually not required,
1676
+ /// so `copy_link` should be used instead if possible.
1677
+ pub fn copy ( & self , src : & Path , dst : & Path ) {
1678
+ self . copy_internal ( src, dst, false , false ) ;
1679
+ }
1680
+
1681
+ fn copy_internal (
1682
+ & self ,
1683
+ src : & Path ,
1684
+ dst : & Path ,
1685
+ dereference_symlinks : bool ,
1686
+ link_if_possible : bool ,
1687
+ ) {
1675
1688
if self . config . dry_run ( ) {
1676
1689
return ;
1677
1690
}
@@ -1691,7 +1704,7 @@ impl Build {
1691
1704
return ;
1692
1705
}
1693
1706
}
1694
- if let Ok ( ( ) ) = fs:: hard_link ( & src, dst) {
1707
+ if link_if_possible && fs:: hard_link ( & src, dst) . is_ok ( ) {
1695
1708
// Attempt to "easy copy" by creating a hard link
1696
1709
// (symlinks don't work on windows), but if that fails
1697
1710
// just fall back to a slow `copy` operation.
@@ -1726,6 +1739,28 @@ impl Build {
1726
1739
}
1727
1740
}
1728
1741
1742
+ /// Copies the `src` directory recursively to `dst`. Both are assumed to exist
1743
+ /// when this function is called.
1744
+ /// Unlike, [`Build::cp_link_r`], this makes an actual copy, which is usually not required,
1745
+ /// so `cp_link_r` should be used instead if possible.
1746
+ pub fn cp_r ( & self , src : & Path , dst : & Path ) {
1747
+ if self . config . dry_run ( ) {
1748
+ return ;
1749
+ }
1750
+ for f in self . read_dir ( src) {
1751
+ let path = f. path ( ) ;
1752
+ let name = path. file_name ( ) . unwrap ( ) ;
1753
+ let dst = dst. join ( name) ;
1754
+ if t ! ( f. file_type( ) ) . is_dir ( ) {
1755
+ t ! ( fs:: create_dir_all( & dst) ) ;
1756
+ self . cp_r ( & path, & dst) ;
1757
+ } else {
1758
+ let _ = fs:: remove_file ( & dst) ;
1759
+ self . copy ( & path, & dst) ;
1760
+ }
1761
+ }
1762
+ }
1763
+
1729
1764
/// Copies the `src` directory recursively to `dst`. Both are assumed to exist
1730
1765
/// when this function is called.
1731
1766
/// Will attempt to use hard links if possible and fall back to copying.
@@ -1779,7 +1814,9 @@ impl Build {
1779
1814
if !src. exists ( ) {
1780
1815
panic ! ( "ERROR: File \" {}\" not found!" , src. display( ) ) ;
1781
1816
}
1782
- self . copy_link_internal ( src, & dst, true ) ;
1817
+
1818
+ self . copy_internal ( src, & dst, true , true ) ;
1819
+
1783
1820
chmod ( & dst, perms) ;
1784
1821
}
1785
1822
0 commit comments