From 822abd9c8823db152154ed844e0622e762cb88fa Mon Sep 17 00:00:00 2001 From: Joseph O'Brien <98370624+89jobrien@users.noreply.github.com> Date: Sat, 11 Apr 2026 23:51:43 -0400 Subject: [PATCH] fix(notfiles): atomic state file writes via temp-file + rename Closes #17 --- crates/notfiles/src/linker.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/crates/notfiles/src/linker.rs b/crates/notfiles/src/linker.rs index 2e7605f..04685ce 100644 --- a/crates/notfiles/src/linker.rs +++ b/crates/notfiles/src/linker.rs @@ -39,9 +39,13 @@ impl State { pub fn save(&self, dotfiles_dir: &Path) -> Result<(), NotfilesError> { let path = dotfiles_dir.join(STATE_FILE); + let tmp_path = dotfiles_dir.join(format!("{STATE_FILE}.tmp")); let content = toml::to_string_pretty(self) .map_err(|e| NotfilesError::State(format!("serializing state: {e}")))?; - fs::write(&path, content)?; + fs::write(&tmp_path, content) + .map_err(|e| NotfilesError::State(format!("writing temp state: {e}")))?; + fs::rename(&tmp_path, &path) + .map_err(|e| NotfilesError::State(format!("renaming temp state: {e}")))?; Ok(()) }