summary refs log tree commit diff
path: root/sys_util/src
diff options
context:
space:
mode:
Diffstat (limited to 'sys_util/src')
-rw-r--r--sys_util/src/lib.rs2
-rw-r--r--sys_util/src/seek_hole.rs11
-rw-r--r--sys_util/src/tempdir.rs102
-rw-r--r--sys_util/src/write_zeroes.rs12
4 files changed, 10 insertions, 117 deletions
diff --git a/sys_util/src/lib.rs b/sys_util/src/lib.rs
index 9e2e623..51c3898 100644
--- a/sys_util/src/lib.rs
+++ b/sys_util/src/lib.rs
@@ -33,7 +33,6 @@ pub mod signal;
 mod signalfd;
 mod sock_ctrl_msg;
 mod struct_util;
-mod tempdir;
 mod terminal;
 mod timerfd;
 mod write_zeroes;
@@ -60,7 +59,6 @@ pub use crate::signal::*;
 pub use crate::signalfd::*;
 pub use crate::sock_ctrl_msg::*;
 pub use crate::struct_util::*;
-pub use crate::tempdir::*;
 pub use crate::terminal::*;
 pub use crate::timerfd::*;
 pub use poll_token_derive::*;
diff --git a/sys_util/src/seek_hole.rs b/sys_util/src/seek_hole.rs
index 8215a8d..8cec06c 100644
--- a/sys_util/src/seek_hole.rs
+++ b/sys_util/src/seek_hole.rs
@@ -54,10 +54,9 @@ impl SeekHole for File {
 #[cfg(test)]
 mod tests {
     use super::*;
-    use crate::TempDir;
     use std::fs::File;
     use std::io::{Seek, SeekFrom, Write};
-    use std::path::PathBuf;
+    use tempfile::TempDir;
 
     fn seek_cur(file: &mut File) -> u64 {
         file.seek(SeekFrom::Current(0)).unwrap()
@@ -65,8 +64,8 @@ mod tests {
 
     #[test]
     fn seek_data() {
-        let tempdir = TempDir::new("/tmp/seek_data_test").unwrap();
-        let mut path = PathBuf::from(tempdir.as_path().unwrap());
+        let tempdir = TempDir::new().unwrap();
+        let mut path = tempdir.path().to_owned();
         path.push("test_file");
         let mut file = File::create(&path).unwrap();
 
@@ -112,8 +111,8 @@ mod tests {
 
     #[test]
     fn seek_hole() {
-        let tempdir = TempDir::new("/tmp/seek_hole_test").unwrap();
-        let mut path = PathBuf::from(tempdir.as_path().unwrap());
+        let tempdir = TempDir::new().unwrap();
+        let mut path = tempdir.path().to_owned();
         path.push("test_file");
         let mut file = File::create(&path).unwrap();
 
diff --git a/sys_util/src/tempdir.rs b/sys_util/src/tempdir.rs
deleted file mode 100644
index 045f245..0000000
--- a/sys_util/src/tempdir.rs
+++ /dev/null
@@ -1,102 +0,0 @@
-// Copyright 2017 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::ffi::CString;
-use std::ffi::OsStr;
-use std::ffi::OsString;
-use std::fs;
-use std::os::unix::ffi::OsStringExt;
-use std::path::Path;
-use std::path::PathBuf;
-
-use libc;
-
-use crate::{errno_result, Result};
-
-/// Create and remove a temporary directory.  The directory will be maintained for the lifetime of
-/// the `TempDir` object.
-pub struct TempDir {
-    path: Option<PathBuf>,
-}
-
-impl TempDir {
-    /// Creates a new tempory directory.
-    /// The directory will be removed when the object goes out of scope.
-    ///
-    /// # Examples
-    ///
-    /// ```
-    /// # use std::path::Path;
-    /// # use std::path::PathBuf;
-    /// # use sys_util::TempDir;
-    /// # fn test_create_temp_dir() -> Result<(), ()> {
-    ///       let t = TempDir::new("/tmp/testdir").map_err(|_| ())?;
-    ///       assert!(t.as_path().unwrap().exists());
-    /// #     Ok(())
-    /// # }
-    /// ```
-    pub fn new<P: AsRef<OsStr>>(prefix: P) -> Result<TempDir> {
-        let mut dir_string = prefix.as_ref().to_os_string();
-        dir_string.push("XXXXXX");
-        // unwrap this result as the internal bytes can't have a null with a valid path.
-        let dir_name = CString::new(dir_string.into_vec()).unwrap();
-        let mut dir_bytes = dir_name.into_bytes_with_nul();
-        let ret = unsafe {
-            // Creating the directory isn't unsafe.  The fact that it modifies the guts of the path
-            // is also OK because it only overwrites the last 6 Xs added above.
-            libc::mkdtemp(dir_bytes.as_mut_ptr() as *mut libc::c_char)
-        };
-        if ret.is_null() {
-            return errno_result();
-        }
-        dir_bytes.pop(); // Remove the null becasue from_vec can't handle it.
-        Ok(TempDir {
-            path: Some(PathBuf::from(OsString::from_vec(dir_bytes))),
-        })
-    }
-
-    /// Removes the temporary directory.  Calling this is optional as dropping a `TempDir` object
-    /// will also remove the directory.  Calling remove explicitly allows for better error handling.
-    pub fn remove(mut self) -> Result<()> {
-        let path = self.path.take();
-        path.map_or(Ok(()), fs::remove_dir_all)?;
-        Ok(())
-    }
-
-    /// Returns the path to the tempdir if it is currently valid
-    pub fn as_path(&self) -> Option<&Path> {
-        self.path.as_ref().map(PathBuf::as_path)
-    }
-}
-
-impl Drop for TempDir {
-    fn drop(&mut self) {
-        if let Some(p) = &self.path {
-            // Nothing can be done here if this returns an error.
-            let _ = fs::remove_dir_all(p);
-        }
-    }
-}
-
-#[cfg(test)]
-mod tests {
-    use super::*;
-
-    #[test]
-    fn create_dir() {
-        let t = TempDir::new("/tmp/asdf").unwrap();
-        let path = t.as_path().unwrap();
-        assert!(path.exists());
-        assert!(path.is_dir());
-        assert!(path.starts_with("/tmp/"));
-    }
-
-    #[test]
-    fn remove_dir() {
-        let t = TempDir::new("/tmp/asdf").unwrap();
-        let path = t.as_path().unwrap().to_owned();
-        assert!(t.remove().is_ok());
-        assert!(!path.exists());
-    }
-}
diff --git a/sys_util/src/write_zeroes.rs b/sys_util/src/write_zeroes.rs
index 51f0dbe..e3b531e 100644
--- a/sys_util/src/write_zeroes.rs
+++ b/sys_util/src/write_zeroes.rs
@@ -59,14 +59,12 @@ mod tests {
     use super::*;
     use std::fs::OpenOptions;
     use std::io::{Read, Seek, SeekFrom};
-    use std::path::PathBuf;
-
-    use crate::TempDir;
+    use tempfile::TempDir;
 
     #[test]
     fn simple_test() {
-        let tempdir = TempDir::new("/tmp/write_zeroes_test").unwrap();
-        let mut path = PathBuf::from(tempdir.as_path().unwrap());
+        let tempdir = TempDir::new().unwrap();
+        let mut path = tempdir.path().to_owned();
         path.push("file");
         let mut f = OpenOptions::new()
             .read(true)
@@ -131,8 +129,8 @@ mod tests {
 
     #[test]
     fn large_write_zeroes() {
-        let tempdir = TempDir::new("/tmp/write_zeroes_test").unwrap();
-        let mut path = PathBuf::from(tempdir.as_path().unwrap());
+        let tempdir = TempDir::new().unwrap();
+        let mut path = tempdir.path().to_owned();
         path.push("file");
         let mut f = OpenOptions::new()
             .read(true)