summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Crichton <alex@alexcrichton.com>2015-02-23 18:38:19 -0800
committerAlex Crichton <alex@alexcrichton.com>2015-02-23 18:41:10 -0800
commit49458f9c8e0430c428794d1e4650ca175ac7286d (patch)
treec04c8feb7b288871aaf9725ae083dc01f1dd2df1
parenta062f8704731821b591887f27e7ce9fb7515b29f (diff)
downloadssh2-rs-49458f9c8e0430c428794d1e4650ca175ac7286d.zip
Slim down custom TempDir
-rw-r--r--Cargo.toml3
-rw-r--r--tests/all.rs2
-rw-r--r--tests/tempdir.rs97
3 files changed, 7 insertions, 95 deletions
diff --git a/Cargo.toml b/Cargo.toml
index a5bbb3f..024fe86 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -23,6 +23,3 @@ libc = "0.1"
[dependencies.libssh2-sys]
path = "libssh2-sys"
version = "0.1.0"
-
-[dev-dependencies]
-rand = "0.1"
diff --git a/tests/all.rs b/tests/all.rs
index a8d50af..b905530 100644
--- a/tests/all.rs
+++ b/tests/all.rs
@@ -1,5 +1,5 @@
#![deny(warnings)]
-#![feature(io, core, path, env, net, fs)]
+#![feature(io, core, path, env, net, fs, old_io, old_path)]
extern crate ssh2;
extern crate libc;
diff --git a/tests/tempdir.rs b/tests/tempdir.rs
index cad52c6..2f83e40 100644
--- a/tests/tempdir.rs
+++ b/tests/tempdir.rs
@@ -1,102 +1,17 @@
-// 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::old_io;
+use std::io;
+use std::path;
-extern crate rand;
-
-use std::env;
-use std::io::{self, Error, ErrorKind};
-use std::fs;
-use std::path::{self, PathBuf, AsPath};
-use self::rand::{thread_rng, Rng};
-
-/// A wrapper for a path to temporary directory implementing automatic
-/// scope-based deletion.
pub struct TempDir {
- path: Option<PathBuf>,
+ inner: old_io::TempDir,
}
-// 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: AsPath + ?Sized>(tmpdir: &P, prefix: &str)
- -> io::Result<TempDir> {
- let tmpdir = tmpdir.as_path();
- assert!(tmpdir.is_absolute());
-
- 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::PathAlreadyExists => {}
- Err(e) => return Err(e)
- }
- }
-
- Err(Error::new(ErrorKind::PathAlreadyExists,
- "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)
+ Ok(TempDir { inner: old_io::TempDir::new(prefix).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(())
- }
+ path::Path::new(self.inner.path().as_str().unwrap())
}
}
-
-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
-