vfs

package
v1.63.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 22, 2026 License: BSD-3-Clause Imports: 13 Imported by: 11 modules, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package vfs gives SQLite a VFS written in Go: a read-only one over a Go fs.FS, made by New, or one a database can be written through, made by Register from a VFS.

New registers the file system with SQLite and returns the name it was registered under. Passing that name as the vfs DSN query parameter opens a database read from the wrapped file system instead of from the host one. Paths are resolved by the wrapped fs.FS, so the database is named relative to its root:

name, fsvfs, err := vfs.New(os.DirFS(dir))
if err != nil {
	return err
}

defer fsvfs.Close()

db, err := sql.Open("sqlite", "file:test.db?vfs="+name)

Any fs.FS will do, an embed.FS included, which is what makes this useful for shipping a database inside the binary. The VFS is read only: it has no write path and refuses to create a journal, so a database opened through it can only be read from. A hot journal next to the database, one SQLite would have to roll back first, makes reading it fail with SQLITE_READONLY_ROLLBACK, as a database opened with mode=ro does.

Register takes a VFS, whose files SQLite reads, writes, truncates, syncs and deletes: the database, its journal and the rest. It is the way to keep a database somewhere a file system does not reach. See Register for what the package does on top of the VFS, and for what it does not do yet.

Registration is process-global, as SQLite's own VFS registry is. Each New and each Register registers a separate VFS under a fresh name, and FS.Close unregisters it again. Close every database opened through an FS before closing it: while any is open, FS.Close refuses with an error wrapping ErrInUse.

Index

Constants

View Source
const (
	SIGEV_SIGNAL = 0
	SIGEV_NONE   = 1
	SIGEV_THREAD = 2

	SIGEV_THREAD_ID = 4
)
View Source
const (
	SEGV_MAPERR  = 1
	SEGV_ACCERR  = 2
	SEGV_BNDERR  = 3
	SEGV_PKUERR  = 4
	SEGV_ACCADI  = 5
	SEGV_ADIDERR = 6
	SEGV_ADIPERR = 7
)
View Source
const (
	BUS_ADRALN    = 1
	BUS_ADRERR    = 2
	BUS_OBJERR    = 3
	BUS_MCEERR_AR = 4
	BUS_MCEERR_AO = 5
)
View Source
const (
	CLD_EXITED    = 1
	CLD_KILLED    = 2
	CLD_DUMPED    = 3
	CLD_TRAPPED   = 4
	CLD_STOPPED   = 5
	CLD_CONTINUED = 6
)
View Source
const (
	POLL_IN  = 1
	POLL_OUT = 2
	POLL_MSG = 3
	POLL_ERR = 4
	POLL_PRI = 5
	POLL_HUP = 6
)
View Source
const (
	SI_ASYNCNL  = -60
	SI_DETHREAD = -7

	SI_TKILL   = -6
	SI_SIGIO   = -5
	SI_ASYNCIO = -4
	SI_MESGQ   = -3
	SI_TIMER   = -2
	SI_QUEUE   = -1
	SI_USER    = 0
	SI_KERNEL  = 128
)
View Source
const (
	ILL_ILLOPC   = 1
	ILL_ILLOPN   = 2
	ILL_ILLADR   = 3
	ILL_ILLTRP   = 4
	ILL_PRVOPC   = 5
	ILL_PRVREG   = 6
	ILL_COPROC   = 7
	ILL_BADSTK   = 8
	ILL_BADIADDR = 9
)
View Source
const (
	FPE_INTDIV   = 1
	FPE_INTOVF   = 2
	FPE_FLTDIV   = 3
	FPE_FLTOVF   = 4
	FPE_FLTUND   = 5
	FPE_FLTRES   = 6
	FPE_FLTINV   = 7
	FPE_FLTSUB   = 8
	FPE_FLTUNK   = 14
	FPE_CONDTRAP = 15
)
View Source
const (
	SS_ONSTACK = 1
	SS_DISABLE = 2
)

Variables

View Source
var ErrFull = errors.New("vfs: storage is full")

ErrFull is what [File.WriteAt] returns, alone or wrapped, when the storage has no room left. SQLite then reports SQLITE_FULL instead of an I/O error.

View Source
var ErrInUse = errors.New("vfs: file system is in use")

ErrInUse is returned, wrapped, by FS.Close while a database opened through the file system is still open.

Functions

func Xsqlite3_fsFS

func Xsqlite3_fsFS(tls *libc.TLS, zName uintptr, pAppData uintptr) uintptr

Types

type AccessFlag

type AccessFlag int32

AccessFlag is the question SQLite asks through VFS.Access. The values are SQLite's.

type FS

type FS struct {
	// contains filtered or unexported fields
}

FS is a VFS registered with SQLite: a read-only one backed by a Go fs.FS, made by New, or one made by Register from a VFS.

func New

func New(fs fs.FS) (name string, _ *FS, _ error)

New creates a new sqlite VFS and registers it. If successful, the file system can be used with the URI parameter `?vfs=<returned name>`.

func Register

func Register(v VFS) (name string, _ *FS, _ error)

Register registers v with SQLite as a VFS a database can be read and written through, and returns the name it was registered under, to be given as the vfs DSN query parameter, and the handle to unregister it with:

name, fsvfs, err := vfs.Register(v)
if err != nil {
	return err
}

defer fsvfs.Close()

db, err := sql.Open("sqlite", "file:test.db?vfs="+name)

The connections of this process that open one database through the returned VFS are kept from one another by the package, which runs SQLite's file locking between them: a File has no lock methods, and a database/sql pool of any size works on a VFS that is safe for concurrent use. Nothing keeps a second process, or a second registration of the same storage, away from the database. Two names for one database are told apart only by VFS.FullPathname.

