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

Commit 1708bf0

Browse files
authored
Merge pull request #22 from dtolnay/cleanup
Avoid second remove_dir_all after close()
2 parents d905b67 + 22ed176 commit 1708bf0

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

src/lib.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -294,14 +294,12 @@ impl TempDir {
294294
/// tmp_dir.close().expect("delete temp dir");
295295
/// ```
296296
pub fn close(mut self) -> io::Result<()> {
297-
self.cleanup_dir()
298-
}
297+
let result = fs::remove_dir_all(self.path());
299298

300-
fn cleanup_dir(&mut self) -> io::Result<()> {
301-
match self.path {
302-
Some(ref p) => fs::remove_dir_all(p),
303-
None => Ok(()),
304-
}
299+
// Prevent the Drop impl from removing the dir a second time.
300+
self.path = None;
301+
302+
result
305303
}
306304
}
307305

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

322320
impl Drop for TempDir {
323321
fn drop(&mut self) {
324-
let _ = self.cleanup_dir();
322+
// Path is `None` if `close()` or `into_path()` has been called.
323+
if let Some(ref p) = self.path {
324+
let _ = fs::remove_dir_all(p);
325+
}
325326
}
326327
}
327328

0 commit comments

Comments
 (0)