@@ -1538,30 +1538,28 @@ impl Build {
1538
1538
if self . config . dry_run ( ) {
1539
1539
return ;
1540
1540
}
1541
- for f in self . read_dir ( src) {
1542
- let path = f. path ( ) ;
1543
- let name = path. file_name ( ) . unwrap ( ) ;
1544
- let dst = dst. join ( name) ;
1545
- if t ! ( f. file_type( ) ) . is_dir ( ) {
1546
- t ! ( fs:: create_dir_all( & dst) ) ;
1547
- self . cp_r ( & path, & dst) ;
1548
- } else {
1549
- let _ = fs:: remove_file ( & dst) ;
1550
- self . copy ( & path, & dst) ;
1551
- }
1552
- }
1541
+ self . recurse_ ( src, dst, Path :: new ( "" ) , & |_| true , false )
1553
1542
}
1554
1543
1555
1544
/// Copies the `src` directory recursively to `dst`. Both are assumed to exist
1556
1545
/// when this function is called. Unwanted files or directories can be skipped
1557
1546
/// by returning `false` from the filter function.
1558
1547
pub fn cp_filtered ( & self , src : & Path , dst : & Path , filter : & dyn Fn ( & Path ) -> bool ) {
1559
1548
// Immediately recurse with an empty relative path
1560
- self . recurse_ ( src, dst, Path :: new ( "" ) , filter)
1549
+ self . recurse_ ( src, dst, Path :: new ( "" ) , filter, true )
1561
1550
}
1562
1551
1563
1552
// Inner function does the actual work
1564
- fn recurse_ ( & self , src : & Path , dst : & Path , relative : & Path , filter : & dyn Fn ( & Path ) -> bool ) {
1553
+ //
1554
+ // FIXME: consider merging cp_filtered and cp_r into one function
1555
+ fn recurse_ (
1556
+ & self ,
1557
+ src : & Path ,
1558
+ dst : & Path ,
1559
+ relative : & Path ,
1560
+ filter : & dyn Fn ( & Path ) -> bool ,
1561
+ remove_dst_dir : bool ,
1562
+ ) {
1565
1563
for f in self . read_dir ( src) {
1566
1564
let path = f. path ( ) ;
1567
1565
let name = path. file_name ( ) . unwrap ( ) ;
@@ -1570,9 +1568,11 @@ impl Build {
1570
1568
// Only copy file or directory if the filter function returns true
1571
1569
if filter ( & relative) {
1572
1570
if t ! ( f. file_type( ) ) . is_dir ( ) {
1573
- let _ = fs:: remove_dir_all ( & dst) ;
1571
+ if remove_dst_dir {
1572
+ let _ = fs:: remove_dir_all ( & dst) ;
1573
+ }
1574
1574
self . create_dir ( & dst) ;
1575
- self . recurse_ ( & path, & dst, & relative, filter) ;
1575
+ self . recurse_ ( & path, & dst, & relative, filter, remove_dst_dir ) ;
1576
1576
} else {
1577
1577
let _ = fs:: remove_file ( & dst) ;
1578
1578
self . copy ( & path, & dst) ;
0 commit comments