From 4f9723396de323ea85d5d5c4caf7415e1f7fd7ec Mon Sep 17 00:00:00 2001 From: klensy Date: Thu, 18 May 2023 20:15:01 +0300 Subject: [PATCH 1/3] half merge cp_r and cp_filtered --- src/bootstrap/lib.rs | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/src/bootstrap/lib.rs b/src/bootstrap/lib.rs index 3756976dee062..e33d0617ab7d4 100644 --- a/src/bootstrap/lib.rs +++ b/src/bootstrap/lib.rs @@ -1538,18 +1538,7 @@ impl Build { if self.config.dry_run() { return; } - for f in self.read_dir(src) { - let path = f.path(); - let name = path.file_name().unwrap(); - let dst = dst.join(name); - if t!(f.file_type()).is_dir() { - t!(fs::create_dir_all(&dst)); - self.cp_r(&path, &dst); - } else { - let _ = fs::remove_file(&dst); - self.copy(&path, &dst); - } - } + self.recurse_(src, dst, Path::new(""), &|_| true, false) } /// Copies the `src` directory recursively to `dst`. Both are assumed to exist @@ -1557,11 +1546,20 @@ impl Build { /// by returning `false` from the filter function. pub fn cp_filtered(&self, src: &Path, dst: &Path, filter: &dyn Fn(&Path) -> bool) { // Immediately recurse with an empty relative path - self.recurse_(src, dst, Path::new(""), filter) + self.recurse_(src, dst, Path::new(""), filter, true) } // Inner function does the actual work - fn recurse_(&self, src: &Path, dst: &Path, relative: &Path, filter: &dyn Fn(&Path) -> bool) { + // + // FIXME: consider merging cp_filtered and cp_r into one function + fn recurse_( + &self, + src: &Path, + dst: &Path, + relative: &Path, + filter: &dyn Fn(&Path) -> bool, + remove_dst_dir: bool, + ) { for f in self.read_dir(src) { let path = f.path(); let name = path.file_name().unwrap(); @@ -1570,9 +1568,11 @@ impl Build { // Only copy file or directory if the filter function returns true if filter(&relative) { if t!(f.file_type()).is_dir() { - let _ = fs::remove_dir_all(&dst); + if remove_dst_dir { + let _ = fs::remove_dir_all(&dst); + } self.create_dir(&dst); - self.recurse_(&path, &dst, &relative, filter); + self.recurse_(&path, &dst, &relative, filter, remove_dst_dir); } else { let _ = fs::remove_file(&dst); self.copy(&path, &dst); From 1d4a0bf875532e875becf72478e649e2296a65ab Mon Sep 17 00:00:00 2001 From: klensy Date: Sun, 21 May 2023 14:10:45 +0300 Subject: [PATCH 2/3] drop remove_dst_dir flag and stop using remove_dir_all inside cp_filtered --- src/bootstrap/lib.rs | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/src/bootstrap/lib.rs b/src/bootstrap/lib.rs index e33d0617ab7d4..f2a150bfe0f53 100644 --- a/src/bootstrap/lib.rs +++ b/src/bootstrap/lib.rs @@ -1538,7 +1538,7 @@ impl Build { if self.config.dry_run() { return; } - self.recurse_(src, dst, Path::new(""), &|_| true, false) + self.recurse_(src, dst, Path::new(""), &|_| true) } /// Copies the `src` directory recursively to `dst`. Both are assumed to exist @@ -1546,20 +1546,13 @@ impl Build { /// by returning `false` from the filter function. pub fn cp_filtered(&self, src: &Path, dst: &Path, filter: &dyn Fn(&Path) -> bool) { // Immediately recurse with an empty relative path - self.recurse_(src, dst, Path::new(""), filter, true) + self.recurse_(src, dst, Path::new(""), filter) } // Inner function does the actual work // // FIXME: consider merging cp_filtered and cp_r into one function - fn recurse_( - &self, - src: &Path, - dst: &Path, - relative: &Path, - filter: &dyn Fn(&Path) -> bool, - remove_dst_dir: bool, - ) { + fn recurse_(&self, src: &Path, dst: &Path, relative: &Path, filter: &dyn Fn(&Path) -> bool) { for f in self.read_dir(src) { let path = f.path(); let name = path.file_name().unwrap(); @@ -1568,11 +1561,8 @@ impl Build { // Only copy file or directory if the filter function returns true if filter(&relative) { if t!(f.file_type()).is_dir() { - if remove_dst_dir { - let _ = fs::remove_dir_all(&dst); - } self.create_dir(&dst); - self.recurse_(&path, &dst, &relative, filter, remove_dst_dir); + self.recurse_(&path, &dst, &relative, filter); } else { let _ = fs::remove_file(&dst); self.copy(&path, &dst); From f3febf3bd16fc2670b7ee446598cfdf6881399f6 Mon Sep 17 00:00:00 2001 From: klensy Date: Sun, 21 May 2023 14:50:41 +0300 Subject: [PATCH 3/3] dont call remove_file in cp_filtered, as self.copy will call copy_internal which will call remove_file itself --- src/bootstrap/lib.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/bootstrap/lib.rs b/src/bootstrap/lib.rs index f2a150bfe0f53..126ef6e31eff0 100644 --- a/src/bootstrap/lib.rs +++ b/src/bootstrap/lib.rs @@ -1564,7 +1564,6 @@ impl Build { self.create_dir(&dst); self.recurse_(&path, &dst, &relative, filter); } else { - let _ = fs::remove_file(&dst); self.copy(&path, &dst); } }