From c8662c67c2ce098c8b5cce449e7d6ef902f8e904 Mon Sep 17 00:00:00 2001 From: Anis Elleuch Date: Tue, 7 Jul 2015 16:40:38 +0100 Subject: [PATCH] Avoid corrupted data when saving with quick --- pkg/quick/quick.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/pkg/quick/quick.go b/pkg/quick/quick.go index 70760c7c..371bd574 100644 --- a/pkg/quick/quick.go +++ b/pkg/quick/quick.go @@ -115,7 +115,9 @@ func (d config) Save(filename string) (err error) { return iodine.New(err, nil) } - file, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600) + tmpfile := filename + ".tmp" + + file, err := os.OpenFile(tmpfile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600) if err != nil { return iodine.New(err, nil) } @@ -128,6 +130,10 @@ func (d config) Save(filename string) (err error) { if err != nil { return iodine.New(err, nil) } + err = os.Rename(tmpfile, filename) + if err != nil { + return iodine.New(err, nil) + } return nil }