From 7f5cbc45718c151dd71971908f33d6102e208ad3 Mon Sep 17 00:00:00 2001 From: asonix Date: Sat, 7 Oct 2023 11:36:49 -0500 Subject: [PATCH] Attempt clean-drop of tmp_dir --- src/lib.rs | 38 +++++++++++++++++++++++++++++--------- src/tmp_file.rs | 26 +++++++++++++++++++++----- 2 files changed, 50 insertions(+), 14 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index c92fd90..4355488 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2196,13 +2196,19 @@ impl PictRsConfiguration { match repo { Repo::Sled(sled_repo) => { - launch_file_store(tmp_dir, arc_repo, store, client, config, move |sc| { - sled_extra_config(sc, sled_repo.clone()) - }) + launch_file_store( + tmp_dir.clone(), + arc_repo, + store, + client, + config, + move |sc| sled_extra_config(sc, sled_repo.clone()), + ) .await?; } Repo::Postgres(_) => { - launch_file_store(tmp_dir, arc_repo, store, client, config, |_| {}).await?; + launch_file_store(tmp_dir.clone(), arc_repo, store, client, config, |_| {}) + .await?; } } } @@ -2260,19 +2266,33 @@ impl PictRsConfiguration { match repo { Repo::Sled(sled_repo) => { - launch_object_store(tmp_dir, arc_repo, store, client, config, move |sc| { - sled_extra_config(sc, sled_repo.clone()) - }) + launch_object_store( + tmp_dir.clone(), + arc_repo, + store, + client, + config, + move |sc| sled_extra_config(sc, sled_repo.clone()), + ) .await?; } Repo::Postgres(_) => { - launch_object_store(tmp_dir, arc_repo, store, client, config, |_| {}) - .await?; + launch_object_store( + tmp_dir.clone(), + arc_repo, + store, + client, + config, + |_| {}, + ) + .await?; } } } } + tmp_dir.cleanup().await?; + Ok(()) } } diff --git a/src/tmp_file.rs b/src/tmp_file.rs index cc772a7..ef9c7e9 100644 --- a/src/tmp_file.rs +++ b/src/tmp_file.rs @@ -6,28 +6,44 @@ pub(crate) type ArcTmpDir = Arc; #[derive(Debug)] pub(crate) struct TmpDir { - path: PathBuf, + path: Option, } impl TmpDir { pub(crate) async fn init() -> std::io::Result> { let path = std::env::temp_dir().join(Uuid::new_v4().to_string()); tokio::fs::create_dir(&path).await?; - Ok(Arc::new(TmpDir { path })) + Ok(Arc::new(TmpDir { path: Some(path) })) } pub(crate) fn tmp_file(&self, ext: Option<&str>) -> PathBuf { if let Some(ext) = ext { - self.path.join(format!("{}{}", Uuid::new_v4(), ext)) + self.path + .as_ref() + .expect("tmp path exists") + .join(format!("{}{}", Uuid::new_v4(), ext)) } else { - self.path.join(Uuid::new_v4().to_string()) + self.path + .as_ref() + .expect("tmp path exists") + .join(Uuid::new_v4().to_string()) } } + + pub(crate) async fn cleanup(self: Arc) -> std::io::Result<()> { + if let Some(path) = Arc::into_inner(self).and_then(|mut this| this.path.take()) { + tokio::fs::remove_dir_all(path).await?; + } + + Ok(()) + } } impl Drop for TmpDir { fn drop(&mut self) { - std::fs::remove_dir_all(&self.path).expect("Removed directory"); + if let Some(path) = self.path.as_ref() { + std::fs::remove_dir_all(path).expect("Removed directory"); + } } }