xref: /netbsd-src/external/public-domain/sqlite/man/sqlite3_vfs.3 (revision b9988867a8ad969c45a52aa7628bc932ec98d46b)
1.Dd January 24, 2024
2.Dt SQLITE3_VFS 3
3.Os
4.Sh NAME
5.Nm sqlite3_vfs ,
6.Nm sqlite3_syscall_ptr ,
7.Nm sqlite3_vfs
8.Nd OS interface object
9.Sh SYNOPSIS
10.In sqlite3.h
11.Vt typedef struct sqlite3_vfs sqlite3_vfs;
12.Vt typedef void (*sqlite3_syscall_ptr)(void);
13.Vt struct sqlite3_vfs ;
14.Sh DESCRIPTION
15An instance of the sqlite3_vfs object defines the interface between
16the SQLite core and the underlying operating system.
17The "vfs" in the name of the object stands for "virtual file system".
18See the VFS documentation for further information.
19.Pp
20The VFS interface is sometimes extended by adding new methods onto
21the end.
22Each time such an extension occurs, the iVersion field is incremented.
23The iVersion value started out as 1 in SQLite version 3.5.0
24on dateof:3.5.0, then increased to 2 with SQLite version 3.7.0
25on dateof:3.7.0, and then increased to 3 with SQLite version 3.7.6
26on dateof:3.7.6.
27Additional fields may be appended to the sqlite3_vfs object and the
28iVersion value may increase again in future versions of SQLite.
29Note that due to an oversight, the structure of the sqlite3_vfs object
30changed in the transition from SQLite version 3.5.9 to
31version 3.6.0 on dateof:3.6.0 and yet the
32iVersion field was not increased.
33.Pp
34The szOsFile field is the size of the subclassed sqlite3_file
35structure used by this VFS.
36mxPathname is the maximum length of a pathname in this VFS.
37.Pp
38Registered sqlite3_vfs objects are kept on a linked list formed by
39the pNext pointer.
40The
41.Fn sqlite3_vfs_register
42and
43.Fn sqlite3_vfs_unregister
44interfaces manage this list in a thread-safe way.
45The
46.Fn sqlite3_vfs_find
47interface searches the list.
48Neither the application code nor the VFS implementation should use
49the pNext pointer.
50.Pp
51The pNext field is the only field in the sqlite3_vfs structure that
52SQLite will ever modify.
53SQLite will only access or modify this field while holding a particular
54static mutex.
55The application should never modify anything within the sqlite3_vfs
56object once the object has been registered.
57.Pp
58The zName field holds the name of the VFS module.
59The name must be unique across all VFS modules.
60.Pp
61SQLite guarantees that the zFilename parameter to xOpen is either a
62NULL pointer or string obtained from xFullPathname() with an optional
63suffix added.
64If a suffix is added to the zFilename parameter, it will consist of
65a single "-" character followed by no more than 11 alphanumeric and/or
66"-" characters.
67SQLite further guarantees that the string will be valid and unchanged
68until xClose() is called.
69Because of the previous sentence, the sqlite3_file can
70safely store a pointer to the filename if it needs to remember the
71filename for some reason.
72If the zFilename parameter to xOpen is a NULL pointer then xOpen must
73invent its own temporary name for the file.
74Whenever the xFilename parameter is NULL it will also be the case that
75the flags parameter will include SQLITE_OPEN_DELETEONCLOSE.
76.Pp
77The flags argument to xOpen() includes all bits set in the flags argument
78to
79.Fn sqlite3_open_v2 .
80Or if
81.Fn sqlite3_open
82or
83.Fn sqlite3_open16
84is used, then flags includes at least SQLITE_OPEN_READWRITE
85| SQLITE_OPEN_CREATE.
86If xOpen() opens a file read-only then it sets *pOutFlags to include
87SQLITE_OPEN_READONLY.
88Other bits in *pOutFlags may be set.
89.Pp
90SQLite will also add one of the following flags to the xOpen() call,
91depending on the object being opened:
92.Bl -bullet
93.It
94SQLITE_OPEN_MAIN_DB
95.It
96SQLITE_OPEN_MAIN_JOURNAL
97.It
98SQLITE_OPEN_TEMP_DB
99.It
100SQLITE_OPEN_TEMP_JOURNAL
101.It
102SQLITE_OPEN_TRANSIENT_DB
103.It
104SQLITE_OPEN_SUBJOURNAL
105.It
106SQLITE_OPEN_SUPER_JOURNAL
107.It
108SQLITE_OPEN_WAL
109.El
110.Pp
111The file I/O implementation can use the object type flags to change
112the way it deals with files.
113For example, an application that does not care about crash recovery
114or rollback might make the open of a journal file a no-op.
115Writes to this journal would also be no-ops, and any attempt to read
116the journal would return SQLITE_IOERR.
117Or the implementation might recognize that a database file will be
118doing page-aligned sector reads and writes in a random order and set
119up its I/O subsystem accordingly.
120.Pp
121SQLite might also add one of the following flags to the xOpen method:
122.Bl -bullet
123.It
124SQLITE_OPEN_DELETEONCLOSE
125.It
126SQLITE_OPEN_EXCLUSIVE
127.El
128.Pp
129The SQLITE_OPEN_DELETEONCLOSE flag means the
130file should be deleted when it is closed.
131The SQLITE_OPEN_DELETEONCLOSE will be set
132for TEMP databases and their journals, transient databases, and subjournals.
133.Pp
134The SQLITE_OPEN_EXCLUSIVE flag is always used
135in conjunction with the SQLITE_OPEN_CREATE flag,
136which are both directly analogous to the O_EXCL and O_CREAT flags of
137the POSIX open() API.
138The SQLITE_OPEN_EXCLUSIVE flag, when paired with the SQLITE_OPEN_CREATE,
139is used to indicate that file should always be created, and that it
140is an error if it already exists.
141It is \fInot\fP used to indicate the file should be opened for exclusive
142access.
143.Pp
144At least szOsFile bytes of memory are allocated by SQLite to hold the
145sqlite3_file structure passed as the third argument to
146xOpen.
147The xOpen method does not have to allocate the structure; it should
148just fill it in.
149Note that the xOpen method must set the sqlite3_file.pMethods to either
150a valid sqlite3_io_methods object or to NULL.
151xOpen must do this even if the open fails.
152SQLite expects that the sqlite3_file.pMethods element will be valid
153after xOpen returns regardless of the success or failure of the xOpen
154call.
155.Pp
156The flags argument to xAccess() may be SQLITE_ACCESS_EXISTS
157to test for the existence of a file, or SQLITE_ACCESS_READWRITE
158to test whether a file is readable and writable, or SQLITE_ACCESS_READ
159to test whether a file is at least readable.
160The SQLITE_ACCESS_READ flag is never actually used and is not implemented
161in the built-in VFSes of SQLite.
162The file is named by the second argument and can be a directory.
163The xAccess method returns SQLITE_OK on success or some non-zero
164error code if there is an I/O error or if the name of the file given
165in the second argument is illegal.
166If SQLITE_OK is returned, then non-zero or zero is written into *pResOut
167to indicate whether or not the file is accessible.
168.Pp
169SQLite will always allocate at least mxPathname+1 bytes for the output
170buffer xFullPathname.
171The exact size of the output buffer is also passed as a parameter to
172both  methods.
173If the output buffer is not large enough, SQLITE_CANTOPEN
174should be returned.
175Since this is handled as a fatal error by SQLite, vfs implementations
176should endeavor to prevent this by setting mxPathname to a sufficiently
177large value.
178.Pp
179The xRandomness(), xSleep(), xCurrentTime(), and xCurrentTimeInt64()
180interfaces are not strictly a part of the filesystem, but they are
181included in the VFS structure for completeness.
182The xRandomness() function attempts to return nBytes bytes of good-quality
183randomness into zOut.
184The return value is the actual number of bytes of randomness obtained.
185The xSleep() method causes the calling thread to sleep for at least
186the number of microseconds given.
187The xCurrentTime() method returns a Julian Day Number for the current
188date and time as a floating point value.
189The xCurrentTimeInt64() method returns, as an integer, the Julian Day
190Number multiplied by 86400000 (the number of milliseconds in a 24-hour
191day).
192SQLite will use the xCurrentTimeInt64() method to get the current date
193and time if that method is available (if iVersion is 2 or greater and
194the function pointer is not NULL) and will fall back to xCurrentTime()
195if xCurrentTimeInt64() is unavailable.
196.Pp
197The xSetSystemCall(), xGetSystemCall(), and xNestSystemCall() interfaces
198are not used by the SQLite core.
199These optional interfaces are provided by some VFSes to facilitate
200testing of the VFS code.
201By overriding system calls with functions under its control, a test
202program can simulate faults and error conditions that would otherwise
203be difficult or impossible to induce.
204The set of system calls that can be overridden varies from one VFS
205to another, and from one version of the same VFS to the next.
206Applications that use these interfaces must be prepared for any or
207all of these interfaces to be NULL or for their behavior to change
208from one release to the next.
209Applications must not attempt to access any of these methods if the
210iVersion of the VFS is less than 3.
211.Sh IMPLEMENTATION NOTES
212These declarations were extracted from the
213interface documentation at line 1295.
214.Bd -literal
215typedef struct sqlite3_vfs sqlite3_vfs;
216typedef void (*sqlite3_syscall_ptr)(void);
217struct sqlite3_vfs {
218  int iVersion;            /* Structure version number (currently 3) */
219  int szOsFile;            /* Size of subclassed sqlite3_file */
220  int mxPathname;          /* Maximum file pathname length */
221  sqlite3_vfs *pNext;      /* Next registered VFS */
222  const char *zName;       /* Name of this virtual file system */
223  void *pAppData;          /* Pointer to application-specific data */
224  int (*xOpen)(sqlite3_vfs*, sqlite3_filename zName, sqlite3_file*,
225               int flags, int *pOutFlags);
226  int (*xDelete)(sqlite3_vfs*, const char *zName, int syncDir);
227  int (*xAccess)(sqlite3_vfs*, const char *zName, int flags, int *pResOut);
228  int (*xFullPathname)(sqlite3_vfs*, const char *zName, int nOut, char *zOut);
229  void *(*xDlOpen)(sqlite3_vfs*, const char *zFilename);
230  void (*xDlError)(sqlite3_vfs*, int nByte, char *zErrMsg);
231  void (*(*xDlSym)(sqlite3_vfs*,void*, const char *zSymbol))(void);
232  void (*xDlClose)(sqlite3_vfs*, void*);
233  int (*xRandomness)(sqlite3_vfs*, int nByte, char *zOut);
234  int (*xSleep)(sqlite3_vfs*, int microseconds);
235  int (*xCurrentTime)(sqlite3_vfs*, double*);
236  int (*xGetLastError)(sqlite3_vfs*, int, char *);
237  /*
238  ** The methods above are in version 1 of the sqlite_vfs object
239  ** definition.  Those that follow are added in version 2 or later
240  */
241  int (*xCurrentTimeInt64)(sqlite3_vfs*, sqlite3_int64*);
242  /*
243  ** The methods above are in versions 1 and 2 of the sqlite_vfs object.
244  ** Those below are for version 3 and greater.
245  */
246  int (*xSetSystemCall)(sqlite3_vfs*, const char *zName, sqlite3_syscall_ptr);
247  sqlite3_syscall_ptr (*xGetSystemCall)(sqlite3_vfs*, const char *zName);
248  const char *(*xNextSystemCall)(sqlite3_vfs*, const char *zName);
249  /*
250  ** The methods above are in versions 1 through 3 of the sqlite_vfs object.
251  ** New fields may be appended in future versions.  The iVersion
252  ** value will increment whenever this happens.
253  */
254};
255.Ed
256.Sh SEE ALSO
257.Xr sqlite3_file 3 ,
258.Xr sqlite3_io_methods 3 ,
259.Xr sqlite3_open 3 ,
260.Xr sqlite3_vfs_find 3 ,
261.Xr SQLITE_ACCESS_EXISTS 3 ,
262.Xr SQLITE_OK 3 ,
263.Xr SQLITE_OPEN_READONLY 3
264