mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-12-04 05:00:05 +08:00
refactor(profile-store): simplify refresh cleanup and legacy writes
This commit is contained in:
@@ -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