]> ruderich.org/simon Gitweb - nsscash/nsscash.git/blobdiff - main.go
.github: update upstream actions to latest version
[nsscash/nsscash.git] / main.go
diff --git a/main.go b/main.go
index 5455dfe0b367cc8bf92f2ba20979273a97dce086..7a7d8b0da8e6855b8545c609f6169184819db7ae 100644 (file)
--- a/main.go
+++ b/main.go
@@ -1,6 +1,6 @@
 // Main file for nsscash
 
-// Copyright (C) 2019  Simon Ruderich
+// Copyright (C) 2019-2021  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
@@ -25,6 +25,8 @@ import (
        "log"
        "os"
        "path/filepath"
+
+       "github.com/google/renameio"
 )
 
 func main() {
@@ -130,29 +132,28 @@ func mainConvert(typ, srcPath, dstPath string) error {
                return fmt.Errorf("unsupported file type %v", t)
        }
 
-       dstDir := filepath.Dir(dstPath)
-       dstName := filepath.Base(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 := ioutil.TempFile(dstDir, "tmp-"+dstName+"-")
+       f, err := renameio.TempFile(filepath.Dir(dstPath), dstPath)
        if err != nil {
                return err
        }
-       tmpPath := f.Name()
-       defer os.Remove(tmpPath)
-       f.Close()
+       defer f.Cleanup()
 
        err = deployFile(&File{
                Type: t,
                Url:  srcPath,
-               Path: tmpPath,
+               Path: f.Name(),
                body: x.Bytes(),
        })
        if err != nil {
                return err
        }
 
-       return os.Rename(tmpPath, dstPath)
+       err = f.CloseAtomicallyReplace()
+       if err != nil {
+               return err
+       }
+       return syncPath(filepath.Dir(dstPath))
 }