2023-05-19 13:47:12 +01:00
|
|
|
// Package sqlite3vfs wraps the C SQLite VFS API.
|
2023-05-17 14:04:00 +01:00
|
|
|
package sqlite3vfs
|
|
|
|
|
|
2023-05-19 14:45:40 +01:00
|
|
|
import "sync"
|
2023-05-17 14:04:00 +01:00
|
|
|
|
2023-05-19 13:47:12 +01:00
|
|
|
// A VFS defines the interface between the SQLite core and the underlying operating system.
|
|
|
|
|
//
|
2023-05-19 14:45:40 +01:00
|
|
|
// Use sqlite3.ErrorCode or sqlite3.ExtendedErrorCode to return specific error codes.
|
|
|
|
|
//
|
2023-05-19 13:47:12 +01:00
|
|
|
// https://www.sqlite.org/c3ref/vfs.html
|
2023-05-17 14:04:00 +01:00
|
|
|
type VFS interface {
|
|
|
|
|
Open(name string, flags OpenFlag) (File, OpenFlag, error)
|
2023-05-18 01:34:54 +01:00
|
|
|
Delete(name string, syncDir bool) error
|
2023-05-17 14:04:00 +01:00
|
|
|
Access(name string, flags AccessFlag) (bool, error)
|
|
|
|
|
FullPathname(name string) (string, error)
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-19 13:47:12 +01:00
|
|
|
// A File represents an open file in the OS interface layer.
|
|
|
|
|
//
|
2023-05-19 14:45:40 +01:00
|
|
|
// Use sqlite3.ErrorCode or sqlite3.ExtendedErrorCode to return specific error codes.
|
|
|
|
|
// In particular, sqlite3.BUSY is necessary to correctly implement lock methods.
|
|
|
|
|
//
|
2023-05-19 13:47:12 +01:00
|
|
|
// https://www.sqlite.org/c3ref/io_methods.html
|
2023-05-17 14:04:00 +01:00
|
|
|
type File interface {
|
|
|
|
|
Close() error
|
|
|
|
|
ReadAt(p []byte, off int64) (n int, err error)
|
|
|
|
|
WriteAt(p []byte, off int64) (n int, err error)
|
|
|
|
|
Truncate(size int64) error
|
2023-05-18 01:34:54 +01:00
|
|
|
Sync(flags SyncFlag) error
|
2023-05-17 14:04:00 +01:00
|
|
|
FileSize() (int64, error)
|
2023-05-18 01:34:54 +01:00
|
|
|
Lock(lock LockLevel) error
|
|
|
|
|
Unlock(lock LockLevel) error
|
2023-05-17 14:04:00 +01:00
|
|
|
CheckReservedLock() (bool, error)
|
2023-05-18 16:00:34 +01:00
|
|
|
SectorSize() int
|
2023-05-19 02:00:16 +01:00
|
|
|
DeviceCharacteristics() DeviceCharacteristic
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-19 13:47:12 +01:00
|
|
|
// FileLockState extends [File] to implement the
|
|
|
|
|
// SQLITE_FCNTL_LOCKSTATE file control opcode.
|
|
|
|
|
//
|
|
|
|
|
// https://www.sqlite.org/c3ref/c_fcntl_begin_atomic_write.html
|
2023-05-19 02:00:16 +01:00
|
|
|
type FileLockState interface {
|
|
|
|
|
File
|
|
|
|
|
LockState() LockLevel
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-19 13:47:12 +01:00
|
|
|
// FileLockState extends [File] to implement the
|
|
|
|
|
// SQLITE_FCNTL_SIZE_HINT file control opcode.
|
|
|
|
|
//
|
|
|
|
|
// https://www.sqlite.org/c3ref/c_fcntl_begin_atomic_write.html
|
2023-05-19 02:00:16 +01:00
|
|
|
type FileSizeHint interface {
|
|
|
|
|
File
|
|
|
|
|
SizeHint(size int64) error
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-19 13:47:12 +01:00
|
|
|
// FileLockState extends [File] to implement the
|
|
|
|
|
// SQLITE_FCNTL_HAS_MOVED file control opcode.
|
|
|
|
|
//
|
|
|
|
|
// https://www.sqlite.org/c3ref/c_fcntl_begin_atomic_write.html
|
2023-05-19 02:00:16 +01:00
|
|
|
type FileHasMoved interface {
|
|
|
|
|
File
|
|
|
|
|
HasMoved() (bool, error)
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-19 13:47:12 +01:00
|
|
|
// FileLockState extends [File] to implement the
|
|
|
|
|
// SQLITE_FCNTL_POWERSAFE_OVERWRITE file control opcode.
|
|
|
|
|
//
|
|
|
|
|
// https://www.sqlite.org/c3ref/c_fcntl_begin_atomic_write.html
|
2023-05-19 02:00:16 +01:00
|
|
|
type FilePowersafeOverwrite interface {
|
|
|
|
|
File
|
|
|
|
|
PowersafeOverwrite() bool
|
|
|
|
|
SetPowersafeOverwrite(bool)
|
2023-05-17 14:04:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
vfsRegistry map[string]VFS
|
|
|
|
|
vfsRegistryMtx sync.Mutex
|
|
|
|
|
)
|
|
|
|
|
|
2023-05-19 13:47:12 +01:00
|
|
|
// Find returns a VFS given its name.
|
|
|
|
|
// If there is no match, nil is returned.
|
|
|
|
|
//
|
|
|
|
|
// https://www.sqlite.org/c3ref/vfs_find.html
|
2023-05-17 14:04:00 +01:00
|
|
|
func Find(name string) VFS {
|
|
|
|
|
vfsRegistryMtx.Lock()
|
|
|
|
|
defer vfsRegistryMtx.Unlock()
|
|
|
|
|
return vfsRegistry[name]
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-19 13:47:12 +01:00
|
|
|
// Register registers a VFS.
|
|
|
|
|
//
|
|
|
|
|
// https://www.sqlite.org/c3ref/vfs_find.html
|
2023-05-17 14:04:00 +01:00
|
|
|
func Register(name string, vfs VFS) {
|
|
|
|
|
vfsRegistryMtx.Lock()
|
|
|
|
|
defer vfsRegistryMtx.Unlock()
|
|
|
|
|
if vfsRegistry == nil {
|
|
|
|
|
vfsRegistry = map[string]VFS{}
|
|
|
|
|
}
|
|
|
|
|
vfsRegistry[name] = vfs
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-19 13:47:12 +01:00
|
|
|
// Unregister unregisters a VFS.
|
|
|
|
|
//
|
|
|
|
|
// https://www.sqlite.org/c3ref/vfs_find.html
|
2023-05-17 14:04:00 +01:00
|
|
|
func Unregister(name string) {
|
|
|
|
|
vfsRegistryMtx.Lock()
|
|
|
|
|
defer vfsRegistryMtx.Unlock()
|
|
|
|
|
delete(vfsRegistry, name)
|
|
|
|
|
}
|