-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Always use transaction in JSONFile.Save() #1724
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,11 +5,13 @@ package libkb | |
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
jsonw "github.com/keybase/go-jsonw" | ||
"io" | ||
"os" | ||
"sync" | ||
|
||
jsonw "github.com/keybase/go-jsonw" | ||
) | ||
|
||
type jsonFileTransaction struct { | ||
|
@@ -84,14 +86,6 @@ func (f *JSONFile) Nuke() error { | |
return err | ||
} | ||
|
||
func (f *JSONFile) getFilename() string { | ||
tx := f.getTx() | ||
if tx != nil { | ||
return tx.tmpname | ||
} | ||
return f.filename | ||
} | ||
|
||
func (f *JSONFile) BeginTransaction() (ConfigWriterTransacter, error) { | ||
tx, err := newJSONFileTransaction(f) | ||
if err != nil { | ||
|
@@ -114,10 +108,25 @@ func (f *JSONFile) setTx(tx *jsonFileTransaction) error { | |
return nil | ||
} | ||
|
||
func (f *JSONFile) getTx() *jsonFileTransaction { | ||
func (f *JSONFile) getOrMakeTx() (*jsonFileTransaction, bool, error) { | ||
f.txMutex.Lock() | ||
defer f.txMutex.Unlock() | ||
return f.tx | ||
|
||
// if a transaction exists, use it | ||
if f.tx != nil { | ||
return f.tx, false, nil | ||
} | ||
|
||
// make a new transaction | ||
tx, err := newJSONFileTransaction(f) | ||
if err != nil { | ||
return nil, false, err | ||
} | ||
|
||
f.tx = tx | ||
|
||
// return true so caller knows that a transaction was created | ||
return f.tx, true, nil | ||
} | ||
|
||
func newJSONFileTransaction(f *JSONFile) (*jsonFileTransaction, error) { | ||
|
@@ -131,13 +140,43 @@ func newJSONFileTransaction(f *JSONFile) (*jsonFileTransaction, error) { | |
} | ||
|
||
func (f *JSONFile) Save() error { | ||
if err := f.save(f.getFilename(), true, 0); err != nil { | ||
tx, txCreated, err := f.getOrMakeTx() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can this function be simplified at all? since it's only a 3-liner, maybe don't use a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wouldn't we want Abort to be called if Commit fails? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Absolutely, but can't we do that in the error case of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But the Commit happens in capital Save() if it creates a transaction. |
||
if err != nil { | ||
return err | ||
} | ||
if txCreated { | ||
// if Save() created a transaction, then abort it if it | ||
// still exists on exit | ||
defer func() { | ||
if tx != nil { | ||
tx.Abort() | ||
} | ||
}() | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Working on this now... |
||
if err := f.save(); err != nil { | ||
return err | ||
} | ||
|
||
if txCreated { | ||
// this Save() call created a transaction, so commit it | ||
if err := tx.Commit(); err != nil { | ||
return err | ||
} | ||
|
||
// Commit worked, clear the transaction so defer() doesn't | ||
// abort it. | ||
tx = nil | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (f *JSONFile) save(filename string, pretty bool, mode os.FileMode) (err error) { | ||
func (f *JSONFile) save() (err error) { | ||
if f.tx == nil { | ||
return errors.New("save() called with nil transaction") | ||
} | ||
filename := f.tx.tmpname | ||
f.G().Log.Debug("+ saving %s file %s", f.which, filename) | ||
|
||
err = MakeParentDirs(filename) | ||
|
@@ -162,25 +201,17 @@ func (f *JSONFile) save(filename string, pretty bool, mode os.FileMode) (err err | |
} | ||
var writer *os.File | ||
flags := (os.O_WRONLY | os.O_CREATE | os.O_TRUNC) | ||
if mode == 0 { | ||
mode = PermFile // By default, secrecy | ||
} | ||
writer, err = os.OpenFile(filename, flags, mode) | ||
writer, err = os.OpenFile(filename, flags, PermFile) | ||
if err != nil { | ||
f.G().Log.Errorf("Failed to open %s file %s for writing: %s", | ||
f.which, filename, err) | ||
return err | ||
} | ||
defer writer.Close() | ||
|
||
if pretty { | ||
encoded, err := json.MarshalIndent(dat, "", " ") | ||
if err == nil { | ||
_, err = writer.Write(encoded) | ||
} | ||
} else { | ||
encoder := json.NewEncoder(writer) | ||
err = encoder.Encode(dat) | ||
encoded, err := json.MarshalIndent(dat, "", " ") | ||
if err == nil { | ||
_, err = writer.Write(encoded) | ||
} | ||
|
||
if err != nil { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is there any more need for
getTx
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not after above change is complete.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool so let's yank it