From 6527435c0fc28b3cfd7fd1a2bca56030672f58df Mon Sep 17 00:00:00 2001 From: Nuno Cruces Date: Sat, 21 Jan 2023 12:24:36 +0000 Subject: [PATCH] File open fixes. --- conn.go | 11 +++++++++++ sqlite3/main.c | 7 ++++--- vfs.go | 18 ++++++------------ 3 files changed, 21 insertions(+), 15 deletions(-) diff --git a/conn.go b/conn.go index d85bd9b..307bc50 100644 --- a/conn.go +++ b/conn.go @@ -241,6 +241,17 @@ func getString(memory api.Memory, ptr, maxlen uint32) string { } } +func (c *Conn) getFile(f *os.File) uint32 { + for i := range c.files { + if c.files[i] == nil { + c.files[i] = f + return uint32(i) + } + } + c.files = append(c.files, f) + return uint32(len(c.files) - 1) +} + type connContext struct{} const ptrSize = 4 diff --git a/sqlite3/main.c b/sqlite3/main.c index 272081a..eb2e532 100644 --- a/sqlite3/main.c +++ b/sqlite3/main.c @@ -20,7 +20,7 @@ int go_full_pathname(sqlite3_vfs *, const char *zName, int nOut, char *zOut); struct go_file { sqlite3_file base; - int fd; + int id; }; int go_close(sqlite3_file *); @@ -59,8 +59,9 @@ static int go_open_c(sqlite3_vfs *vfs, sqlite3_filename zName, .xSectorSize = no_sector_size, .xDeviceCharacteristics = no_device_characteristics, }; - file->pMethods = &go_io; - return go_open(vfs, zName, file, flags, pOutFlags); + int rc = go_open(vfs, zName, file, flags, pOutFlags); + file->pMethods = rc == SQLITE_OK ? &go_io : NULL; + return rc; } int sqlite3_os_init() { diff --git a/vfs.go b/vfs.go index a573823..6c045af 100644 --- a/vfs.go +++ b/vfs.go @@ -186,19 +186,13 @@ func vfsOpen(ctx context.Context, mod api.Module, vfs, zName, file, flags, pOutF return uint32(CANTOPEN) } - var id int - for i := range c.files { - if c.files[i] == nil { - id = i - c.files[i] = f - goto found - } + if ok := mod.Memory().WriteUint32Le(file+ptrSize, c.getFile(f)); !ok { + panic(rangeErr) } - id = len(c.files) - c.files = append(c.files, f) -found: - - if ok := mod.Memory().WriteUint32Le(file+ptrSize, uint32(id)); !ok { + if pOutFlags == 0 { + return _OK + } + if ok := mod.Memory().WriteUint32Le(pOutFlags, flags); !ok { panic(rangeErr) } return _OK