Files
HackBrowserData/internal/atomicfile/atomicfile.go
T
Roger 8ce917d57e fix: write keys.json atomically, unify zip handling (#686)
dumpkeys truncated keys.json before writing it, so a failed write left a partial file; app now writes it through the
same temp-file-and-rename path zips use. Unzipping moves next to zip writing and checks entries with filepath.IsLocal.
2026-11-09 17:25:35 +08:00

35 lines
1.0 KiB
Go

// Package atomicfile writes a file in full or not at all, so no reader ever sees a partial one.
package atomicfile
import (
"errors"
"fmt"
"io"
"os"
"path/filepath"
)
// Write renames a finished temp file into place, so path is never partial and a failure keeps the old file.
func Write(path string, write func(io.Writer) error) (err error) {
// CreateTemp makes a new 0600 file under an unpredictable name, so a planted file or symlink is never written through.
temporary, err := os.CreateTemp(filepath.Dir(path), filepath.Base(path)+".*.tmp")
if err != nil {
return fmt.Errorf("create %s: %w", path, err)
}
defer func() {
if err != nil {
err = errors.Join(err, os.Remove(temporary.Name()))
}
}()
if err := write(temporary); err != nil {
return errors.Join(err, temporary.Close())
}
if err := temporary.Close(); err != nil {
return fmt.Errorf("finish %s: %w", path, err)
}
if err := os.Rename(temporary.Name(), path); err != nil {
return fmt.Errorf("move %s into place: %w", path, err)
}
return nil
}