From bc000a8e390188957005c5ebc43b178fbdf668d2 Mon Sep 17 00:00:00 2001 From: Tunglies <77394545+Tunglies@users.noreply.github.com> Date: Tue, 1 Dec 2026 22:58:35 +0800 Subject: [PATCH] refactor(profile-store): reuse the journal install scratch file --- crates/clash-verge-profile-store/src/lib.rs | 28 ++++++++++----------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/crates/clash-verge-profile-store/src/lib.rs b/crates/clash-verge-profile-store/src/lib.rs index 08276896d..729124cac 100644 --- a/crates/clash-verge-profile-store/src/lib.rs +++ b/crates/clash-verge-profile-store/src/lib.rs @@ -27,6 +27,7 @@ const BODY_NEXT: &str = "body.next"; const INDEX_NEXT: &str = "index.next"; const DECISION_NEXT: &str = "decision.next"; const DECISION_BUILDING: &str = "decision.building"; +const APPLY: &str = "body.apply"; type Result = std::result::Result; @@ -158,16 +159,15 @@ impl Session { checkpoint("decision-synced")?; self.install(checkpoint) })(); - match published { - Err(error) => CommitOutcome::NeedsRecovery(error), - Ok(InstallOutcome::Installed) => match self.disarm_and_clean(checkpoint) { - Ok(()) => CommitOutcome::Committed, - Err(error) => CommitOutcome::CommittedCleanupPending(error), - }, - Ok(InstallOutcome::Reverted(rejection)) => match self.disarm_and_clean(checkpoint) { - Ok(()) => CommitOutcome::Retryable(rejection), - Err(error) => CommitOutcome::NeedsRecovery(error), - }, + let installed = match published { + Ok(installed) => installed, + Err(error) => return CommitOutcome::NeedsRecovery(error), + }; + match (installed, self.disarm_and_clean(checkpoint)) { + (InstallOutcome::Installed, Ok(())) => CommitOutcome::Committed, + (InstallOutcome::Installed, Err(error)) => CommitOutcome::CommittedCleanupPending(error), + (InstallOutcome::Reverted(rejection), Ok(())) => CommitOutcome::Retryable(rejection), + (InstallOutcome::Reverted(_), Err(error)) => CommitOutcome::NeedsRecovery(error), } } @@ -277,6 +277,7 @@ impl Session { let profiles_dir = self.root.join(PROFILES); let body_path = profiles_dir.join(&decision.file); let index_path = self.root.join(INDEX); + let apply = journal.join(APPLY); let body_installed = verify_live(&body_path, decision.old_body_digest, decision.body_digest)?; let index_installed = verify_live(&index_path, Some(decision.old_index_digest), decision.index_digest)?; let can_revert_new_body = decision.old_body_digest.is_none() @@ -293,7 +294,6 @@ impl Session { // Equality alone does not establish durability of a prior in-place edit. platform::sync_file(&body_path)?; } else { - let apply = journal.join("body.apply"); remove_file_if_exists(&apply)?; copy_verified(&body_next, &apply, decision.body_digest)?; platform::replace(&apply, &body_path).map_err(|source| StoreError::PathIo { @@ -329,7 +329,7 @@ impl Session { if index_installed { platform::sync_file(&index_path)?; } else { - let apply = journal.join("index.apply"); + // The body rename consumed the scratch file. Replay can discard it at either step. remove_file_if_exists(&apply)?; write_new(&apply, &index, Some(index_permissions))?; platform::replace(&apply, &index_path).map_err(|source| StoreError::PathIo { @@ -363,8 +363,8 @@ impl Session { DECISION_BUILDING, BODY_NEXT, INDEX_NEXT, - "body.apply", - "index.apply", + APPLY, + "index.apply", // Older v1 journals used a separate scratch file for the index. ] { remove_file_if_exists(&journal.join(name))?; }