10 // writeFileAtomic replaces the file at path with data through a temporary file
11 // and a rename. A reader or a crash of the process sees the old file or the new
12 // file, never a part of one.
13 func writeFileAtomic(path string, data []byte) error {
14 file, err := os.CreateTemp(filepath.Dir(path), "."+filepath.Base(path)+".*")
16 return fmt.Errorf("create temporary file: %w", err)
19 _, err = file.Write(data)
20 err = errors.Join(err, file.Close())
22 err = os.Rename(file.Name(), path)
26 return errors.Join(fmt.Errorf("replace file: %w", err), os.Remove(file.Name()))