From ce86ecc1b4b74683a4f477bff8e76f57bcf2b964 Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Sat, 19 Oct 2024 12:55:11 +0000 Subject: [PATCH] Use tempfile by default Per discussion this crate is widely used and there's no reason to carry even a small reimplementation. --- Cargo.toml | 2 +- src/lib.rs | 1 - src/mount.rs | 15 ++++++--------- src/tmpdir.rs | 35 ----------------------------------- 4 files changed, 7 insertions(+), 46 deletions(-) delete mode 100644 src/tmpdir.rs diff --git a/Cargo.toml b/Cargo.toml index 79bc9d5..e501093 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,10 +12,10 @@ rand = "0.8.5" rustix = { version = "0.38.37", features = ["fs", "mount", "process"] } sha2 = "0.10.8" tar = "0.4.42" +tempfile = "3.13.0" zstd = "0.13.2" [dev-dependencies] -tempfile = "3.13.0" [profile.dev.package.sha2] # this is *really* slow otherwise diff --git a/src/lib.rs b/src/lib.rs index 46209d3..cdfe8cd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,4 +6,3 @@ pub mod mount; pub mod oci; pub mod repository; pub mod splitstream; -pub mod tmpdir; diff --git a/src/mount.rs b/src/mount.rs index 45d5993..aa422b3 100644 --- a/src/mount.rs +++ b/src/mount.rs @@ -23,10 +23,7 @@ use rustix::mount::{ unmount, }; -use crate::{ - fsverity, - tmpdir, -}; +use crate::fsverity; struct FsHandle { pub fd: OwnedFd, @@ -58,21 +55,21 @@ impl Drop for FsHandle { } struct TmpMount { - pub dir: tmpdir::TempDir, + pub dir: tempfile::TempDir, } impl TmpMount { pub fn mount(fs: BorrowedFd) -> Result { - let tmp = tmpdir::TempDir::new()?; + let tmp = tempfile::TempDir::new()?; let mnt = fsmount(fs, FsMountFlags::FSMOUNT_CLOEXEC, MountAttrFlags::empty())?; - move_mount(mnt.as_fd(), "", rustix::fs::CWD, &tmp.path, MoveMountFlags::MOVE_MOUNT_F_EMPTY_PATH)?; + move_mount(mnt.as_fd(), "", rustix::fs::CWD, tmp.path(), MoveMountFlags::MOVE_MOUNT_F_EMPTY_PATH)?; Ok(TmpMount { dir: tmp }) } } impl Drop for TmpMount { fn drop(&mut self) { - unmount(&self.dir.path, UnmountFlags::DETACH) + unmount(self.dir.path(), UnmountFlags::DETACH) .expect("umount(MNT_DETACH) failed"); } } @@ -92,7 +89,7 @@ pub fn mount_fd(image: F, basedir: &Path, mountpoint: &str) -> Result<( // unfortunately we can't do this via the fd: we need a tmpdir mountpoint let tmp = TmpMount::mount(erofs.as_fd())?; // NB: must live until the "create" operation - fsconfig_set_string(overlayfs.as_fd(), "lowerdir+", &tmp.dir.path)?; + fsconfig_set_string(overlayfs.as_fd(), "lowerdir+", tmp.dir.path())?; fsconfig_set_string(overlayfs.as_fd(), "datadir+", basedir)?; fsconfig_create(overlayfs.as_fd())?; diff --git a/src/tmpdir.rs b/src/tmpdir.rs deleted file mode 100644 index ff384d1..0000000 --- a/src/tmpdir.rs +++ /dev/null @@ -1,35 +0,0 @@ -use std::path::PathBuf; - -use anyhow::{ - Result, - bail, -}; -use rand::distributions::{Alphanumeric, DistString}; - -pub struct TempDir { - pub path: PathBuf, -} - -impl TempDir { - pub fn new() -> Result{ - let tmp = PathBuf::from("/tmp"); - - for _ in 0 .. 26*26*26 { // this is how many times glibc tries - let suffix = Alphanumeric.sample_string(&mut rand::thread_rng(), 6); - let path = tmp.join(format!("composefs.{}", suffix)); - match std::fs::create_dir(&path) { - Ok(()) => return Ok(TempDir { path }), - Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => continue, - Err(e) => Err(e)? - } - } - - bail!("Failed to find free name for temporary directory"); - } -} - -impl Drop for TempDir { - fn drop(&mut self) { - std::fs::remove_dir(&self.path).expect("can't remove tempdir"); - } -}