From 9a5e133820f19c462b818a1e95678d5961a2827a Mon Sep 17 00:00:00 2001 From: Daniel Barney Date: Wed, 7 Oct 2015 11:24:38 -0600 Subject: [PATCH] the file should be written to a tmp location first --- scribble.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/scribble.go b/scribble.go index eb9712d..e22fa7c 100644 --- a/scribble.go +++ b/scribble.go @@ -114,14 +114,20 @@ func (d *Driver) write(trans Transaction) error { return err } + finalPath := dir + "/" + trans.ResourceID + ".json" + tmpPath := finalPath + "~" + // write marshaled data to a file, named by the resourceID - if err := ioutil.WriteFile(dir+"/"+trans.ResourceID, b, 0666); err != nil { + if err := ioutil.WriteFile(tmpPath, b, 0666); err != nil { return err } + // move final file into place + err = os.Rename(tmpPath, finalPath) + mutex.Unlock() - return nil + return err } // read does the opposite operation as write. Reading a record from the database