X-Git-Url: https://ruderich.org/simon/gitweb/?p=nsscash%2Fnsscash.git;a=blobdiff_plain;f=main.go;h=5455dfe0b367cc8bf92f2ba20979273a97dce086;hp=a563b511355273983d0a0ea3060a288ad64e456d;hb=73d81832a59ada7f3713bd8ab5ea6c3bca54f01b;hpb=78239472f30846add39a4f14455cc92aba8ea79b diff --git a/main.go b/main.go index a563b51..5455dfe 100644 --- a/main.go +++ b/main.go @@ -24,6 +24,7 @@ import ( "io/ioutil" "log" "os" + "path/filepath" ) func main() { @@ -129,21 +130,29 @@ func mainConvert(typ, srcPath, dstPath string) error { return fmt.Errorf("unsupported file type %v", t) } - // We must create the file first or deployFile() will abort - f, err := os.Create(dstPath) + 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+"-") if err != nil { return err } + tmpPath := f.Name() + defer os.Remove(tmpPath) f.Close() err = deployFile(&File{ Type: t, Url: srcPath, - Path: dstPath, + Path: tmpPath, body: x.Bytes(), }) if err != nil { return err } - return nil + + return os.Rename(tmpPath, dstPath) }