dedac5eb3c
This commit stabilizes the `ErrorKind` enumeration which is consumed by and generated by the `io::Error` type. The purpose of this type is to serve as a cross-platform namespace to categorize errors into. Two specific issues are addressed as part of this stablization: * The naming of each variant was scrutinized and some were tweaked. An example is how `FileNotFound` was renamed to simply `NotFound`. These names should not show either a Unix or Windows bias and the set of names is intended to grow over time. For now the names will likely largely consist of those errors generated by the I/O APIs in the standard library. * The mapping of OS error codes onto kinds has been altered. Coalescing no longer occurs (multiple error codes become one kind). It is intended that each OS error code, if bound, corresponds to only one `ErrorKind`. The current set of error kinds was expanded slightly to include some networking errors. This commit also adds a `raw_os_error` function which returns an `Option<i32>` to extract the underlying raw error code from the `Error`.
129 lines
4.7 KiB
Rust
129 lines
4.7 KiB
Rust
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
|
// 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.
|
|
|
|
#![unstable(feature = "tempdir", reason = "needs an RFC before stabilization")]
|
|
#![deprecated(since = "1.0.0",
|
|
reason = "use the `tempdir` crate from crates.io instead")]
|
|
#![allow(deprecated)]
|
|
|
|
use prelude::v1::*;
|
|
|
|
use env;
|
|
use io::{self, Error, ErrorKind};
|
|
use fs;
|
|
use path::{self, PathBuf, AsPath};
|
|
use rand::{thread_rng, Rng};
|
|
|
|
/// A wrapper for a path to temporary directory implementing automatic
|
|
/// scope-based deletion.
|
|
pub struct TempDir {
|
|
path: Option<PathBuf>,
|
|
}
|
|
|
|
// How many times should we (re)try finding an unused random name? It should be
|
|
// enough that an attacker will run out of luck before we run out of patience.
|
|
const NUM_RETRIES: u32 = 1 << 31;
|
|
// How many characters should we include in a random file name? It needs to
|
|
// be enough to dissuade an attacker from trying to preemptively create names
|
|
// of that length, but not so huge that we unnecessarily drain the random number
|
|
// generator of entropy.
|
|
const NUM_RAND_CHARS: uint = 12;
|
|
|
|
impl TempDir {
|
|
/// Attempts to make a temporary directory inside of `tmpdir` whose name
|
|
/// will have the prefix `prefix`. The directory will be automatically
|
|
/// deleted once the returned wrapper is destroyed.
|
|
///
|
|
/// If no directory can be created, `Err` is returned.
|
|
#[allow(deprecated)] // rand usage
|
|
pub fn new_in<P: AsPath + ?Sized>(tmpdir: &P, prefix: &str)
|
|
-> io::Result<TempDir> {
|
|
let storage;
|
|
let mut tmpdir = tmpdir.as_path();
|
|
if !tmpdir.is_absolute() {
|
|
let cur_dir = try!(env::current_dir());
|
|
storage = cur_dir.join(tmpdir);
|
|
tmpdir = &storage;
|
|
// return TempDir::new_in(&cur_dir.join(tmpdir), prefix);
|
|
}
|
|
|
|
let mut rng = thread_rng();
|
|
for _ in 0..NUM_RETRIES {
|
|
let suffix: String = rng.gen_ascii_chars().take(NUM_RAND_CHARS).collect();
|
|
let leaf = if prefix.len() > 0 {
|
|
format!("{}.{}", prefix, suffix)
|
|
} else {
|
|
// If we're given an empty string for a prefix, then creating a
|
|
// directory starting with "." would lead to it being
|
|
// semi-invisible on some systems.
|
|
suffix
|
|
};
|
|
let path = tmpdir.join(&leaf);
|
|
match fs::create_dir(&path) {
|
|
Ok(_) => return Ok(TempDir { path: Some(path) }),
|
|
Err(ref e) if e.kind() == ErrorKind::AlreadyExists => {}
|
|
Err(e) => return Err(e)
|
|
}
|
|
}
|
|
|
|
Err(Error::new(ErrorKind::AlreadyExists,
|
|
"too many temporary directories already exist",
|
|
None))
|
|
}
|
|
|
|
/// Attempts to make a temporary directory inside of `env::temp_dir()` whose
|
|
/// name will have the prefix `prefix`. The directory will be automatically
|
|
/// deleted once the returned wrapper is destroyed.
|
|
///
|
|
/// If no directory can be created, `Err` is returned.
|
|
#[allow(deprecated)]
|
|
pub fn new(prefix: &str) -> io::Result<TempDir> {
|
|
TempDir::new_in(&env::temp_dir(), prefix)
|
|
}
|
|
|
|
/// 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 into_path(mut self) -> PathBuf {
|
|
self.path.take().unwrap()
|
|
}
|
|
|
|
/// Access the wrapped `std::path::Path` to the temporary directory.
|
|
pub fn path(&self) -> &path::Path {
|
|
self.path.as_ref().unwrap()
|
|
}
|
|
|
|
/// 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) -> io::Result<()> {
|
|
self.cleanup_dir()
|
|
}
|
|
|
|
fn cleanup_dir(&mut self) -> io::Result<()> {
|
|
match self.path {
|
|
Some(ref p) => fs::remove_dir_all(p),
|
|
None => Ok(())
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Drop for TempDir {
|
|
fn drop(&mut self) {
|
|
let _ = self.cleanup_dir();
|
|
}
|
|
}
|
|
|
|
// 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
|