Skip to content

Commit

Permalink
More tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickxb committed Sep 9, 2017
1 parent 186991f commit f3268c9
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 18 deletions.
2 changes: 0 additions & 2 deletions go/auth/credential_authority.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,8 +416,6 @@ func (u *user) check(ca checkArg) {
return
}
}

return
}

// getUserFromServer runs the UserKeyAPIer GetUser() API call while paying
Expand Down
4 changes: 0 additions & 4 deletions go/libkb/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ func (f File) GetFilename() string {
return f.filename
}

func (f File) DataLen() int64 {
return int64(len(f.data))
}

// WriteTo is for SafeWriter
func (f File) WriteTo(w io.Writer) (int64, error) {
n, err := w.Write(f.data)
Expand Down
36 changes: 35 additions & 1 deletion go/libkb/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ package libkb

import (
"os"
"sync"
"testing"
)

func TestFileSave(t *testing.T) {
filename := "file_test.tmp"

defer os.Remove(filename)

file := NewFile(filename, []byte("test data"), 0644)
Expand All @@ -20,3 +20,37 @@ func TestFileSave(t *testing.T) {
t.Fatal(err)
}
}

func TestFileSaveConcurrent(t *testing.T) {
filename := "file_test.tmp"
defer os.Remove(filename)

var wg sync.WaitGroup
for i := 0; i < 20; i++ {
wg.Add(1)
go func() {
file := NewFile(filename, []byte("test data"), 0644)
t.Logf("Saving")
err := file.Save(G.Log)
if err != nil {
t.Errorf("save err: %s", err)
}
wg.Done()
}()
}
wg.Wait()

var wg2 sync.WaitGroup
file := NewFile(filename, []byte("test data"), 0644)
for i := 0; i < 20; i++ {
wg2.Add(1)
go func() {
err := file.Save(G.Log)
if err != nil {
t.Errorf("save err: %s", err)
}
wg2.Done()
}()
}
wg2.Wait()
}
4 changes: 2 additions & 2 deletions go/libkb/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ func (f *JSONFile) save() (err error) {
f.G().Log.Debug("- saved %s file %s", f.which, filename)

if runtime.GOOS == "android" {
f.G().Log.Debug("| Android extra-checking in JSONFile.save")
f.G().Log.Debug("| Android extra checks in JSONFile.save")
info, err := os.Stat(filename)
if err != nil {
f.G().Log.Errorf("| Error os.Stat(%s): %s", filename, err)
Expand All @@ -259,7 +259,7 @@ func (f *JSONFile) save() (err error) {
return fmt.Errorf("file info size (%d) does not match encoded len (%d)", info.Size(), len(encoded))
}

f.G().Log.Debug("| Android extra-checking done")
f.G().Log.Debug("| Android extra checks done")
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion go/libkb/leveldb.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type levelDBOps interface {

func levelDbPut(ops levelDBOps, id DbKey, aliases []DbKey, value []byte) error {
idb := id.ToBytes(levelDbTableKv)
if aliases == nil || len(aliases) == 0 {
if len(aliases) == 0 {
// if no aliases, just do a put
return ops.Put(idb, value, nil)
}
Expand Down
19 changes: 11 additions & 8 deletions go/libkb/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,37 +187,40 @@ func safeWriteToFileOnce(g SafeWriteLogger, t SafeWriter, mode os.FileMode) (err
return err
}
g.Debug("| Temporary file generated: %s", tmpfn)
defer tmp.Close()
defer os.Remove(tmpfn)

g.Debug("| WriteTo %s", tmpfn)
_, err = t.WriteTo(tmp)
n, err := t.WriteTo(tmp)
if err != nil {
g.Errorf("| Error writing temporary file %s: %s", tmpfn, err)
tmp.Close()
os.Remove(tmpfn)
return err
}
if n != 0 {
// unfortunately, some implementations always return 0 for the number
// of bytes written, so not much info there, but will log it when
// it isn't 0.
g.Debug("| bytes written to temporary file %s: %d", tmpfn, n)
}

if err := tmp.Sync(); err != nil {
g.Errorf("| Error syncing temporary file %s: %s", tmpfn, err)
os.Remove(tmpfn)
return err
}

if err := tmp.Close(); err != nil {
g.Errorf("| Error closing temporary file %s: %s", tmpfn, err)
os.Remove(tmpfn)
return err
}

g.Debug("| Renaming temporary file %s -> permanent file %s", tmpfn, fn)
if err := os.Rename(tmpfn, fn); err != nil {
g.Errorf("| Error renaming temporary file %s -> permanent file %s: %s", tmpfn, fn, err)
os.Remove(tmpfn)
return err
}

if runtime.GOOS == "android" {
g.Debug("| Android extra-checking in safeWriteToFile")
g.Debug("| Android extra checks in safeWriteToFile")
info, err := os.Stat(fn)
if err != nil {
g.Errorf("| Error os.Stat(%s): %s", fn, err)
Expand All @@ -228,7 +231,7 @@ func safeWriteToFileOnce(g SafeWriteLogger, t SafeWriter, mode os.FileMode) (err
g.Debug("| File info: mode = %s", info.Mode())
g.Debug("| File info: mod time = %s", info.ModTime())

g.Debug("| Android extra-checking done")
g.Debug("| Android extra checks done")
}

g.Debug("| Done writing to file %s", fn)
Expand Down

0 comments on commit f3268c9

Please sign in to comment.