2013-01-16 04:28:39 -06:00
|
|
|
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
|
2012-12-03 18:48:01 -06: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 16:53:12 -05:00
|
|
|
//! Temporary files and directories
|
2011-11-08 22:35:15 -06:00
|
|
|
|
2014-03-14 13:16:10 -05:00
|
|
|
use io::fs;
|
|
|
|
use io;
|
|
|
|
use iter::{Iterator, range};
|
|
|
|
use libc;
|
|
|
|
use ops::Drop;
|
|
|
|
use option::{Option, None, Some};
|
|
|
|
use os;
|
|
|
|
use path::{Path, GenericPath};
|
|
|
|
use result::{Ok, Err};
|
|
|
|
use sync::atomics;
|
2011-11-08 22:35:15 -06:00
|
|
|
|
2013-10-11 08:55:37 -05:00
|
|
|
/// A wrapper for a path to temporary directory implementing automatic
|
2013-12-14 23:34:14 -06:00
|
|
|
/// scope-based deletion.
|
2013-10-11 08:55:37 -05:00
|
|
|
pub struct TempDir {
|
2014-03-27 17:09:47 -05:00
|
|
|
path: Option<Path>
|
2013-10-11 08:55:37 -05: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.
|
|
|
|
///
|
|
|
|
/// If no directory can be created, None is returned.
|
|
|
|
pub fn new_in(tmpdir: &Path, suffix: &str) -> Option<TempDir> {
|
|
|
|
if !tmpdir.is_absolute() {
|
2014-03-14 13:16:10 -05:00
|
|
|
return TempDir::new_in(&os::make_absolute(tmpdir), suffix);
|
2013-10-11 08:55:37 -05:00
|
|
|
}
|
|
|
|
|
2014-03-14 13:16:10 -05:00
|
|
|
static mut CNT: atomics::AtomicUint = atomics::INIT_ATOMIC_UINT;
|
|
|
|
|
2013-10-11 08:55:37 -05:00
|
|
|
for _ in range(0u, 1000) {
|
2014-03-14 13:16:10 -05:00
|
|
|
let filename = format!("rs-{}-{}-{}",
|
|
|
|
unsafe { libc::getpid() },
|
|
|
|
unsafe { CNT.fetch_add(1, atomics::SeqCst) },
|
|
|
|
suffix);
|
|
|
|
let p = tmpdir.join(filename);
|
2014-01-29 19:39:12 -06:00
|
|
|
match fs::mkdir(&p, io::UserRWX) {
|
2013-11-28 14:22:53 -06:00
|
|
|
Err(..) => {}
|
2013-10-25 19:04:37 -05:00
|
|
|
Ok(()) => return Some(TempDir { path: Some(p) })
|
2013-10-11 08:55:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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.
|
|
|
|
///
|
|
|
|
/// If no directory can be created, None is returned.
|
|
|
|
pub fn new(suffix: &str) -> Option<TempDir> {
|
|
|
|
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;
|
|
|
|
tmpdir.path.take_unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Access the wrapped `std::path::Path` to the temporary directory.
|
|
|
|
pub fn path<'a>(&'a self) -> &'a Path {
|
|
|
|
self.path.get_ref()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for TempDir {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
for path in self.path.iter() {
|
2013-10-25 19:04:37 -05:00
|
|
|
if path.exists() {
|
2014-01-29 19:39:12 -06:00
|
|
|
// FIXME: is failing the right thing to do?
|
|
|
|
fs::rmdir_recursive(path).unwrap();
|
2013-10-25 19:04:37 -05:00
|
|
|
}
|
2011-11-08 22:35:15 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-01-17 21:05:07 -06:00
|
|
|
|
2013-09-13 23:41:28 -05: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
|