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

Avoid second remove_dir_all after close() #22

Merged
merged 1 commit into from
Mar 17, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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