mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-12-02 11:40:04 +08:00
refactor(profile-store): simplify refresh cleanup and legacy writes
This commit is contained in:
@@ -37,6 +37,7 @@ pub enum CommitOutcome {
|
||||
Committed,
|
||||
CommittedCleanupPending(StoreError),
|
||||
Retryable(StoreError),
|
||||
RetryCleanupFailed(StoreError),
|
||||
NeedsRecovery(StoreError),
|
||||
}
|
||||
|
||||
@@ -96,20 +97,32 @@ impl Session {
|
||||
self.root.join(RECOVERY_JOURNAL_NAME)
|
||||
}
|
||||
|
||||
pub fn read_index(&self) -> Result<IProfiles> {
|
||||
let bytes = fs::read(self.root.join(INDEX))?;
|
||||
Ok(serde_yaml_ng::from_slice(&bytes)?)
|
||||
}
|
||||
|
||||
#[cfg(feature = "test-support")]
|
||||
#[doc(hidden)]
|
||||
pub fn snapshot(&self, uid: &str) -> Result<(IProfiles, Vec<u8>)> {
|
||||
let profiles = self.read_index()?;
|
||||
let profiles = serde_yaml_ng::from_slice(&fs::read(self.root.join(INDEX))?)?;
|
||||
let file = body_filename(&profiles, uid)?;
|
||||
let body = fs::read(self.root.join(PROFILES).join(file))?;
|
||||
Ok((profiles, body))
|
||||
}
|
||||
|
||||
pub fn commit_refresh(self, uid: &str, candidate: &IProfiles, body: &[u8]) -> CommitOutcome {
|
||||
self.commit_with_checkpoint(uid, candidate, body, &mut |_| Ok(()))
|
||||
let outcome = self.commit_with_checkpoint(uid, candidate, body, &mut |_| Ok(()));
|
||||
self.finish_commit(outcome)
|
||||
}
|
||||
|
||||
fn finish_commit(&self, outcome: CommitOutcome) -> CommitOutcome {
|
||||
match outcome {
|
||||
CommitOutcome::Retryable(error) => match self.recover(&mut |_| Ok(())) {
|
||||
Ok(()) => CommitOutcome::Retryable(error),
|
||||
Err(cleanup) => CommitOutcome::RetryCleanupFailed(cleanup),
|
||||
},
|
||||
CommitOutcome::CommittedCleanupPending(error) => match self.recover(&mut |_| Ok(())) {
|
||||
Ok(()) => CommitOutcome::Committed,
|
||||
Err(_) => CommitOutcome::CommittedCleanupPending(error),
|
||||
},
|
||||
outcome => outcome,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "test-support")]
|
||||
@@ -125,7 +138,7 @@ impl Session {
|
||||
}
|
||||
|
||||
fn commit_with_checkpoint(
|
||||
self,
|
||||
&self,
|
||||
uid: &str,
|
||||
candidate: &IProfiles,
|
||||
body: &[u8],
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#![cfg(any(unix, windows))]
|
||||
|
||||
use clash_verge_profile::IProfiles;
|
||||
use clash_verge_profile_store::{CommitOutcome, Session};
|
||||
use std::fs;
|
||||
|
||||
@@ -19,7 +20,8 @@ fn refresh_creates_missing_body_for_legacy_index() -> Result<(), clash_verge_pro
|
||||
let outcome = Session::open(root)?.commit_refresh("assigned", &candidate, body);
|
||||
assert!(matches!(outcome, CommitOutcome::Committed), "{outcome:?}");
|
||||
|
||||
let (index, stored_body) = Session::open(root)?.snapshot("assigned")?;
|
||||
let index: IProfiles = serde_yaml_ng::from_slice(&fs::read(root.join("profiles.yaml"))?)?;
|
||||
let stored_body = fs::read(root.join("profiles/Rnew.yaml"))?;
|
||||
assert_eq!(index.get_item("assigned")?.updated, Some(42));
|
||||
assert_eq!(index.get_item("assigned")?.file.as_deref(), Some("Rnew.yaml"));
|
||||
assert_eq!(stored_body, body);
|
||||
|
||||
@@ -96,19 +96,47 @@ async fn journal_exists(root: &std::path::Path) -> Result<bool> {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(unix, all(windows, feature = "profile-store-windows")))]
|
||||
pub(super) async fn commit_refresh(
|
||||
uid: String,
|
||||
candidate: super::IProfiles,
|
||||
body: tristr::alias::String,
|
||||
) -> Result<super::IProfiles> {
|
||||
use clash_verge_profile_store::{CommitOutcome, Session, recover_pending};
|
||||
#[cfg(any(unix, all(windows, feature = "profile-store-windows")))]
|
||||
return commit_recoverable_refresh(uid, candidate, body).await;
|
||||
#[cfg(not(any(unix, all(windows, feature = "profile-store-windows"))))]
|
||||
{
|
||||
write_legacy_refresh(&uid, &candidate, body.as_bytes()).await?;
|
||||
Ok(candidate)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(unix))]
|
||||
async fn write_legacy_refresh(uid: &str, candidate: &super::IProfiles, body: &[u8]) -> Result<()> {
|
||||
use anyhow::Context as _;
|
||||
|
||||
let file = candidate
|
||||
.get_item(uid)?
|
||||
.file
|
||||
.as_deref()
|
||||
.context("missing profile filename")?;
|
||||
let path = crate::utils::dirs::app_profiles_dir()?.join(file);
|
||||
tokio::fs::write(path, body)
|
||||
.await
|
||||
.with_context(|| format!("failed to write to file \"{file}\""))?;
|
||||
super::profiles::save_profiles_unlocked(candidate).await
|
||||
}
|
||||
|
||||
#[cfg(any(unix, all(windows, feature = "profile-store-windows")))]
|
||||
async fn commit_recoverable_refresh(
|
||||
uid: String,
|
||||
candidate: super::IProfiles,
|
||||
body: tristr::alias::String,
|
||||
) -> Result<super::IProfiles> {
|
||||
use clash_verge_profile_store::{CommitOutcome, Session};
|
||||
|
||||
let root = crate::utils::dirs::app_home_dir()?;
|
||||
#[cfg(windows)]
|
||||
{
|
||||
use anyhow::Context as _;
|
||||
|
||||
let probe_root = root.clone();
|
||||
let supported =
|
||||
tokio::task::spawn_blocking(move || clash_verge_profile_store::supports_redo(&probe_root)).await??;
|
||||
@@ -118,16 +146,7 @@ pub(super) async fn commit_refresh(
|
||||
block(&error);
|
||||
return Err(error);
|
||||
}
|
||||
let file = candidate
|
||||
.get_item(&uid)?
|
||||
.file
|
||||
.as_deref()
|
||||
.context("missing profile filename")?;
|
||||
let path = crate::utils::dirs::app_profiles_dir()?.join(file);
|
||||
tokio::fs::write(path, body.as_bytes())
|
||||
.await
|
||||
.with_context(|| format!("failed to write to file \"{file}\""))?;
|
||||
super::profiles::save_profiles_unlocked(&candidate).await?;
|
||||
write_legacy_refresh(&uid, &candidate, body.as_bytes()).await?;
|
||||
return Ok(candidate);
|
||||
}
|
||||
}
|
||||
@@ -156,18 +175,17 @@ pub(super) async fn commit_refresh(
|
||||
match session.commit_refresh(&uid, &candidate, body.as_bytes()) {
|
||||
CommitOutcome::Committed => Ok(candidate),
|
||||
CommitOutcome::CommittedCleanupPending(error) => {
|
||||
if recover_pending(&root).is_err() {
|
||||
block(&error.into());
|
||||
}
|
||||
block(&error.into());
|
||||
Ok(candidate)
|
||||
}
|
||||
CommitOutcome::Retryable(error) => {
|
||||
if let Err(recovery) = recover_pending(&root).map_err(anyhow::Error::from) {
|
||||
block(&recovery);
|
||||
return Err(recovery.context("failed to discard the incomplete profile refresh"));
|
||||
}
|
||||
Err(anyhow::Error::from(error).context("profile refresh stopped before commit"))
|
||||
}
|
||||
CommitOutcome::RetryCleanupFailed(error) => {
|
||||
let error = anyhow::Error::from(error);
|
||||
block(&error);
|
||||
Err(error.context("failed to discard the incomplete profile refresh"))
|
||||
}
|
||||
CommitOutcome::NeedsRecovery(error) => {
|
||||
let error = anyhow::Error::from(error);
|
||||
block(&error);
|
||||
|
||||
@@ -343,21 +343,7 @@ pub(crate) async fn apply_remote_refresh_safe(
|
||||
"profile source or filename changed during refresh"
|
||||
);
|
||||
stage_remote_refresh(&mut candidate, &uid, &refresh)?;
|
||||
#[cfg(any(unix, all(windows, feature = "profile-store-windows")))]
|
||||
let candidate = profile_storage::commit_refresh(uid, candidate, refresh.body).await?;
|
||||
#[cfg(not(any(unix, all(windows, feature = "profile-store-windows"))))]
|
||||
{
|
||||
let file = candidate
|
||||
.get_item(&uid)?
|
||||
.file
|
||||
.as_deref()
|
||||
.context("missing profile filename")?;
|
||||
let path = dirs::app_profiles_dir()?.join(file);
|
||||
fs::write(&path, refresh.body.as_bytes())
|
||||
.await
|
||||
.with_context(|| format!("failed to write to file \"{file}\""))?;
|
||||
save_profiles_unlocked(&candidate).await?;
|
||||
}
|
||||
*persisted = true;
|
||||
Ok((candidate, ()))
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user