X-Git-Url: https://ruderich.org/simon/gitweb/?p=nsscash%2Fnsscash.git;a=blobdiff_plain;f=file.go;fp=file.go;h=f382ba4ef8bdcae5100d568cdd09009fef0b33b4;hp=c7e580990d7ba54ada969e6d454acecc54c07704;hb=4d994341317c2e700370950bd5fd9ac05963fc66;hpb=1002c514a8530bb6608c556b4446e853be390917 diff --git a/file.go b/file.go index c7e5809..f382ba4 100644 --- a/file.go +++ b/file.go @@ -30,6 +30,7 @@ import ( "syscall" "time" + "github.com/google/renameio" "github.com/pkg/errors" ) @@ -164,18 +165,11 @@ func deployFile(file *File) error { return fmt.Errorf("refusing to write empty file") } - // Write the file in an atomic fashion by creating a temporary file - // and renaming it over the target file - - dir := filepath.Dir(file.Path) - name := filepath.Base(file.Path) - - f, err := ioutil.TempFile(dir, "tmp-"+name+"-") + f, err := renameio.TempFile(filepath.Dir(file.Path), file.Path) if err != nil { return err } - defer os.Remove(f.Name()) - defer f.Close() + defer f.Cleanup() // Apply permissions/user/group from the target file but remove the // write permissions to discourage manual modifications, use Stat @@ -204,9 +198,5 @@ func deployFile(file *File) error { if err != nil { return err } - err = f.Sync() - if err != nil { - return err - } - return os.Rename(f.Name(), file.Path) + return f.CloseAtomicallyReplace() }