Skip to content

Commit

Permalink
cross-platform
Browse files Browse the repository at this point in the history
  • Loading branch information
yihuang committed Jul 13, 2023
1 parent 189734e commit 192639f
Show file tree
Hide file tree
Showing 11 changed files with 26 additions and 64 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ require (
github.com/tklauser/numcpus v0.4.0 // indirect
github.com/tyler-smith/go-bip39 v1.1.0 // indirect
github.com/ulikunitz/xz v0.5.10 // indirect
github.com/zbiljic/go-filelock v0.0.0-20170914061330-1dbf7103ab7d // indirect
github.com/zondax/hid v0.9.1 // indirect
github.com/zondax/ledger-go v0.14.1 // indirect
go.etcd.io/bbolt v1.3.7 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1307,6 +1307,8 @@ github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
github.com/zbiljic/go-filelock v0.0.0-20170914061330-1dbf7103ab7d h1:XQyeLr7N9iY9mi+TGgsBFkj54+j3fdoo8e2u6zrGP5A=
github.com/zbiljic/go-filelock v0.0.0-20170914061330-1dbf7103ab7d/go.mod h1:hoMeDjlNXTNqVwrCk8YDyaBS2g5vFfEX2ezMi4vb6CY=
github.com/zondax/hid v0.9.1 h1:gQe66rtmyZ8VeGFcOpbuH3r7erYtNEAezCAYu8LdkJo=
github.com/zondax/hid v0.9.1/go.mod h1:l5wttcP0jwtdLjqjMMWFVEE7d1zO0jvSPA9OPZxWpEM=
github.com/zondax/ledger-go v0.14.1 h1:Pip65OOl4iJ84WTpA4BKChvOufMhhbxED3BaihoZN4c=
Expand Down
3 changes: 3 additions & 0 deletions gomod2nix.toml
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,9 @@ schema = 3
[mod."github.com/ulikunitz/xz"]
version = "v0.5.10"
hash = "sha256-bogOwQNmQVS7W+C7wci7XEUeYm9TB7PnxnyBIXKYbm0="
[mod."github.com/zbiljic/go-filelock"]
version = "v0.0.0-20170914061330-1dbf7103ab7d"
hash = "sha256-JqNj/Wg8nGFSmndgYC7+FZzL2zG7rwOQMjlqYs3ZGvw="
[mod."github.com/zondax/hid"]
version = "v0.9.1"
hash = "sha256-hSVmN/f/lQHFhF60o6ej78ELC0MMoqQgqIX2hHjdTXg="
Expand Down
4 changes: 2 additions & 2 deletions memiavl/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type DB struct {
MultiTree
dir string
logger log.Logger
fileLock *FileLock
fileLock FileLock
readOnly bool

// result channel of snapshot rewrite goroutine
Expand Down Expand Up @@ -143,7 +143,7 @@ func Load(dir string, opts Options) (*DB, error) {

var (
err error
fileLock *FileLock
fileLock FileLock
)
if !opts.ReadOnly {
fileLock, err = LockFile(filepath.Join(dir, LockFileName))
Expand Down
71 changes: 9 additions & 62 deletions memiavl/filelock.go
Original file line number Diff line number Diff line change
@@ -1,72 +1,19 @@
package memiavl

import (
"errors"
"fmt"
"io"
"os"
"sync"
"syscall"
)
import "github.com/zbiljic/go-filelock"

var (
lockedFiles = make(map[string]struct{})
lockedFilesMutex sync.Mutex
)

type FileLock struct {
Fname string
File *os.File
}

func (fl *FileLock) Unlock() error {
lockedFilesMutex.Lock()
defer lockedFilesMutex.Unlock()

err := errors.Join(
LockOrUnlock(fl.File, false),
fl.File.Close(),
)

delete(lockedFiles, fl.Fname)
fl.File = nil
return err
type FileLock interface {
Unlock() error
}

func LockFile(fname string) (*FileLock, error) {
lockedFilesMutex.Lock()
defer lockedFilesMutex.Unlock()

if _, ok := lockedFiles[fname]; ok {
return nil, errors.New("lock is already hold by current process")
}

lockFile, err := os.OpenFile(fname, os.O_RDWR|os.O_CREATE, 0o600)
func LockFile(fname string) (FileLock, error) {
fl, err := filelock.New(fname)
if err != nil {
return nil, fmt.Errorf("failed to open lock file: %w", err)
return nil, err
}

Check warning on line 13 in memiavl/filelock.go

View check run for this annotation

Codecov / codecov/patch

memiavl/filelock.go#L12-L13

Added lines #L12 - L13 were not covered by tests
lockedFiles[fname] = struct{}{}

if err := LockOrUnlock(lockFile, true); err != nil {
return nil, fmt.Errorf("failed to lock file: %w", err)
if _, err := fl.TryLock(); err != nil {
return nil, err
}

return &FileLock{
Fname: fname,
File: lockFile,
}, nil
}

// LockOrUnlock grab or release the exclusive lock on an opened file
func LockOrUnlock(file *os.File, lock bool) error {
op := int16(syscall.F_WRLCK)
if !lock {
op = syscall.F_UNLCK
}
return syscall.FcntlFlock(file.Fd(), syscall.F_SETLK, &syscall.Flock_t{
Start: 0,
Len: 0,
Type: op,
Whence: io.SeekStart,
})
return fl, nil
}
1 change: 1 addition & 0 deletions memiavl/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ require (
github.com/tidwall/btree v1.5.0
github.com/tidwall/gjson v1.10.2
github.com/tidwall/wal v1.1.7
github.com/zbiljic/go-filelock v0.0.0-20170914061330-1dbf7103ab7d
golang.org/x/exp v0.0.0-20230310171629-522b1b587ee0
golang.org/x/sync v0.1.0
golang.org/x/sys v0.7.0
Expand Down
2 changes: 2 additions & 0 deletions memiavl/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,8 @@ github.com/yudai/golcs v0.0.0-20170316035057-ecda9a501e82/go.mod h1:lgjkn3NuSvDf
github.com/yudai/pp v2.0.1+incompatible/go.mod h1:PuxR/8QJ7cyCkFp/aUDS+JY727OFEZkTdatxwunjIkc=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/zbiljic/go-filelock v0.0.0-20170914061330-1dbf7103ab7d h1:XQyeLr7N9iY9mi+TGgsBFkj54+j3fdoo8e2u6zrGP5A=
github.com/zbiljic/go-filelock v0.0.0-20170914061330-1dbf7103ab7d/go.mod h1:hoMeDjlNXTNqVwrCk8YDyaBS2g5vFfEX2ezMi4vb6CY=
github.com/zondax/hid v0.9.1 h1:gQe66rtmyZ8VeGFcOpbuH3r7erYtNEAezCAYu8LdkJo=
github.com/zondax/ledger-go v0.14.1 h1:Pip65OOl4iJ84WTpA4BKChvOufMhhbxED3BaihoZN4c=
go.etcd.io/bbolt v1.3.6 h1:/ecaJf0sk1l4l6V4awd65v2C3ILy7MSj+s/x1ADCIMU=
Expand Down
1 change: 1 addition & 0 deletions store/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ require (
github.com/tidwall/pretty v1.2.0 // indirect
github.com/tidwall/tinylru v1.1.0 // indirect
github.com/tidwall/wal v1.1.7 // indirect
github.com/zbiljic/go-filelock v0.0.0-20170914061330-1dbf7103ab7d // indirect
github.com/zondax/hid v0.9.1 // indirect
github.com/zondax/ledger-go v0.14.1 // indirect
go.etcd.io/bbolt v1.3.6 // indirect
Expand Down
2 changes: 2 additions & 0 deletions store/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,8 @@ github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/zbiljic/go-filelock v0.0.0-20170914061330-1dbf7103ab7d h1:XQyeLr7N9iY9mi+TGgsBFkj54+j3fdoo8e2u6zrGP5A=
github.com/zbiljic/go-filelock v0.0.0-20170914061330-1dbf7103ab7d/go.mod h1:hoMeDjlNXTNqVwrCk8YDyaBS2g5vFfEX2ezMi4vb6CY=
github.com/zondax/hid v0.9.1 h1:gQe66rtmyZ8VeGFcOpbuH3r7erYtNEAezCAYu8LdkJo=
github.com/zondax/hid v0.9.1/go.mod h1:l5wttcP0jwtdLjqjMMWFVEE7d1zO0jvSPA9OPZxWpEM=
github.com/zondax/ledger-go v0.14.1 h1:Pip65OOl4iJ84WTpA4BKChvOufMhhbxED3BaihoZN4c=
Expand Down
1 change: 1 addition & 0 deletions versiondb/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ require (
github.com/tidwall/pretty v1.2.0 // indirect
github.com/tidwall/tinylru v1.1.0 // indirect
github.com/tidwall/wal v1.1.7 // indirect
github.com/zbiljic/go-filelock v0.0.0-20170914061330-1dbf7103ab7d // indirect
github.com/zondax/hid v0.9.1 // indirect
github.com/zondax/ledger-go v0.14.1 // indirect
go.etcd.io/bbolt v1.3.7 // indirect
Expand Down
2 changes: 2 additions & 0 deletions versiondb/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1028,6 +1028,8 @@ github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
github.com/zbiljic/go-filelock v0.0.0-20170914061330-1dbf7103ab7d h1:XQyeLr7N9iY9mi+TGgsBFkj54+j3fdoo8e2u6zrGP5A=
github.com/zbiljic/go-filelock v0.0.0-20170914061330-1dbf7103ab7d/go.mod h1:hoMeDjlNXTNqVwrCk8YDyaBS2g5vFfEX2ezMi4vb6CY=
github.com/zondax/hid v0.9.1 h1:gQe66rtmyZ8VeGFcOpbuH3r7erYtNEAezCAYu8LdkJo=
github.com/zondax/hid v0.9.1/go.mod h1:l5wttcP0jwtdLjqjMMWFVEE7d1zO0jvSPA9OPZxWpEM=
github.com/zondax/ledger-go v0.14.1 h1:Pip65OOl4iJ84WTpA4BKChvOufMhhbxED3BaihoZN4c=
Expand Down

0 comments on commit 192639f

Please sign in to comment.