Debian Codegraph @

www/vfs.html

raw
1 <!DOCTYPE html>
2 <html><head>
3 <meta name="viewport" content="width=device-width, initial-scale=1.0">
4 <meta http-equiv="content-type" content="text/html; charset=UTF-8">
5 <link href="sqlite.css" rel="stylesheet">
6 <title>The SQLite OS Interface or "VFS"</title>
7 <!-- path= -->
8 </head>
9 <body>
10 <div class=nosearch>
11 <a href="index.html">
12 <img class="logo" src="images/sqlite370_banner.gif" alt="SQLite" border="0">
13 </a>
14 <div><!-- IE hack to prevent disappearing logo --></div>
15 <div class="tagline desktoponly">
16 Small. Fast. Reliable.<br>Choose any three.
17 </div>
18 <div class="menu mainmenu">
19 <ul>
20 <li><a href="index.html">Home</a>
21 <li class='mobileonly'><a href="javascript:void(0)" onclick='toggle_div("submenu")'>Menu</a>
22 <li class='wideonly'><a href='about.html'>About</a>
23 <li class='desktoponly'><a href="docs.html">Documentation</a>
24 <li class='desktoponly'><a href="download.html">Download</a>
25 <li class='wideonly'><a href='copyright.html'>License</a>
26 <li class='desktoponly'><a href="support.html">Support</a>
27 <li class='desktoponly'><a href="prosupport.html">Purchase</a>
28 <li class='search' id='search_menubutton'>
29 <a href="javascript:void(0)" onclick='toggle_search()'>Search</a>
30 </ul>
31 </div>
32 <div class="menu submenu" id="submenu">
33 <ul>
34 <li><a href='about.html'>About</a>
35 <li><a href='docs.html'>Documentation</a>
36 <li><a href='download.html'>Download</a>
37 <li><a href='support.html'>Support</a>
38 <li><a href='prosupport.html'>Purchase</a>
39 </ul>
40 </div>
41 <div class="searchmenu" id="searchmenu">
42 <form method="GET" action="search">
43 <select name="s" id="searchtype">
44 <option value="d">Search Documentation</option>
45 <option value="c">Search Changelog</option>
46 </select>
47 <input type="text" name="q" id="searchbox" value="">
48 <input type="submit" value="Go">
49 </form>
50 </div>
51 </div>
52 <script>
53 function toggle_div(nm) {
54 var w = document.getElementById(nm);
55 if( w.style.display=="block" ){
56 w.style.display = "none";
57 }else{
58 w.style.display = "block";
59 }
60 }
61 function toggle_search() {
62 var w = document.getElementById("searchmenu");
63 if( w.style.display=="block" ){
64 w.style.display = "none";
65 } else {
66 w.style.display = "block";
67 setTimeout(function(){
68 document.getElementById("searchbox").focus()
69 }, 30);
70 }
71 }
72 function div_off(nm){document.getElementById(nm).style.display="none";}
73 window.onbeforeunload = function(e){div_off("submenu");}
74 /* Disable the Search feature if we are not operating from CGI, since */
75 /* Search is accomplished using CGI and will not work without it. */
76 if( !location.origin || !location.origin.match || !location.origin.match(/http/) ){
77 document.getElementById("search_menubutton").style.display = "none";
78 }
79 /* Used by the Hide/Show button beside syntax diagrams, to toggle the */
80 function hideorshow(btn,obj){
81 var x = document.getElementById(obj);
82 var b = document.getElementById(btn);
83 if( x.style.display!='none' ){
84 x.style.display = 'none';
85 b.innerHTML='show';
86 }else{
87 x.style.display = '';
88 b.innerHTML='hide';
89 }
90 return false;
91 }
92 var antiRobot = 0;
93 function antiRobotGo(){
94 if( antiRobot!=3 ) return;
95 antiRobot = 7;
96 var j = document.getElementById("mtimelink");
97 if(j && j.hasAttribute("data-href")) j.href=j.getAttribute("data-href");
98 }
99 function antiRobotDefense(){
100 document.body.onmousedown=function(){
101 antiRobot |= 2;
102 antiRobotGo();
103 document.body.onmousedown=null;
104 }
105 document.body.onmousemove=function(){
106 antiRobot |= 2;
107 antiRobotGo();
108 document.body.onmousemove=null;
109 }
110 setTimeout(function(){
111 antiRobot |= 1;
112 antiRobotGo();
113 }, 100)
114 antiRobotGo();
115 }
116 antiRobotDefense();
117 </script>
118 <div class=fancy>
119 <div class=nosearch>
120 <div class="fancy_title">
121 The SQLite OS Interface or "VFS"
122 </div>
123 <div class="fancy_toc">
124 <a onclick="toggle_toc()">
125 <span class="fancy_toc_mark" id="toc_mk">&#x25ba;</span>
126 Table Of Contents
127 </a>
128 <div id="toc_sub"><div class="fancy-toc1"><a href="#introduction">1. Introduction</a></div>
129 <div class="fancy-toc1"><a href="#the_vfs_in_relation_to_the_rest_of_sqlite">2. The VFS In Relation To The Rest Of SQLite</a></div>
130 <div class="fancy-toc1"><a href="#multiple_vfses">3. Multiple VFSes</a></div>
131 <div class="fancy-toc2"><a href="#standard_unix_vfses">3.1. Standard Unix VFSes</a></div>
132 <div class="fancy-toc2"><a href="#standard_windows_vfses">3.2. Standard Windows VFSes</a></div>
133 <div class="fancy-toc2"><a href="#specifying_which_vfs_to_use">3.3. Specifying Which VFS To Use</a></div>
134 <div class="fancy-toc2"><a href="#vfs_shims">3.4. VFS Shims</a></div>
135 <div class="fancy-toc2"><a href="#other_example_vfses">3.5. Other Example VFSes</a></div>
136 <div class="fancy-toc1"><a href="#vfs_implementations">4. VFS Implementations</a></div>
137 </div>
138 </div>
139 <script>
140 function toggle_toc(){
141 var sub = document.getElementById("toc_sub")
142 var mk = document.getElementById("toc_mk")
143 if( sub.style.display!="block" ){
144 sub.style.display = "block";
145 mk.innerHTML = "&#x25bc;";
146 } else {
147 sub.style.display = "none";
148 mk.innerHTML = "&#x25ba;";
149 }
150 }
151 </script>
152 </div>
153
154
155
156
157
158 <h1 id="introduction"><span>1. </span>Introduction</h1>
159
160 <p>
161 This article describes the SQLite OS portability layer or "VFS" - the
162 module at the bottom of the SQLite implementation stack
163 that provides portability across operating systems.
164 </p>
165
166 <h1 id="the_vfs_in_relation_to_the_rest_of_sqlite"><span>2. </span>The VFS In Relation To The Rest Of SQLite</h1>
167
168 <div>
169 <img src="images/vfs1.gif" align="right" hspace="10">
170 </div>
171
172 <p>
173 The internal organization of the SQLite library can be viewed as the
174 stack of modules shown to the right.
175 The Tokenizer, Parser, and Code Generator components are used to
176 process SQL statements and convert them into executable programs
177 in a virtual machine language or byte code.
178 Roughly speaking, these top three layers implement
179 <a href="c3ref/prepare.html">sqlite3_prepare_v2()</a>. The byte code generated by the top three
180 layers is a <a href="c3ref/stmt.html">prepared statement</a>.
181 The Virtual Machine module is responsible for running the SQL statement
182 byte code. The B-Tree module organizes a database file into multiple
183 key/value stores with ordered keys and logarithmic performance.
184 The Pager module is responsible for loading pages of the database
185 file into memory, for implementing and controlling transactions, and
186 for creating and maintaining the journal files that prevent database
187 corruption following a crash or power failure.
188 The OS Interface is a thin abstraction that provides a common set of
189 routines for adapting SQLite to run on different operating systems.
190 Roughly speaking, the bottom four layers implement
191 <a href="c3ref/step.html">sqlite3_step()</a>.
192 </p>
193
194 <p>
195 This article is about the bottom layer.
196 </p>
197
198 <p>The OS Interface - also called the "VFS" - is what makes SQLite
199 portable across operating systems. Whenever any of the other modules
200 in SQLite needs to communicate with the operating
201 system, they invoke methods in the VFS. The VFS then invokes the
202 operating-specific code needed to satisfy the request.
203 Hence, porting SQLite to a new
204 operating system is simply a matter of writing a new OS interface layer
205 or "VFS".</p>
206
207 <h1 id="multiple_vfses"><span>3. </span>Multiple VFSes</h1>
208
209 <p>
210 The standard SQLite source tree contains built-in VFSes for unix
211 and windows. Alternative VFSes can be
212 added at start-time or run-time using the
213 <a href="c3ref/vfs_find.html">sqlite3_vfs_register()</a> interface.
214 </p>
215
216 <p>
217 Multiple VFSes can be registered at the same time.
218 Each VFS has a unique names.
219 Separate <a href="c3ref/sqlite3.html">database connections</a> within the same process can be using
220 different VFSes at the same time. For that matter, if a single
221 database connection has multiple database files open using
222 the <a href="lang_attach.html">ATTACH</a> command, then each attached database might be using a
223 different VFS.
224 </p>
225
226 <h2 id="standard_unix_vfses"><span>3.1. </span>Standard Unix VFSes</h2>
227
228 <p>
229 Unix builds come with multiple built-in VFSes. The default VFS
230 for unix is called "unix" and is used in most applications.
231 Other VFSes that might be found in unix (depending on compile-time
232 options) include:
233 </p>
234
235 <ol>
236 <li><p><b>unix-dotfile</b> - uses dot-file locking rather than
237 POSIX advisory locks.
238 </p></li><li><p><b>unix-excl</b> - obtains and holds an exclusive lock on
239 database files, preventing other processes from accessing the
240 database. Also keeps the <a href="walformat.html#shm">wal-index</a> in heap rather than in
241 shared memory.
242 </p></li><li><p><b>unix-none</b> - all file locking operations are no-ops.
243 </p></li><li><p><b>unix-namedsem</b> - uses named semaphores for file locking.
244 VXWorks only.
245 </p></li></ol>
246
247 <p>
248 The various unix VFSes differ only in the way they handle file locking -
249 they share most of their implementation in common with one another and
250 are all located in the same SQLite source file:
251 <a href="https://www.sqlite.org/src/doc/trunk/src/os_unix.c">os_unix.c</a>.
252 Note that except for "unix" and "unix-excl", the various unix VFSes all
253 use incompatible locking implementations. If two processes are accessing
254 the same SQLite database using different unix VFSes, they may
255 not see each others locks and may end up interfering with one another,
256 resulting in database corruption. The "unix-none" VFS in particular
257 does no locking at all and will easily result in database corruption if
258 used by two or more database connections at the same time.
259 Programmers are encouraged to use only "unix" or "unix-excl" unless
260 there is a compelling reason to do otherwise.
261 </p>
262
263 <h2 id="standard_windows_vfses"><span>3.2. </span>Standard Windows VFSes</h2>
264
265
266 <p>
267 Windows builds also come with multiple built-in VFSes. The default
268 Windows VFS is called "win32" and is used in most applications.
269 Other VFSes that might be found on windows builds include:
270 </p>
271
272 <ol>
273 <li><p><b>win32-longpath</b> - like "win32" except that pathnames can
274 be up to 65534 bytes in length, whereas pathnames max out at
275 1040 bytes in "win32".
276 </p></li><li><p><b>win32-none</b> - all file locking operations are no-ops.
277 </p></li><li><p><b>win32-longpath-none</b> - combination of "win32-longpath"
278 and "win32-none" - long pathnames are supported and all lock
279 operations are no-ops.
280 </p></li></ol>
281
282 <p>As with unix, most of the code for the various Windows VFSes is shared.
283
284 </p><h2 id="specifying_which_vfs_to_use"><span>3.3. </span>Specifying Which VFS To Use</h2>
285
286 <p>
287 There is always one VFS which is the default VFS. On unix systems,
288 the "unix" VFS comes up as the default and on windows it is "win32".
289 If no other actions are taken, new database connections will make use
290 of the default VFS.
291 </p>
292
293 <p>
294 The default VFS can be changed by registering or re-registering the
295 VFS using the <a href="c3ref/vfs_find.html">sqlite3_vfs_register()</a> interface with a second parameter
296 of 1. Hence, if a (unix) process wants to always use the "unix-nolock" VFS
297 in place of "unix", the following code would work:
298 </p>
299
300 <blockquote><pre>
301 sqlite3_vfs_register(sqlite3_vfs_find("unix-nolock"), 1);
302 </pre></blockquote>
303
304 <p>
305 An alternate VFS can also be specified as the 4th parameter to the
306 <a href="c3ref/open.html">sqlite3_open_v2()</a> function. For example:
307 </p>
308
309 <blockquote><pre>
310 int rc = sqlite3_open_v2("demo.db", &db, SQLITE_OPEN_READWRITE, "unix-nolock");
311 </pre></blockquote>
312
313 <p>
314 Finally, if <a href="uri.html">URI filenames</a> have been enabled, then the alternative
315 VFS can be specified using the "vfs=" parameter on the URI. This technique
316 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
317 when a new database is <a href="lang_attach.html">ATTACH</a>-ed to an existing database connection.
318 For example:
319 </p>
320
321 <blockquote><pre>
322 ATTACH 'file:demo2.db?vfs=unix-none' AS demo2;
323 </pre></blockquote>
324
325 <p>
326 The VFS specified by a URI has the highest priority. After that comes
327 a VFS specified as the fourth argument to <a href="c3ref/open.html">sqlite3_open_v2()</a>. The
328 default VFS is used if no VFS is specified otherwise.
329 </p>
330
331 <a name="shim"></a>
332
333 <h2 id="vfs_shims"><span>3.4. </span>VFS Shims</h2>
334
335 <p>
336 From the point of view of the uppers layers of the SQLite stack, each
337 open database file uses exactly one VFS.
338 But in practice, a particular VFS might
339 just be a thin wrapper around another VFS that does the real work.
340 We call a wrapper VFS a "shim".
341 </p>
342
343 <p>
344 A simple example of a shim is the "vfstrace" VFS. This is a VFS
345 (implemented in the
346 <a href="https://www.sqlite.org/src/doc/trunk/src/test_vfstrace.c">test_vfstrace.c</a>
347 source file) that writes a message associated with each VFS method call
348 into a log file, then passes control off to another VFS to do the actual
349 work.
350 </p>
351
352 <h2 id="other_example_vfses"><span>3.5. </span>Other Example VFSes</h2>
353
354 <p>
355 The following are other VFS implementations available in the public
356 SQLite source tree:
357 </p>
358
359 <ul>
360 <li><p>
361 <a href="https://www.sqlite.org/src/file/ext/misc/appendvfs.c">appendvfs.c</a> -
362 This VFS allows an SQLite database to be appended to the end of some
363 other file. This can be used, for example, to append an SQLite database
364 onto the end of an executable such that, when run, it can easily
365 locate the appended database. The <a href="cli.html">command-line shell</a> will use this
366 VFS if launched with the --append option, and its .archive command
367 will use it given the --append flag.
368
369 </p></li><li><p>
370 <a href="https://www.sqlite.org/src/doc/trunk/src/test_demovfs.c">test_demovfs.c</a> -
371 This file implements a very simple VFS named "demo" that uses POSIX
372 functions such as
373 open(), read(), write(), fsync(), close(), fsync(), sleep(), time(),
374 and so forth. This VFS only works on unix systems. But it is not
375 intended as a replacement for the standard "unix" VFS used by default
376 on unix platforms. The "demo" VFS is deliberately kept very simple
377 so that it can be used as a learning aid or as template for building
378 other VFSes or for porting SQLite to new operating systems.
379
380 </p></li><li><p>
381 <a href="https://www.sqlite.org/src/doc/trunk/src/test_quota.c">test_quota.c</a> -
382 This file implements a shim called "quota" that enforces cumulative
383 file size limits on a collection of database files. An auxiliary
384 interface is used to define "quota groups". A quota group is a
385 set of files (database files, journals, and temporary files) whose
386 names all match a <a href="lang_expr.html#glob">GLOB</a> pattern. The sum of the sizes of all files
387 in each quota group is tracked, and if that sum exceeds a threshold
388 defined for the quota group, a callback function is invoked. That
389 callback can either increase the threshold or cause the operation
390 that would have exceeded the quota to fail with an
391 <a href="rescode.html#full">SQLITE_FULL</a> error. One of the uses of this shim is used to enforce
392 resource limits on application databases in Firefox.
393
394 </p></li><li><p>
395 <a href="https://www.sqlite.org/src/doc/trunk/src/test_multiplex.c">test_multiplex.c</a> -
396 This file implements a shim that allows database files to exceed the
397 maximum file size of the underlying filesystem. This shim presents
398 an interface to the upper six layers of SQLite that makes it look like
399 very large files are being used, when in reality each such large file
400 is split up into many smaller files on the underlying system.
401 This shim has been used, for example, to allow databases to grow
402 larger than 2 gibibytes on FAT16 filesystems.
403
404 </p></li><li><p>
405 <a href="https://www.sqlite.org/src/doc/trunk/src/test_onefile.c">test_onefile.c</a> -
406 This file implements a demonstration VFS named "fs" that shows how SQLite
407 can be used on an embedded device that lacks a filesystem. Content is
408 written directly to the underlying media. A VFS derived from this
409 demonstration code could be used by a gadget with a limited amount of
410 flash memory to make SQLite behave as the filesystem for the flash memory
411 on the device.
412
413 </p></li><li><p>
414 <a href="https://www.sqlite.org/src/doc/trunk/src/test_journal.c">test_journal.c</a> -
415 This file implements a shim used during SQLite testing that verifies that
416 the database and rollback journal are written in the correct order and
417 are "synced" at appropriate times in order to guarantee that the database
418 can recover from a power loss or hard reset at any time. The shim
419 checks several invariants on the operation of databases and rollback
420 journals and raises exceptions if any of those invariants are violated.
421 These invariants, in turn, assure that the database is always recoverable.
422 Running a large suite of test cases using this shim provides added
423 assurance that SQLite databases will not be damaged by unexpected
424 power failures or device resets.
425
426 </p></li><li><p>
427 <a href="https://www.sqlite.org/src/doc/trunk/src/test_vfs.c">test_vfs.c</a> -
428 This file implements a shim that can be used to simulate filesystem faults.
429 This shim is used during testing to verify that SQLite responses sanely
430 to hardware malfunctions or to other error conditions such as running out
431 of filesystem space that are difficult to test on a real system.
432 </p></li></ul>
433
434 <p>
435 There are other VFS implementations both in the core SQLite source code
436 library and in available extensions. The list above is not meant to be
437 exhaustive but merely representative of the kinds of features that can
438 be realized using the VFS interface.
439 </p>
440
441 <h1 id="vfs_implementations"><span>4. </span>VFS Implementations</h1>
442
443 <p>
444 A new VFS is implemented by subclassing three objects:
445 </p>
446
447 <ul>
448 <li><a href="c3ref/vfs.html">sqlite3_vfs</a>
449 </li><li><a href="c3ref/io_methods.html">sqlite3_io_methods</a>
450 </li><li><a href="c3ref/file.html">sqlite3_file</a>
451 </li></ul>
452
453 <p>
454 An <a href="c3ref/vfs.html">sqlite3_vfs</a> object defines the name of the VFS and the core
455 methods that implement the interface to the operating system, such
456 as checking for existence of files, deleting files, creating files
457 and opening and for reading and/or writing, converting filenames
458 into their canonical form. The <a href="c3ref/vfs.html">sqlite3_vfs</a> object also contains
459 methods for obtaining randomness from the operating system, for
460 suspending a process (sleeping) and for finding the current date and
461 time.
462 </p>
463
464 <p>
465 The <a href="c3ref/file.html">sqlite3_file</a> object represents an open file.
466 The xOpen method of <a href="c3ref/vfs.html">sqlite3_vfs</a> constructs an <a href="c3ref/file.html">sqlite3_file</a>
467 object when the file is opened. The <a href="c3ref/file.html">sqlite3_file</a> keeps track
468 of the state of the file while it is opened.
469 </p>
470
471 <p>
472 The <a href="c3ref/io_methods.html">sqlite3_io_methods</a> object holds the methods used to interact
473 with an open file. Each <a href="c3ref/file.html">sqlite3_file</a> contains a pointer to
474 an <a href="c3ref/io_methods.html">sqlite3_io_methods</a> object that is appropriate for the file it
475 represents. The <a href="c3ref/io_methods.html">sqlite3_io_methods</a> object contains methods to do
476 things such as read and write from the file, to truncate the file,
477 to flush any changes to persistent storage, to find the size of the
478 file, to lock and unlock the file, and to close file and destroy
479 the <a href="c3ref/file.html">sqlite3_file</a> object.
480 </p>
481
482 <p>
483 Writing the code for a new VFS involves constructing a subclass for
484 the <a href="c3ref/vfs.html">sqlite3_vfs</a> object and then registering that VFS object using
485 a call to <a href="c3ref/vfs_find.html">sqlite3_vfs_register()</a>. The VFS implementation also
486 provides subclasses for <a href="c3ref/file.html">sqlite3_file</a> and <a href="c3ref/io_methods.html">sqlite3_io_methods</a> but
487 those objects are not registered directly with SQLite. Instead, the
488 <a href="c3ref/file.html">sqlite3_file</a> object is returned from the xOpen method of
489 <a href="c3ref/vfs.html">sqlite3_vfs</a> and the <a href="c3ref/file.html">sqlite3_file</a> object points to an instance
490 of the <a href="c3ref/io_methods.html">sqlite3_io_methods</a> object.
491 </p>
492 <p align="center"><small><i>This page last modified on <a href="https://sqlite.org/docsrc/honeypot" id="mtimelink" data-href="https://sqlite.org/docsrc/finfo/pages/vfs.in?m=90886f1151">2024-02-22 15:53:45</a> UTC </small></i></p>
493