From 85a6763a37e9d14aff61fe28000db0e7329e249d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Halber?= Date: Mon, 4 May 2026 22:19:18 +0000 Subject: [PATCH] fix: clean up temp file on failed config save --- src/config/mod.rs | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/config/mod.rs b/src/config/mod.rs index c0d8dc8..32f2f3e 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -202,17 +202,15 @@ pub(crate) fn trimmed_option(value: Option<&str>) -> Option<&str> { } pub fn save_file(path: &Path, config: &Config) -> Result<()> { - if let Some(parent) = path.parent() { - fs::create_dir_all(parent)?; - } + let parent = path.parent().unwrap_or_else(|| Path::new(".")); + fs::create_dir_all(parent)?; let json = serde_json::to_string_pretty(config)?; - let temp_path = path.with_extension("tmp"); - let mut file = fs::File::create(&temp_path)?; + let mut file = tempfile::NamedTempFile::new_in(parent)?; file.write_all(json.as_bytes())?; file.write_all(b"\n")?; - file.sync_all()?; - fs::rename(&temp_path, path)?; + file.as_file().sync_all()?; + file.persist(path)?; Ok(()) }