diff options
author | David Tolnay <dtolnay@chromium.org> | 2019-04-10 17:59:15 -0700 |
---|---|---|
committer | chrome-bot <chrome-bot@chromium.org> | 2019-04-12 14:49:56 -0700 |
commit | fd67ec5ffc84d8726ebcb141ddb93e10a046ee72 (patch) | |
tree | 1372d65c4a93f144a0ef39afc0e4ceaffd52f6a2 /tempfile | |
parent | 0268e26e1ac9e09aa51d733482c5df139cd8d588 (diff) | |
download | crosvm-fd67ec5ffc84d8726ebcb141ddb93e10a046ee72.tar crosvm-fd67ec5ffc84d8726ebcb141ddb93e10a046ee72.tar.gz crosvm-fd67ec5ffc84d8726ebcb141ddb93e10a046ee72.tar.bz2 crosvm-fd67ec5ffc84d8726ebcb141ddb93e10a046ee72.tar.lz crosvm-fd67ec5ffc84d8726ebcb141ddb93e10a046ee72.tar.xz crosvm-fd67ec5ffc84d8726ebcb141ddb93e10a046ee72.tar.zst crosvm-fd67ec5ffc84d8726ebcb141ddb93e10a046ee72.zip |
protos: Update to protobuf 2.3
This matches the version already used by crostini_client. The newer protobuf version depends on the tempfile crate rather than tempdir, the latter being now deprecated. So I replaced our immitation tempdir crate with one that matches the API of tempfile instead. As a reminder, we use this crate as a patch to avoid pulling in all of the rand crate and its many dependencies. TEST=cargo check --features plugin CQ-DEPEND=CL:1553971 Change-Id: I28eed3ceadb1013f015400b4c582aaf8dc89eee1 Reviewed-on: https://chromium-review.googlesource.com/1562924 Commit-Ready: ChromeOS CL Exonerator Bot <chromiumos-cl-exonerator@appspot.gserviceaccount.com> Tested-by: David Tolnay <dtolnay@chromium.org> Tested-by: kokoro <noreply+kokoro@google.com> Reviewed-by: Zach Reizner <zachr@chromium.org> Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Diffstat (limited to 'tempfile')
-rw-r--r-- | tempfile/Cargo.toml | 8 | ||||
-rw-r--r-- | tempfile/src/lib.rs | 77 |
2 files changed, 85 insertions, 0 deletions
diff --git a/tempfile/Cargo.toml b/tempfile/Cargo.toml new file mode 100644 index 0000000..b12e976 --- /dev/null +++ b/tempfile/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "tempfile" +version = "3.0.7" +authors = ["The Chromium OS Authors"] +edition = "2018" + +[dependencies] +rand_ish = { path = "../rand_ish" } diff --git a/tempfile/src/lib.rs b/tempfile/src/lib.rs new file mode 100644 index 0000000..c04f0f7 --- /dev/null +++ b/tempfile/src/lib.rs @@ -0,0 +1,77 @@ +// 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 tempfile which doesn't depend on the `rand` crate, instead using +//! /dev/urandom as a source of entropy + +extern crate rand_ish; + +use rand_ish::urandom_str; +use std::env; +use std::fs; +use std::io::{Error, ErrorKind, Result}; +use std::path::{Path, PathBuf}; + +pub struct Builder { + prefix: String, +} + +impl Builder { + pub fn new() -> Self { + Builder { + prefix: ".tmp".to_owned(), + } + } + + /// Set a custom filename prefix. + /// + /// Default: `.tmp` + pub fn prefix(&mut self, prefix: &str) -> &mut Self { + self.prefix = prefix.to_owned(); + self + } + + /// 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 tempdir(&self) -> Result<TempDir> { + for _ in 0..NUM_RETRIES { + let suffix = urandom_str(12)?; + let path = env::temp_dir().join(format!("{}.{}", self.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", + )) + } +} + +pub struct TempDir { + path: PathBuf, +} + +const NUM_RETRIES: u32 = 4; + +impl TempDir { + /// 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); + } +} |