aboutsummaryrefslogtreecommitdiff
path: root/nvdmirror/jsoncache.go
diff options
context:
space:
mode:
authorPaul Duncan <pabs@pablotron.org>2022-02-22 21:58:02 -0500
committerPaul Duncan <pabs@pablotron.org>2022-02-22 21:58:02 -0500
commitf7c4aa78652fd7211c9b4f41ddc897cd956e43e0 (patch)
tree36f114a8c50f68c9c6bdc620ee076d47fb17c3b3 /nvdmirror/jsoncache.go
parentb6c5f5f40635ed83c0d0439fe145ed5b96cab3c5 (diff)
downloadcvez-f7c4aa78652fd7211c9b4f41ddc897cd956e43e0.tar.bz2
cvez-f7c4aa78652fd7211c9b4f41ddc897cd956e43e0.zip
nvdmirror: use atomictemp
Diffstat (limited to 'nvdmirror/jsoncache.go')
-rw-r--r--nvdmirror/jsoncache.go35
1 files changed, 17 insertions, 18 deletions
diff --git a/nvdmirror/jsoncache.go b/nvdmirror/jsoncache.go
index 7e84a94..7bcd0ca 100644
--- a/nvdmirror/jsoncache.go
+++ b/nvdmirror/jsoncache.go
@@ -3,9 +3,10 @@ package nvdmirror
import (
"compress/gzip"
"encoding/json"
+ "io"
"os"
- "path/filepath"
"time"
+ "github.com/pablotron/cvez/atomictemp"
)
// JSON file backed URL cache.
@@ -102,26 +103,24 @@ func (me *JsonCache) Close() error {
}
// open temp output file
- t, err := os.CreateTemp(filepath.Dir(me.path), "")
- if err != nil {
- return err
- }
- defer os.Remove(t.Name())
+ err := atomictemp.Create(me.path, func(w io.Writer) error {
+ // encode/compress JSON
+ zw := gzip.NewWriter(w)
+ e := json.NewEncoder(zw)
+ if err := e.Encode(me.vals); err != nil {
+ return err
+ }
- // encode/compress JSON
- zw := gzip.NewWriter(t)
- e := json.NewEncoder(zw)
- if err = e.Encode(me.vals); err != nil {
- return err
- }
+ // flush changes
+ if err := zw.Flush(); err != nil {
+ return err
+ }
- // flush changes
- if err = zw.Flush(); err != nil {
- return err
- }
+ // return success
+ return nil
+ })
- // rename to destination file
- if err = os.Rename(t.Name(), me.path); err != nil {
+ if err != nil {
return err
}