From d4d4533a410f464ebcc69810c95055e73737032a Mon Sep 17 00:00:00 2001 From: Nuno Cruces Date: Fri, 2 Jun 2023 03:32:13 +0100 Subject: [PATCH] Docs. --- vfs/api.go | 10 +++++----- vfs/memdb/api.go | 1 + vfs/memdb/memdb.go | 2 +- vfs/readervfs/api.go | 1 + vfs/registry.go | 13 ++++++++++--- vfs/vfs.go | 25 ++++++++++--------------- 6 files changed, 28 insertions(+), 24 deletions(-) diff --git a/vfs/api.go b/vfs/api.go index aa66c06..cfe7dd0 100644 --- a/vfs/api.go +++ b/vfs/api.go @@ -15,7 +15,7 @@ type VFS interface { FullPathname(name string) (string, error) } -// VFSParams extends [VFS] to with the ability to handle URI parameters +// VFSParams extends VFS to with the ability to handle URI parameters // through the OpenParams method. // // https://www.sqlite.org/c3ref/uri_boolean.html @@ -44,7 +44,7 @@ type File interface { DeviceCharacteristics() DeviceCharacteristic } -// FileLockState extends [File] to implement the +// FileLockState extends File to implement the // SQLITE_FCNTL_LOCKSTATE file control opcode. // // https://www.sqlite.org/c3ref/c_fcntl_begin_atomic_write.html @@ -53,7 +53,7 @@ type FileLockState interface { LockState() LockLevel } -// FileSizeHint extends [File] to implement the +// FileSizeHint extends File to implement the // SQLITE_FCNTL_SIZE_HINT file control opcode. // // https://www.sqlite.org/c3ref/c_fcntl_begin_atomic_write.html @@ -62,7 +62,7 @@ type FileSizeHint interface { SizeHint(size int64) error } -// FileHasMoved extends [File] to implement the +// FileHasMoved extends File to implement the // SQLITE_FCNTL_HAS_MOVED file control opcode. // // https://www.sqlite.org/c3ref/c_fcntl_begin_atomic_write.html @@ -71,7 +71,7 @@ type FileHasMoved interface { HasMoved() (bool, error) } -// FilePowersafeOverwrite extends [File] to implement the +// FilePowersafeOverwrite extends File to implement the // SQLITE_FCNTL_POWERSAFE_OVERWRITE file control opcode. // // https://www.sqlite.org/c3ref/c_fcntl_begin_atomic_write.html diff --git a/vfs/memdb/api.go b/vfs/memdb/api.go index d2e4034..55f415f 100644 --- a/vfs/memdb/api.go +++ b/vfs/memdb/api.go @@ -21,6 +21,7 @@ func init() { var ( memoryMtx sync.Mutex + // +checklocks:memoryMtx memoryDBs = map[string]*memDB{} ) diff --git a/vfs/memdb/memdb.go b/vfs/memdb/memdb.go index ade8229..984d408 100644 --- a/vfs/memdb/memdb.go +++ b/vfs/memdb/memdb.go @@ -40,7 +40,7 @@ func (memVFS) Open(name string, flags vfs.OpenFlag) (vfs.File, vfs.OpenFlag, err db = new(memDB) } if shared { - memoryDBs[name[1:]] = db + memoryDBs[name[1:]] = db // +checklocksignore: lock is held } return &memFile{ diff --git a/vfs/readervfs/api.go b/vfs/readervfs/api.go index ddd96e9..eed04f3 100644 --- a/vfs/readervfs/api.go +++ b/vfs/readervfs/api.go @@ -23,6 +23,7 @@ func init() { var ( readerMtx sync.RWMutex + // +checklocks:readerMtx readerDBs = map[string]SizeReaderAt{} ) diff --git a/vfs/registry.go b/vfs/registry.go index a8a37a5..33bc54b 100644 --- a/vfs/registry.go +++ b/vfs/registry.go @@ -5,16 +5,20 @@ import "sync" var ( // +checklocks:vfsRegistryMtx vfsRegistry map[string]VFS - vfsRegistryMtx sync.Mutex + vfsRegistryMtx sync.RWMutex ) // Find returns a VFS given its name. // If there is no match, nil is returned. +// If name is empty, the default VFS is returned. // // https://www.sqlite.org/c3ref/vfs_find.html func Find(name string) VFS { - vfsRegistryMtx.Lock() - defer vfsRegistryMtx.Unlock() + if name == "" || name == "os" { + return vfsOS{} + } + vfsRegistryMtx.RLock() + defer vfsRegistryMtx.RUnlock() return vfsRegistry[name] } @@ -22,6 +26,9 @@ func Find(name string) VFS { // // https://www.sqlite.org/c3ref/vfs_find.html func Register(name string, vfs VFS) { + if name == "" || name == "os" { + return + } vfsRegistryMtx.Lock() defer vfsRegistryMtx.Unlock() if vfsRegistry == nil { diff --git a/vfs/vfs.go b/vfs/vfs.go index eaa6dfd..9167d82 100644 --- a/vfs/vfs.go +++ b/vfs/vfs.go @@ -14,10 +14,10 @@ import ( "github.com/tetratelabs/wazero/api" ) +// ExportHostFunctions is an internal API users need not call directly. +// // ExportHostFunctions registers the required VFS host functions // with the provided env module. -// -// Users of the [github.com/ncruces/go-sqlite3] package need not call this directly. func ExportHostFunctions(env wazero.HostModuleBuilder) wazero.HostModuleBuilder { util.ExportFuncII(env, "go_vfs_find", vfsFind) util.ExportFuncIIJ(env, "go_localtime", vfsLocaltime) @@ -49,15 +49,13 @@ type vfsState struct { files []File } +// NewContext is an internal API users need not call directly. +// // NewContext creates a new context to hold [api.Module] specific VFS data. -// -// This context should be passed to any [api.Function] calls that might +// The context should be passed to any [api.Function] calls that might // generate VFS host callbacks. -// // The returned [io.Closer] should be closed after the [api.Module] is closed, // to release any associated resources. -// -// Users of the [github.com/ncruces/go-sqlite3] package need not call this directly. func NewContext(ctx context.Context) (context.Context, io.Closer) { vfs := new(vfsState) return context.WithValue(ctx, vfsKey{}, vfs), vfs @@ -75,7 +73,7 @@ func (vfs *vfsState) Close() error { func vfsFind(ctx context.Context, mod api.Module, zVfsName uint32) uint32 { name := util.ReadString(mod, zVfsName, _MAX_STRING) - if Find(name) != nil { + if vfs := Find(name); vfs != nil && vfs != (vfsOS{}) { return 1 } return 0 @@ -399,13 +397,10 @@ func vfsURIParameters(ctx context.Context, mod api.Module, zPath uint32, flags O } func vfsGet(mod api.Module, pVfs uint32) VFS { - if pVfs == 0 { - return vfsOS{} - } - const zNameOffset = 16 - name := util.ReadString(mod, util.ReadUint32(mod, pVfs+zNameOffset), _MAX_STRING) - if name == "os" { - return vfsOS{} + var name string + if pVfs != 0 { + const zNameOffset = 16 + name = util.ReadString(mod, util.ReadUint32(mod, pVfs+zNameOffset), _MAX_STRING) } if vfs := Find(name); vfs != nil { return vfs