Files
sqlite3/vfs.go

288 lines
7.4 KiB
Go
Raw Normal View History

2023-01-18 01:30:11 +00:00
package sqlite3
import (
"context"
2023-01-19 00:18:39 +00:00
"errors"
2023-01-18 23:45:22 +00:00
"io"
2023-01-19 00:18:39 +00:00
"io/fs"
2023-01-18 01:30:11 +00:00
"math/rand"
2023-01-18 18:58:49 +00:00
"os"
2023-01-18 11:40:08 +00:00
"path/filepath"
2023-01-19 13:27:32 +00:00
"syscall"
2023-01-18 01:30:11 +00:00
"time"
"github.com/ncruces/julianday"
"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/api"
"github.com/tetratelabs/wazero/sys"
)
func vfsInstantiate(ctx context.Context, r wazero.Runtime) (err error) {
wasi := r.NewHostModuleBuilder("wasi_snapshot_preview1")
wasi.NewFunctionBuilder().WithFunc(vfsExit).Export("proc_exit")
_, err = wasi.Instantiate(ctx)
if err != nil {
return err
}
env := r.NewHostModuleBuilder("env")
env.NewFunctionBuilder().WithFunc(vfsRandomness).Export("go_randomness")
env.NewFunctionBuilder().WithFunc(vfsSleep).Export("go_sleep")
env.NewFunctionBuilder().WithFunc(vfsCurrentTime).Export("go_current_time")
env.NewFunctionBuilder().WithFunc(vfsCurrentTime64).Export("go_current_time_64")
2023-01-18 18:58:49 +00:00
env.NewFunctionBuilder().WithFunc(vfsFullPathname).Export("go_full_pathname")
env.NewFunctionBuilder().WithFunc(vfsDelete).Export("go_delete")
env.NewFunctionBuilder().WithFunc(vfsAccess).Export("go_access")
env.NewFunctionBuilder().WithFunc(vfsOpen).Export("go_open")
env.NewFunctionBuilder().WithFunc(vfsClose).Export("go_close")
env.NewFunctionBuilder().WithFunc(vfsRead).Export("go_read")
env.NewFunctionBuilder().WithFunc(vfsWrite).Export("go_write")
env.NewFunctionBuilder().WithFunc(vfsTruncate).Export("go_truncate")
env.NewFunctionBuilder().WithFunc(vfsSync).Export("go_sync")
env.NewFunctionBuilder().WithFunc(vfsFileSize).Export("go_file_size")
2023-01-22 17:33:21 +00:00
env.NewFunctionBuilder().WithFunc(vfsLock).Export("go_lock")
env.NewFunctionBuilder().WithFunc(vfsUnlock).Export("go_unlock")
env.NewFunctionBuilder().WithFunc(vfsCheckReservedLock).Export("go_check_reserved_lock")
2023-01-18 01:30:11 +00:00
_, err = env.Instantiate(ctx)
return err
}
func vfsExit(ctx context.Context, mod api.Module, exitCode uint32) {
// Ensure other callers see the exit code.
_ = mod.CloseWithExitCode(ctx, exitCode)
// Prevent any code from executing after this function.
panic(sys.NewExitError(mod.Name(), exitCode))
}
2023-01-22 15:44:39 +00:00
func vfsRandomness(ctx context.Context, mod api.Module, pVfs, nByte, zByte uint32) uint32 {
mem, ok := mod.Memory().Read(zByte, nByte)
2023-01-18 01:30:11 +00:00
if !ok {
2023-01-19 14:31:32 +00:00
panic(rangeErr)
2023-01-18 01:30:11 +00:00
}
n, _ := rand.Read(mem)
return uint32(n)
}
2023-01-22 15:44:39 +00:00
func vfsSleep(ctx context.Context, pVfs, nMicro uint32) uint32 {
time.Sleep(time.Duration(nMicro) * time.Microsecond)
2023-01-18 01:30:11 +00:00
return _OK
}
2023-01-22 15:44:39 +00:00
func vfsCurrentTime(ctx context.Context, mod api.Module, pVfs, prNow uint32) uint32 {
2023-01-18 01:30:11 +00:00
day := julianday.Float(time.Now())
2023-01-22 15:44:39 +00:00
if ok := mod.Memory().WriteFloat64Le(prNow, day); !ok {
2023-01-19 14:31:32 +00:00
panic(rangeErr)
2023-01-18 01:30:11 +00:00
}
return _OK
}
2023-01-22 15:44:39 +00:00
func vfsCurrentTime64(ctx context.Context, mod api.Module, pVfs, piNow uint32) uint32 {
2023-01-18 01:30:11 +00:00
day, nsec := julianday.Date(time.Now())
msec := day*86_400_000 + nsec/1_000_000
2023-01-22 15:44:39 +00:00
if ok := mod.Memory().WriteUint64Le(piNow, uint64(msec)); !ok {
2023-01-19 14:31:32 +00:00
panic(rangeErr)
2023-01-18 01:30:11 +00:00
}
return _OK
}
2023-01-18 11:40:08 +00:00
2023-01-22 15:44:39 +00:00
func vfsFullPathname(ctx context.Context, mod api.Module, pVfs, zRelative, nFull, zFull uint32) uint32 {
rel := getString(mod.Memory(), zRelative, _MAX_PATHNAME)
abs, err := filepath.Abs(rel)
2023-01-18 11:40:08 +00:00
if err != nil {
return uint32(IOERR)
}
2023-01-22 17:33:21 +00:00
// Consider either using [filepath.EvalSymlinks] to canonicalize the path (as the Unix VFS does).
// Or using [os.Readlink] to resolve a symbolic link (as the Unix VFS did).
// This might be buggy on Windows (the Windows VFS doesn't try).
2023-01-22 15:44:39 +00:00
siz := uint32(len(abs) + 1)
if siz > nFull {
2023-01-18 11:40:08 +00:00
return uint32(IOERR)
}
2023-01-22 15:44:39 +00:00
mem, ok := mod.Memory().Read(zFull, siz)
2023-01-18 11:40:08 +00:00
if !ok {
2023-01-19 14:31:32 +00:00
panic(rangeErr)
2023-01-18 11:40:08 +00:00
}
2023-01-22 15:44:39 +00:00
mem[len(abs)] = 0
copy(mem, abs)
2023-01-18 11:40:08 +00:00
return _OK
}
2023-01-18 18:58:49 +00:00
2023-01-22 15:44:39 +00:00
func vfsDelete(ctx context.Context, mod api.Module, pVfs, zPath, syncDir uint32) uint32 {
path := getString(mod.Memory(), zPath, _MAX_PATHNAME)
err := os.Remove(path)
2023-01-19 01:12:09 +00:00
if errors.Is(err, fs.ErrNotExist) {
return _OK
}
if err != nil {
return uint32(IOERR_DELETE)
}
if syncDir != 0 {
2023-01-22 15:44:39 +00:00
f, err := os.Open(filepath.Dir(path))
2023-01-19 01:12:09 +00:00
if err == nil {
err = f.Sync()
f.Close()
}
if err != nil {
return uint32(IOERR_DELETE)
}
}
return _OK
}
2023-01-18 18:58:49 +00:00
2023-01-22 15:44:39 +00:00
func vfsAccess(ctx context.Context, mod api.Module, pVfs, zPath, flags, pResOut uint32) uint32 {
2023-01-23 14:00:37 +00:00
// Consider using [syscall.Access] for [ACCESS_READWRITE]/[ACCESS_READ]
// (as the Unix VFS does).
2023-01-22 15:44:39 +00:00
path := getString(mod.Memory(), zPath, _MAX_PATHNAME)
fi, err := os.Stat(path)
2023-01-19 00:18:39 +00:00
var res uint32
if flags == uint32(ACCESS_EXISTS) {
switch {
case err == nil:
res = 1
case errors.Is(err, fs.ErrNotExist):
res = 0
default:
return uint32(IOERR_ACCESS)
}
2023-01-19 13:27:32 +00:00
} else if err == nil {
var want fs.FileMode = syscall.S_IRUSR
2023-01-19 00:18:39 +00:00
if flags == uint32(ACCESS_READWRITE) {
2023-01-19 13:27:32 +00:00
want |= syscall.S_IWUSR
2023-01-19 00:18:39 +00:00
}
2023-01-19 13:27:32 +00:00
if fi.IsDir() {
want |= syscall.S_IXUSR
}
if fi.Mode()&want == want {
2023-01-19 00:18:39 +00:00
res = 1
} else {
res = 0
}
2023-01-19 13:27:32 +00:00
} else if errors.Is(err, fs.ErrPermission) {
res = 0
} else {
return uint32(IOERR_ACCESS)
2023-01-19 00:18:39 +00:00
}
2023-01-19 14:31:32 +00:00
if ok := mod.Memory().WriteUint32Le(pResOut, res); !ok {
panic(rangeErr)
2023-01-19 00:18:39 +00:00
}
return _OK
}
2023-01-18 18:58:49 +00:00
2023-01-22 15:44:39 +00:00
func vfsOpen(ctx context.Context, mod api.Module, pVfs, zName, pFile, flags, pOutFlags uint32) uint32 {
2023-01-18 18:58:49 +00:00
name := getString(mod.Memory(), zName, _MAX_PATHNAME)
var oflags int
if OpenFlag(flags)&OPEN_EXCLUSIVE != 0 {
oflags |= os.O_EXCL
}
if OpenFlag(flags)&OPEN_CREATE != 0 {
oflags |= os.O_CREATE
}
if OpenFlag(flags)&OPEN_READONLY != 0 {
oflags |= os.O_RDONLY
}
if OpenFlag(flags)&OPEN_READWRITE != 0 {
oflags |= os.O_RDWR
}
2023-01-22 17:33:21 +00:00
file, err := os.OpenFile(name, oflags, 0600)
2023-01-18 18:58:49 +00:00
if err != nil {
return uint32(CANTOPEN)
}
2023-01-23 14:00:37 +00:00
id, err := vfsGetOpenFileID(file)
2023-01-22 17:33:21 +00:00
if err != nil {
return uint32(CANTOPEN)
2023-01-18 18:58:49 +00:00
}
2023-01-23 14:00:37 +00:00
vfsFilePtr{mod, pFile}.SetID(id).SetLock(_NO_LOCK)
2023-01-22 17:33:21 +00:00
2023-01-21 12:24:36 +00:00
if pOutFlags == 0 {
return _OK
}
if ok := mod.Memory().WriteUint32Le(pOutFlags, flags); !ok {
2023-01-19 14:31:32 +00:00
panic(rangeErr)
2023-01-19 00:25:56 +00:00
}
2023-01-18 18:58:49 +00:00
return _OK
}
2023-01-22 15:44:39 +00:00
func vfsClose(ctx context.Context, mod api.Module, pFile uint32) uint32 {
2023-01-23 14:00:37 +00:00
id := vfsFilePtr{mod, pFile}.ID()
err := vfsReleaseOpenFile(id)
2023-01-18 18:58:49 +00:00
if err != nil {
2023-01-19 00:18:39 +00:00
return uint32(IOERR_CLOSE)
2023-01-18 18:58:49 +00:00
}
return _OK
}
2023-01-22 15:44:39 +00:00
func vfsRead(ctx context.Context, mod api.Module, pFile, zBuf, iAmt uint32, iOfst uint64) uint32 {
mem, ok := mod.Memory().Read(zBuf, iAmt)
2023-01-18 23:45:22 +00:00
if !ok {
2023-01-19 14:31:32 +00:00
panic(rangeErr)
2023-01-18 23:45:22 +00:00
}
2023-01-23 14:00:37 +00:00
file := vfsFilePtr{mod, pFile}.OSFile()
2023-01-22 15:44:39 +00:00
n, err := file.ReadAt(mem, int64(iOfst))
2023-01-18 23:45:22 +00:00
if n == int(iAmt) {
return _OK
}
if n == 0 && err != io.EOF {
return uint32(IOERR_READ)
}
for i := range mem[n:] {
mem[i] = 0
}
return uint32(IOERR_SHORT_READ)
2023-01-18 18:58:49 +00:00
}
2023-01-22 15:44:39 +00:00
func vfsWrite(ctx context.Context, mod api.Module, pFile, zBuf, iAmt uint32, iOfst uint64) uint32 {
mem, ok := mod.Memory().Read(zBuf, iAmt)
2023-01-19 00:58:57 +00:00
if !ok {
2023-01-19 14:31:32 +00:00
panic(rangeErr)
2023-01-19 00:58:57 +00:00
}
2023-01-23 14:00:37 +00:00
file := vfsFilePtr{mod, pFile}.OSFile()
2023-01-22 15:44:39 +00:00
_, err := file.WriteAt(mem, int64(iOfst))
2023-01-19 00:58:57 +00:00
if err != nil {
return uint32(IOERR_WRITE)
}
return _OK
}
2023-01-18 18:58:49 +00:00
2023-01-22 15:44:39 +00:00
func vfsTruncate(ctx context.Context, mod api.Module, pFile uint32, nByte uint64) uint32 {
2023-01-23 14:00:37 +00:00
file := vfsFilePtr{mod, pFile}.OSFile()
2023-01-22 15:44:39 +00:00
err := file.Truncate(int64(nByte))
2023-01-19 01:12:09 +00:00
if err != nil {
return uint32(IOERR_TRUNCATE)
}
return _OK
}
2023-01-18 18:58:49 +00:00
2023-01-22 15:44:39 +00:00
func vfsSync(ctx context.Context, mod api.Module, pFile, flags uint32) uint32 {
2023-01-23 14:00:37 +00:00
file := vfsFilePtr{mod, pFile}.OSFile()
2023-01-22 15:44:39 +00:00
err := file.Sync()
2023-01-19 00:58:57 +00:00
if err != nil {
return uint32(IOERR_FSYNC)
}
return _OK
}
2023-01-18 18:58:49 +00:00
2023-01-22 15:44:39 +00:00
func vfsFileSize(ctx context.Context, mod api.Module, pFile, pSize uint32) uint32 {
2023-01-22 17:33:21 +00:00
// This uses [file.Seek] because we don't care about the offset for reading/writing.
// But consider using [file.Stat] instead (as other VFSes do).
2023-01-23 14:00:37 +00:00
file := vfsFilePtr{mod, pFile}.OSFile()
2023-01-22 15:44:39 +00:00
off, err := file.Seek(0, io.SeekEnd)
2023-01-19 00:25:56 +00:00
if err != nil {
return uint32(IOERR_SEEK)
}
2023-01-19 14:31:32 +00:00
if ok := mod.Memory().WriteUint64Le(pSize, uint64(off)); !ok {
panic(rangeErr)
2023-01-19 00:25:56 +00:00
}
return _OK
}