From 73d81832a59ada7f3713bd8ab5ea6c3bca54f01b Mon Sep 17 00:00:00 2001 From: Simon Ruderich Date: Fri, 14 Jun 2019 20:28:29 +0200 Subject: [PATCH] nsscash: convert: create file atomically Previously the file was first truncated or created and then written to. Although "convert" is not designed to be used for deployment, it is a unexpected behavior. --- main.go | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) 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) } -- 2.43.2