mirror of https://github.com/hak5/bolt.git
move to separate lock file on windows
parent
6fa1249006
commit
220b61e988
|
@ -11,7 +11,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// flock acquires an advisory lock on a file descriptor.
|
// flock acquires an advisory lock on a file descriptor.
|
||||||
func flock(f *os.File, exclusive bool, timeout time.Duration) error {
|
func flock(db *DB, mode os.FileMode, exclusive bool, timeout time.Duration) error {
|
||||||
var t time.Time
|
var t time.Time
|
||||||
for {
|
for {
|
||||||
// If we're beyond our timeout then return an error.
|
// If we're beyond our timeout then return an error.
|
||||||
|
@ -27,7 +27,7 @@ func flock(f *os.File, exclusive bool, timeout time.Duration) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Otherwise attempt to obtain an exclusive lock.
|
// Otherwise attempt to obtain an exclusive lock.
|
||||||
err := syscall.Flock(int(f.Fd()), flag|syscall.LOCK_NB)
|
err := syscall.Flock(int(db.file.Fd()), flag|syscall.LOCK_NB)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return nil
|
return nil
|
||||||
} else if err != syscall.EWOULDBLOCK {
|
} else if err != syscall.EWOULDBLOCK {
|
||||||
|
@ -40,8 +40,8 @@ func flock(f *os.File, exclusive bool, timeout time.Duration) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// funlock releases an advisory lock on a file descriptor.
|
// funlock releases an advisory lock on a file descriptor.
|
||||||
func funlock(f *os.File) error {
|
func funlock(db *DB) error {
|
||||||
return syscall.Flock(int(f.Fd()), syscall.LOCK_UN)
|
return syscall.Flock(int(db.file.Fd()), syscall.LOCK_UN)
|
||||||
}
|
}
|
||||||
|
|
||||||
// mmap memory maps a DB's data file.
|
// mmap memory maps a DB's data file.
|
||||||
|
|
|
@ -11,7 +11,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// flock acquires an advisory lock on a file descriptor.
|
// flock acquires an advisory lock on a file descriptor.
|
||||||
func flock(f *os.File, exclusive bool, timeout time.Duration) error {
|
func flock(db *DB, mode os.FileMode, exclusive bool, timeout time.Duration) error {
|
||||||
var t time.Time
|
var t time.Time
|
||||||
for {
|
for {
|
||||||
// If we're beyond our timeout then return an error.
|
// If we're beyond our timeout then return an error.
|
||||||
|
@ -32,7 +32,7 @@ func flock(f *os.File, exclusive bool, timeout time.Duration) error {
|
||||||
} else {
|
} else {
|
||||||
lock.Type = syscall.F_RDLCK
|
lock.Type = syscall.F_RDLCK
|
||||||
}
|
}
|
||||||
err := syscall.FcntlFlock(f.Fd(), syscall.F_SETLK, &lock)
|
err := syscall.FcntlFlock(db.file.Fd(), syscall.F_SETLK, &lock)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return nil
|
return nil
|
||||||
} else if err != syscall.EAGAIN {
|
} else if err != syscall.EAGAIN {
|
||||||
|
@ -45,13 +45,13 @@ func flock(f *os.File, exclusive bool, timeout time.Duration) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// funlock releases an advisory lock on a file descriptor.
|
// funlock releases an advisory lock on a file descriptor.
|
||||||
func funlock(f *os.File) error {
|
func funlock(db *DB) error {
|
||||||
var lock syscall.Flock_t
|
var lock syscall.Flock_t
|
||||||
lock.Start = 0
|
lock.Start = 0
|
||||||
lock.Len = 0
|
lock.Len = 0
|
||||||
lock.Type = syscall.F_UNLCK
|
lock.Type = syscall.F_UNLCK
|
||||||
lock.Whence = 0
|
lock.Whence = 0
|
||||||
return syscall.FcntlFlock(uintptr(f.Fd()), syscall.F_SETLK, &lock)
|
return syscall.FcntlFlock(uintptr(db.file.Fd()), syscall.F_SETLK, &lock)
|
||||||
}
|
}
|
||||||
|
|
||||||
// mmap memory maps a DB's data file.
|
// mmap memory maps a DB's data file.
|
||||||
|
|
|
@ -16,6 +16,8 @@ var (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
lockExt = ".lock"
|
||||||
|
|
||||||
// see https://msdn.microsoft.com/en-us/library/windows/desktop/aa365203(v=vs.85).aspx
|
// see https://msdn.microsoft.com/en-us/library/windows/desktop/aa365203(v=vs.85).aspx
|
||||||
flagLockExclusive = 2
|
flagLockExclusive = 2
|
||||||
flagLockFailImmediately = 1
|
flagLockFailImmediately = 1
|
||||||
|
@ -46,7 +48,16 @@ func fdatasync(db *DB) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// flock acquires an advisory lock on a file descriptor.
|
// flock acquires an advisory lock on a file descriptor.
|
||||||
func flock(f *os.File, exclusive bool, timeout time.Duration) error {
|
func flock(db *DB, mode os.FileMode, exclusive bool, timeout time.Duration) error {
|
||||||
|
// Create a separate lock file on windows because a process
|
||||||
|
// cannot share an exclusive lock on the same file. This is
|
||||||
|
// needed during Tx.WriteTo().
|
||||||
|
f, err := os.OpenFile(db.path+lockExt, os.O_CREATE, mode)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
db.lockfile = f
|
||||||
|
|
||||||
var t time.Time
|
var t time.Time
|
||||||
for {
|
for {
|
||||||
// If we're beyond our timeout then return an error.
|
// If we're beyond our timeout then return an error.
|
||||||
|
@ -62,7 +73,7 @@ func flock(f *os.File, exclusive bool, timeout time.Duration) error {
|
||||||
flag |= flagLockExclusive
|
flag |= flagLockExclusive
|
||||||
}
|
}
|
||||||
|
|
||||||
err := lockFileEx(syscall.Handle(f.Fd()), flag, 0, 1, 0, &syscall.Overlapped{})
|
err := lockFileEx(syscall.Handle(db.lockfile.Fd()), flag, 0, 1, 0, &syscall.Overlapped{})
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return nil
|
return nil
|
||||||
} else if err != errLockViolation {
|
} else if err != errLockViolation {
|
||||||
|
@ -75,8 +86,11 @@ func flock(f *os.File, exclusive bool, timeout time.Duration) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// funlock releases an advisory lock on a file descriptor.
|
// funlock releases an advisory lock on a file descriptor.
|
||||||
func funlock(f *os.File) error {
|
func funlock(db *DB) error {
|
||||||
return unlockFileEx(syscall.Handle(f.Fd()), 0, 1, 0, &syscall.Overlapped{})
|
err := unlockFileEx(syscall.Handle(db.lockfile.Fd()), 0, 1, 0, &syscall.Overlapped{})
|
||||||
|
db.lockfile.Close()
|
||||||
|
os.Remove(db.path+lockExt)
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// mmap memory maps a DB's data file.
|
// mmap memory maps a DB's data file.
|
||||||
|
|
15
db.go
15
db.go
|
@ -93,6 +93,7 @@ type DB struct {
|
||||||
|
|
||||||
path string
|
path string
|
||||||
file *os.File
|
file *os.File
|
||||||
|
lockfile *os.File // windows only
|
||||||
dataref []byte // mmap'ed readonly, write throws SEGV
|
dataref []byte // mmap'ed readonly, write throws SEGV
|
||||||
data *[maxMapSize]byte
|
data *[maxMapSize]byte
|
||||||
datasz int
|
datasz int
|
||||||
|
@ -177,7 +178,7 @@ func Open(path string, mode os.FileMode, options *Options) (*DB, error) {
|
||||||
// if !options.ReadOnly.
|
// if !options.ReadOnly.
|
||||||
// The database file is locked using the shared lock (more than one process may
|
// The database file is locked using the shared lock (more than one process may
|
||||||
// hold a lock at the same time) otherwise (options.ReadOnly is set).
|
// hold a lock at the same time) otherwise (options.ReadOnly is set).
|
||||||
if err := flock(db.file, !db.readOnly, options.Timeout); err != nil {
|
if err := flock(db, mode, !db.readOnly, options.Timeout); err != nil {
|
||||||
_ = db.close()
|
_ = db.close()
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -379,6 +380,10 @@ func (db *DB) Close() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *DB) close() error {
|
func (db *DB) close() error {
|
||||||
|
if !db.opened {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
db.opened = false
|
db.opened = false
|
||||||
|
|
||||||
db.freelist = nil
|
db.freelist = nil
|
||||||
|
@ -397,7 +402,7 @@ func (db *DB) close() error {
|
||||||
// No need to unlock read-only file.
|
// No need to unlock read-only file.
|
||||||
if !db.readOnly {
|
if !db.readOnly {
|
||||||
// Unlock the file.
|
// Unlock the file.
|
||||||
if err := funlock(db.file); err != nil {
|
if err := funlock(db); err != nil {
|
||||||
log.Printf("bolt.Close(): funlock error: %s", err)
|
log.Printf("bolt.Close(): funlock error: %s", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -824,8 +829,10 @@ func (db *DB) grow(sz int) error {
|
||||||
// Truncate and fsync to ensure file size metadata is flushed.
|
// Truncate and fsync to ensure file size metadata is flushed.
|
||||||
// https://github.com/boltdb/bolt/issues/284
|
// https://github.com/boltdb/bolt/issues/284
|
||||||
if !db.NoGrowSync && !db.readOnly {
|
if !db.NoGrowSync && !db.readOnly {
|
||||||
if err := db.file.Truncate(int64(sz)); err != nil {
|
if runtime.GOOS != "windows" {
|
||||||
return fmt.Errorf("file resize error: %s", err)
|
if err := db.file.Truncate(int64(sz)); err != nil {
|
||||||
|
return fmt.Errorf("file resize error: %s", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if err := db.file.Sync(); err != nil {
|
if err := db.file.Sync(); err != nil {
|
||||||
return fmt.Errorf("file sync error: %s", err)
|
return fmt.Errorf("file sync error: %s", err)
|
||||||
|
|
Loading…
Reference in New Issue