From a8121dbc90d35f6d9e71dac0f5897c23363cf5a9 Mon Sep 17 00:00:00 2001 From: Horizontal42 <75383519+Horizontal42@users.noreply.github.com> Date: Wed, 12 Aug 2026 19:54:37 +0300 Subject: [PATCH] share atomic-write bytes helper between write_atomic and image export write_image_file hand-rolled the same temp+rename dance write_atomic already did for text, just for bytes. Split write_atomic into a write_atomic_bytes core and have both callers use it. --- src-tauri/src/main.rs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 49a40f5..7aaa5b0 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -76,13 +76,7 @@ fn write_image_file(app: AppHandle, data_base64: String) -> Result .blocking_save_file() { let path = p.into_path().map_err(|e| e.to_string())?; - use std::io::Write; - let bytes = base64_decode(&data_base64)?; - let tmp = path.with_extension("tmp"); - std::fs::File::create(&tmp) - .and_then(|mut f| f.write_all(&bytes)) - .map_err(|e| e.to_string())?; - fs::rename(&tmp, path).map_err(|e| e.to_string())?; + write_atomic_bytes(&path, &base64_decode(&data_base64)?)?; Ok(true) } else { Ok(false) @@ -123,12 +117,16 @@ fn base64_decode(s: &str) -> Result, String> { } /// write via a temp file + rename so a crash mid-write cannot corrupt the target -fn write_atomic(path: &Path, contents: &str) -> Result<(), String> { +fn write_atomic_bytes(path: &Path, contents: &[u8]) -> Result<(), String> { let tmp = path.with_extension("tmp"); fs::write(&tmp, contents).map_err(|e| e.to_string())?; fs::rename(&tmp, path).map_err(|e| e.to_string()) } +fn write_atomic(path: &Path, contents: &str) -> Result<(), String> { + write_atomic_bytes(path, contents.as_bytes()) +} + #[tauri::command] fn save_file(app: AppHandle, name: String, contents: String) -> Result<(), String> { if !safe_name(&name) {