From d2f0f3c0c17e7edd7fd143eefcfe596b6354cc5b Mon Sep 17 00:00:00 2001 From: Tunglies <77394545+Tunglies@users.noreply.github.com> Date: Fri, 4 Dec 2026 01:48:33 +0800 Subject: [PATCH] refactor(backup): name the archive before staging verification --- crates/clash-verge-backup-archive/src/lib.rs | 10 +++++----- src-tauri/src/feat/backup.rs | 14 +++++++------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/crates/clash-verge-backup-archive/src/lib.rs b/crates/clash-verge-backup-archive/src/lib.rs index f2100a426..700448ce1 100644 --- a/crates/clash-verge-backup-archive/src/lib.rs +++ b/crates/clash-verge-backup-archive/src/lib.rs @@ -35,12 +35,12 @@ impl BackupWriter { } } -pub struct ValidatedBackup { +pub struct BackupArchive { zip: ZipArchive, } -impl ValidatedBackup { - /// Checks the archive layout. Extract into staging to validate file contents before restore. +impl BackupArchive { + /// Checks entry paths and lengths. Extract into staging to verify stored file checksums before restore. pub fn open(source: R, reserved_names: &[&str]) -> Result { let mut zip = ZipArchive::new(source)?; for index in 0..zip.len() { @@ -98,7 +98,7 @@ mod tests { bytes[22..26].copy_from_slice(&4u32.to_le_bytes()); bytes[directory + 24..directory + 28].copy_from_slice(&4u32.to_le_bytes()); assert!( - ValidatedBackup::open(Cursor::new(bytes), &[]).is_err(), + BackupArchive::open(Cursor::new(bytes), &[]).is_err(), "{name} {method:?}" ); } @@ -128,7 +128,7 @@ mod tests { let index = zip.by_index(2)?; assert_eq!(index.name()?, "profiles.yaml"); assert_eq!(index.last_modified(), profile_time); - ValidatedBackup::open(source, &[".profile-refresh", ".profile-refresh.lock"])?; + BackupArchive::open(source, &[".profile-refresh", ".profile-refresh.lock"])?; Ok(()) } } diff --git a/src-tauri/src/feat/backup.rs b/src-tauri/src/feat/backup.rs index 15094f238..626ae953e 100644 --- a/src-tauri/src/feat/backup.rs +++ b/src-tauri/src/feat/backup.rs @@ -14,7 +14,7 @@ use crate::{ }; use anyhow::{Context as _, Result, anyhow}; use chrono::Utc; -use clash_verge_backup_archive::ValidatedBackup; +use clash_verge_backup_archive::BackupArchive; use clash_verge_draft::DraftConflict; use clash_verge_logging::{Type, logging}; use clash_verge_profile_store::RECOVERY_ENTRY_NAMES; @@ -40,7 +40,7 @@ async fn finalize_restored_verge_config( webdav_url: Option, webdav_username: Option, webdav_password: Option, - archive: ValidatedBackup, + archive: BackupArchive, ) -> Result<()> { let config_write = Config::lock_config_write().await; let profiles_draft = Config::profiles().await; @@ -169,10 +169,10 @@ async fn restore_input_path(staged: &Path, home: &Path, name: &str, retry: bool) } async fn validate_restore( - mut archive: ValidatedBackup, + mut archive: BackupArchive, home_dir: &Path, retry: bool, -) -> Result> { +) -> Result> { let staging_root = home_dir.to_owned(); let (staged, archive) = AsyncHandler::spawn_blocking(move || -> Result<_> { let staged = tempfile::tempdir_in(staging_root)?; @@ -215,7 +215,7 @@ async fn validate_restore( Ok(archive) } -async fn restore_files(archive: ValidatedBackup, retry: bool) -> Result<(IProfiles, IVerge)> { +async fn restore_files(archive: BackupArchive, retry: bool) -> Result<(IProfiles, IVerge)> { let home_dir = app_home_dir()?; let mut archive = validate_restore(archive, &home_dir, retry).await?; AsyncHandler::spawn_blocking(move || -> Result<()> { @@ -246,7 +246,7 @@ async fn restore_files(archive: ValidatedBackup, retry: bool) -> Result<(I /// Backup creation strips the WebDAV credentials, so the live ones are kept. async fn restore_backup_archive(path: PathBuf) -> Result<()> { let archive = AsyncHandler::spawn_blocking(move || -> Result<_> { - Ok(ValidatedBackup::open(File::open(path)?, RECOVERY_ENTRY_NAMES)?) + Ok(BackupArchive::open(File::open(path)?, RECOVERY_ENTRY_NAMES)?) }) .await .map_err(|join| anyhow!("backup preflight task failed: {join}"))??; @@ -499,7 +499,7 @@ mod tests { if include_body { writer.start_profile("r.yaml")?.write_all(b"proxies: []\n")?; } - Ok(ValidatedBackup::open(writer.finish()?, RECOVERY_ENTRY_NAMES)?) + Ok(BackupArchive::open(writer.finish()?, RECOVERY_ENTRY_NAMES)?) }; let invalid = open_backup(Some(b"items: invalid\n"), false)?;