diff --git a/crates/clash-verge-profile-store/src/lib.rs b/crates/clash-verge-profile-store/src/lib.rs index 0dc6a695a..f7b91f232 100644 --- a/crates/clash-verge-profile-store/src/lib.rs +++ b/crates/clash-verge-profile-store/src/lib.rs @@ -182,7 +182,6 @@ impl Session { let old_index = fs::read(&index_path)?; let current: IProfiles = serde_yaml_ng::from_slice(&old_index)?; let file = body_filename(candidate, uid)?; - let new_item = candidate.get_item(uid)?; let mut exact = current .items .iter() @@ -195,6 +194,7 @@ impl Session { item } else { // Loading an old index may assign a UID without rewriting the file. + let new_item = candidate.get_item(uid)?; let mut legacy = current.items.iter().flatten().filter(|item| { item.uid.is_none() && item.itype.as_deref() == Some("remote") @@ -231,10 +231,8 @@ impl Session { if old_item.file.is_none() && old_body_digest.is_some() { return Err(StoreError::ExistingBodyPath); } - let index = format!( - "# Profiles Config for Clash Verge\n\n{}", - serde_yaml_ng::to_string(candidate)? - ); + let mut index = b"# Profiles Config for Clash Verge\n\n".to_vec(); + serde_yaml_ng::to_writer(&mut index, candidate)?; let decision = Decision { version: 1, uid: uid.into(), @@ -242,7 +240,7 @@ impl Session { old_body_digest, old_index_digest: Sha256::digest(&old_index).into(), body_digest: Sha256::digest(body).into(), - index_digest: Sha256::digest(index.as_bytes()).into(), + index_digest: Sha256::digest(&index).into(), }; let decision = serde_json::to_vec(&decision)?; @@ -255,7 +253,7 @@ impl Session { checkpoint("journal-created")?; write_new(&journal.join(BODY_NEXT), body, metadata.map(|meta| meta.permissions()))?; checkpoint("body-staged")?; - write_new(&journal.join(INDEX_NEXT), index.as_bytes(), Some(index_permissions))?; + write_new(&journal.join(INDEX_NEXT), &index, Some(index_permissions))?; checkpoint("index-staged")?; write_new(&journal.join(DECISION_BUILDING), &decision, None)?; platform::replace(journal.join(DECISION_BUILDING), journal.join(DECISION_NEXT))?; @@ -488,7 +486,7 @@ fn body_filename<'a>(profiles: &'a IProfiles, uid: &str) -> Result<&'a str> { } fn unshared_body(dir: &Path, profiles: &IProfiles, file: &str) -> Result> { - let path = dir.join(file); + let mut path = dir.join(file); let target = optional_regular(&path)?; let identity = target .as_ref() @@ -512,11 +510,11 @@ fn unshared_body(dir: &Path, profiles: &IProfiles, file: &str) -> Result { if let Some(identity) = &identity - && platform::file_identity(&other, &metadata)? == *identity + && platform::file_identity(&path, &metadata)? == *identity { return Err(StoreError::AliasedBody); }