Files
sqlite3/vfs_os_linux.go

93 lines
1.9 KiB
Go
Raw Normal View History

2023-03-17 17:13:03 +00:00
package sqlite3
import (
"os"
"time"
"golang.org/x/sys/unix"
)
func (vfsOSMethods) Sync(file *os.File, fullsync, dataonly bool) error {
if dataonly {
_, _, err := unix.Syscall(unix.SYS_FDATASYNC, file.Fd(), 0, 0)
if err != 0 {
return err
}
return nil
}
return file.Sync()
}
func (vfsOSMethods) Allocate(file *os.File, size int64) error {
if size == 0 {
return nil
}
return unix.Fallocate(int(file.Fd()), 0, 0, size)
}
2023-03-22 03:15:54 +00:00
func (vfsOSMethods) unlock(file *os.File, start, len int64) xErrorCode {
err := unix.FcntlFlock(file.Fd(), unix.F_OFD_SETLK, &unix.Flock_t{
Type: unix.F_UNLCK,
Start: start,
Len: len,
})
if err != nil {
return IOERR_UNLOCK
}
return _OK
2023-03-17 17:13:03 +00:00
}
2023-03-22 03:15:54 +00:00
func (vfsOSMethods) readLock(file *os.File, start, len int64, timeout time.Duration) xErrorCode {
lock := unix.Flock_t{
Type: unix.F_RDLCK,
Start: start,
Len: len,
}
var err error
for {
err = unix.FcntlFlock(file.Fd(), unix.F_OFD_SETLK, &lock)
if errno, _ := err.(unix.Errno); errno != unix.EAGAIN {
break
}
if timeout < time.Millisecond {
break
}
timeout -= time.Millisecond
time.Sleep(time.Millisecond)
}
return vfsOS.lockErrorCode(err, IOERR_RDLOCK)
2023-03-17 17:13:03 +00:00
}
2023-03-22 03:15:54 +00:00
func (vfsOSMethods) writeLock(file *os.File, start, len int64, timeout time.Duration) xErrorCode {
lock := unix.Flock_t{
Type: unix.F_WRLCK,
Start: start,
Len: len,
}
var err error
2023-03-18 01:13:31 +00:00
for {
2023-03-22 03:15:54 +00:00
err = unix.FcntlFlock(file.Fd(), unix.F_OFD_SETLK, &lock)
2023-03-18 01:13:31 +00:00
if errno, _ := err.(unix.Errno); errno != unix.EAGAIN {
2023-03-22 03:15:54 +00:00
break
2023-03-18 01:13:31 +00:00
}
if timeout < time.Millisecond {
2023-03-22 03:15:54 +00:00
break
2023-03-18 01:13:31 +00:00
}
timeout -= time.Millisecond
time.Sleep(time.Millisecond)
}
2023-03-22 03:15:54 +00:00
return vfsOS.lockErrorCode(err, IOERR_RDLOCK)
}
func (vfsOSMethods) checkLock(file *os.File, start, len int64) (bool, xErrorCode) {
lock := unix.Flock_t{
Type: unix.F_RDLCK,
Start: start,
Len: len,
}
if unix.FcntlFlock(file.Fd(), unix.F_OFD_GETLK, &lock) != nil {
return false, IOERR_CHECKRESERVEDLOCK
}
return lock.Type != unix.F_UNLCK, _OK
2023-03-17 17:13:03 +00:00
}