Files
sqlite3/vfs/os_darwin.go

96 lines
2.2 KiB
Go
Raw Normal View History

//go:build !(sqlite3_flock || sqlite3_nosys)
2023-03-22 11:30:53 +00:00
2023-06-01 18:11:37 +01:00
package vfs
2023-03-17 17:13:03 +00:00
import (
2023-03-22 03:15:54 +00:00
"io"
2023-03-17 17:13:03 +00:00
"os"
"time"
"golang.org/x/sys/unix"
)
2023-03-22 03:15:54 +00:00
const (
// https://github.com/apple/darwin-xnu/blob/main/bsd/sys/fcntl.h
_F_OFD_SETLK = 90
_F_OFD_SETLKW = 91
_F_OFD_SETLKWTIMEOUT = 93
)
type flocktimeout_t struct {
fl unix.Flock_t
timeout unix.Timespec
}
2024-03-15 16:14:49 +00:00
func osSync(file *os.File, fullsync, _ /*dataonly*/ bool) error {
2023-03-23 01:07:38 +00:00
if fullsync {
return file.Sync()
2023-03-17 17:13:03 +00:00
}
2023-03-23 01:07:38 +00:00
return unix.Fsync(int(file.Fd()))
2023-03-17 17:13:03 +00:00
}
2023-03-29 15:01:25 +01:00
func osAllocate(file *os.File, size int64) error {
2023-03-22 03:15:54 +00:00
off, err := file.Seek(0, io.SeekEnd)
if err != nil {
return err
}
if size <= off {
return nil
}
2023-03-17 17:13:03 +00:00
store := unix.Fstore_t{
Flags: unix.F_ALLOCATEALL | unix.F_ALLOCATECONTIG,
2023-03-17 17:13:03 +00:00
Posmode: unix.F_PEOFPOSMODE,
Offset: 0,
Length: size - off,
2023-03-17 17:13:03 +00:00
}
2023-05-19 13:47:12 +01:00
// Try to get a continuous chunk of disk space.
2023-03-22 03:15:54 +00:00
err = unix.FcntlFstore(file.Fd(), unix.F_PREALLOCATE, &store)
2023-03-17 17:13:03 +00:00
if err != nil {
// OK, perhaps we are too fragmented, allocate non-continuous.
store.Flags = unix.F_ALLOCATEALL
2023-03-22 03:15:54 +00:00
unix.FcntlFstore(file.Fd(), unix.F_PREALLOCATE, &store)
2023-03-17 17:13:03 +00:00
}
2023-03-22 03:15:54 +00:00
return file.Truncate(size)
2023-03-17 17:13:03 +00:00
}
2023-03-29 15:01:25 +01:00
func osUnlock(file *os.File, start, len int64) _ErrorCode {
2023-03-22 03:15:54 +00:00
err := unix.FcntlFlock(file.Fd(), _F_OFD_SETLK, &unix.Flock_t{
Type: unix.F_UNLCK,
Start: start,
Len: len,
})
if err != nil {
2023-03-29 15:01:25 +01:00
return _IOERR_UNLOCK
2023-03-22 03:15:54 +00:00
}
return _OK
2023-03-17 17:13:03 +00:00
}
2023-03-29 15:01:25 +01:00
func osLock(file *os.File, typ int16, start, len int64, timeout time.Duration, def _ErrorCode) _ErrorCode {
2023-03-22 03:15:54 +00:00
lock := flocktimeout_t{fl: unix.Flock_t{
2023-03-23 01:07:38 +00:00
Type: typ,
2023-03-22 03:15:54 +00:00
Start: start,
Len: len,
}}
var err error
2024-03-15 16:14:49 +00:00
switch {
case timeout == 0:
2023-03-22 03:15:54 +00:00
err = unix.FcntlFlock(file.Fd(), _F_OFD_SETLK, &lock.fl)
2024-03-15 16:14:49 +00:00
case timeout < 0:
err = unix.FcntlFlock(file.Fd(), _F_OFD_SETLKW, &lock.fl)
default:
2023-03-22 03:15:54 +00:00
lock.timeout = unix.NsecToTimespec(int64(timeout / time.Nanosecond))
err = unix.FcntlFlock(file.Fd(), _F_OFD_SETLKWTIMEOUT, &lock.fl)
}
2023-03-29 15:01:25 +01:00
return osLockErrorCode(err, def)
2023-03-23 01:07:38 +00:00
}
2023-03-29 15:01:25 +01:00
func osReadLock(file *os.File, start, len int64, timeout time.Duration) _ErrorCode {
return osLock(file, unix.F_RDLCK, start, len, timeout, _IOERR_RDLOCK)
2023-03-17 17:13:03 +00:00
}
2023-03-29 15:01:25 +01:00
func osWriteLock(file *os.File, start, len int64, timeout time.Duration) _ErrorCode {
return osLock(file, unix.F_WRLCK, start, len, timeout, _IOERR_LOCK)
2023-03-22 03:15:54 +00:00
}