X-Git-Url: https://ruderich.org/simon/gitweb/?p=nsscash%2Fnsscash.git;a=blobdiff_plain;f=main.go;fp=main.go;h=5248a77b8794b6ba8eccf6f46ca396053cae3471;hp=31ef0493a770cffebf38eb6c4d57c628743d17fd;hb=4d994341317c2e700370950bd5fd9ac05963fc66;hpb=1002c514a8530bb6608c556b4446e853be390917 diff --git a/main.go b/main.go index 31ef049..5248a77 100644 --- a/main.go +++ b/main.go @@ -25,6 +25,8 @@ import ( "log" "os" "path/filepath" + + "github.com/google/renameio" ) func main() { @@ -130,29 +132,24 @@ 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) + return f.CloseAtomicallyReplace() }