Attempt clean-drop of tmp_dir
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
asonix 2023-10-07 11:36:49 -05:00
parent 7b5a3020fa
commit 7f5cbc4571
2 changed files with 50 additions and 14 deletions

View file

@ -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(())
}
}

View file

@ -6,28 +6,44 @@ pub(crate) type ArcTmpDir = Arc<TmpDir>;
#[derive(Debug)]
pub(crate) struct TmpDir {
path: PathBuf,
path: Option<PathBuf>,
}
impl TmpDir {
pub(crate) async fn init() -> std::io::Result<Arc<Self>> {
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<Self>) -> 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");
}
}
}