mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-12-05 05:50:04 +08:00
fix(profile): report partial writes on the legacy refresh path
This commit is contained in:
@@ -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(())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<()> {
|
||||
|
||||
Reference in New Issue
Block a user