Skip to content

Commit 4d3bf1f

Browse files
committed
run-make-support: re-export recursive_remove
This facade is like other `run_make_support::fs` APIs that panic-on-failure but includes the path that the operation was called on in the panic message.
1 parent 7e22403 commit 4d3bf1f

File tree

1 file changed

+19
-2
lines changed
  • src/tools/run-make-support/src

1 file changed

+19
-2
lines changed

src/tools/run-make-support/src/fs.rs

+19-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::fs::FileType;
12
use std::io;
23
use std::path::{Path, PathBuf};
34

@@ -6,7 +7,8 @@ use std::path::{Path, PathBuf};
67
pub fn copy_symlink(src: impl AsRef<Path>, dst: impl AsRef<Path>) {
78
let src = src.as_ref();
89
let dst = dst.as_ref();
9-
if let Err(e) = copy_symlink_raw(src, dst) {
10+
let metadata = symlink_metadata(src);
11+
if let Err(e) = copy_symlink_raw(metadata.file_type(), src, dst) {
1012
panic!("failed to copy symlink from `{}` to `{}`: {e}", src.display(), dst.display(),);
1113
}
1214
}
@@ -56,7 +58,7 @@ pub fn copy_dir_all(src: impl AsRef<Path>, dst: impl AsRef<Path>) {
5658
if ty.is_dir() {
5759
copy_dir_all_inner(entry.path(), dst.join(entry.file_name()))?;
5860
} else if ty.is_symlink() {
59-
copy_symlink_raw(entry.path(), dst.join(entry.file_name()))?;
61+
copy_symlink_raw(ty, entry.path(), dst.join(entry.file_name()))?;
6062
} else {
6163
std::fs::copy(entry.path(), dst.join(entry.file_name()))?;
6264
}
@@ -82,6 +84,21 @@ pub fn read_dir_entries<P: AsRef<Path>, F: FnMut(&Path)>(dir: P, mut callback: F
8284
}
8385
}
8486

87+
/// A wrapper around [`build_helper::fs::recursive_remove`] which includes the file path in the
88+
/// panic message.
89+
///
90+
/// This handles removing symlinks on Windows (e.g. symlink-to-file will be removed via
91+
/// [`std::fs::remove_file`] while symlink-to-dir will be removed via [`std::fs::remove_dir`]).
92+
#[track_caller]
93+
pub fn recursive_remove<P: AsRef<Path>>(path: P) {
94+
if let Err(e) = build_helper::fs::recursive_remove(path.as_ref()) {
95+
panic!(
96+
"failed to recursive remove filesystem entities at `{}`: {e}",
97+
path.as_ref().display()
98+
);
99+
}
100+
}
101+
85102
/// A wrapper around [`std::fs::remove_file`] which includes the file path in the panic message.
86103
#[track_caller]
87104
pub fn remove_file<P: AsRef<Path>>(path: P) {

0 commit comments

Comments
 (0)