// Package persistence provides crash-resistant single-file replacement. // It does not serialize callers or make multi-file operations transactional. package persistence import ( "errors" "fmt" "os" "path/filepath" ) // ErrDurabilityUncertain reports that the atomic replacement was renamed into // place but the directory could not be synced (or a post-rename chmod // failed). The new content is visible to readers, but a crash may still lose // it. Callers that must distinguish "nothing happened" from "the replacement // landed but may not survive a crash" should test errors.Is against this. var ErrDurabilityUncertain = errors.New("replacement completed; durability uncertain") // WriteFile writes and syncs a temporary file in the destination directory, // atomically renames it over path, then syncs the directory. Existing file // permissions are preserved; perm is used for new files. Callers must own the // data being serialized and coordinate competing read-modify-write operations. // A directory-sync error means replacement occurred but durability is uncertain. func WriteFile(path string, data []byte, perm os.FileMode) error { if info, err := os.Stat(path); err == nil { perm = info.Mode().Perm() } else if !os.IsNotExist(err) { return err } dir := filepath.Dir(path) f, err := os.CreateTemp(dir, ".save-*") if err != nil { return err } defer os.Remove(f.Name()) defer f.Close() if _, err := f.Write(data); err != nil { return err } if err := f.Chmod(perm); err != nil { return err } if err := f.Sync(); err != nil { return err } if err := f.Close(); err != nil { return err } if err := os.Rename(f.Name(), path); err != nil { return err } d, err := os.Open(dir) if err != nil { return fmt.Errorf("%w: open directory for sync: %v", ErrDurabilityUncertain, err) } defer d.Close() if err := d.Sync(); err != nil { return fmt.Errorf("%w: sync directory: %v", ErrDurabilityUncertain, err) } return nil }