diff options
author | Daniel Prilik <prilik@google.com> | 2019-01-14 14:19:04 -0800 |
---|---|---|
committer | chrome-bot <chrome-bot@chromium.org> | 2019-01-17 20:20:50 -0800 |
commit | 2200604d9c101888df658f9483290a13952c6b1c (patch) | |
tree | 8b3982baeeb7f61824f95bd8a91a972f3104b729 /tempdir | |
parent | a4d5bd4bc271c505097f18ea0506926dbdf8d682 (diff) | |
download | crosvm-2200604d9c101888df658f9483290a13952c6b1c.tar crosvm-2200604d9c101888df658f9483290a13952c6b1c.tar.gz crosvm-2200604d9c101888df658f9483290a13952c6b1c.tar.bz2 crosvm-2200604d9c101888df658f9483290a13952c6b1c.tar.lz crosvm-2200604d9c101888df658f9483290a13952c6b1c.tar.xz crosvm-2200604d9c101888df658f9483290a13952c6b1c.tar.zst crosvm-2200604d9c101888df658f9483290a13952c6b1c.zip |
remove rand crate
the few uses of rand::thread_rng() have been replaced with either prngs or reads from /dev/urandom. the implementations are under the `rand_ish` minicrate. `protoc-rust` depends on `tempdir`, which relies on rand, so `tempdir` has been patched with a rewritten version that does not have rand as a dependency. BUG=chromium:921795 TEST=cargo test --features plugin Change-Id: I6f1c7d7a1aeef4dd55ac71e58294d16c291b8871 Reviewed-on: https://chromium-review.googlesource.com/1409705 Commit-Ready: Daniel Prilik <prilik@google.com> Tested-by: kokoro <noreply+kokoro@google.com> Reviewed-by: Zach Reizner <zachr@chromium.org>
Diffstat (limited to 'tempdir')
-rw-r--r-- | tempdir/Cargo.toml | 7 | ||||
-rw-r--r-- | tempdir/src/lib.rs | 54 |
2 files changed, 61 insertions, 0 deletions
diff --git a/tempdir/Cargo.toml b/tempdir/Cargo.toml new file mode 100644 index 0000000..9a3bed9 --- /dev/null +++ b/tempdir/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "tempdir" +version = "0.3.5" +authors = ["The Chromium OS Authors"] + +[dependencies] +rand_ish = { path = "../rand_ish" } diff --git a/tempdir/src/lib.rs b/tempdir/src/lib.rs new file mode 100644 index 0000000..ab0bb8a --- /dev/null +++ b/tempdir/src/lib.rs @@ -0,0 +1,54 @@ +// Copyright 2019 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +//! Simplified tempdir which doesn't depend on the `rand` crate, instead using +//! /dev/urandom as a source of entropy + +extern crate rand_ish; + +use std::env; +use std::fs; +use std::io::{self, Error, ErrorKind}; +use std::path::{Path, PathBuf}; +use rand_ish::urandom_str; + +pub struct TempDir { + path: PathBuf, +} + +const NUM_RETRIES: u32 = 4; + +impl TempDir { + /// Tries to make a tempdir inside of `env::temp_dir()` with a specified + /// prefix. The directory and it's content is destroyed when TempDir is + /// dropped. + /// If the directory can not be created, `Err` is returned. + pub fn new(prefix: &str) -> io::Result<TempDir> { + for _ in 0..NUM_RETRIES { + let suffix = urandom_str(12)?; + let path = env::temp_dir().join(format!("{}.{}", prefix, suffix)); + + match fs::create_dir(&path) { + Ok(_) => return Ok(TempDir { path }), + Err(ref e) if e.kind() == ErrorKind::AlreadyExists => {} + Err(e) => return Err(e), + } + } + + Err(Error::new(ErrorKind::AlreadyExists, "too many tempdirs exist")) + } + + /// Accesses the tempdir's [`Path`]. + /// + /// [`Path`]: http://doc.rust-lang.org/std/path/struct.Path.html + pub fn path(&self) -> &Path { + self.path.as_ref() + } +} + +impl Drop for TempDir { + fn drop(&mut self) { + let _ = fs::remove_dir_all(&self.path); + } +} |