Skip to content

Commit

Permalink
run-make-support: re-export recursive_remove
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jieyouxu committed Dec 22, 2024
1 parent 7e22403 commit 4d3bf1f
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/tools/run-make-support/src/fs.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::fs::FileType;
use std::io;
use std::path::{Path, PathBuf};

Expand All @@ -6,7 +7,8 @@ use std::path::{Path, PathBuf};
pub fn copy_symlink(src: impl AsRef<Path>, dst: impl AsRef<Path>) {
let src = src.as_ref();
let dst = dst.as_ref();
if let Err(e) = copy_symlink_raw(src, dst) {
let metadata = symlink_metadata(src);
if let Err(e) = copy_symlink_raw(metadata.file_type(), src, dst) {
panic!("failed to copy symlink from `{}` to `{}`: {e}", src.display(), dst.display(),);
}
}
Expand Down Expand Up @@ -56,7 +58,7 @@ pub fn copy_dir_all(src: impl AsRef<Path>, dst: impl AsRef<Path>) {
if ty.is_dir() {
copy_dir_all_inner(entry.path(), dst.join(entry.file_name()))?;
} else if ty.is_symlink() {
copy_symlink_raw(entry.path(), dst.join(entry.file_name()))?;
copy_symlink_raw(ty, entry.path(), dst.join(entry.file_name()))?;
} else {
std::fs::copy(entry.path(), dst.join(entry.file_name()))?;
}
Expand All @@ -82,6 +84,21 @@ pub fn read_dir_entries<P: AsRef<Path>, F: FnMut(&Path)>(dir: P, mut callback: F
}
}

/// A wrapper around [`build_helper::fs::recursive_remove`] which includes the file path in the
/// panic message.
///
/// This handles removing symlinks on Windows (e.g. symlink-to-file will be removed via
/// [`std::fs::remove_file`] while symlink-to-dir will be removed via [`std::fs::remove_dir`]).
#[track_caller]
pub fn recursive_remove<P: AsRef<Path>>(path: P) {
if let Err(e) = build_helper::fs::recursive_remove(path.as_ref()) {
panic!(
"failed to recursive remove filesystem entities at `{}`: {e}",
path.as_ref().display()
);
}
}

/// A wrapper around [`std::fs::remove_file`] which includes the file path in the panic message.
#[track_caller]
pub fn remove_file<P: AsRef<Path>>(path: P) {
Expand Down

0 comments on commit 4d3bf1f

Please sign in to comment.