aboutsummaryrefslogtreecommitdiff
path: root/nvdmirror/jsoncache.go
diff options
context:
space:
mode:
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
}