// Main file for nsscash
-// Copyright (C) 2019 Simon Ruderich
+// Copyright (C) 2019-2020 Simon Ruderich
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
"io/ioutil"
"log"
"os"
+ "path/filepath"
+
+ "github.com/google/renameio"
)
func main() {
return fmt.Errorf("unsupported file type %v", t)
}
- // We must create the file first or deployFile() will abort
- f, err := os.Create(dstPath)
+ // We must create the file first or deployFile() will abort; this is
+ // ugly because deployFile() already performs an atomic replacement
+ // but the simplest solution with the least duplicate code
+ f, err := renameio.TempFile(filepath.Dir(dstPath), dstPath)
if err != nil {
return err
}
- f.Close()
+ defer f.Cleanup()
err = deployFile(&File{
Type: t,
Url: srcPath,
- Path: dstPath,
+ Path: f.Name(),
body: x.Bytes(),
})
if err != nil {
return err
}
- return nil
+
+ return f.CloseAtomicallyReplace()
}