mirror of
https://github.com/chatmail/core.git
synced 2026-05-06 06:46:35 +03:00
Use a RAII guard to remove the db export
This commit is contained in:
@@ -23,6 +23,7 @@
|
|||||||
//! download to an impersonated getter.
|
//! download to an impersonated getter.
|
||||||
|
|
||||||
use std::future::Future;
|
use std::future::Future;
|
||||||
|
use std::ops::Deref;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
use std::pin::Pin;
|
use std::pin::Pin;
|
||||||
use std::task::Poll;
|
use std::task::Poll;
|
||||||
@@ -98,6 +99,7 @@ impl BackupProvider {
|
|||||||
fs::remove_file(&dbfile).await?;
|
fs::remove_file(&dbfile).await?;
|
||||||
warn!(context, "Previous database export deleted");
|
warn!(context, "Previous database export deleted");
|
||||||
}
|
}
|
||||||
|
let dbfile = TempPathGuard::new(dbfile);
|
||||||
let res = tokio::select! {
|
let res = tokio::select! {
|
||||||
biased;
|
biased;
|
||||||
res = Self::prepare_inner(context, &dbfile) => {
|
res = Self::prepare_inner(context, &dbfile) => {
|
||||||
@@ -178,8 +180,9 @@ impl BackupProvider {
|
|||||||
context: Context,
|
context: Context,
|
||||||
mut provider: Provider,
|
mut provider: Provider,
|
||||||
cancel_token: Receiver<()>,
|
cancel_token: Receiver<()>,
|
||||||
dbfile: PathBuf,
|
_dbfile: TempPathGuard,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
|
// _dbfile exists so we can clean up the file once it is no longer needed
|
||||||
let mut events = provider.subscribe();
|
let mut events = provider.subscribe();
|
||||||
let res = loop {
|
let res = loop {
|
||||||
tokio::select! {
|
tokio::select! {
|
||||||
@@ -225,9 +228,6 @@ impl BackupProvider {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
if let Err(err) = fs::remove_file(&dbfile).await {
|
|
||||||
error!(context, "Failed to remove database export: {err:#}");
|
|
||||||
}
|
|
||||||
context
|
context
|
||||||
.export_provider
|
.export_provider
|
||||||
.lock()
|
.lock()
|
||||||
@@ -259,6 +259,37 @@ impl Future for BackupProvider {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A guard which will remove the path when dropped.
|
||||||
|
///
|
||||||
|
/// It implements [`Deref`] it it can be used as a `&Path`.
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct TempPathGuard {
|
||||||
|
path: PathBuf,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TempPathGuard {
|
||||||
|
fn new(path: PathBuf) -> Self {
|
||||||
|
Self { path }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Drop for TempPathGuard {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
let path = self.path.clone();
|
||||||
|
tokio::spawn(async move {
|
||||||
|
fs::remove_file(&path).await.ok();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Deref for TempPathGuard {
|
||||||
|
type Target = Path;
|
||||||
|
|
||||||
|
fn deref(&self) -> &Self::Target {
|
||||||
|
&self.path
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Create [`EventType::ImexProgress`] events using readable names.
|
/// Create [`EventType::ImexProgress`] events using readable names.
|
||||||
///
|
///
|
||||||
/// Plus you get warnings if you don't use all variants.
|
/// Plus you get warnings if you don't use all variants.
|
||||||
|
|||||||
Reference in New Issue
Block a user