From 601a333b0e309520582ce016972f6cc350ff022e Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 10 Feb 2023 11:24:39 +0530 Subject: [PATCH] Atomically update cached values file --- tools/utils/atomic-write.go | 1 + tools/utils/cached_values.go | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/tools/utils/atomic-write.go b/tools/utils/atomic-write.go index 8e21d3f22..f67f1cd04 100644 --- a/tools/utils/atomic-write.go +++ b/tools/utils/atomic-write.go @@ -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) diff --git a/tools/utils/cached_values.go b/tools/utils/cached_values.go index 3785f4185..ced751557 100644 --- a/tools/utils/cached_values.go +++ b/tools/utils/cached_values.go @@ -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) } }