From b1475abeb0afe3e9e18f63f9fcd9fbc5788a1ef1 Mon Sep 17 00:00:00 2001 From: Allen Webb Date: Tue, 14 Jun 2022 21:48:12 +0000 Subject: [PATCH] base: Remove unused scoped_path. scoped path is not exported and isn't used in crosvm, so delete it (and move it to libchromeos-rs). BUG=b:229016539 TEST=crosvm presubmit passes Change-Id: I661c253db909874aec356538a687e99f3aa95e3c Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3706096 Reviewed-by: Dennis Kempin Tested-by: kokoro Auto-Submit: Allen Webb Commit-Queue: Dennis Kempin --- base/src/sys/unix/scoped_path.rs | 138 ------------------------------- 1 file changed, 138 deletions(-) delete mode 100644 base/src/sys/unix/scoped_path.rs diff --git a/base/src/sys/unix/scoped_path.rs b/base/src/sys/unix/scoped_path.rs deleted file mode 100644 index 745137eaec..0000000000 --- a/base/src/sys/unix/scoped_path.rs +++ /dev/null @@ -1,138 +0,0 @@ -// Copyright 2021 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. - -use std::env::{current_exe, temp_dir}; -use std::fs::{create_dir_all, remove_dir_all}; -use std::io::Result; -use std::ops::Deref; -use std::path::{Path, PathBuf}; -use std::thread::panicking; - -use crate::{getpid, gettid}; - -/// Returns a stable path based on the label, pid, and tid. If the label isn't provided the -/// current_exe is used instead. -pub fn get_temp_path(label: Option<&str>) -> PathBuf { - if let Some(label) = label { - temp_dir().join(format!("{}-{}-{}", label, getpid(), gettid())) - } else { - get_temp_path(Some( - current_exe() - .unwrap() - .file_name() - .unwrap() - .to_str() - .unwrap(), - )) - } -} - -/// Automatically deletes the path it contains when it goes out of scope unless it is a test and -/// drop is called after a panic!. -/// -/// This is particularly useful for creating temporary directories for use with tests. -pub struct ScopedPath>(P); - -impl> ScopedPath

{ - pub fn create(p: P) -> Result { - create_dir_all(p.as_ref())?; - Ok(ScopedPath(p)) - } -} - -impl> AsRef for ScopedPath

{ - fn as_ref(&self) -> &Path { - self.0.as_ref() - } -} - -impl> Deref for ScopedPath

{ - type Target = Path; - - fn deref(&self) -> &Self::Target { - self.0.as_ref() - } -} - -impl> Drop for ScopedPath

{ - fn drop(&mut self) { - // Leave the files on a failed test run for debugging. - if panicking() && cfg!(test) { - eprintln!("NOTE: Not removing {}", self.display()); - return; - } - if let Err(e) = remove_dir_all(&**self) { - eprintln!("Failed to remove {}: {}", self.display(), e); - } - } -} - -#[cfg(test)] -pub(crate) mod tests { - use super::*; - - use std::panic::catch_unwind; - - #[test] - fn gettemppath() { - assert_ne!("", get_temp_path(None).to_string_lossy()); - assert!(get_temp_path(None).starts_with(temp_dir())); - assert_eq!( - get_temp_path(None), - get_temp_path(Some( - current_exe() - .unwrap() - .file_name() - .unwrap() - .to_str() - .unwrap() - )) - ); - assert_ne!( - get_temp_path(Some("label")), - get_temp_path(Some( - current_exe() - .unwrap() - .file_name() - .unwrap() - .to_str() - .unwrap() - )) - ); - } - - #[test] - fn scopedpath_exists() { - let tmp_path = get_temp_path(None); - { - let scoped_path = ScopedPath::create(&tmp_path).unwrap(); - assert!(scoped_path.exists()); - } - assert!(!tmp_path.exists()); - } - - #[test] - fn scopedpath_notexists() { - let tmp_path = get_temp_path(None); - { - let _scoped_path = ScopedPath(&tmp_path); - } - assert!(!tmp_path.exists()); - } - - #[test] - fn scopedpath_panic() { - let tmp_path = get_temp_path(None); - assert!(catch_unwind(|| { - { - let scoped_path = ScopedPath::create(&tmp_path).unwrap(); - assert!(scoped_path.exists()); - panic!() - } - }) - .is_err()); - assert!(tmp_path.exists()); - remove_dir_all(&tmp_path).unwrap(); - } -}