fix(profile): report partial writes on the legacy refresh path

This commit is contained in:
Tunglies
2026-12-04 01:48:33 +08:00
parent 2b02e2de3e
commit 731b3fc8c9
2 changed files with 45 additions and 13 deletions
+40 -7
View File
@@ -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(())
}
}
+5 -6
View File
@@ -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<()> {