Skip to content

Commit 90071cb

Browse files
committed
fs: Provide _remove_dir_contents
This splits remove_dir_all into two: one part is the thin wrapper for _delete_dir_contents, and the other part is responsible for deleting the top-level directory. We are going to expose a function for only deleting the contents, and this will be its primary building block. I provided two entrypoints, one of which wraps the other, rather than a boolean and a conditional, because IMO it mekes the code clearer. (It would also have been possible to duplicate the canonicalize call, but I felt that was worse.) There is no functional change yet. Signed-off-by: Ian Jackson <[email protected]>
1 parent 0b8a207 commit 90071cb

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/fs.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ pub fn remove_dir_all<P: AsRef<Path>>(path: P) -> io::Result<()> {
6060
// To handle files with names like `CON` and `morse .. .`, and when a
6161
// directory structure is so deep it needs long path names the path is first
6262
// converted to the Win32 file namespace by calling `canonicalize()`.
63-
let path = path.as_ref().canonicalize()?;
64-
_delete_dir_contents(&path)?;
63+
64+
let path = _remove_dir_contents(path)?;
6565
let metadata = path.metadata()?;
6666
if metadata.permissions().readonly() {
6767
delete_readonly(metadata, &path)?;
@@ -76,6 +76,13 @@ pub fn remove_dir_all<P: AsRef<Path>>(path: P) -> io::Result<()> {
7676
Ok(())
7777
}
7878

79+
/// Returns the canonicalised path, for one of caller's convenience.
80+
pub fn _remove_dir_contents<P: AsRef<Path>>(path: P) -> io::Result<PathBuf> {
81+
let path = path.as_ref().canonicalize()?;
82+
_delete_dir_contents(&path)?;
83+
Ok(path)
84+
}
85+
7986
fn _delete_dir_contents(path: &PathBuf) -> io::Result<()> {
8087
log::trace!("scanning {}", &path.display());
8188
let iter = path.read_dir()?.par_bridge();

0 commit comments

Comments
 (0)