Skip to content

Commit 549d377

Browse files
committed
unix: Provide remove_file_or_dir_all
The implementation is relatively straightforward. It's a shame that we need to reach for libc to get the error handling right! Signed-off-by: Ian Jackson <[email protected]>
1 parent 128ebda commit 549d377

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,8 @@ categories = ["filesystem"]
1919
[target.'cfg(windows)'.dependencies]
2020
winapi = { version = "0.3", features = ["std", "errhandlingapi", "winerror", "fileapi", "winbase"]}
2121

22+
[target.'cfg(not(windows))'.dependencies]
23+
libc = "0.2"
24+
2225
[dev-dependencies]
2326
doc-comment = "0.3"

src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,11 @@ pub use self::fs::remove_dir_all;
2828
#[cfg(windows)]
2929
use self::fs::remove_file_or_dir_all;
3030

31+
#[cfg(not(windows))]
32+
mod unix;
33+
34+
#[cfg(not(windows))]
35+
use self::unix::remove_file_or_dir_all;
36+
3137
#[cfg(not(windows))]
3238
pub use std::fs::remove_dir_all;

src/unix.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
use std::fs;
2+
use std::io;
3+
use std::path::Path;
4+
5+
extern crate libc;
6+
7+
pub(crate) fn remove_file_or_dir_all<P: AsRef<Path>>(path: P) -> io::Result<()> {
8+
match fs::remove_file(&path) {
9+
Err(e) if e.raw_os_error() == Some(libc::EISDIR) => fs::remove_dir_all(&path),
10+
r => r,
11+
}
12+
}

0 commit comments

Comments
 (0)