The VFS has no shared memory, so the write-ahead log works only for a connection that holds the database exclusively. With "PRAGMA locking_mode=EXCLUSIVE" run before the database is first read, "PRAGMA journal_mode=WAL" works; without it the pragma leaves the journal mode as it was, and a database that already is in WAL mode cannot be opened at all. A WAL database therefore needs the pragma in the DSN and a pool of one connection.

SQLite's temporary files are kept in memory and take that much of it for as long as they are open. VACUUM builds a copy of the whole database in one; a sort or a CREATE INDEX too large for SQLite's cache, a large temporary table and a large statement journal are others. Nothing bounds them: where SQLite's own VFS would fill a disk, SQL run through this one can make the process run out of memory.

Registration is process-global, and FS.Close has the same contract as for a file system made by New.

func (*FS) Close

func (f *FS) Close() error

Close unregisters f and releases its resources.

Every database opened through f must be closed first. SQLite keeps a pointer to the VFS in each connection and calls through it for as long as the connection is open, so while any file is open through f, Close does nothing and returns an error wrapping ErrInUse; close the databases and call it again. Close must also not run concurrently with opening a database through f: SQLite looks the VFS up before it opens the file, and nothing can tell Close about an open that is between the two.

type File

type File interface {
	// ReadAt is [io.ReaderAt]. A read that ends past the end of the file
	// returns what there is and io.EOF.
	io.ReaderAt

	// WriteAt is [io.WriterAt]. A write past the end of the file extends it,
	// and the bytes between the old end and the write read as zeros.
	io.WriterAt

	// Close releases the file. SQLite does not look at the error.
	io.Closer

	// Truncate sets the size of the file.
	Truncate(size int64) error

	// Sync returns when everything written to the file is durable.
	Sync(flags SyncFlag) error

	// Size returns the size of the file in bytes.
	Size() (int64, error)
}

File is an open file of a VFS.

SQLite owns the buffers it passes to ReadAt and WriteAt: a File must not keep them, and must not change the one given to WriteAt.

SQLite is told a sector of 4096 bytes and no property of the storage: a write cut short by a loss of power may damage the 4096-byte sectors it touches and nothing further, and what Sync returned from is safe.

type Fts5ExtensionApi

type Fts5ExtensionApi = Fts5ExtensionApi1

type Fts5ExtensionApi1

type Fts5ExtensionApi1 = struct {
	// contains filtered or unexported fields
}

type Fts5PhraseIter

type Fts5PhraseIter = Fts5PhraseIter1

type Fts5PhraseIter1

type Fts5PhraseIter1 = struct {
	// contains filtered or unexported fields
}

type OpenFlag

type OpenFlag int32

OpenFlag is the set of SQLITE_OPEN_ flags SQLite passes to VFS.Open. The values are SQLite's.

Flags of VFS.Open. One of OpenMainDB, OpenMainJournal, OpenSuperJournal and OpenWAL says what kind of file SQLite is opening.

type SyncFlag

type SyncFlag int32

SyncFlag says how much File.Sync has to make durable. The values are SQLite's.

Values of SyncFlag. SyncDataOnly is or-ed into one of the other two when only the content of the file, and not its metadata, has to reach the medium.

type VFS

type VFS interface {
	// Open opens the named file. Flags holds OpenReadWrite or OpenReadOnly;
	// with OpenCreate the file is to be created if it does not exist, and
	// with OpenCreate and OpenExclusive together the open must fail if it
	// does. The returned flags say how the file was opened in fact: return
	// OpenReadOnly for a file that cannot be written although OpenReadWrite
	// was asked for, and flags otherwise. No other bit of the result is used.
	// A File returned together with an error is closed.
	Open(name string, flags OpenFlag) (File, OpenFlag, error)

	// Delete removes the named file. With syncDir, the removal itself must be
	// durable when Delete returns. Deleting a file that does not exist is an
	// error for which errors.Is(err, fs.ErrNotExist) holds.
	Delete(name string, syncDir bool) error

	// Access reports whether the named file exists (AccessExists), exists
	// and can be read (AccessRead), or exists and can be read and written
	// (AccessReadWrite). A file that does not exist is not an error.
	Access(name string, flags AccessFlag) (bool, error)

	// FullPathname returns the canonical name of the database called name in
	// the DSN. Two names of one database must give the same result: it is
	// the name the other files are derived from, and the name under which the
	// connections of this process are kept from one another.
	FullPathname(name string) (string, error)
}

VFS is a file system SQLite can read and write through, registered with Register.

SQLite opens the database, its rollback journal, its super-journals and its write-ahead log through Open, each under the name FullPathname returned for the database with a suffix added: "-journal", "-wal", or "-mj" and nine more characters. Temporary files are not opened through the VFS: the package keeps them in memory, see Register.

The names come from SQLite and not all of them from the DSN: the name of a super-journal is read from the journal found next to a database, so whoever can put a journal there chooses a name that Open, Access and Delete are then called with. A VFS that maps names to something wider than its own storage has to confine them.

The methods are called on the goroutine that runs the statement, so the connections of a pool call them, and the methods of the Files of one database, at the same time. The package serializes nothing but the locking of SQLite: a VFS and whatever its Files share must be safe for concurrent use. A method must not run SQL on a connection that goes through the same VFS, and a panic in a method is not recovered: it leaves the connection, and with it the database, in an undefined state.

type VFSFile

type VFSFile = VFSFile1

type VFSFile1

type VFSFile1 = struct {
	// contains filtered or unexported fields
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL