Files
sqlite3/vfs/os_darwin.go

122 lines
2.6 KiB
Go
Raw Normal View History

2024-11-29 15:51:51 +00:00
//go:build !sqlite3_flock
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"
2024-07-23 13:28:09 +01:00
"runtime"
2023-03-17 17:13:03 +00:00
"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
}
2025-09-30 12:54:18 +01:00
func osSync(file *os.File, open OpenFlag, sync SyncFlag) error {
var cmd int
if sync&SYNC_FULL == SYNC_FULL {
// For rollback journals all we really need is a barrier.
if open&OPEN_MAIN_JOURNAL != 0 {
cmd = unix.F_BARRIERFSYNC
} else {
cmd = unix.F_FULLFSYNC
}
2023-03-17 17:13:03 +00:00
}
2025-09-30 12:54:18 +01:00
fd := file.Fd()
2025-03-11 12:07:14 +00:00
for {
2025-09-30 12:54:18 +01:00
err := error(unix.ENOTSUP)
if cmd != 0 {
_, err = unix.FcntlInt(fd, cmd, 0)
}
if err == unix.ENOTSUP {
err = unix.Fsync(int(fd))
}
2025-03-11 12:07:14 +00:00
if err != unix.EINTR {
return err
}
}
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
}
2025-10-17 16:40:15 +01:00
func osReadLock(file *os.File, start, len int64, timeout time.Duration) error {
2024-12-12 10:42:23 +00:00
return osLock(file, unix.F_RDLCK, start, len, timeout, _IOERR_RDLOCK)
}
2025-10-17 16:40:15 +01:00
func osWriteLock(file *os.File, start, len int64, timeout time.Duration) error {
2024-12-12 10:42:23 +00:00
return osLock(file, unix.F_WRLCK, start, len, timeout, _IOERR_LOCK)
2023-03-17 17:13:03 +00:00
}
2025-10-17 16:40:15 +01:00
func osLock(file *os.File, typ int16, start, len int64, timeout time.Duration, def _ErrorCode) error {
2024-07-23 13:28:09 +01: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)
2024-07-23 13:28:09 +01:00
runtime.KeepAlive(lock)
2023-03-22 03:15:54 +00:00
}
2023-03-29 15:01:25 +01:00
return osLockErrorCode(err, def)
2023-03-23 01:07:38 +00:00
}
2025-10-17 16:40:15 +01:00
func osUnlock(file *os.File, start, len int64) error {
2025-03-13 23:38:50 +00:00
lock := unix.Flock_t{
2024-12-12 10:42:23 +00:00
Type: unix.F_UNLCK,
Start: start,
Len: len,
}
2025-03-13 23:38:50 +00:00
for {
err := unix.FcntlFlock(file.Fd(), _F_OFD_SETLK, &lock)
if err == nil {
2025-10-17 16:40:15 +01:00
return nil
2025-03-13 23:38:50 +00:00
}
if err != unix.EINTR {
2025-10-17 16:40:15 +01:00
return sysError{err, _IOERR_UNLOCK}
2025-03-13 23:38:50 +00:00
}
}
2023-03-22 03:15:54 +00:00
}