2013-01-16 05:28:39 -05:00
|
|
|
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
|
2012-12-03 16:48:01 -08:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2012-07-04 22:53:12 +01:00
|
|
|
//! Temporary files and directories
|
2011-11-08 23:35:15 -05:00
|
|
|
|
2014-05-14 17:49:14 -07:00
|
|
|
use io::{fs, IoResult};
|
2014-03-14 11:16:10 -07:00
|
|
|
use io;
|
|
|
|
use libc;
|
|
|
|
use ops::Drop;
|
|
|
|
use option::{Option, None, Some};
|
|
|
|
use os;
|
|
|
|
use path::{Path, GenericPath};
|
2014-05-14 17:49:14 -07:00
|
|
|
use result::{Ok, Err};
|
2014-08-04 15:42:36 -07:00
|
|
|
use sync::atomic;
|
2011-11-08 23:35:15 -05:00
|
|
|
|
2013-10-11 15:55:37 +02:00
|
|
|
/// A wrapper for a path to temporary directory implementing automatic
|
2013-12-15 16:34:14 +11:00
|
|
|
/// scope-based deletion.
|
2013-10-11 15:55:37 +02:00
|
|
|
pub struct TempDir {
|
2014-05-14 17:49:14 -07:00
|
|
|
path: Option<Path>,
|
|
|
|
disarmed: bool
|
2013-10-11 15:55:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl TempDir {
|
|
|
|
/// Attempts to make a temporary directory inside of `tmpdir` whose name
|
|
|
|
/// will have the suffix `suffix`. The directory will be automatically
|
|
|
|
/// deleted once the returned wrapper is destroyed.
|
|
|
|
///
|
2014-08-30 13:12:47 +01:00
|
|
|
/// If no directory can be created, `Err` is returned.
|
|
|
|
pub fn new_in(tmpdir: &Path, suffix: &str) -> IoResult<TempDir> {
|
2013-10-11 15:55:37 +02:00
|
|
|
if !tmpdir.is_absolute() {
|
2014-03-14 11:16:10 -07:00
|
|
|
return TempDir::new_in(&os::make_absolute(tmpdir), suffix);
|
2013-10-11 15:55:37 +02:00
|
|
|
}
|
|
|
|
|
2014-08-04 15:42:36 -07:00
|
|
|
static mut CNT: atomic::AtomicUint = atomic::INIT_ATOMIC_UINT;
|
2014-03-14 11:16:10 -07:00
|
|
|
|
2014-08-30 13:12:47 +01:00
|
|
|
let mut attempts = 0u;
|
|
|
|
loop {
|
2014-05-16 10:45:16 -07:00
|
|
|
let filename =
|
2014-05-27 20:44:58 -07:00
|
|
|
format!("rs-{}-{}-{}",
|
|
|
|
unsafe { libc::getpid() },
|
2014-08-04 15:42:36 -07:00
|
|
|
unsafe { CNT.fetch_add(1, atomic::SeqCst) },
|
2014-05-27 20:44:58 -07:00
|
|
|
suffix);
|
2014-03-14 11:16:10 -07:00
|
|
|
let p = tmpdir.join(filename);
|
2014-01-29 17:39:12 -08:00
|
|
|
match fs::mkdir(&p, io::UserRWX) {
|
2014-08-30 13:12:47 +01:00
|
|
|
Err(error) => {
|
|
|
|
if attempts >= 1000 {
|
|
|
|
return Err(error)
|
|
|
|
}
|
|
|
|
attempts += 1;
|
|
|
|
}
|
|
|
|
Ok(()) => return Ok(TempDir { path: Some(p), disarmed: false })
|
2013-10-11 15:55:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Attempts to make a temporary directory inside of `os::tmpdir()` whose
|
|
|
|
/// name will have the suffix `suffix`. The directory will be automatically
|
|
|
|
/// deleted once the returned wrapper is destroyed.
|
|
|
|
///
|
2014-08-30 13:12:47 +01:00
|
|
|
/// If no directory can be created, `Err` is returned.
|
|
|
|
pub fn new(suffix: &str) -> IoResult<TempDir> {
|
2013-10-11 15:55:37 +02:00
|
|
|
TempDir::new_in(&os::tmpdir(), suffix)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Unwrap the wrapped `std::path::Path` from the `TempDir` wrapper.
|
|
|
|
/// This discards the wrapper so that the automatic deletion of the
|
|
|
|
/// temporary directory is prevented.
|
|
|
|
pub fn unwrap(self) -> Path {
|
|
|
|
let mut tmpdir = self;
|
2014-08-18 17:52:38 -07:00
|
|
|
tmpdir.path.take().unwrap()
|
2013-10-11 15:55:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Access the wrapped `std::path::Path` to the temporary directory.
|
|
|
|
pub fn path<'a>(&'a self) -> &'a Path {
|
|
|
|
self.path.get_ref()
|
|
|
|
}
|
2014-05-14 17:49:14 -07:00
|
|
|
|
|
|
|
/// Close and remove the temporary directory
|
|
|
|
///
|
|
|
|
/// Although `TempDir` removes the directory on drop, in the destructor
|
|
|
|
/// any errors are ignored. To detect errors cleaning up the temporary
|
|
|
|
/// directory, call `close` instead.
|
|
|
|
pub fn close(mut self) -> IoResult<()> {
|
|
|
|
self.cleanup_dir()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn cleanup_dir(&mut self) -> IoResult<()> {
|
|
|
|
assert!(!self.disarmed);
|
|
|
|
self.disarmed = true;
|
|
|
|
match self.path {
|
|
|
|
Some(ref p) => {
|
|
|
|
fs::rmdir_recursive(p)
|
|
|
|
}
|
|
|
|
None => Ok(())
|
|
|
|
}
|
|
|
|
}
|
2013-10-11 15:55:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for TempDir {
|
|
|
|
fn drop(&mut self) {
|
2014-05-14 17:49:14 -07:00
|
|
|
if !self.disarmed {
|
|
|
|
let _ = self.cleanup_dir();
|
2011-11-08 23:35:15 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-01-17 19:05:07 -08:00
|
|
|
|
2013-09-13 21:41:28 -07:00
|
|
|
// the tests for this module need to change the path using change_dir,
|
|
|
|
// and this doesn't play nicely with other tests so these unit tests are located
|
|
|
|
// in src/test/run-pass/tempfile.rs
|