Skip to content

Commit

Permalink
Update changelog
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewhickman committed Mar 14, 2020
1 parent 9fcb40e commit f61ae29
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# fs-err Changelog

## Unreleased
* Added `remove_file`, `remove_dir`, and `remove_dir_all`. ([#16](https://github.com/andrewhickman/fs-err/pull/16))

## 2.2.0
* Added `metadata`. ([#15](https://github.com/andrewhickman/fs-err/pull/15))

Expand Down
48 changes: 46 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,12 @@ pub fn remove_dir_all<P>(path: P) -> io::Result<()>
where
P: AsRef<Path> + Into<PathBuf>,
{
fs::remove_dir_all(path.as_ref())
.map_err(|source| Error::new(source, ErrorKind::RemoveDir, path))
let filetype = symlink_metadata(path.as_ref())?.file_type();
if filetype.is_symlink() {
remove_file(path)
} else {
remove_dir_all_recursive(path)
}
}

/// Wrapper for [`fs::remove_file`](https://doc.rust-lang.org/stable/std/fs/fn.remove_file.html).
Expand All @@ -145,9 +149,49 @@ pub fn metadata<P: AsRef<Path> + Into<PathBuf>>(path: P) -> io::Result<fs::Metad
fs::metadata(path.as_ref()).map_err(|source| Error::new(source, ErrorKind::Metadata, path))
}

/// Wrapper for [`fs::symlink_metadata`](https://doc.rust-lang.org/stable/std/fs/fn.symlink_metadata.html).
pub fn symlink_metadata<P: AsRef<Path> + Into<PathBuf>>(path: P) -> io::Result<fs::Metadata> {
fs::symlink_metadata(path.as_ref())
.map_err(|source| Error::new(source, ErrorKind::Metadata, path))
}

fn initial_buffer_size(file: &File) -> usize {
file.file()
.metadata()
.map(|m| m.len() as usize + 1)
.unwrap_or(0)
}

#[cfg(not(windows))]
fn remove_dir_all_recursive<P>(path: P) -> io::Result<()>
where
P: AsRef<Path> + Into<PathBuf>,
{
for child in read_dir(path.as_ref())? {
let child = child?;
if child.file_type()?.is_dir() {
remove_dir_all_recursive(child.path())?;
} else {
remove_file(child.path())?;
}
}
remove_dir(path)
}

#[cfg(windows)]
fn remove_dir_all_recursive(path: &Path) -> io::Result<()> {
use std::os::windows::fs::FileTypeExt;

for child in read_dir(path)? {
let child = child?;
let child_type = child.file_type()?;
if child_type.is_dir() {
remove_dir_all_recursive(&child.path())?;
} else if child_type.is_symlink_dir() {
remove_dir(&child.path())?;
} else {
unlink(&child.path())?;
}
}
rmdir(path)
}

0 comments on commit f61ae29

Please sign in to comment.