mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-12-04 21:30:06 +08:00
refactor(backup): name the archive before staging verification
This commit is contained in:
@@ -35,12 +35,12 @@ impl<W: Write + Seek> BackupWriter<W> {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ValidatedBackup<R: Read + Seek> {
|
||||
pub struct BackupArchive<R: Read + Seek> {
|
||||
zip: ZipArchive<R>,
|
||||
}
|
||||
|
||||
impl<R: Read + Seek> ValidatedBackup<R> {
|
||||
/// Checks the archive layout. Extract into staging to validate file contents before restore.
|
||||
impl<R: Read + Seek> BackupArchive<R> {
|
||||
/// Checks entry paths and lengths. Extract into staging to verify stored file checksums before restore.
|
||||
pub fn open(source: R, reserved_names: &[&str]) -> Result<Self, ArchiveError> {
|
||||
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(())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<String>,
|
||||
webdav_username: Option<String>,
|
||||
webdav_password: Option<String>,
|
||||
archive: ValidatedBackup<File>,
|
||||
archive: BackupArchive<File>,
|
||||
) -> 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<File>,
|
||||
mut archive: BackupArchive<File>,
|
||||
home_dir: &Path,
|
||||
retry: bool,
|
||||
) -> Result<ValidatedBackup<File>> {
|
||||
) -> Result<BackupArchive<File>> {
|
||||
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<File>, retry: bool) -> Result<(IProfiles, IVerge)> {
|
||||
async fn restore_files(archive: BackupArchive<File>, 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<File>, 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)?;
|
||||
|
||||
Reference in New Issue
Block a user