refactor(profile-store): centralize recovery entry names

This commit is contained in:
Tunglies
2026-11-30 19:19:02 +08:00
parent 00c1febf33
commit 549bf91838
3 changed files with 12 additions and 12 deletions
+7 -4
View File
@@ -15,7 +15,10 @@ use std::{
path::{Path, PathBuf},
};
const JOURNAL: &str = ".profile-refresh";
pub const RECOVERY_JOURNAL_NAME: &str = ".profile-refresh";
pub const RECOVERY_ENTRY_NAMES: &[&str] = &[RECOVERY_JOURNAL_NAME, RECOVERY_LOCK_NAME];
const RECOVERY_LOCK_NAME: &str = ".profile-refresh.lock";
const DECISION: &str = "decision";
const MAX_DECISION_BYTES: u64 = 4096;
const PROFILES: &str = "profiles";
@@ -77,7 +80,7 @@ impl Session {
if !platform::supports_redo(&root)? {
return Err(StoreError::UnsupportedVolume);
}
let lock = platform::open_lock(&root.join(".profile-refresh.lock"))?;
let lock = platform::open_lock(&root.join(RECOVERY_LOCK_NAME))?;
lock.lock()?;
let session = Self { root, _lock: lock };
session.recover(checkpoint)?;
@@ -85,7 +88,7 @@ impl Session {
}
fn journal(&self) -> PathBuf {
self.root.join(JOURNAL)
self.root.join(RECOVERY_JOURNAL_NAME)
}
pub fn read_index(&self) -> Result<IProfiles> {
@@ -572,7 +575,7 @@ fn remove_file_if_exists(path: &Path) -> Result<()> {
/// Replays a saved decision before the application creates or loads its index.
pub fn recover_pending(root: &Path) -> Result<()> {
match fs::symlink_metadata(root.join(JOURNAL)) {
match fs::symlink_metadata(root.join(RECOVERY_JOURNAL_NAME)) {
Ok(_) => Session::open(root).map(drop),
Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(()),
Err(error) => Err(error.into()),
+3 -4
View File
@@ -1,9 +1,8 @@
use anyhow::{Result, anyhow};
use clash_verge_profile_store::RECOVERY_JOURNAL_NAME;
use std::sync::OnceLock;
use tokio::sync::{RwLock, RwLockReadGuard, RwLockWriteGuard};
const PROFILE_JOURNAL: &str = ".profile-refresh";
static PROFILE_IO: RwLock<()> = RwLock::const_new(());
static BLOCKED: OnceLock<String> = OnceLock::new();
@@ -54,7 +53,7 @@ pub(crate) async fn recover_at_startup() -> Result<()> {
}
async fn journal_exists(root: &std::path::Path) -> Result<bool> {
match tokio::fs::symlink_metadata(root.join(PROFILE_JOURNAL)).await {
match tokio::fs::symlink_metadata(root.join(RECOVERY_JOURNAL_NAME)).await {
Ok(_) => Ok(true),
Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(false),
Err(error) => Err(error.into()),
@@ -98,7 +97,7 @@ pub(super) async fn commit_refresh(
}
let result = tokio::task::spawn_blocking(move || {
let session = Session::open(&root).map_err(anyhow::Error::from).inspect_err(|error| {
match std::fs::symlink_metadata(root.join(PROFILE_JOURNAL)) {
match std::fs::symlink_metadata(root.join(RECOVERY_JOURNAL_NAME)) {
Err(status) if status.kind() == std::io::ErrorKind::NotFound => {}
_ => block(error),
}
+2 -4
View File
@@ -11,6 +11,7 @@ use anyhow::{Result, anyhow};
use chrono::Utc;
use clash_verge_backup_archive::ValidatedBackup;
use clash_verge_logging::{Type, logging};
use clash_verge_profile_store::RECOVERY_ENTRY_NAMES;
use reqwest_dav::list_cmd::ListFile;
use serde::Serialize;
use std::{fs::File, path::PathBuf};
@@ -168,10 +169,7 @@ async fn restore_files(archive: ValidatedBackup<File>) -> Result<(crate::config:
async fn restore_backup_archive(path: PathBuf) -> Result<()> {
crate::config::profile_storage::check()?;
let archive = AsyncHandler::spawn_blocking(move || -> Result<_> {
Ok(ValidatedBackup::open(
File::open(path)?,
&[".profile-refresh", ".profile-refresh.lock"],
)?)
Ok(ValidatedBackup::open(File::open(path)?, RECOVERY_ENTRY_NAMES)?)
})
.await
.map_err(|join| anyhow!("backup preflight task failed: {join}"))??;