registry.go

 1package vfs
 2
 3import "sync"
 4
 5var (
 6	// +checklocks:vfsRegistryMtx
 7	vfsRegistry    map[string]VFS
 8	vfsRegistryMtx sync.RWMutex
 9)
10
11// Find returns a VFS given its name.
12// If there is no match, nil is returned.
13// If name is empty, the default VFS is returned.
14//
15// https://sqlite.org/c3ref/vfs_find.html
16func Find(name string) VFS {
17	if name == "" || name == "os" {
18		return vfsOS{}
19	}
20	vfsRegistryMtx.RLock()
21	defer vfsRegistryMtx.RUnlock()
22	return vfsRegistry[name]
23}
24
25// Register registers a VFS.
26// Empty and "os" are reserved names.
27//
28// https://sqlite.org/c3ref/vfs_find.html
29func Register(name string, vfs VFS) {
30	if name == "" || name == "os" {
31		return
32	}
33	vfsRegistryMtx.Lock()
34	defer vfsRegistryMtx.Unlock()
35	if vfsRegistry == nil {
36		vfsRegistry = map[string]VFS{}
37	}
38	vfsRegistry[name] = vfs
39}
40
41// Unregister unregisters a VFS.
42//
43// https://sqlite.org/c3ref/vfs_find.html
44func Unregister(name string) {
45	vfsRegistryMtx.Lock()
46	defer vfsRegistryMtx.Unlock()
47	delete(vfsRegistry, name)
48}