diff options
Diffstat (limited to 'nvdmirror/nvdmirror.go')
-rw-r--r-- | nvdmirror/nvdmirror.go | 25 |
1 files changed, 9 insertions, 16 deletions
diff --git a/nvdmirror/nvdmirror.go b/nvdmirror/nvdmirror.go index da758ad..cba638b 100644 --- a/nvdmirror/nvdmirror.go +++ b/nvdmirror/nvdmirror.go @@ -3,11 +3,11 @@ package nvdmirror import ( "fmt" + "github.com/pablotron/cvez/atomictemp" "github.com/rs/zerolog/log" "io" "net/http" "net/url" - "os" "path/filepath" "time" ) @@ -83,14 +83,6 @@ func fetch(ch chan fetchResult, config SyncConfig, cache Cache, client *http.Cli path := filepath.Join(dstDir, filepath.Base(src.Path)) log.Debug().Str("url", srcUrl).Str("path", path).Send() - // create temporary output file - f, err := os.CreateTemp(filepath.Dir(path), "") - if err != nil { - ch <- fetchResult { src: srcUrl, err: err } - return - } - defer os.Remove(f.Name()) - // create request req, err := http.NewRequest("GET", srcUrl, nil) if err != nil { @@ -116,15 +108,16 @@ func fetch(ch chan fetchResult, config SyncConfig, cache Cache, client *http.Cli switch resp.StatusCode { case 200: - // copy body to result - if size, err := io.Copy(f, resp.Body); err != nil { - // copy failed - ch <- fetchResult { src: srcUrl, err: err, modified: false } - } else if err = os.Rename(f.Name(), path); err != nil { - // rename failed + // create temporary output file + err := atomictemp.Create(path, func(f io.Writer) error { + _, err := io.Copy(f, resp.Body) + return err + }) + + if err != nil { + // write failed ch <- fetchResult { src: srcUrl, err: err, modified: false } } else { - log.Debug().Str("url", srcUrl).Int64("size", size).Send() ch <- fetchResult { src: srcUrl, modified: true, |