diff options
author | Ben Johnson <benbjohnson@yahoo.com> | 2015-05-18 13:45:09 -0600 |
---|---|---|
committer | Ben Johnson <benbjohnson@yahoo.com> | 2015-05-18 13:45:09 -0600 |
commit | d4363a920813e181d1a65a7e4c2ac7bdbcbd94a1 (patch) | |
tree | 2fce56e5ba9f85b2dd8cdc2fd2255b7675cca6f7 /bolt_unix.go | |
parent | Merge pull request #375 from benbjohnson/min-mmap-size (diff) | |
parent | Add test case inline documentation. (diff) | |
download | dedo-d4363a920813e181d1a65a7e4c2ac7bdbcbd94a1.tar.gz dedo-d4363a920813e181d1a65a7e4c2ac7bdbcbd94a1.tar.xz |
Merge branch 'ro'
Diffstat (limited to 'bolt_unix.go')
-rw-r--r-- | bolt_unix.go | 10 |
1 files changed, 7 insertions, 3 deletions
diff --git a/bolt_unix.go b/bolt_unix.go index 35dce08..266222a 100644 --- a/bolt_unix.go +++ b/bolt_unix.go @@ -11,7 +11,7 @@ import ( ) // flock acquires an advisory lock on a file descriptor. -func flock(f *os.File, timeout time.Duration) error { +func flock(f *os.File, exclusive bool, timeout time.Duration) error { var t time.Time for { // If we're beyond our timeout then return an error. @@ -21,9 +21,13 @@ func flock(f *os.File, timeout time.Duration) error { } else if timeout > 0 && time.Since(t) > timeout { return ErrTimeout } + flag := syscall.LOCK_SH + if exclusive { + flag = syscall.LOCK_EX + } // Otherwise attempt to obtain an exclusive lock. - err := syscall.Flock(int(f.Fd()), syscall.LOCK_EX|syscall.LOCK_NB) + err := syscall.Flock(int(f.Fd()), flag|syscall.LOCK_NB) if err == nil { return nil } else if err != syscall.EWOULDBLOCK { @@ -44,7 +48,7 @@ func funlock(f *os.File) error { func mmap(db *DB, sz int) error { // Truncate and fsync to ensure file size metadata is flushed. // https://github.com/boltdb/bolt/issues/284 - if !db.NoGrowSync { + if !db.NoGrowSync && !db.readOnly { if err := db.file.Truncate(int64(sz)); err != nil { return fmt.Errorf("file resize error: %s", err) } |