diff options
Diffstat (limited to 'tempfile/src/lib.rs')
-rw-r--r-- | tempfile/src/lib.rs | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/tempfile/src/lib.rs b/tempfile/src/lib.rs new file mode 100644 index 0000000..c04f0f7 --- /dev/null +++ b/tempfile/src/lib.rs @@ -0,0 +1,77 @@ +// Copyright 2019 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +//! Simplified tempfile which doesn't depend on the `rand` crate, instead using +//! /dev/urandom as a source of entropy + +extern crate rand_ish; + +use rand_ish::urandom_str; +use std::env; +use std::fs; +use std::io::{Error, ErrorKind, Result}; +use std::path::{Path, PathBuf}; + +pub struct Builder { + prefix: String, +} + +impl Builder { + pub fn new() -> Self { + Builder { + prefix: ".tmp".to_owned(), + } + } + + /// Set a custom filename prefix. + /// + /// Default: `.tmp` + pub fn prefix(&mut self, prefix: &str) -> &mut Self { + self.prefix = prefix.to_owned(); + self + } + + /// Tries to make a tempdir inside of `env::temp_dir()` with a specified + /// prefix. The directory and it's content is destroyed when TempDir is + /// dropped. + /// If the directory can not be created, `Err` is returned. + pub fn tempdir(&self) -> Result<TempDir> { + for _ in 0..NUM_RETRIES { + let suffix = urandom_str(12)?; + let path = env::temp_dir().join(format!("{}.{}", self.prefix, suffix)); + + match fs::create_dir(&path) { + Ok(_) => return Ok(TempDir { path }), + Err(ref e) if e.kind() == ErrorKind::AlreadyExists => {} + Err(e) => return Err(e), + } + } + + Err(Error::new( + ErrorKind::AlreadyExists, + "too many tempdirs exist", + )) + } +} + +pub struct TempDir { + path: PathBuf, +} + +const NUM_RETRIES: u32 = 4; + +impl TempDir { + /// Accesses the tempdir's [`Path`]. + /// + /// [`Path`]: http://doc.rust-lang.org/std/path/struct.Path.html + pub fn path(&self) -> &Path { + self.path.as_ref() + } +} + +impl Drop for TempDir { + fn drop(&mut self) { + let _ = fs::remove_dir_all(&self.path); + } +} |