Skip to content

Commit 3cd211c

Browse files
committed
Provide remove_dir_contents
We introduce a new module `portable` to contain it, rather than just putting it striaght in `lib.rs`. (Whether to do this seems a matter of taste.) This is the caller for unix::_remove_file_dir_contents so we can get rid of the temporary `#[allow]` annotation. Signed-off-by: Ian Jackson <[email protected]>
1 parent 18f185b commit 3cd211c

File tree

4 files changed

+29
-2
lines changed

4 files changed

+29
-2
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
A reliable implementation of `remove_dir_all` for Windows. For Unix systems
1010
re-exports `std::fs::remove_dir_all`.
1111

12+
Also provides `remove_dir_contents` for both Windows and Unix. This
13+
removes just the contents of the directory, if it exists.
14+
1215
```rust,no_run
1316
extern crate remove_dir_all;
1417

src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
//!
33
//! This library provides a reliable implementation of `remove_dir_all` for Windows.
44
//! For Unix systems, it re-exports `std::fs::remove_dir_all`.
5+
//!
6+
//! It also provides `remove_dir_contents` for both Unix and Windows.
57
68
#![deny(missing_debug_implementations)]
79
#![deny(missing_docs)]
@@ -22,8 +24,12 @@ mod fs;
2224
#[cfg(not(windows))]
2325
mod unix;
2426

27+
mod portable;
28+
2529
#[cfg(windows)]
2630
pub use self::fs::remove_dir_all;
2731

2832
#[cfg(not(windows))]
2933
pub use std::fs::remove_dir_all;
34+
35+
pub use portable::remove_dir_contents;

src/portable.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use std::io;
2+
use std::path::Path;
3+
4+
#[cfg(windows)]
5+
use crate::fs::_remove_dir_contents;
6+
7+
#[cfg(not(windows))]
8+
use crate::unix::_remove_dir_contents;
9+
10+
/// Deletes the contents of `dir_path`, but not the directory iteself.
11+
///
12+
/// If `dir_path` is a symlink to a directory, deletes the contents
13+
/// of that directory. Fails if `dir_path` does not exist.
14+
pub fn remove_dir_contents<P: AsRef<Path>>(path: P) -> io::Result<()> {
15+
// This wrapper function exists because the core function
16+
// for Windows, in crate::fs, returns a PathBuf, which our
17+
// caller shouldn't see.
18+
_remove_dir_contents(path)?;
19+
Ok(())
20+
}

src/unix.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#![allow(dead_code)]
2-
31
use std::fs;
42
use std::io;
53
use std::path::Path;

0 commit comments

Comments
 (0)