Files
gastown/internal/util/atomic.go
Steve Yegge 213b3bab20 feat: Add atomic write pattern for state files (gt-wled7)
Prevents data loss from concurrent/interrupted state file writes by using
atomic write pattern (write to .tmp, then rename).

Changes:
- Add internal/util package with AtomicWriteJSON/AtomicWriteFile helpers
- Update witness/manager.go saveState to use atomic writes
- Update refinery/manager.go saveState to use atomic writes
- Update crew/manager.go saveState to use atomic writes
- Update daemon/types.go SaveState to use atomic writes
- Update polecat/namepool.go Save to use atomic writes
- Add comprehensive tests for atomic write utilities

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-28 15:57:53 -08:00

42 lines
1.1 KiB
Go

// Package util provides common utilities for Gas Town.
package util
import (
"encoding/json"
"os"
)
// AtomicWriteJSON writes JSON data to a file atomically.
// It first writes to a temporary file, then renames it to the target path.
// This prevents data corruption if the process crashes during write.
// The rename operation is atomic on POSIX systems.
func AtomicWriteJSON(path string, v interface{}) error {
data, err := json.MarshalIndent(v, "", " ")
if err != nil {
return err
}
return AtomicWriteFile(path, data, 0644)
}
// AtomicWriteFile writes data to a file atomically.
// It first writes to a temporary file, then renames it to the target path.
// This prevents data corruption if the process crashes during write.
// The rename operation is atomic on POSIX systems.
func AtomicWriteFile(path string, data []byte, perm os.FileMode) error {
tmpFile := path + ".tmp"
// Write to temp file
if err := os.WriteFile(tmpFile, data, perm); err != nil {
return err
}
// Atomic rename (on POSIX systems)
if err := os.Rename(tmpFile, path); err != nil {
// Clean up temp file on failure
_ = os.Remove(tmpFile)
return err
}
return nil
}