rustc_back: replace tempdir with crates.io version.

This commit is contained in:
Irina-Gabriela Popa 2017-11-27 17:21:13 +02:00
parent dda924ab6a
commit 2c175df013
14 changed files with 18 additions and 127 deletions

2
src/Cargo.lock generated
View File

@ -1960,6 +1960,7 @@ dependencies = [
"serialize 0.0.0",
"syntax 0.0.0",
"syntax_pos 0.0.0",
"tempdir 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@ -2013,6 +2014,7 @@ dependencies = [
"html-diff 0.0.5 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
"pulldown-cmark 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"tempdir 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]

View File

@ -36,7 +36,6 @@ extern crate serialize;
extern crate serialize as rustc_serialize; // used by deriving
pub mod tempdir;
pub mod target;
use std::str::FromStr;

View File

@ -1,114 +0,0 @@
// 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.
use std::env;
use std::io::{self, Error, ErrorKind};
use std::fs;
use std::path::{self, PathBuf, Path};
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: usize = 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: AsRef<Path>>(tmpdir: P, prefix: &str)
-> io::Result<TempDir> {
Self::_new_in(tmpdir.as_ref(), prefix)
}
fn _new_in(tmpdir: &Path, prefix: &str) -> io::Result<TempDir> {
let storage;
let mut tmpdir = tmpdir;
if !tmpdir.is_absolute() {
let cur_dir = 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.is_empty() {
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"))
}
/// 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.
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()
}
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

View File

@ -32,6 +32,7 @@ rustc_trans_utils = { path = "../librustc_trans_utils" }
serialize = { path = "../libserialize" }
syntax = { path = "../libsyntax" }
syntax_pos = { path = "../libsyntax_pos" }
tempdir = "0.3"
[target."cfg(windows)".dependencies]
cc = "1.0.1"

View File

@ -26,7 +26,7 @@ use {CrateTranslation, CrateInfo};
use rustc::util::common::time;
use rustc::util::fs::fix_windows_verbatim_for_gcc;
use rustc::hir::def_id::CrateNum;
use rustc_back::tempdir::TempDir;
use tempdir::TempDir;
use rustc_back::{PanicStrategy, RelroLevel, LinkerFlavor};
use context::get_reloc_model;
use llvm;

View File

@ -63,6 +63,7 @@ extern crate rustc_errors as errors;
extern crate serialize;
#[cfg(windows)]
extern crate cc; // Used to locate MSVC
extern crate tempdir;
pub use base::trans_crate;
use back::bytecode::RLIB_BYTECODE_EXTENSION;

View File

@ -14,6 +14,7 @@ doctest = false
log = "0.3"
pulldown-cmark = { version = "0.1.0", default-features = false }
html-diff = "0.0.5"
tempdir = "0.3"
[build-dependencies]
build_helper = { path = "../build_helper" }

View File

@ -48,6 +48,7 @@ extern crate std_unicode;
#[macro_use] extern crate log;
extern crate rustc_errors as errors;
extern crate pulldown_cmark;
extern crate tempdir;
extern crate serialize as rustc_serialize; // used by deriving

View File

@ -28,7 +28,7 @@ use rustc::session::{self, CompileIncomplete, config};
use rustc::session::config::{OutputType, OutputTypes, Externs};
use rustc::session::search_paths::{SearchPaths, PathKind};
use rustc_metadata::dynamic_lib::DynamicLibrary;
use rustc_back::tempdir::TempDir;
use tempdir::TempDir;
use rustc_driver::{self, driver, Compilation};
use rustc_driver::driver::phase_2_configure_and_expand;
use rustc_metadata::cstore::CStore;

View File

@ -12,11 +12,11 @@
#![feature(rustc_private)]
extern crate rustc_back;
extern crate tempdir;
use std::env;
use std::fs;
use rustc_back::tempdir::TempDir;
use tempdir::TempDir;
fn main() {
let td = TempDir::new("create-dir-all-bare").unwrap();

View File

@ -13,13 +13,13 @@
#![feature(rustc_private)]
extern crate rustc_back;
extern crate tempdir;
use std::env;
use std::fs;
use std::process;
use std::str;
use rustc_back::tempdir::TempDir;
use tempdir::TempDir;
fn main() {
// If we're the child, make sure we were invoked correctly

View File

@ -15,11 +15,11 @@
#![feature(rustc_private)]
extern crate rustc_back;
extern crate tempdir;
use std::ffi::CString;
use std::fs::{self, File};
use rustc_back::tempdir::TempDir;
use tempdir::TempDir;
fn rename_directory() {
let tmpdir = TempDir::new("rename_directory").ok().expect("rename_directory failed");

View File

@ -12,7 +12,7 @@
#![feature(rustc_private)]
extern crate rustc_back;
extern crate tempdir;
use std::env;
use std::fs::File;
@ -20,7 +20,7 @@ use std::io;
use std::io::{Read, Write};
use std::process::{Command, Stdio};
use rustc_back::tempdir::TempDir;
use tempdir::TempDir;
fn main() {
if env::args().len() > 1 {

View File

@ -10,12 +10,12 @@
#![feature(rustc_private)]
extern crate rustc_back;
extern crate tempdir;
use std::fs::File;
use std::io::{Read, Write};
use rustc_back::tempdir::TempDir;
use tempdir::TempDir;
#[cfg(unix)]
fn switch_stdout_to(file: File) {