Files
sqlite3/vfs_windows.go

103 lines
2.6 KiB
Go
Raw Permalink Normal View History

2023-01-22 17:33:21 +00:00
package sqlite3
2023-01-25 14:59:02 +00:00
2023-02-08 15:58:36 +00:00
import (
"os"
"syscall"
"golang.org/x/sys/windows"
)
2023-01-26 00:05:52 +00:00
2023-02-23 13:29:51 +00:00
func (vfsOSMethods) DeleteOnClose(file *os.File) {}
2023-01-25 16:23:18 +00:00
2023-02-23 13:29:51 +00:00
func (vfsOSMethods) GetExclusiveLock(file *os.File) xErrorCode {
2023-02-08 15:58:36 +00:00
// Release the SHARED lock.
2023-02-23 13:29:51 +00:00
vfsOS.unlock(file, _SHARED_FIRST, _SHARED_SIZE)
2023-02-08 15:58:36 +00:00
// Acquire the EXCLUSIVE lock.
2023-02-23 13:29:51 +00:00
rc := vfsOS.writeLock(file, _SHARED_FIRST, _SHARED_SIZE)
2023-02-08 15:58:36 +00:00
// Reacquire the SHARED lock.
if rc != _OK {
2023-02-23 13:29:51 +00:00
vfsOS.readLock(file, _SHARED_FIRST, _SHARED_SIZE)
2023-02-08 15:58:36 +00:00
}
return rc
2023-01-26 00:05:52 +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
// Release the SHARED lock.
2023-02-23 13:29:51 +00:00
vfsOS.unlock(file, _SHARED_FIRST, _SHARED_SIZE)
2023-02-08 15:58:36 +00:00
2023-02-21 12:51:52 +00:00
// Reacquire the 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
// This should never happen.
// We should always be able to reacquire the read lock.
return IOERR_RDLOCK
}
2023-02-08 15:58:36 +00:00
}
// Release the PENDING and RESERVED locks.
2023-02-23 13:29:51 +00:00
if state >= _RESERVED_LOCK {
vfsOS.unlock(file, _RESERVED_BYTE, 1)
2023-02-21 12:51:52 +00:00
}
2023-02-23 13:29:51 +00:00
if state >= _PENDING_LOCK {
vfsOS.unlock(file, _PENDING_BYTE, 1)
2023-02-21 12:51:52 +00:00
}
2023-01-26 00:05:52 +00:00
return _OK
}
2023-02-23 13:29:51 +00:00
func (vfsOSMethods) ReleaseLock(file *os.File, state vfsLockState) xErrorCode {
2023-02-08 15:58:36 +00:00
// Release all locks.
2023-02-23 13:29:51 +00:00
if state >= _RESERVED_LOCK {
vfsOS.unlock(file, _RESERVED_BYTE, 1)
2023-02-21 12:51:52 +00:00
}
2023-02-23 13:29:51 +00:00
if state >= _SHARED_LOCK {
vfsOS.unlock(file, _SHARED_FIRST, _SHARED_SIZE)
2023-02-21 12:51:52 +00:00
}
2023-02-23 13:29:51 +00:00
if state >= _PENDING_LOCK {
vfsOS.unlock(file, _PENDING_BYTE, 1)
2023-02-21 12:51:52 +00:00
}
2023-01-26 00:05:52 +00:00
return _OK
}
2023-02-23 13:29:51 +00:00
func (vfsOSMethods) unlock(file *os.File, start, len uint32) xErrorCode {
err := windows.UnlockFileEx(windows.Handle(file.Fd()),
2023-02-08 15:58:36 +00:00
0, len, 0, &windows.Overlapped{Offset: start})
if err != nil {
return IOERR_UNLOCK
}
return _OK
}
2023-02-23 13:29:51 +00:00
func (vfsOSMethods) readLock(file *os.File, start, len uint32) xErrorCode {
return vfsOS.lockErrorCode(windows.LockFileEx(windows.Handle(file.Fd()),
2023-02-08 15:58:36 +00:00
windows.LOCKFILE_FAIL_IMMEDIATELY,
0, len, 0, &windows.Overlapped{Offset: start}),
2023-02-14 11:38:05 +00:00
IOERR_RDLOCK)
2023-02-08 15:58:36 +00:00
}
2023-02-23 13:29:51 +00:00
func (vfsOSMethods) writeLock(file *os.File, start, len uint32) xErrorCode {
return vfsOS.lockErrorCode(windows.LockFileEx(windows.Handle(file.Fd()),
2023-02-08 15:58:36 +00:00
windows.LOCKFILE_FAIL_IMMEDIATELY|windows.LOCKFILE_EXCLUSIVE_LOCK,
0, len, 0, &windows.Overlapped{Offset: start}),
IOERR_LOCK)
}
2023-02-23 13:29:51 +00:00
func (vfsOSMethods) checkLock(file *os.File, start, len uint32) (bool, xErrorCode) {
rc := vfsOS.readLock(file, start, len)
if rc == _OK {
vfsOS.unlock(file, start, len)
}
return rc != _OK, _OK
}
func (vfsOSMethods) lockErrorCode(err error, def xErrorCode) xErrorCode {
2023-02-08 15:58:36 +00:00
if err == nil {
return _OK
}
if errno, _ := err.(syscall.Errno); errno == windows.ERROR_INVALID_HANDLE {
return def
}
return xErrorCode(BUSY)
2023-01-26 00:05:52 +00:00
}