Atomically update cached values file

This commit is contained in:
Kovid Goyal 2023-02-10 11:24:39 +05:30
parent cc5107d0db
commit 601a333b0e
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 2 additions and 1 deletions

View File

@ -24,6 +24,7 @@ func AtomicWriteFile(path string, data []byte, perm os.FileMode) (err error) {
f.Close()
if !removed {
os.Remove(f.Name())
removed = true
}
}()
_, err = f.Write(data)

View File

@ -31,7 +31,7 @@ func (self *CachedValues[T]) Load() T {
func (self *CachedValues[T]) Save() {
raw, err := json.Marshal(self.Opts)
if err == nil {
os.WriteFile(self.Path(), raw, 0600)
AtomicUpdateFile(self.Path(), raw, 0o600)
}
}