Files
sqlite3/vfs_unix.go

138 lines
3.6 KiB
Go
Raw Permalink Normal View History

2023-01-26 14:52:38 +00:00
//go:build unix
2023-01-22 17:33:21 +00:00
package sqlite3
2023-01-25 14:59:02 +00:00
2023-01-25 16:23:18 +00:00
import (
"os"
2023-01-25 17:58:42 +00:00
"runtime"
2023-01-25 16:23:18 +00:00
"syscall"
)
2023-01-25 14:59:02 +00:00
2023-02-23 13:29:51 +00:00
func (vfsOSMethods) DeleteOnClose(file *os.File) {
_ = os.Remove(file.Name())
2023-01-25 14:59:02 +00:00
}
2023-01-25 16:23:18 +00:00
2023-02-23 13:29:51 +00:00
func (vfsOSMethods) GetExclusiveLock(file *os.File) xErrorCode {
2023-01-25 16:23:18 +00:00
// Acquire the EXCLUSIVE lock.
2023-02-23 13:29:51 +00:00
return vfsOS.writeLock(file, _SHARED_FIRST, _SHARED_SIZE)
2023-01-25 16:23:18 +00:00
}
2023-02-23 13:29:51 +00:00
func (vfsOSMethods) DowngradeLock(file *os.File, state vfsLockState) xErrorCode {
if state >= _EXCLUSIVE_LOCK {
2023-02-21 12:51:52 +00:00
// Downgrade to a SHARED lock.
2023-02-23 13:29:51 +00:00
if rc := vfsOS.readLock(file, _SHARED_FIRST, _SHARED_SIZE); rc != _OK {
2023-02-21 12:51:52 +00:00
// In theory, the downgrade to a SHARED cannot fail because another
// process is holding an incompatible lock. If it does, this
// indicates that the other process is not following the locking
// protocol. If this happens, return IOERR_RDLOCK. Returning
// BUSY would confuse the upper layer.
return IOERR_RDLOCK
}
2023-01-25 16:23:18 +00:00
}
// Release the PENDING and RESERVED locks.
2023-02-23 13:29:51 +00:00
return vfsOS.unlock(file, _PENDING_BYTE, 2)
2023-01-25 16:23:18 +00:00
}
2023-02-23 13:29:51 +00:00
func (vfsOSMethods) ReleaseLock(file *os.File, _ vfsLockState) xErrorCode {
2023-01-25 16:23:18 +00:00
// Release all locks.
2023-02-23 13:29:51 +00:00
return vfsOS.unlock(file, 0, 0)
2023-02-13 13:53:32 +00:00
}
2023-02-23 13:29:51 +00:00
func (vfsOSMethods) unlock(file *os.File, start, len int64) xErrorCode {
err := vfsOS.fcntlSetLock(file, &syscall.Flock_t{
2023-02-08 12:10:17 +00:00
Type: syscall.F_UNLCK,
Start: start,
Len: len,
})
if err != nil {
2023-01-25 16:23:18 +00:00
return IOERR_UNLOCK
}
return _OK
}
2023-02-23 13:29:51 +00:00
func (vfsOSMethods) readLock(file *os.File, start, len int64) xErrorCode {
return vfsOS.lockErrorCode(vfsOS.fcntlSetLock(file, &syscall.Flock_t{
2023-02-08 12:10:17 +00:00
Type: syscall.F_RDLCK,
Start: start,
Len: len,
2023-02-14 11:38:05 +00:00
}), IOERR_RDLOCK)
2023-02-08 12:10:17 +00:00
}
2023-02-23 13:29:51 +00:00
func (vfsOSMethods) writeLock(file *os.File, start, len int64) xErrorCode {
return vfsOS.lockErrorCode(vfsOS.fcntlSetLock(file, &syscall.Flock_t{
2023-02-08 12:10:17 +00:00
Type: syscall.F_WRLCK,
Start: start,
Len: len,
}), IOERR_LOCK)
}
2023-02-23 13:29:51 +00:00
func (vfsOSMethods) checkLock(file *os.File, start, len int64) (bool, xErrorCode) {
2023-01-25 16:23:18 +00:00
lock := syscall.Flock_t{
2023-02-07 03:11:59 +00:00
Type: syscall.F_RDLCK,
2023-02-08 12:10:17 +00:00
Start: start,
Len: len,
2023-01-25 16:23:18 +00:00
}
2023-02-23 13:29:51 +00:00
if vfsOS.fcntlGetLock(file, &lock) != nil {
2023-01-25 16:23:18 +00:00
return false, IOERR_CHECKRESERVEDLOCK
}
2023-01-30 08:12:12 +00:00
return lock.Type != syscall.F_UNLCK, _OK
2023-01-25 16:23:18 +00:00
}
2023-02-23 13:29:51 +00:00
func (vfsOSMethods) fcntlGetLock(file *os.File, lock *syscall.Flock_t) error {
var F_OFD_GETLK int
2023-01-29 02:44:10 +00:00
switch runtime.GOOS {
case "linux":
// https://github.com/torvalds/linux/blob/master/include/uapi/asm-generic/fcntl.h
2023-02-23 13:29:51 +00:00
F_OFD_GETLK = 36 // F_OFD_GETLK
2023-01-29 02:44:10 +00:00
case "darwin":
// https://github.com/apple/darwin-xnu/blob/main/bsd/sys/fcntl.h
2023-02-23 13:29:51 +00:00
F_OFD_GETLK = 92 // F_OFD_GETLK
2023-02-13 13:51:35 +00:00
case "illumos":
2023-02-08 12:10:17 +00:00
// https://github.com/illumos/illumos-gate/blob/master/usr/src/uts/common/sys/fcntl.h
2023-02-23 13:29:51 +00:00
F_OFD_GETLK = 47 // F_OFD_GETLK
default:
return notImplErr
2023-01-25 17:58:42 +00:00
}
2023-02-23 13:29:51 +00:00
return syscall.FcntlFlock(file.Fd(), F_OFD_GETLK, lock)
2023-01-25 16:23:18 +00:00
}
2023-02-23 13:29:51 +00:00
func (vfsOSMethods) fcntlSetLock(file *os.File, lock *syscall.Flock_t) error {
var F_OFD_SETLK int
2023-01-29 02:44:10 +00:00
switch runtime.GOOS {
case "linux":
// https://github.com/torvalds/linux/blob/master/include/uapi/asm-generic/fcntl.h
2023-02-23 13:29:51 +00:00
F_OFD_SETLK = 37 // F_OFD_SETLK
2023-01-29 02:44:10 +00:00
case "darwin":
// https://github.com/apple/darwin-xnu/blob/main/bsd/sys/fcntl.h
2023-02-23 13:29:51 +00:00
F_OFD_SETLK = 90 // F_OFD_SETLK
2023-02-13 13:51:35 +00:00
case "illumos":
2023-02-08 12:10:17 +00:00
// https://github.com/illumos/illumos-gate/blob/master/usr/src/uts/common/sys/fcntl.h
2023-02-23 13:29:51 +00:00
F_OFD_SETLK = 48 // F_OFD_SETLK
default:
return notImplErr
2023-01-25 17:58:42 +00:00
}
2023-02-23 13:29:51 +00:00
return syscall.FcntlFlock(file.Fd(), F_OFD_SETLK, lock)
2023-01-27 01:45:38 +00:00
}
2023-02-23 13:29:51 +00:00
func (vfsOSMethods) lockErrorCode(err error, def xErrorCode) xErrorCode {
2023-02-07 15:04:42 +00:00
if err == nil {
return _OK
}
2023-01-27 01:45:38 +00:00
if errno, ok := err.(syscall.Errno); ok {
switch errno {
2023-02-13 14:49:15 +00:00
case
syscall.EACCES,
syscall.EAGAIN,
syscall.EBUSY,
syscall.EINTR,
syscall.ENOLCK,
syscall.EDEADLK,
syscall.ETIMEDOUT:
2023-02-08 00:31:06 +00:00
return xErrorCode(BUSY)
2023-01-27 01:45:38 +00:00
case syscall.EPERM:
2023-02-08 00:31:06 +00:00
return xErrorCode(PERM)
2023-01-27 01:45:38 +00:00
}
}
return def
2023-01-25 16:23:18 +00:00
}