Skip to content

Commit

Permalink
#138 Custom links storage. Recover storage file on initalizing
Browse files Browse the repository at this point in the history
  • Loading branch information
arturalbov authored and hleb-albau committed Mar 13, 2019
1 parent d8d5419 commit 3b28308
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module github.com/cybercongress/cyberd

require (
github.com/ZondaX/hid-go v0.0.0-20180905224704-48b08affede2 // indirect
github.com/arturalbov/atomicf v0.1.0
github.com/arturalbov/atomicf v0.1.1
github.com/bartekn/go-bip39 v0.0.0-20171116152956-a05967ea095d // indirect
github.com/bgentry/speakeasy v0.1.0 // indirect
github.com/cosmos/cosmos-sdk v0.33.0-0.20190227001305-3804d0dbb44d
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ github.com/allegro/bigcache v1.1.0/go.mod h1:Cb/ax3seSYIx7SuZdm2G2xzfwmv3TPSk2uc
github.com/aristanetworks/goarista v0.0.0-20181220211322-0ca71131d8f7/go.mod h1:D/tb0zPVXnP7fmsLZjtdUhSsumbK/ij54UXjjVgMGxQ=
github.com/arturalbov/atomicf v0.1.0 h1:0HIy5Up+3ZWtHjFGjWuCCLBUmKQpMkRhVTrMFNbxKCY=
github.com/arturalbov/atomicf v0.1.0/go.mod h1:9YJWSVIJGlczi2b4HSWul016jK8yQsfIm/tFrbyThZ0=
github.com/arturalbov/atomicf v0.1.1 h1:ccGx5acFqmaaimc4o/CU9tcw/fB4xLSO7jrloBdod6E=
github.com/arturalbov/atomicf v0.1.1/go.mod h1:9YJWSVIJGlczi2b4HSWul016jK8yQsfIm/tFrbyThZ0=
github.com/bartekn/go-bip39 v0.0.0-20171116152956-a05967ea095d h1:1aAija9gr0Hyv4KfQcRcwlmFIrhkDmIj2dz5bkg/s/8=
github.com/bartekn/go-bip39 v0.0.0-20171116152956-a05967ea095d/go.mod h1:icNx/6QdFblhsEjZehARqbNumymUT/ydwlLojFdv7Sk=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973 h1:xJ4a3vCFaGF/jqvzLMYoU8P317H5OQ+Via4RmuPwCS0=
Expand Down
9 changes: 7 additions & 2 deletions store/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,18 @@ func NewBaseStorage(name string, dir string, elementLen uint64) (*BaseStorage, e
func NewBaseStorageBuf(name string, dir string, elementLen uint64, bufferSize uint) (*BaseStorage, error) {

dbFilePath := filepath.Join(dir, name+DbFileFormat)
file, err := os.OpenFile(dbFilePath, os.O_RDWR|os.O_CREATE|os.O_SYNC, 0666)
file, err := atomicf.OpenFile(dbFilePath, os.O_RDWR|os.O_CREATE|os.O_SYNC, 0666)
defer file.Close()

if err != nil {
return nil, err
}

// Try to recover file if it was corrupted
if err = file.Recover(); err != nil {
return nil, err
}

lastVersion, err := getLastVersion(file)
if err != nil {
return nil, err
Expand All @@ -65,7 +70,7 @@ func NewBaseStorageBuf(name string, dir string, elementLen uint64, bufferSize ui
}, nil
}

func getLastVersion(file *os.File) (ver int64, err error) {
func getLastVersion(file *atomicf.AtomicFile) (ver int64, err error) {
ver = int64(-1)
fileStat, _ := file.Stat()
if fileStat.Size() > 8 {
Expand Down

0 comments on commit 3b28308

Please sign in to comment.