VFS xFullPathname.

This commit is contained in:
Nuno Cruces
2023-01-18 11:40:08 +00:00
parent 9c04d4c5e6
commit 77ff4ea51b
4 changed files with 52 additions and 5 deletions

10
conn.go
View File

@@ -213,10 +213,14 @@ func (c *Conn) newString(s string) uint32 {
}
func (c *Conn) getString(ptr, maxlen uint32) string {
mem, ok := c.memory.Read(ptr, maxlen)
return getString(c.memory, ptr, maxlen)
}
func getString(memory api.Memory, ptr, maxlen uint32) string {
mem, ok := memory.Read(ptr, maxlen)
if !ok {
if size := c.memory.Size(); ptr < size {
mem, ok = c.memory.Read(ptr, size-ptr)
if size := memory.Size(); ptr < size {
mem, ok = memory.Read(ptr, size-ptr)
}
if !ok {
panic("sqlite3: out of range")

View File

@@ -6,6 +6,8 @@ const (
_DONE = 101 /* sqlite3_step() has finished executing */
_UTF8 = 1
_MAX_PATHNAME = 512
)
type ErrorCode int

View File

@@ -12,10 +12,20 @@ int go_sleep(sqlite3_vfs *, int microseconds);
int go_current_time(sqlite3_vfs *, double *);
int go_current_time_64(sqlite3_vfs *, sqlite3_int64 *);
int go_open(sqlite3_vfs *, sqlite3_filename zName, sqlite3_file *, int flags,
int *pOutFlags);
int go_full_pathname(sqlite3_vfs *, const char *zName, int nOut, char *zOut);
int sqlite3_os_init() {
static sqlite3_vfs go_vfs = {
.iVersion = 2,
.szOsFile = sizeof(sqlite3_file),
.mxPathname = 512,
.zName = "go",
.xOpen = go_open,
.xFullPathname = go_full_pathname,
.xRandomness = go_randomness,
.xSleep = go_sleep,
.xCurrentTime = go_current_time,

35
vfs.go
View File

@@ -2,7 +2,9 @@ package sqlite3
import (
"context"
"log"
"math/rand"
"path/filepath"
"time"
"github.com/ncruces/julianday"
@@ -20,6 +22,8 @@ func vfsInstantiate(ctx context.Context, r wazero.Runtime) (err error) {
}
env := r.NewHostModuleBuilder("env")
env.NewFunctionBuilder().WithFunc(vfsOpen).Export("go_open")
env.NewFunctionBuilder().WithFunc(vfsFullPathname).Export("go_full_pathname")
env.NewFunctionBuilder().WithFunc(vfsRandomness).Export("go_randomness")
env.NewFunctionBuilder().WithFunc(vfsSleep).Export("go_sleep")
env.NewFunctionBuilder().WithFunc(vfsCurrentTime).Export("go_current_time")
@@ -35,8 +39,8 @@ func vfsExit(ctx context.Context, mod api.Module, exitCode uint32) {
panic(sys.NewExitError(mod.Name(), exitCode))
}
func vfsRandomness(ctx context.Context, mod api.Module, vfs, nByte, out uint32) uint32 {
mem, ok := mod.Memory().Read(out, nByte)
func vfsRandomness(ctx context.Context, mod api.Module, vfs, nByte, zOut uint32) uint32 {
mem, ok := mod.Memory().Read(zOut, nByte)
if !ok {
return 0
}
@@ -67,3 +71,30 @@ func vfsCurrentTime64(ctx context.Context, mod api.Module, vfs, out uint32) uint
}
return _OK
}
func vfsOpen(ctx context.Context, mod api.Module, vfs, zName, file, flags, pOutFlags uint32) uint32 {
name := getString(mod.Memory(), zName, _MAX_PATHNAME)
log.Println("vfsOpen", name)
return uint32(IOERR)
}
func vfsFullPathname(ctx context.Context, mod api.Module, vfs, zName, nOut, zOut uint32) uint32 {
name := getString(mod.Memory(), zName, _MAX_PATHNAME)
s, err := filepath.Abs(name)
if err != nil {
return uint32(IOERR)
}
siz := uint32(len(s) + 1)
if siz > zOut {
return uint32(IOERR)
}
mem, ok := mod.Memory().Read(zOut, siz)
if !ok {
return uint32(IOERR)
}
mem[len(s)] = 0
copy(mem, s)
return _OK
}