@@ -1538,30 +1538,28 @@ impl Build {
15381538 if self . config . dry_run ( ) {
15391539 return ;
15401540 }
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 )
15531542 }
15541543
15551544 /// Copies the `src` directory recursively to `dst`. Both are assumed to exist
15561545 /// when this function is called. Unwanted files or directories can be skipped
15571546 /// by returning `false` from the filter function.
15581547 pub fn cp_filtered ( & self , src : & Path , dst : & Path , filter : & dyn Fn ( & Path ) -> bool ) {
15591548 // Immediately recurse with an empty relative path
1560- self . recurse_ ( src, dst, Path :: new ( "" ) , filter)
1549+ self . recurse_ ( src, dst, Path :: new ( "" ) , filter, true )
15611550 }
15621551
15631552 // 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+ ) {
15651563 for f in self . read_dir ( src) {
15661564 let path = f. path ( ) ;
15671565 let name = path. file_name ( ) . unwrap ( ) ;
@@ -1570,9 +1568,11 @@ impl Build {
15701568 // Only copy file or directory if the filter function returns true
15711569 if filter ( & relative) {
15721570 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+ }
15741574 self . create_dir ( & dst) ;
1575- self . recurse_ ( & path, & dst, & relative, filter) ;
1575+ self . recurse_ ( & path, & dst, & relative, filter, remove_dst_dir ) ;
15761576 } else {
15771577 let _ = fs:: remove_file ( & dst) ;
15781578 self . copy ( & path, & dst) ;
0 commit comments