Skip to content
This repository has been archived by the owner on Aug 20, 2021. It is now read-only.

Commit

Permalink
Merge pull request #22 from dtolnay/cleanup
Browse files Browse the repository at this point in the history
Avoid second remove_dir_all after close()
  • Loading branch information
alexcrichton authored Mar 17, 2017
2 parents d905b67 + 22ed176 commit 1708bf0
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,14 +294,12 @@ impl TempDir {
/// tmp_dir.close().expect("delete temp dir");
/// ```
pub fn close(mut self) -> io::Result<()> {
self.cleanup_dir()
}
let result = fs::remove_dir_all(self.path());

fn cleanup_dir(&mut self) -> io::Result<()> {
match self.path {
Some(ref p) => fs::remove_dir_all(p),
None => Ok(()),
}
// Prevent the Drop impl from removing the dir a second time.
self.path = None;

result
}
}

Expand All @@ -321,7 +319,10 @@ impl fmt::Debug for TempDir {

impl Drop for TempDir {
fn drop(&mut self) {
let _ = self.cleanup_dir();
// Path is `None` if `close()` or `into_path()` has been called.
if let Some(ref p) = self.path {
let _ = fs::remove_dir_all(p);
}
}
}

Expand Down

0 comments on commit 1708bf0

Please sign in to comment.