Stop using unix time for temp files.

This commit is contained in:
Josh Rickmar 2014-01-15 10:55:09 -05:00
parent cec819a887
commit 7dd7cffbe9

View file

@ -19,6 +19,7 @@ package main
import ( import (
"fmt" "fmt"
"github.com/conformal/btcwire" "github.com/conformal/btcwire"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"sync" "sync"
@ -135,19 +136,21 @@ func (a *Account) writeDirtyToDisk() error {
dirty := a.TxStore.dirty dirty := a.TxStore.dirty
a.UtxoStore.RUnlock() a.UtxoStore.RUnlock()
if dirty { if dirty {
a.UtxoStore.Lock() netdir, filename := filepath.Split(utxofilepath)
defer a.UtxoStore.Unlock() tmpfile, err := ioutil.TempFile(netdir, filename)
tmpfilepath := utxofilepath + "-" + timeStr
tmpfile, err := os.Create(tmpfilepath)
if err != nil { if err != nil {
return err return err
} }
a.UtxoStore.Lock()
defer a.UtxoStore.Unlock()
if _, err = a.UtxoStore.s.WriteTo(tmpfile); err != nil { if _, err = a.UtxoStore.s.WriteTo(tmpfile); err != nil {
return err return err
} }
tmpfile.Close() tmpfile.Close()
if err = Rename(tmpfilepath, utxofilepath); err != nil { if err = Rename(tmpfile.Name(), utxofilepath); err != nil {
return err return err
} }
@ -159,19 +162,21 @@ func (a *Account) writeDirtyToDisk() error {
dirty = a.TxStore.dirty dirty = a.TxStore.dirty
a.TxStore.RUnlock() a.TxStore.RUnlock()
if dirty { if dirty {
a.TxStore.Lock() netdir, filename := filepath.Split(txfilepath)
defer a.TxStore.Unlock() tmpfile, err := ioutil.TempFile(netdir, filename)
tmpfilepath := txfilepath + "-" + timeStr
tmpfile, err := os.Create(tmpfilepath)
if err != nil { if err != nil {
return err return err
} }
a.TxStore.Lock()
defer a.TxStore.Unlock()
if _, err = a.TxStore.s.WriteTo(tmpfile); err != nil { if _, err = a.TxStore.s.WriteTo(tmpfile); err != nil {
return err return err
} }
tmpfile.Close() tmpfile.Close()
if err = Rename(tmpfilepath, txfilepath); err != nil { if err = Rename(tmpfile.Name(), txfilepath); err != nil {
return err return err
} }
@ -183,19 +188,21 @@ func (a *Account) writeDirtyToDisk() error {
dirty = a.dirty dirty = a.dirty
a.mtx.RUnlock() a.mtx.RUnlock()
if dirty { if dirty {
a.mtx.Lock() netdir, filename := filepath.Split(wfilepath)
defer a.mtx.Unlock() tmpfile, err := ioutil.TempFile(netdir, filename)
tmpfilepath := wfilepath + "-" + timeStr
tmpfile, err := os.Create(tmpfilepath)
if err != nil { if err != nil {
return err return err
} }
a.mtx.Lock()
defer a.mtx.Unlock()
if _, err = a.WriteTo(tmpfile); err != nil { if _, err = a.WriteTo(tmpfile); err != nil {
return err return err
} }
tmpfile.Close() tmpfile.Close()
if err = Rename(tmpfilepath, wfilepath); err != nil { if err = Rename(tmpfile.Name(), wfilepath); err != nil {
return err return err
} }