1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
3
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
4
<title>The OS Backend (VFS) To SQLite</title>
5
<style type="text/css">
8
font-family: Verdana, sans-serif;
13
a:visited { color: #734559 }
15
.logo { position:absolute; margin:3px; }
31
.toolbar a { color: white; text-decoration: none; padding: 6px 12px; }
32
.toolbar a:visited { color: white; }
33
.toolbar a:hover { color: #044a64; background: white; }
35
.content { margin: 5%; }
36
.content dt { font-weight:bold; }
37
.content dd { margin-bottom: 25px; margin-left:20%; }
38
.content ul { padding:0px; padding-left: 15px; margin:0px; }
41
.se { background: url(images/se.gif) 100% 100% no-repeat #044a64}
42
.sw { background: url(images/sw.gif) 0% 100% no-repeat }
43
.ne { background: url(images/ne.gif) 100% 0% no-repeat }
44
.nw { background: url(images/nw.gif) 0% 0% no-repeat }
46
/* Things for "fancyformat" documents start here. */
47
.fancy img+p {font-style:italic}
48
.fancy .codeblock i { color: darkblue; }
49
.fancy h1,.fancy h2,.fancy h3,.fancy h4 {font-weight:normal;color:#044a64}
50
.fancy h2 { margin-left: 10px }
51
.fancy h3 { margin-left: 20px }
52
.fancy h4 { margin-left: 30px }
53
.fancy th {white-space:nowrap;text-align:left;border-bottom:solid 1px #444}
54
.fancy th, .fancy td {padding: 0.2em 1ex; vertical-align:top}
55
.fancy #toc a { color: darkblue ; text-decoration: none }
56
.fancy .todo { color: #AA3333 ; font-style : italic }
57
.fancy .todo:before { content: 'TODO:' }
58
.fancy p.todo { border: solid #AA3333 1px; padding: 1ex }
59
.fancy img { display:block; }
60
.fancy :link:hover, .fancy :visited:hover { background: wheat }
61
.fancy p,.fancy ul,.fancy ol { margin: 1em 5ex }
62
.fancy li p { margin: 1em 0 }
63
/* End of "fancyformat" specific rules. */
69
<div><!-- container div to satisfy validator -->
72
<img class="logo" src="images/sqlite370_banner.gif" alt="SQLite Logo"
74
<div><!-- IE hack to prevent disappearing logo--></div>
75
<div class="tagline">Small. Fast. Reliable.<br>Choose any three.</div>
77
<table width=100% style="clear:both"><tr><td>
78
<div class="se"><div class="sw"><div class="ne"><div class="nw">
79
<table width=100% style="padding:0;margin:0;cell-spacing:0"><tr>
82
<a href="about.html">About</a>
83
<a href="sitemap.html">Sitemap</a>
84
<a href="docs.html">Documentation</a>
85
<a href="download.html">Download</a>
86
<a href="copyright.html">License</a>
87
<a href="news.html">News</a>
88
<a href="support.html">Support</a>
91
gMsg = "Search SQLite Docs..."
92
function entersearch() {
93
var q = document.getElementById("q");
94
if( q.value == gMsg ) { q.value = "" }
95
q.style.color = "black"
96
q.style.fontStyle = "normal"
98
function leavesearch() {
99
var q = document.getElementById("q");
100
if( q.value == "" ) {
102
q.style.color = "#044a64"
103
q.style.fontStyle = "italic"
108
<div style="padding:0 1em 0px 0;white-space:nowrap">
109
<form name=f method="GET" action="http://www.sqlite.org/search">
110
<input id=q name=q type=text
111
onfocus="entersearch()" onblur="leavesearch()" style="width:24ex;padding:1px 1ex; border:solid white 1px; font-size:0.9em ; font-style:italic;color:#044a64;" value="Search SQLite Docs...">
112
<input type=submit value="Go" style="border:solid white 1px;background-color:#044a64;color:white;font-size:0.9em;padding:0 1ex">
116
</div></div></div></div>
118
<div class=startsearch></div>
124
The SQLite OS Interface or "VFS"
128
This article describes the SQLite OS portability layer or "VFS" - the
129
module at the bottom of the SQLite implementation stack
130
that provides portability across operating systems.
133
<img src="images/vfs1.gif" align="right" hspace="10">
134
<h2>1.0 The VFS In Relation To The Rest Of SQLite</h2>
137
The internal organization of the SQLite library can be viewed as the
138
stack of modules shown to the right.
139
The Tokenizer, Parser, and Code Generator components are used to
140
process SQL statements and convert them into executable programs
141
in a virtual machine language or byte code.
142
Roughly speaking, these top three layers implement
143
<a href="c3ref/prepare.html">sqlite3_prepare_v2()</a>. The byte code generated by the top three
144
layers is a <a href="c3ref/stmt.html">prepared statement</a>.
145
The Virtual Machine module is responsible for running the SQL statement
146
byte code. The B-Tree module organizes a database file into multiple
147
key/value stores with ordered keys and logarithmic performance.
148
The Pager module is responsible for loading pages of the database
149
file into memory, for implementing and controlling transactions, and
150
for creating and maintaining the journal files that prevent database
151
corruption following a crash or power failure.
152
The OS Interface is a thin abstraction that provides a common set of
153
routines for adapting SQLite to run on different operating systems.
154
Roughly speaking, the bottom four layers implement
155
<a href="c3ref/step.html">sqlite3_step()</a>.
159
This article is about the bottom layer.
162
<p>The OS Interface - also called the "VFS" - is what makes SQLite
163
portable across operating systems. Whenever any of the other modules
164
in SQLite needs to communicate with the operating
165
system, they invoke methods in the VFS. The VFS then invokes the
166
operating-specific code needed to satisfy the request.
167
Hence, porting SQLite to a new
168
operating system is simply a matter of writing a new OS interface layer
171
<h2>2.0 Multiple VFSes</h2>
174
The standard SQLite source tree contains built-in VFSes for os/2, unix,
175
and windows. Alternative VFSes can be
176
added at start-time or run-time using the
177
<a href="c3ref/vfs_find.html">sqlite3_vfs_register()</a> interface.
181
Multiple VFSes can be registered at the same time.
182
Each VFS has a unique names.
183
Separate <a href="c3ref/sqlite3.html">database connections</a> within the same process can be using
184
different VFSes at the same time. For that matter, if a single
185
database connection has multiple database files open using
186
the <a href="lang_attach.html">ATTACH</a> command, then each attached database might be using a
191
Unix builds come with multiple VFSes built-in. The default VFS
192
for unix is called "unix" and is the VFS used in an overwhelming
193
majority of applications. Other VFSes that can be found in unix
198
<li><p><b>unix-dotfile</b> - uses dot-file locking rather than
199
POSIX advisory locks.
200
<li><p><b>unix-excl</b> - obtains and holds an exclusive lock on
201
database files, preventing other processes from accessing the
202
database. Also keeps the <a href="fileformat2.html#walindexformat">wal-index</a> in heap rather than in
204
<li><p><b>unix-none</b> - all file locking operations are no-ops.
205
<li><p><b>unix-namedsem</b> - uses named semaphores for file locking.
210
The various unix VFSes differ only in the way they handle file locking -
211
they share most of their implementation in common with one another and
212
are all located in the same SQLite source file:
213
<a href="http://www.sqlite.org/src/doc/trunk/src/os_unix.c">os_unix.c</a>.
214
Note that except for "unix" and "unix-excl", the various unix VFSes all
215
use incompatible locking implementations. If two processes are accessing
216
the same SQLite database using different unix VFSes, they may
217
not see each others locks and may end up interfering with one another,
218
resulting in database corruption. The "unix-none" VFS in particular
219
does no locking at all and will easily result in database corruption if
220
used by two or more database connections at the same time.
221
Programmers are encouraged to use only "unix" or "unix-excl" unless
222
there is a compelling reason to do otherwise.
225
<h2>2.1 Specifying Which VFS To Use</h2>
228
There is always one VFS which is the default VFS. On unix systems,
229
the "unix" VFS comes up as the default and on windows it is "win32".
230
If no other actions are taken, new database connections will make use
235
The default VFS can be changed by registering or re-registering the
236
VFS using the <a href="c3ref/vfs_find.html">sqlite3_vfs_register()</a> interface with a second parameter
237
of 1. Hence, if a (unix) process to always use the "unix-nolock" VFS
238
in place of "unix", the following code would work:
242
sqlite3_vfs_register(sqlite3_vfs_find("unix-nolock"), 1);
246
An alternate VFS can also be specified as the 4th parameter to the
247
<a href="c3ref/open.html">sqlite3_open_v2()</a> function. For example:
251
int rc = sqlite3_open_v2("demo.db", &db, SQLITE_OPEN_READWRITE, "unix-nolock");
255
Finally, if <a href="uri.html">URI filenames</a> have been enabled, then the alternative
256
VFS can be specified using the "vfs=" parameter on the URI. This technique
257
works with <a href="c3ref/open.html">sqlite3_open()</a>, <a href="c3ref/open.html">sqlite3_open16()</a>, <a href="c3ref/open.html">sqlite3_open_v2()</a>, and
258
when a new database is <a href="lang_attach.html">ATTACH</a>-ed to an existing database connection.
263
ATTACH 'file:demo2.db?vfs=unix-none' AS demo2;
267
The VFS specified by a URI has the highest priority. After that comes
268
a VFS specified as the fourth argument to <a href="c3ref/open.html">sqlite3_open_v2()</a>. The
269
default VFS is used if no VFS is specified otherwise.
272
<h2>2.2 VFS Shims</h2>
275
From the point of view of the uppers layers of the SQLite stack, each
276
open database file uses exactly one VFS.
277
But in practice, a particular VFS might
278
just be a thin wrapper around another VFS that does the real work.
279
We call a wrapper VFS a "shim".
283
A simple example of a shim is the "vfstrace" VFS. This is a VFS
285
<a href="http://www.sqlite.org/src/doc/trunk/src/test_vfstrace.c">test_vfstrace.c</a>
286
source file) that writes a message associated with each VFS method call
287
into a log file, then passes control off to another VFS to do the actual
291
<h2>2.3 Other Example VFSes</h2>
294
The following are other VFS implementations available in the public
300
<a href="http://www.sqlite.org/src/doc/trunk/src/test_demovfs.c">test_demovfs.c</a> -
301
This file implements a very simple VFS named "demo" that uss POSIX
303
open(), read(), write(), fsync(), close(), fsync(), sleep(), time(),
304
and so forth. This VFS only works on unix systems. But it is not
305
intended as a replacement for the standard "unix" VFS used by default
306
on unix platforms. The "demo" VFS is deliberately kept very simple
307
so that it can be used as a learning aid or as template for building
308
other VFSes or for porting SQLite to new operating systems.
311
<a href="http://www.sqlite.org/src/doc/trunk/src/test_quota.c">test_quota.c</a> -
312
This file implements a shim called "quota" that enforces cumulative
313
file size limits on a collection of database files. An auxiliary
314
interface is used to define "quote groups". A quota group is a
315
set of files (database files, journals, and temporary files) whose
316
names all match a <a href="lang_expr.html#glob">GLOB</a> pattern. The sum of the sizes of all files
317
in each quota group is tracked, and if that sum exceeds a threshold
318
defined for the quota group, a callback function is invoked. That
319
callback can either increase the threshold or cause the operation
320
that would have exceeded the quota to fail with an
321
<a href="c3ref/c_abort.html">SQLITE_FULL</a> error. One of the uses of this shim is used to enforce
322
resource limits on application databases in Firefox.
325
<a href="http://www.sqlite.org/src/doc/trunk/src/test_multiplex.c">test_multiplex.c</a> -
326
This file implements a shim that allows database files to exceed the
327
maximum file size of the underlying filesystem. This shim presents
328
an interface to the upper six layers of SQLite that makes it look like
329
very large files are being used, when in reality each such large file
330
is split up into many smaller files on the underlying system.
331
This shim has been used, for example, to allow databases to grow
332
larger than 2 gibibytes on FAT16 filesystems.
335
<a href="http://www.sqlite.org/src/doc/trunk/src/test_onefile.c">test_onefile.c</a> -
336
This file implements a demonstration VFS named "fs" that shows how SQLite
337
can be used on an embedded device that lacks a filesystem. Content is
338
written directly to the underlying media. A VFS derived from this
339
demonstration code could be used by a gadget with a limited amount of
340
flash memory to make SQLite behave as the filesystem for the flash memory
344
<a href="http://www.sqlite.org/src/doc/trunk/src/test_journal.c">test_journal.c</a> -
345
This file implements a shim used during SQLite testing that verifies that
346
the database and rollback journal are written in the correct order and
347
are "synced" at appropriate times in order to guarantee that the database
348
can recover from a power lose are hard reset at any time. The shim
349
checks several invariants on the operation of databases and rollback
350
journals and raises exceptions if any of those invariants are violated.
351
These invariants, in turn, assure that the database is always recoverable.
352
Running a large suite of test cases using this shim provides added
353
assurance that SQLite databases will not be damaged by unexpected
354
power failures or device resets.
357
<a href="http://www.sqlite.org/src/doc/trunk/src/test_vfs.c">test_vfs.c</a> -
358
This file implements a shim that can be used to simulate filesystem faults.
359
This shim is used during testing to verify that SQLite responses sanely
360
to hardware malfunctions or to other error conditions such as running out
361
of filesystem space that are difficult to test on a real system.
365
There are other VFS implementations both in the core SQLite source code
366
library and in available extensions. The list above is not meant to be
367
exhaustive but merely representative of the kinds of features that can
368
be realized using the VFS interface.
371
<h2>3.0 VFS Implementations</h2>
374
A new VFS is implemented by subclassing three objects:
378
<li><a href="c3ref/vfs.html">sqlite3_vfs</a>
379
<li><a href="c3ref/io_methods.html">sqlite3_io_methods</a>
380
<li><a href="c3ref/file.html">sqlite3_file</a>
384
An <a href="c3ref/vfs.html">sqlite3_vfs</a> object defines the name of the VFS and the core
385
methods that implement the interface to the operating system, such
386
as checking for existence of files, deleting files, creating files
387
and opening and for reading and/or writing, converting filenames
388
into their canonical form. The <a href="c3ref/vfs.html">sqlite3_vfs</a> object also contains
389
methods for obtaining randomness from the operating system, for
390
suspending a process (sleeping) and for finding the current date and
395
The <a href="c3ref/file.html">sqlite3_file</a> object represents an open file.
396
The xOpen method of <a href="c3ref/vfs.html">sqlite3_vfs</a> constructs an <a href="c3ref/file.html">sqlite3_file</a>
397
object when the file is opened. The <a href="c3ref/file.html">sqlite3_file</a> keeps track
398
of the state of the file while it is opened.
402
The <a href="c3ref/io_methods.html">sqlite3_io_methods</a> object holds the methods used to interact
403
with an open file. Each <a href="c3ref/file.html">sqlite3_file</a> contains a pointer to
404
an <a href="c3ref/io_methods.html">sqlite3_io_methods</a> object that is appropriate for the file it
405
represents. The <a href="c3ref/io_methods.html">sqlite3_io_methods</a> object contains methods to do
406
things such as read and write from the file, to truncate the file,
407
to flush any changes to persistent storage, to find the size of the
408
file, to lock and unlock the file, and to close file and destroy
409
the <a href="c3ref/file.html">sqlite3_file</a> object.
413
Writing the code for a new VFS involves constructing a subclass for
414
the <a href="c3ref/vfs.html">sqlite3_vfs</a> object and then registering that VFS object using
415
a call to <a href="c3ref/vfs_find.html">sqlite3_vfs_register()</a>. The VFS implementation also
416
provides subclasses for <a href="c3ref/file.html">sqlite3_file</a> and <a href="c3ref/io_methods.html">sqlite3_io_methods</a> but
417
those objects are not registered directly with SQLite. Instead, the
418
<a href="c3ref/file.html">sqlite3_file</a> object is returned from the xOpen method of
419
<a href="c3ref/vfs.html">sqlite3_vfs</a> and the <a href="c3ref/file.html">sqlite3_file</a> object points to an instance
420
of the <a href="c3ref/io_methods.html">sqlite3_io_methods</a> object.
423
<h2>To Be Continued...</h2>