refactor: move TempPathGuard into tools and use instead of DeleteOnDrop

This commit is contained in:
link2xt
2024-07-10 18:33:48 +00:00
parent 2c14bd353f
commit d77459e4fc
3 changed files with 43 additions and 46 deletions

View File

@@ -22,7 +22,9 @@ use crate::log::LogExt;
use crate::message::{Message, Viewtype};
use crate::pgp;
use crate::sql;
use crate::tools::{create_folder, delete_file, get_filesuffix_lc, read_file, time, write_file};
use crate::tools::{
create_folder, delete_file, get_filesuffix_lc, read_file, time, write_file, TempPathGuard,
};
mod key_transfer;
mod transfer;
@@ -359,8 +361,8 @@ async fn export_backup(context: &Context, dir: &Path, passphrase: String) -> Res
let now = time();
let self_addr = context.get_primary_self_addr().await?;
let (temp_db_path, temp_path, dest_path) = get_next_backup_path(dir, &self_addr, now)?;
let _d1 = DeleteOnDrop(temp_db_path.clone());
let _d2 = DeleteOnDrop(temp_path.clone());
let temp_db_path = TempPathGuard::new(temp_db_path);
let temp_path = TempPathGuard::new(temp_path);
export_database(context, &temp_db_path, passphrase, now)
.await
@@ -387,15 +389,6 @@ async fn export_backup(context: &Context, dir: &Path, passphrase: String) -> Res
res
}
struct DeleteOnDrop(PathBuf);
impl Drop for DeleteOnDrop {
fn drop(&mut self) {
let file = self.0.clone();
// Not using `tools::delete_file` here because it would send a DeletedBlobFile event
// Hack to avoid panic in nested runtime calls of tokio
std::fs::remove_file(file).ok();
}
}
async fn export_backup_inner(
context: &Context,

View File

@@ -24,8 +24,7 @@
use std::future::Future;
use std::net::Ipv4Addr;
use std::ops::Deref;
use std::path::{Path, PathBuf};
use std::path::Path;
use std::pin::Pin;
use std::task::Poll;
@@ -53,7 +52,7 @@ use crate::context::Context;
use crate::message::{Message, Viewtype};
use crate::qr::{self, Qr};
use crate::stock_str::backup_transfer_msg_body;
use crate::tools::time;
use crate::tools::{time, TempPathGuard};
use crate::{e2ee, EventType};
use super::{export_database, DBFILE_BACKUP_NAME};
@@ -306,37 +305,6 @@ 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.
///
/// Plus you get warnings if you don't use all variants.

View File

@@ -6,6 +6,7 @@
use std::borrow::Cow;
use std::io::{Cursor, Write};
use std::mem;
use std::ops::Deref;
use std::path::{Path, PathBuf};
use std::str::from_utf8;
// If a time value doesn't need to be sent to another host, saved to the db or otherwise used across
@@ -361,6 +362,41 @@ pub async fn delete_files_in_dir(context: &Context, path: impl AsRef<Path>) -> R
Ok(())
}
/// A guard which will remove the path when dropped.
///
/// It implements [`Deref`] so it can be used as a `&Path`.
#[derive(Debug)]
pub(crate) struct TempPathGuard {
path: PathBuf,
}
impl TempPathGuard {
pub(crate) fn new(path: PathBuf) -> Self {
Self { path }
}
}
impl Drop for TempPathGuard {
fn drop(&mut self) {
let path = self.path.clone();
std::fs::remove_file(path).ok();
}
}
impl Deref for TempPathGuard {
type Target = Path;
fn deref(&self) -> &Self::Target {
&self.path
}
}
impl AsRef<Path> for TempPathGuard {
fn as_ref(&self) -> &Path {
self
}
}
pub(crate) async fn create_folder(
context: &Context,
path: impl AsRef<Path>,