diff --git a/crates/clash-verge-profile-store/src/lib.rs b/crates/clash-verge-profile-store/src/lib.rs index f3b1b32bf..08276896d 100644 --- a/crates/clash-verge-profile-store/src/lib.rs +++ b/crates/clash-verge-profile-store/src/lib.rs @@ -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 { - 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)> { - 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], diff --git a/crates/clash-verge-profile-store/tests/legacy_refresh.rs b/crates/clash-verge-profile-store/tests/legacy_refresh.rs index 597203086..53e16d537 100644 --- a/crates/clash-verge-profile-store/tests/legacy_refresh.rs +++ b/crates/clash-verge-profile-store/tests/legacy_refresh.rs @@ -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); diff --git a/src-tauri/src/config/profile_storage.rs b/src-tauri/src/config/profile_storage.rs index e7665dff7..d2f8f701b 100644 --- a/src-tauri/src/config/profile_storage.rs +++ b/src-tauri/src/config/profile_storage.rs @@ -96,19 +96,47 @@ async fn journal_exists(root: &std::path::Path) -> Result { } } -#[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 { - 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 { + 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); diff --git a/src-tauri/src/config/profiles.rs b/src-tauri/src/config/profiles.rs index 9d4b82ee9..a3f4d850a 100644 --- a/src-tauri/src/config/profiles.rs +++ b/src-tauri/src/config/profiles.rs @@ -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, ())) })