From fd67ec5ffc84d8726ebcb141ddb93e10a046ee72 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Wed, 10 Apr 2019 17:59:15 -0700 Subject: 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 Tested-by: David Tolnay Tested-by: kokoro Reviewed-by: Zach Reizner Reviewed-by: Daniel Verkamp --- tempfile/Cargo.toml | 8 ++++++ tempfile/src/lib.rs | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 tempfile/Cargo.toml create mode 100644 tempfile/src/lib.rs (limited to 'tempfile') 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 { + 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); + } +} -- cgit 1.4.1