diff options
Diffstat (limited to 'vendor/github.com/ncruces/go-sqlite3/vfs/os_bsd.go')
-rw-r--r-- | vendor/github.com/ncruces/go-sqlite3/vfs/os_bsd.go | 57 |
1 files changed, 39 insertions, 18 deletions
diff --git a/vendor/github.com/ncruces/go-sqlite3/vfs/os_bsd.go b/vendor/github.com/ncruces/go-sqlite3/vfs/os_bsd.go index 9f3c99daf..1f54a6929 100644 --- a/vendor/github.com/ncruces/go-sqlite3/vfs/os_bsd.go +++ b/vendor/github.com/ncruces/go-sqlite3/vfs/os_bsd.go @@ -4,31 +4,15 @@ package vfs import ( "os" - "time" "golang.org/x/sys/unix" ) -func osUnlock(file *os.File, start, len int64) _ErrorCode { - if start == 0 && len == 0 { - err := unix.Flock(int(file.Fd()), unix.LOCK_UN) - if err != nil { - return _IOERR_UNLOCK - } - } - return _OK -} - -func osLock(file *os.File, how int, def _ErrorCode) _ErrorCode { - err := unix.Flock(int(file.Fd()), how) - return osLockErrorCode(err, def) -} - -func osReadLock(file *os.File, _ /*start*/, _ /*len*/ int64, _ /*timeout*/ time.Duration) _ErrorCode { +func osGetSharedLock(file *os.File) _ErrorCode { return osLock(file, unix.LOCK_SH|unix.LOCK_NB, _IOERR_RDLOCK) } -func osWriteLock(file *os.File, _ /*start*/, _ /*len*/ int64, _ /*timeout*/ time.Duration) _ErrorCode { +func osGetReservedLock(file *os.File) _ErrorCode { rc := osLock(file, unix.LOCK_EX|unix.LOCK_NB, _IOERR_LOCK) if rc == _BUSY { // The documentation states the lock is upgraded by releasing the previous lock, @@ -38,3 +22,40 @@ func osWriteLock(file *os.File, _ /*start*/, _ /*len*/ int64, _ /*timeout*/ time } return rc } + +func osGetExclusiveLock(file *os.File, state *LockLevel) _ErrorCode { + if *state >= LOCK_RESERVED { + return _OK + } + return osGetReservedLock(file) +} + +func osDowngradeLock(file *os.File, _ LockLevel) _ErrorCode { + rc := osLock(file, unix.LOCK_SH|unix.LOCK_NB, _IOERR_RDLOCK) + if rc == _BUSY { + // The documentation states the lock is upgraded by releasing the previous lock, + // then acquiring the new lock. + // This is a race, so return IOERR_RDLOCK to ensure the transaction is aborted. + return _IOERR_RDLOCK + } + return _OK +} + +func osReleaseLock(file *os.File, _ LockLevel) _ErrorCode { + err := unix.Flock(int(file.Fd()), unix.LOCK_UN) + if err != nil { + return _IOERR_UNLOCK + } + return _OK +} + +func osCheckReservedLock(file *os.File) (bool, _ErrorCode) { + // Test the RESERVED lock. + lock, rc := osTestLock(file, _RESERVED_BYTE, 1) + return lock == unix.F_WRLCK, rc +} + +func osLock(file *os.File, how int, def _ErrorCode) _ErrorCode { + err := unix.Flock(int(file.Fd()), how) + return osLockErrorCode(err, def) +} |