fix(notfiles): atomic state file writes via temp-file + rename

Closes #17
This commit is contained in:
Joseph O'Brien
2026-04-11 23:51:43 -04:00
parent bdc6582aea
commit 822abd9c88

View File

@@ -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(())
}