mirror of
https://github.com/ncruces/go-sqlite3.git
synced 2026-01-12 05:59:14 +00:00
xFileControl, xDeviceCharacteristics, PSOW.
This commit is contained in:
46
const.go
46
const.go
@@ -227,3 +227,49 @@ const (
|
||||
_SYNC_FULL _SyncFlag = 0x00003
|
||||
_SYNC_DATAONLY _SyncFlag = 0x00010
|
||||
)
|
||||
|
||||
type _FcntlOpcode uint32
|
||||
|
||||
const (
|
||||
_FCNTL_LOCKSTATE = 1
|
||||
_FCNTL_GET_LOCKPROXYFILE = 2
|
||||
_FCNTL_SET_LOCKPROXYFILE = 3
|
||||
_FCNTL_LAST_ERRNO = 4
|
||||
_FCNTL_SIZE_HINT = 5
|
||||
_FCNTL_CHUNK_SIZE = 6
|
||||
_FCNTL_FILE_POINTER = 7
|
||||
_FCNTL_SYNC_OMITTED = 8
|
||||
_FCNTL_WIN32_AV_RETRY = 9
|
||||
_FCNTL_PERSIST_WAL = 10
|
||||
_FCNTL_OVERWRITE = 11
|
||||
_FCNTL_VFSNAME = 12
|
||||
_FCNTL_POWERSAFE_OVERWRITE = 13
|
||||
_FCNTL_PRAGMA = 14
|
||||
_FCNTL_BUSYHANDLER = 15
|
||||
_FCNTL_TEMPFILENAME = 16
|
||||
_FCNTL_MMAP_SIZE = 18
|
||||
_FCNTL_TRACE = 19
|
||||
_FCNTL_HAS_MOVED = 20
|
||||
_FCNTL_SYNC = 21
|
||||
_FCNTL_COMMIT_PHASETWO = 22
|
||||
_FCNTL_WIN32_SET_HANDLE = 23
|
||||
_FCNTL_WAL_BLOCK = 24
|
||||
_FCNTL_ZIPVFS = 25
|
||||
_FCNTL_RBU = 26
|
||||
_FCNTL_VFS_POINTER = 27
|
||||
_FCNTL_JOURNAL_POINTER = 28
|
||||
_FCNTL_WIN32_GET_HANDLE = 29
|
||||
_FCNTL_PDB = 30
|
||||
_FCNTL_BEGIN_ATOMIC_WRITE = 31
|
||||
_FCNTL_COMMIT_ATOMIC_WRITE = 32
|
||||
_FCNTL_ROLLBACK_ATOMIC_WRITE = 33
|
||||
_FCNTL_LOCK_TIMEOUT = 34
|
||||
_FCNTL_DATA_VERSION = 35
|
||||
_FCNTL_SIZE_LIMIT = 36
|
||||
_FCNTL_CKPT_DONE = 37
|
||||
_FCNTL_RESERVE_BYTES = 38
|
||||
_FCNTL_CKPT_START = 39
|
||||
_FCNTL_EXTERNAL_READER = 40
|
||||
_FCNTL_CKSM_FILE = 41
|
||||
_FCNTL_RESET_CACHE = 42
|
||||
)
|
||||
|
||||
@@ -8,7 +8,7 @@ cd -P -- "$(dirname -- "$0")"
|
||||
|
||||
# build SQLite
|
||||
zig cc --target=wasm32-wasi -flto -g0 -Os \
|
||||
-o sqlite3.wasm ../sqlite3/amalg.c \
|
||||
-o sqlite3.wasm ../sqlite3/main.c \
|
||||
-mmutable-globals \
|
||||
-mbulk-memory -mreference-types \
|
||||
-mnontrapping-fptoint -msign-ext \
|
||||
|
||||
Binary file not shown.
2
go.mod
2
go.mod
@@ -4,7 +4,7 @@ go 1.19
|
||||
|
||||
require (
|
||||
github.com/ncruces/julianday v0.1.5
|
||||
github.com/tetratelabs/wazero v1.0.0-rc.1
|
||||
github.com/tetratelabs/wazero v1.0.0-rc.2
|
||||
golang.org/x/sync v0.1.0
|
||||
golang.org/x/sys v0.6.0
|
||||
)
|
||||
|
||||
4
go.sum
4
go.sum
@@ -1,7 +1,7 @@
|
||||
github.com/ncruces/julianday v0.1.5 h1:hDJ9ejiMp3DHsoZ5KW4c1lwfMjbARS7u/gbYcd0FBZk=
|
||||
github.com/ncruces/julianday v0.1.5/go.mod h1:Dusn2KvZrrovOMJuOt0TNXL6tB7U2E8kvza5fFc9G7g=
|
||||
github.com/tetratelabs/wazero v1.0.0-rc.1 h1:ytecMV5Ue0BwezjKh/cM5yv1Mo49ep2R2snSsQUyToc=
|
||||
github.com/tetratelabs/wazero v1.0.0-rc.1/go.mod h1:wYx2gNRg8/WihJfSDxA1TIL8H+GkfLYm+bIfbblu9VQ=
|
||||
github.com/tetratelabs/wazero v1.0.0-rc.2 h1:OA3UUynnoqxrjCQ94mpAtdO4/oMxFQVNL2BXDMOc66Q=
|
||||
github.com/tetratelabs/wazero v1.0.0-rc.2/go.mod h1:wYx2gNRg8/WihJfSDxA1TIL8H+GkfLYm+bIfbblu9VQ=
|
||||
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
|
||||
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
|
||||
|
||||
23
mem.go
23
mem.go
@@ -25,6 +25,27 @@ func (m memory) view(ptr uint32, size uint64) []byte {
|
||||
return buf
|
||||
}
|
||||
|
||||
func (m memory) readUint8(ptr uint32) uint8 {
|
||||
if ptr == 0 {
|
||||
panic(nilErr)
|
||||
}
|
||||
v, ok := m.mod.Memory().ReadByte(ptr)
|
||||
if !ok {
|
||||
panic(rangeErr)
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
func (m memory) writeUint8(ptr uint32, v uint8) {
|
||||
if ptr == 0 {
|
||||
panic(nilErr)
|
||||
}
|
||||
ok := m.mod.Memory().WriteByte(ptr, v)
|
||||
if !ok {
|
||||
panic(rangeErr)
|
||||
}
|
||||
}
|
||||
|
||||
func (m memory) readUint32(ptr uint32) uint32 {
|
||||
if ptr == 0 {
|
||||
panic(nilErr)
|
||||
@@ -36,7 +57,7 @@ func (m memory) readUint32(ptr uint32) uint32 {
|
||||
return v
|
||||
}
|
||||
|
||||
func (m memory) writeUint32(ptr, v uint32) {
|
||||
func (m memory) writeUint32(ptr uint32, v uint32) {
|
||||
if ptr == 0 {
|
||||
panic(nilErr)
|
||||
}
|
||||
|
||||
@@ -1,10 +0,0 @@
|
||||
#include <stddef.h>
|
||||
|
||||
#include "main.c"
|
||||
#include "os.c"
|
||||
#include "qsort.c"
|
||||
#include "sqlite3.c"
|
||||
#include "time.c"
|
||||
|
||||
sqlite3_destructor_type malloc_destructor = &free;
|
||||
size_t sqlite3_interrupt_offset = offsetof(sqlite3, u1.isInterrupted);
|
||||
@@ -1,14 +1,20 @@
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#include "sqlite3.h"
|
||||
#include "sqlite3.c"
|
||||
//
|
||||
#include "os.c"
|
||||
#include "qsort.c"
|
||||
#include "time.c"
|
||||
|
||||
sqlite3_destructor_type malloc_destructor = &free;
|
||||
size_t sqlite3_interrupt_offset = offsetof(sqlite3, u1.isInterrupted);
|
||||
|
||||
int sqlite3_os_init() {
|
||||
return sqlite3_vfs_register(os_vfs(), /*default=*/true);
|
||||
}
|
||||
|
||||
int main() {
|
||||
int rc = sqlite3_initialize();
|
||||
if (rc != SQLITE_OK) return 1;
|
||||
}
|
||||
|
||||
sqlite3_vfs *os_vfs();
|
||||
|
||||
int sqlite3_os_init() {
|
||||
return sqlite3_vfs_register(os_vfs(), /*default=*/true);
|
||||
}
|
||||
|
||||
94
sqlite3/os.c
94
sqlite3/os.c
@@ -18,36 +18,73 @@ int os_full_pathname(sqlite3_vfs *, const char *zName, int nOut, char *zOut);
|
||||
struct os_file {
|
||||
sqlite3_file base;
|
||||
int id;
|
||||
int lock;
|
||||
char lock;
|
||||
char psow;
|
||||
int lockTimeout;
|
||||
};
|
||||
|
||||
static_assert(offsetof(struct os_file, id) == 4, "Unexpected offset");
|
||||
static_assert(offsetof(struct os_file, lock) == 8, "Unexpected offset");
|
||||
static_assert(offsetof(struct os_file, psow) == 9, "Unexpected offset");
|
||||
static_assert(offsetof(struct os_file, lockTimeout) == 12, "Unexpected offset");
|
||||
|
||||
int os_close(sqlite3_file *);
|
||||
int os_read(sqlite3_file *, void *, int iAmt, sqlite3_int64 iOfst);
|
||||
int os_write(sqlite3_file *, const void *, int iAmt, sqlite3_int64 iOfst);
|
||||
int os_truncate(sqlite3_file *, sqlite3_int64 size);
|
||||
int os_sync(sqlite3_file *, int flags);
|
||||
int os_file_size(sqlite3_file *, sqlite3_int64 *pSize);
|
||||
int os_file_control(sqlite3_file *pFile, int op, void *pArg);
|
||||
int os_file_control(sqlite3_file *, int op, void *pArg);
|
||||
|
||||
int os_lock(sqlite3_file *pFile, int eLock);
|
||||
int os_unlock(sqlite3_file *pFile, int eLock);
|
||||
int os_check_reserved_lock(sqlite3_file *pFile, int *pResOut);
|
||||
int os_lock(sqlite3_file *, int eLock);
|
||||
int os_unlock(sqlite3_file *, int eLock);
|
||||
int os_check_reserved_lock(sqlite3_file *, int *pResOut);
|
||||
|
||||
static int no_lock(sqlite3_file *pFile, int eLock) { return SQLITE_OK; }
|
||||
static int no_unlock(sqlite3_file *pFile, int eLock) { return SQLITE_OK; }
|
||||
static int no_check_reserved_lock(sqlite3_file *pFile, int *pResOut) {
|
||||
*pResOut = 0;
|
||||
return SQLITE_OK;
|
||||
}
|
||||
|
||||
static int no_file_control(sqlite3_file *pFile, int op, void *pArg) {
|
||||
static int os_file_control_w(sqlite3_file *file, int op, void *pArg) {
|
||||
struct os_file *pFile = (struct os_file *)file;
|
||||
switch (op) {
|
||||
case SQLITE_FCNTL_VFSNAME: {
|
||||
*(char **)pArg = sqlite3_mprintf("%s", "os");
|
||||
return SQLITE_OK;
|
||||
}
|
||||
case SQLITE_FCNTL_LOCKSTATE: {
|
||||
*(int *)pArg = pFile->lock;
|
||||
return SQLITE_OK;
|
||||
}
|
||||
case SQLITE_FCNTL_LOCK_TIMEOUT: {
|
||||
int iOld = pFile->lockTimeout;
|
||||
pFile->lockTimeout = *(int *)pArg;
|
||||
*(int *)pArg = iOld;
|
||||
return SQLITE_OK;
|
||||
}
|
||||
case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
|
||||
if (*(int *)pArg < 0) {
|
||||
*(int *)pArg = pFile->psow;
|
||||
} else {
|
||||
pFile->psow = *(int *)pArg;
|
||||
}
|
||||
return SQLITE_OK;
|
||||
}
|
||||
case SQLITE_FCNTL_SIZE_HINT:
|
||||
case SQLITE_FCNTL_HAS_MOVED:
|
||||
return os_file_control(file, op, pArg);
|
||||
}
|
||||
// Consider also implementing these opcodes (in use by SQLite):
|
||||
// SQLITE_FCNTL_BUSYHANDLER
|
||||
// SQLITE_FCNTL_COMMIT_PHASETWO
|
||||
// SQLITE_FCNTL_PDB
|
||||
// SQLITE_FCNTL_PRAGMA
|
||||
// SQLITE_FCNTL_SYNC
|
||||
return SQLITE_NOTFOUND;
|
||||
}
|
||||
static int no_sector_size(sqlite3_file *pFile) { return 0; }
|
||||
static int no_device_characteristics(sqlite3_file *pFile) { return 0; }
|
||||
|
||||
int localtime_s(struct tm *const pTm, time_t const *const pTime) {
|
||||
return os_localtime((sqlite3_int64)*pTime, pTm);
|
||||
static int os_sector_size(sqlite3_file *file) {
|
||||
return SQLITE_DEFAULT_SECTOR_SIZE;
|
||||
}
|
||||
|
||||
static int os_device_characteristics(sqlite3_file *file) {
|
||||
struct os_file *pFile = (struct os_file *)file;
|
||||
return pFile->psow ? SQLITE_IOCAP_POWERSAFE_OVERWRITE : 0;
|
||||
}
|
||||
|
||||
static int os_open_w(sqlite3_vfs *vfs, sqlite3_filename zName,
|
||||
@@ -63,12 +100,23 @@ static int os_open_w(sqlite3_vfs *vfs, sqlite3_filename zName,
|
||||
.xLock = os_lock,
|
||||
.xUnlock = os_unlock,
|
||||
.xCheckReservedLock = os_check_reserved_lock,
|
||||
.xFileControl = no_file_control,
|
||||
.xDeviceCharacteristics = no_device_characteristics,
|
||||
.xFileControl = os_file_control_w,
|
||||
.xSectorSize = os_sector_size,
|
||||
.xDeviceCharacteristics = os_device_characteristics,
|
||||
};
|
||||
memset(file, 0, sizeof(struct os_file));
|
||||
int rc = os_open(vfs, zName, file, flags, pOutFlags);
|
||||
file->pMethods = (char)rc == SQLITE_OK ? &os_io : NULL;
|
||||
return rc;
|
||||
if (rc) {
|
||||
return rc;
|
||||
}
|
||||
|
||||
struct os_file *pFile = (struct os_file *)file;
|
||||
pFile->base.pMethods = &os_io;
|
||||
if (flags & SQLITE_OPEN_MAIN_DB) {
|
||||
pFile->psow =
|
||||
sqlite3_uri_boolean(zName, "psow", SQLITE_POWERSAFE_OVERWRITE);
|
||||
}
|
||||
return SQLITE_OK;
|
||||
}
|
||||
|
||||
sqlite3_vfs *os_vfs() {
|
||||
@@ -90,3 +138,7 @@ sqlite3_vfs *os_vfs() {
|
||||
};
|
||||
return &os_vfs;
|
||||
}
|
||||
|
||||
int localtime_s(struct tm *const pTm, time_t const *const pTime) {
|
||||
return os_localtime((sqlite3_int64)*pTime, pTm);
|
||||
}
|
||||
|
||||
2
tests/mptest/testdata/build.sh
vendored
2
tests/mptest/testdata/build.sh
vendored
@@ -13,7 +13,7 @@ if [ ! -f "mptest.c" ]; then
|
||||
fi
|
||||
|
||||
zig cc --target=wasm32-wasi -flto -g0 -Os \
|
||||
-o mptest.wasm main.c test.c \
|
||||
-o mptest.wasm main.c \
|
||||
-I../../../sqlite3 \
|
||||
-mmutable-globals \
|
||||
-mbulk-memory -mreference-types \
|
||||
|
||||
24
tests/mptest/testdata/main.c
vendored
24
tests/mptest/testdata/main.c
vendored
@@ -1,22 +1,24 @@
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#include "sqlite3.c"
|
||||
//
|
||||
#include "os.c"
|
||||
#include "qsort.c"
|
||||
#include "sqlite3.c"
|
||||
#include "time.c"
|
||||
|
||||
sqlite3_destructor_type malloc_destructor = &free;
|
||||
size_t sqlite3_interrupt_offset = offsetof(sqlite3, u1.isInterrupted);
|
||||
|
||||
void __attribute__((constructor)) premain() { sqlite3_initialize(); }
|
||||
|
||||
int sqlite3_enable_load_extension(sqlite3 *db, int onoff) { return SQLITE_OK; }
|
||||
|
||||
void *sqlite3_trace(sqlite3 *db, void (*xTrace)(void *, const char *),
|
||||
void *pArg) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int sqlite3_os_init() {
|
||||
return sqlite3_vfs_register(os_vfs(), /*default=*/true);
|
||||
}
|
||||
}
|
||||
|
||||
__attribute__((constructor)) void premain() { sqlite3_initialize(); }
|
||||
|
||||
static int dont_unlink(const char *pathname) { return 0; }
|
||||
#define sqlite3_enable_load_extension(...)
|
||||
#define sqlite3_trace(...)
|
||||
#define unlink dont_unlink
|
||||
#undef UNUSED_PARAMETER
|
||||
#include "mptest.c"
|
||||
|
||||
4
tests/mptest/testdata/mptest.wasm
vendored
4
tests/mptest/testdata/mptest.wasm
vendored
@@ -1,3 +1,3 @@
|
||||
version https://git-lfs.github.com/spec/v1
|
||||
oid sha256:56ba9702a874d9ebf8a7eab2b39f7f74127931038fa8c9298b59120ae20a4653
|
||||
size 969013
|
||||
oid sha256:af307c3555fcf5f78e07a079d8f099400c3e013508f30f5efbe3b7f259be2092
|
||||
size 969309
|
||||
|
||||
5
tests/mptest/testdata/test.c
vendored
5
tests/mptest/testdata/test.c
vendored
@@ -1,5 +0,0 @@
|
||||
#define unlink dont_unlink
|
||||
|
||||
#include "mptest.c"
|
||||
|
||||
int dont_unlink(const char *pathname) { return 0; }
|
||||
16
vfs.go
16
vfs.go
@@ -309,14 +309,12 @@ func vfsFileSize(ctx context.Context, mod api.Module, pFile, pSize uint32) uint3
|
||||
return _OK
|
||||
}
|
||||
|
||||
func vfsFileControl(ctx context.Context, pFile, op, pArg uint32) uint32 {
|
||||
// SQLite calls vfsFileControl with these opcodes:
|
||||
// SQLITE_FCNTL_SIZE_HINT
|
||||
// SQLITE_FCNTL_PRAGMA
|
||||
// SQLITE_FCNTL_BUSYHANDLER
|
||||
// SQLITE_FCNTL_HAS_MOVED
|
||||
// SQLITE_FCNTL_SYNC
|
||||
// SQLITE_FCNTL_COMMIT_PHASETWO
|
||||
// SQLITE_FCNTL_PDB
|
||||
func vfsFileControl(ctx context.Context, mod api.Module, pFile uint32, op _FcntlOpcode, pArg uint32) uint32 {
|
||||
switch op {
|
||||
case _FCNTL_SIZE_HINT:
|
||||
//
|
||||
case _FCNTL_HAS_MOVED:
|
||||
//
|
||||
}
|
||||
return uint32(NOTFOUND)
|
||||
}
|
||||
|
||||
18
vfs_file.go
18
vfs_file.go
@@ -7,6 +7,13 @@ import (
|
||||
"github.com/tetratelabs/wazero/api"
|
||||
)
|
||||
|
||||
const (
|
||||
// These need to match the offsets asserted in os.c
|
||||
vfsFileIDOffset = 4
|
||||
vfsFileLockOffset = 8
|
||||
vfsFileLockTimeoutOffset = 12
|
||||
)
|
||||
|
||||
func (vfsFileMethods) NewID(ctx context.Context, file *os.File) uint32 {
|
||||
vfs := ctx.Value(vfsKey{}).(*vfsState)
|
||||
|
||||
@@ -26,13 +33,12 @@ func (vfsFileMethods) NewID(ctx context.Context, file *os.File) uint32 {
|
||||
func (vfsFileMethods) Open(ctx context.Context, mod api.Module, pFile uint32, file *os.File) {
|
||||
mem := memory{mod}
|
||||
id := vfsFile.NewID(ctx, file)
|
||||
mem.writeUint32(pFile+ptrlen, id)
|
||||
mem.writeUint32(pFile+2*ptrlen, _NO_LOCK)
|
||||
mem.writeUint32(pFile+vfsFileIDOffset, id)
|
||||
}
|
||||
|
||||
func (vfsFileMethods) Close(ctx context.Context, mod api.Module, pFile uint32) error {
|
||||
mem := memory{mod}
|
||||
id := mem.readUint32(pFile + ptrlen)
|
||||
id := mem.readUint32(pFile + vfsFileIDOffset)
|
||||
vfs := ctx.Value(vfsKey{}).(*vfsState)
|
||||
file := vfs.files[id]
|
||||
vfs.files[id] = nil
|
||||
@@ -41,17 +47,17 @@ func (vfsFileMethods) Close(ctx context.Context, mod api.Module, pFile uint32) e
|
||||
|
||||
func (vfsFileMethods) GetOS(ctx context.Context, mod api.Module, pFile uint32) *os.File {
|
||||
mem := memory{mod}
|
||||
id := mem.readUint32(pFile + ptrlen)
|
||||
id := mem.readUint32(pFile + vfsFileIDOffset)
|
||||
vfs := ctx.Value(vfsKey{}).(*vfsState)
|
||||
return vfs.files[id]
|
||||
}
|
||||
|
||||
func (vfsFileMethods) GetLock(ctx context.Context, mod api.Module, pFile uint32) vfsLockState {
|
||||
mem := memory{mod}
|
||||
return vfsLockState(mem.readUint32(pFile + 2*ptrlen))
|
||||
return vfsLockState(mem.readUint8(pFile + vfsFileLockOffset))
|
||||
}
|
||||
|
||||
func (vfsFileMethods) SetLock(ctx context.Context, mod api.Module, pFile uint32, lock vfsLockState) {
|
||||
mem := memory{mod}
|
||||
mem.writeUint32(pFile+2*ptrlen, uint32(lock))
|
||||
mem.writeUint8(pFile+vfsFileLockOffset, uint8(lock))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user