1//! Misc filesystem related helpers for use by bootstrap and tools.
2use std::fs::Metadata;
3use std::path::Path;
4use std::{fs, io};
56#[cfg(test)]
7mod tests;
89/// Helper to ignore [`std::io::ErrorKind::NotFound`], but still propagate other
10/// [`std::io::ErrorKind`]s.
11pub fn ignore_not_found<Op>(mut op: Op) -> io::Result<()>
12where
13Op: FnMut() -> io::Result<()>,
14{
15match op() {
16Ok(()) => Ok(()),
17Err(e) if e.kind() == io::ErrorKind::NotFound => Ok(()),
18Err(e) => Err(e),
19 }
20}
2122/// A wrapper around [`std::fs::remove_dir_all`] that can also be used on *non-directory entries*,
23/// including files and symbolic links.
24///
25/// - This will produce an error if the target path is not found.
26/// - Like [`std::fs::remove_dir_all`], this helper does not traverse symbolic links, will remove
27/// symbolic link itself.
28/// - This helper is **not** robust against races on the underlying filesystem, behavior is
29/// unspecified if this helper is called concurrently.
30/// - This helper is not robust against TOCTOU problems.
31///
32/// FIXME: this implementation is insufficiently robust to replace bootstrap's clean `rm_rf`
33/// implementation:
34///
35/// - This implementation currently does not perform retries.
36#[track_caller]
37pub fn recursive_remove<P: AsRef<Path>>(path: P) -> io::Result<()> {
38let path = path.as_ref();
39let metadata = fs::symlink_metadata(path)?;
40#[cfg(windows)]
41let is_dir_like = |meta: &fs::Metadata| {
42use std::os::windows::fs::FileTypeExt;
43 meta.is_dir() || meta.file_type().is_symlink_dir()
44 };
45#[cfg(not(windows))]
46let is_dir_like = fs::Metadata::is_dir;
4748if is_dir_like(&metadata) {
49 fs::remove_dir_all(path)
50 } else {
51 try_remove_op_set_perms(fs::remove_file, path, metadata)
52 }
53}
5455fn try_remove_op_set_perms<'p, Op>(mut op: Op, path: &'p Path, metadata: Metadata) -> io::Result<()>
56where
57Op: FnMut(&'p Path) -> io::Result<()>,
58{
59match op(path) {
60Ok(()) => Ok(()),
61Err(e) if e.kind() == io::ErrorKind::PermissionDenied => {
62let mut perms = metadata.permissions();
63 perms.set_readonly(false);
64 fs::set_permissions(path, perms)?;
65 op(path)
66 }
67Err(e) => Err(e),
68 }
69}