Files
sqlite3/vfs/os_unix.go

104 lines
1.9 KiB
Go
Raw Normal View History

2024-11-29 15:51:51 +00:00
//go:build unix
2023-01-26 14:52:38 +00:00
2023-06-01 18:11:37 +01:00
package vfs
2023-01-25 14:59:02 +00:00
2023-01-25 16:23:18 +00:00
import (
"os"
2023-06-02 02:31:15 +01:00
"syscall"
2023-03-09 01:59:46 +00:00
"golang.org/x/sys/unix"
2023-01-25 16:23:18 +00:00
)
2023-01-25 14:59:02 +00:00
2025-01-07 16:31:12 +00:00
const (
2025-03-26 02:13:30 +00:00
isUnix = true
2025-01-07 16:31:12 +00:00
_O_NOFOLLOW = unix.O_NOFOLLOW
)
2024-09-27 12:32:11 +01:00
2023-05-19 03:04:07 +01:00
func osAccess(path string, flags AccessFlag) error {
2023-03-23 13:28:25 +00:00
var access uint32 // unix.F_OK
2023-03-09 01:59:46 +00:00
switch flags {
2023-05-19 03:04:07 +01:00
case ACCESS_READWRITE:
2023-03-09 01:59:46 +00:00
access = unix.R_OK | unix.W_OK
2023-05-19 03:04:07 +01:00
case ACCESS_READ:
2023-03-09 01:59:46 +00:00
access = unix.R_OK
}
2023-03-17 17:13:03 +00:00
return unix.Access(path, access)
2023-03-10 14:02:34 +00:00
}
2025-02-24 12:25:19 +00:00
func osReadAt(file *os.File, p []byte, off int64) (int, error) {
n, err := file.ReadAt(p, off)
if errno, ok := err.(unix.Errno); ok {
switch errno {
case
unix.ERANGE,
unix.EIO,
unix.ENXIO:
return n, _IOERR_CORRUPTFS
}
}
return n, err
}
func osWriteAt(file *os.File, p []byte, off int64) (int, error) {
n, err := file.WriteAt(p, off)
if errno, ok := err.(unix.Errno); ok && errno == unix.ENOSPC {
return n, _FULL
}
return n, err
}
2023-06-02 02:31:15 +01:00
func osSetMode(file *os.File, modeof string) error {
fi, err := os.Stat(modeof)
if err != nil {
return err
}
file.Chmod(fi.Mode())
if sys, ok := fi.Sys().(*syscall.Stat_t); ok {
file.Chown(int(sys.Uid), int(sys.Gid))
}
return nil
}
2024-09-17 23:09:05 +01:00
func osTestLock(file *os.File, start, len int64) (int16, _ErrorCode) {
lock := unix.Flock_t{
Type: unix.F_WRLCK,
Start: start,
Len: len,
}
2025-03-13 23:38:50 +00:00
for {
err := unix.FcntlFlock(file.Fd(), unix.F_GETLK, &lock)
if err == nil {
return lock.Type, _OK
}
if err != unix.EINTR {
return 0, _IOERR_CHECKRESERVEDLOCK
}
2024-09-17 23:09:05 +01:00
}
}
func osLockErrorCode(err error, def _ErrorCode) _ErrorCode {
if err == nil {
return _OK
}
if errno, ok := err.(unix.Errno); ok {
switch errno {
case
unix.EACCES,
unix.EAGAIN,
unix.EBUSY,
unix.EINTR,
unix.ENOLCK,
unix.EDEADLK,
unix.ETIMEDOUT:
return _BUSY
case unix.EPERM:
return _PERM
}
// notest // usually EWOULDBLOCK == EAGAIN
if errno == unix.EWOULDBLOCK && unix.EWOULDBLOCK != unix.EAGAIN {
return _BUSY
}
}
return def
}