win32_remove_dir_all/
lib.rs

1// Copyright 2020 Theodore Cipicchio
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9//! A [`std::fs::remove_dir_all`] replacement using the Windows Shell and Property System APIs on
10//! Windows.
11//!
12//! The current Windows implementation of `remove_dir_all` in the Rust standard library has a
13//! [long-standing issue] with consistently deleting directories. The [Windows Shell] and [Windows
14//! Property System] APIs both provide methods for recursively deleting directories with consistent
15//! results by way of [`SHFileOperationW`] and [`IFileOperation`], respectively, although a stable
16//! solution compatible with UWP apps has not been settled on in the context of the associated
17//! GitHub issue.
18//!
19//! This crate provides a `remove_dir_all` implementation based on both [`SHFileOperationW`] and
20//! [`IFileOperation`], with the former used as a fallback if the latter is not supported
21//! ([`IFileOperation`] is recommended over [`SHFileOperationW`], but it is only supported on
22//! Windows Vista and later). For non-Windows platforms, the standard library `remove_dir_all`
23//! function is re-exported for convenience.
24//!
25//! Due to the lack of Shell and Property System API support for UWP apps, UWP app developers are
26//! recommended to use the [`remove_dir_all` crate] instead, as it provides an alternative
27//! implementation that does not rely on the Shell or Property System APIs.
28//!
29//! # Examples
30//!
31//! The [`remove_dir_all`](fn.remove_dir_all.html) function provided by this crate can be used as a
32//! drop-in replacement for [`std::fs::remove_dir_all`], even in code targeting multiple platforms;
33//! [`std::fs::remove_dir_all`] will be used automatically on non-Windows targets.
34//!
35//! ```no_run
36//! use std::{error::Error, fs, path::Path};
37//! use win32_remove_dir_all::remove_dir_all;
38//!
39//! fn main() -> Result<(), Box<dyn Error>> {
40//!     // Create a directory with a couple files residing in it.
41//!     fs::create_dir("foo")?;
42//!     fs::OpenOptions::new().create(true).write(true).open("foo/bar")?;
43//!     fs::OpenOptions::new().create(true).write(true).open("foo/baz")?;
44//!
45//!     // Delete the directory and all its contents as you would with `std::fs::remove_dir_all`.
46//!     remove_dir_all("foo")?;
47//!
48//!     Ok(())
49//! }
50//! ```
51//!
52//! # Disabling Property System ([`IFileOperation`]) Support
53//!
54//! Support for [`IFileOperation`] is gated behind the `property_system_api` crate feature, which is
55//! enabled by default. It is not necessary to disable this feature in order to support Windows
56//! versions prior to Windows Vista, as [`SHFileOperationW`] will be used instead if
57//! [`IFileOperation`] is unavailable. It can still be disabled if desired, such as if build times
58//! or sizes are of concern, in which case [`SHFileOperationW`] will always be used regardless of
59//! the Windows version.
60//!
61//! [`std::fs::remove_dir_all`]: https://doc.rust-lang.org/std/fs/fn.remove_dir_all.html
62//! [long-standing issue]: https://github.com/rust-lang/rust/issues/29497
63//! [Windows Shell]: https://docs.microsoft.com/en-us/previous-versions/windows/desktop/legacy/bb773177(v=vs.85)
64//! [Windows Property System]: https://docs.microsoft.com/en-us/windows/win32/properties/windows-properties-system
65//! [`SHFileOperationW`]: https://docs.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-shfileoperationw
66//! [`IFileOperation`]: https://docs.microsoft.com/en-us/windows/win32/api/shobjidl_core/nn-shobjidl_core-ifileoperation
67//! [`remove_dir_all` crate]: https://crates.io/crates/remove_dir_all
68
69#[cfg(windows)]
70mod windows;
71#[cfg(windows)]
72pub use windows::remove_dir_all;
73
74#[cfg(not(windows))]
75pub use std::fs::remove_dir_all;