From 7dd7cffbe9b01a24b2a1b37ede70ca8af76d6ebf Mon Sep 17 00:00:00 2001 From: Josh Rickmar Date: Wed, 15 Jan 2014 10:55:09 -0500 Subject: [PATCH] Stop using unix time for temp files. --- disksync.go | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/disksync.go b/disksync.go index 6f2247c..f19a28d 100644 --- a/disksync.go +++ b/disksync.go @@ -19,6 +19,7 @@ package main import ( "fmt" "github.com/conformal/btcwire" + "io/ioutil" "os" "path/filepath" "sync" @@ -135,19 +136,21 @@ func (a *Account) writeDirtyToDisk() error { dirty := a.TxStore.dirty a.UtxoStore.RUnlock() if dirty { - a.UtxoStore.Lock() - defer a.UtxoStore.Unlock() - tmpfilepath := utxofilepath + "-" + timeStr - tmpfile, err := os.Create(tmpfilepath) + netdir, filename := filepath.Split(utxofilepath) + tmpfile, err := ioutil.TempFile(netdir, filename) if err != nil { return err } + + a.UtxoStore.Lock() + defer a.UtxoStore.Unlock() + if _, err = a.UtxoStore.s.WriteTo(tmpfile); err != nil { return err } tmpfile.Close() - if err = Rename(tmpfilepath, utxofilepath); err != nil { + if err = Rename(tmpfile.Name(), utxofilepath); err != nil { return err } @@ -159,19 +162,21 @@ func (a *Account) writeDirtyToDisk() error { dirty = a.TxStore.dirty a.TxStore.RUnlock() if dirty { - a.TxStore.Lock() - defer a.TxStore.Unlock() - tmpfilepath := txfilepath + "-" + timeStr - tmpfile, err := os.Create(tmpfilepath) + netdir, filename := filepath.Split(txfilepath) + tmpfile, err := ioutil.TempFile(netdir, filename) if err != nil { return err } + + a.TxStore.Lock() + defer a.TxStore.Unlock() + if _, err = a.TxStore.s.WriteTo(tmpfile); err != nil { return err } tmpfile.Close() - if err = Rename(tmpfilepath, txfilepath); err != nil { + if err = Rename(tmpfile.Name(), txfilepath); err != nil { return err } @@ -183,19 +188,21 @@ func (a *Account) writeDirtyToDisk() error { dirty = a.dirty a.mtx.RUnlock() if dirty { - a.mtx.Lock() - defer a.mtx.Unlock() - tmpfilepath := wfilepath + "-" + timeStr - tmpfile, err := os.Create(tmpfilepath) + netdir, filename := filepath.Split(wfilepath) + tmpfile, err := ioutil.TempFile(netdir, filename) if err != nil { return err } + + a.mtx.Lock() + defer a.mtx.Unlock() + if _, err = a.WriteTo(tmpfile); err != nil { return err } tmpfile.Close() - if err = Rename(tmpfilepath, wfilepath); err != nil { + if err = Rename(tmpfile.Name(), wfilepath); err != nil { return err }