From 731b3fc8c92c058e7b24866310a6f83748b6d729 Mon Sep 17 00:00:00 2001 From: Tunglies <77394545+Tunglies@users.noreply.github.com> Date: Fri, 4 Dec 2026 01:48:33 +0800 Subject: [PATCH] fix(profile): report partial writes on the legacy refresh path --- src-tauri/src/config/profile_storage.rs | 47 +++++++++++++++++++++---- src-tauri/src/config/profiles.rs | 11 +++--- 2 files changed, 45 insertions(+), 13 deletions(-) diff --git a/src-tauri/src/config/profile_storage.rs b/src-tauri/src/config/profile_storage.rs index 6d2cda3e5..c4901f922 100644 --- a/src-tauri/src/config/profile_storage.rs +++ b/src-tauri/src/config/profile_storage.rs @@ -105,13 +105,18 @@ pub(super) async fn commit_refresh( 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?; + write_legacy_refresh(&crate::utils::dirs::app_home_dir()?, &uid, &candidate, body.as_bytes()).await?; Ok(candidate) } } -#[cfg(not(unix))] -async fn write_legacy_refresh(uid: &str, candidate: &super::IProfiles, body: &[u8]) -> Result<()> { +#[cfg(any(not(unix), test))] +async fn write_legacy_refresh( + root: &std::path::Path, + uid: &str, + candidate: &super::IProfiles, + body: &[u8], +) -> Result<()> { use anyhow::Context as _; let file = candidate @@ -119,11 +124,21 @@ async fn write_legacy_refresh(uid: &str, candidate: &super::IProfiles, body: &[u .file .as_deref() .context("missing profile filename")?; - let path = crate::utils::dirs::app_profiles_dir()?.join(file); - tokio::fs::write(path, body) + let path = root.join(crate::utils::dirs::PROFILES_DIR).join(file); + let mut target = tokio::fs::File::create(path) .await .with_context(|| format!("failed to write to file \"{file}\""))?; - super::profiles::save_profiles_unlocked(candidate).await + use tokio::io::AsyncWriteExt as _; + async { + target.write_all(body).await?; + target.flush().await + } + .await + .with_context(|| format!("profile body {file} may be incomplete; the index was not updated"))?; + drop(target); + super::profiles::save_profile_index(&root.join(crate::utils::dirs::PROFILE_YAML), candidate) + .await + .with_context(|| format!("profile body was written to {file}, but the index was not updated")) } #[cfg(any(unix, all(windows, feature = "profile-store-windows")))] @@ -146,7 +161,7 @@ async fn commit_recoverable_refresh( block(&error); return Err(error); } - write_legacy_refresh(&uid, &candidate, body.as_bytes()).await?; + write_legacy_refresh(&root, &uid, &candidate, body.as_bytes()).await?; return Ok(candidate); } } @@ -204,3 +219,21 @@ async fn commit_recoverable_refresh( } } } + +#[cfg(test)] +mod tests { + use super::*; + + #[tokio::test] + async fn legacy_refresh_reports_a_written_body_when_index_replace_fails() -> Result<()> { + let home = tempfile::tempdir()?; + let root = home.path(); + std::fs::create_dir(root.join("profiles"))?; + std::fs::create_dir(root.join("profiles.yaml"))?; + let profiles = serde_yaml_ng::from_str("items: [{uid: r, file: old.yaml}]")?; + let result = write_legacy_refresh(root, "r", &profiles, b"new body").await; + assert_eq!(std::fs::read(root.join("profiles/old.yaml"))?, b"new body"); + assert!(result.unwrap_err().to_string().contains("body was written")); + Ok(()) + } +} diff --git a/src-tauri/src/config/profiles.rs b/src-tauri/src/config/profiles.rs index 7c2f534ce..8ca4a837f 100644 --- a/src-tauri/src/config/profiles.rs +++ b/src-tauri/src/config/profiles.rs @@ -122,12 +122,11 @@ pub(crate) async fn save_profiles(profiles: &IProfiles) -> Result<()> { } pub(crate) async fn save_profiles_unlocked(profiles: &IProfiles) -> Result<()> { - help::save_yaml( - &dirs::profiles_path()?, - profiles, - Some("# Profiles Config for Clash Verge"), - ) - .await + save_profile_index(&dirs::profiles_path()?, profiles).await +} + +pub(super) async fn save_profile_index(path: &Path, profiles: &IProfiles) -> Result<()> { + help::save_yaml(path, profiles, Some("# Profiles Config for Clash Verge")).await } async fn append_item(profiles: &mut IProfiles, item: &mut PrfItem) -> Result<()> {