mktemp/
temp_dir.rs

1use std::{env, fs};
2use std::ffi::CString;
3use std::io::{Error, ErrorKind, Result};
4
5#[cfg(unix)]
6use libc::mkdtemp;
7
8/// A temporary directory
9pub struct TempDir {
10    path: String,
11}
12
13impl TempDir {
14    /// Creates a new temporary directory with the given prefix
15    ///
16    /// # Errors
17    ///
18    /// Any of the following will produce errors:
19    /// * Failure to parse a CString from the given data
20    /// * `mkdtemp` returning NULL
21    ///
22    /// # Examples
23    ///
24    /// ```
25    /// use std::fs;
26    /// use mktemp::TempDir;
27    ///
28    /// let td = TempDir::new("my-groovy-tempdir-").unwrap();
29    /// assert!(fs::metadata(td.path()).is_ok());
30    /// ```
31    pub fn new(prefix: &str) -> Result<TempDir> {
32        debug!("init new TempDir");
33        // get temporary directory
34        let tmp_dir = env::temp_dir();
35        debug!("found temp dir: {:?}", tmp_dir);
36
37        // CString --> &c_char
38        let ptr = match CString::new(format!("{}/{}XXXXXX", tmp_dir.display(), prefix)) {
39            Ok(p) => p.into_raw(),
40            Err(e) => return Err(Error::new(ErrorKind::Other, e)),
41        };
42        debug!("CString to raw done");
43
44        // mkdir and null check
45        let ptr = unsafe { mkdtemp(ptr) };
46        if ptr.is_null() {
47            debug!("mkdtemp returned NULL pointer");
48            return Err(Error::last_os_error());
49        }
50        debug!("directory created");
51
52        // &c_char --> CString --> String
53        let path = match unsafe { CString::from_raw(ptr) }.into_string() {
54            Ok(s) => s,
55            Err(e) => return Err(Error::new(ErrorKind::Other, e)),
56        };
57        debug!("raw to CString to String done");
58        debug!("got file path: {}", &path);
59
60        // yay!
61        Ok(TempDir { path: path })
62    }
63
64    /// Return the path to the temporary directory
65    pub fn path(&self) -> &str {
66        &self.path
67    }
68}
69
70impl Drop for TempDir {
71    fn drop(&mut self) {
72        debug!("Dropping TempDir: {}", &self.path);
73        let _ = fs::remove_dir_all(&self.path);
74    }
75}