From ebe09d0afe77a25dbae3e2769a76548dfc7463c0 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 25 Apr 2026 13:31:26 +0000 Subject: [PATCH] refactor(sync): use shared atomic_write in queue/state save OfflineQueue::save and SyncState::save each had their own copy of the temp-file + rename + cleanup-on-failure dance, even though storage's pub(crate) atomic_write is already imported in this file. Inline the shared helper so the crate has one canonical atomic write path. --- crates/onyx-core/src/sync.rs | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/crates/onyx-core/src/sync.rs b/crates/onyx-core/src/sync.rs index a21cf9d..457e1a3 100644 --- a/crates/onyx-core/src/sync.rs +++ b/crates/onyx-core/src/sync.rs @@ -341,13 +341,7 @@ impl OfflineQueue { return Ok(()); } let content = serde_json::to_string_pretty(self)?; - // Atomic write: write to temp then rename - let temp_path = workspace_path.join(".syncqueue.json.tmp"); - std::fs::write(&temp_path, &content)?; - if let Err(e) = std::fs::rename(&temp_path, &queue_path) { - let _ = std::fs::remove_file(&temp_path); - return Err(e.into()); - } + atomic_write(&queue_path, content.as_bytes())?; Ok(()) } @@ -534,14 +528,7 @@ impl SyncState { pub fn save(&self, workspace_path: &Path) -> Result<()> { let state_path = workspace_path.join(".syncstate.json"); let content = serde_json::to_string_pretty(self)?; - // Atomic write: write to temp file then rename to prevent corruption on crash - let temp_path = workspace_path.join(".syncstate.json.tmp"); - std::fs::write(&temp_path, &content)?; - if let Err(e) = std::fs::rename(&temp_path, &state_path) { - // Clean up temp file on rename failure to prevent accumulation - let _ = std::fs::remove_file(&temp_path); - return Err(e.into()); - } + atomic_write(&state_path, content.as_bytes())?; Ok(()) }