1 // Copyright (c) 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "sql/vfs_wrapper.h"
11 #include "base/debug/leak_annotations.h"
12 #include "base/logging.h"
13 #include "base/memory/ptr_util.h"
14 #include "base/metrics/histogram_macros.h"
15 #include "base/strings/string_piece.h"
17 #if defined(OS_MACOSX) && !defined(OS_IOS)
18 #include <CoreFoundation/CoreFoundation.h>
19 #include <CoreServices/CoreServices.h>
21 #include "base/mac/mac_util.h"
22 #include "base/mac/scoped_cftyperef.h"
28 // https://www.sqlite.org/vfs.html - documents the overall VFS system.
30 // https://www.sqlite.org/c3ref/vfs.html - VFS methods. This code tucks the
31 // wrapped VFS pointer into the wrapper's pAppData pointer.
33 // https://www.sqlite.org/c3ref/file.html - instance of an open file. This code
34 // allocates a VfsFile for this, which contains a pointer to the wrapped file.
35 // Idiomatic SQLite would take the wrapped VFS szOsFile and increase it to store
36 // additional data as a prefix.
38 sqlite3_vfs* GetWrappedVfs(sqlite3_vfs* wrapped_vfs) {
39 return static_cast<sqlite3_vfs*>(wrapped_vfs->pAppData);
42 // NOTE(shess): This structure is allocated by SQLite using malloc. Do not add
43 // C++ objects, they will not be correctly constructed and destructed. Instead,
44 // manually manage a pointer to a C++ object in Open() and Close().
46 const sqlite3_io_methods* methods;
47 sqlite3_file* wrapped_file;
50 VfsFile* AsVfsFile(sqlite3_file* wrapper_file) {
51 return reinterpret_cast<VfsFile*>(wrapper_file);
54 sqlite3_file* GetWrappedFile(sqlite3_file* wrapper_file) {
55 return AsVfsFile(wrapper_file)->wrapped_file;
58 int Close(sqlite3_file* sqlite_file)
60 VfsFile* file = AsVfsFile(sqlite_file);
62 int r = file->wrapped_file->pMethods->xClose(file->wrapped_file);
63 sqlite3_free(file->wrapped_file);
64 memset(file, '\0', sizeof(*file));
68 int Read(sqlite3_file* sqlite_file, void* buf, int amt, sqlite3_int64 ofs)
70 sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file);
71 return wrapped_file->pMethods->xRead(wrapped_file, buf, amt, ofs);
74 int Write(sqlite3_file* sqlite_file, const void* buf, int amt,
77 sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file);
78 return wrapped_file->pMethods->xWrite(wrapped_file, buf, amt, ofs);
81 int Truncate(sqlite3_file* sqlite_file, sqlite3_int64 size)
83 sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file);
84 return wrapped_file->pMethods->xTruncate(wrapped_file, size);
87 int Sync(sqlite3_file* sqlite_file, int flags)
89 sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file);
90 return wrapped_file->pMethods->xSync(wrapped_file, flags);
93 int FileSize(sqlite3_file* sqlite_file, sqlite3_int64* size)
95 sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file);
96 return wrapped_file->pMethods->xFileSize(wrapped_file, size);
99 int Lock(sqlite3_file* sqlite_file, int file_lock)
101 sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file);
102 return wrapped_file->pMethods->xLock(wrapped_file, file_lock);
105 int Unlock(sqlite3_file* sqlite_file, int file_lock)
107 sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file);
108 return wrapped_file->pMethods->xUnlock(wrapped_file, file_lock);
111 int CheckReservedLock(sqlite3_file* sqlite_file, int* result)
113 sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file);
114 return wrapped_file->pMethods->xCheckReservedLock(wrapped_file, result);
117 int FileControl(sqlite3_file* sqlite_file, int op, void* arg)
119 sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file);
120 return wrapped_file->pMethods->xFileControl(wrapped_file, op, arg);
123 int SectorSize(sqlite3_file* sqlite_file)
125 sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file);
126 return wrapped_file->pMethods->xSectorSize(wrapped_file);
129 int DeviceCharacteristics(sqlite3_file* sqlite_file)
131 sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file);
132 return wrapped_file->pMethods->xDeviceCharacteristics(wrapped_file);
135 int ShmMap(sqlite3_file *sqlite_file, int region, int size,
136 int extend, void volatile **pp) {
137 sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file);
138 return wrapped_file->pMethods->xShmMap(
139 wrapped_file, region, size, extend, pp);
142 int ShmLock(sqlite3_file *sqlite_file, int ofst, int n, int flags) {
143 sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file);
144 return wrapped_file->pMethods->xShmLock(wrapped_file, ofst, n, flags);
147 void ShmBarrier(sqlite3_file *sqlite_file) {
148 sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file);
149 wrapped_file->pMethods->xShmBarrier(wrapped_file);
152 int ShmUnmap(sqlite3_file *sqlite_file, int del) {
153 sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file);
154 return wrapped_file->pMethods->xShmUnmap(wrapped_file, del);
157 int Fetch(sqlite3_file *sqlite_file, sqlite3_int64 off, int amt, void **pp) {
158 sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file);
159 return wrapped_file->pMethods->xFetch(wrapped_file, off, amt, pp);
162 int Unfetch(sqlite3_file *sqlite_file, sqlite3_int64 off, void *p) {
163 sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file);
164 return wrapped_file->pMethods->xUnfetch(wrapped_file, off, p);
167 #if defined(OS_MACOSX) && !defined(OS_IOS)
168 // Helper to convert a POSIX path into a CoreFoundation path.
169 base::ScopedCFTypeRef<CFURLRef> CFURLRefForPath(const char* path){
170 base::ScopedCFTypeRef<CFStringRef> urlString(
171 CFStringCreateWithFileSystemRepresentation(kCFAllocatorDefault, path));
172 base::ScopedCFTypeRef<CFURLRef> url(
173 CFURLCreateWithFileSystemPath(kCFAllocatorDefault, urlString,
174 kCFURLPOSIXPathStyle, FALSE));
179 int Open(sqlite3_vfs* vfs, const char* file_name, sqlite3_file* wrapper_file,
180 int desired_flags, int* used_flags) {
181 sqlite3_vfs* wrapped_vfs = GetWrappedVfs(vfs);
183 sqlite3_file* wrapped_file = static_cast<sqlite3_file*>(
184 sqlite3_malloc(wrapped_vfs->szOsFile));
188 // NOTE(shess): SQLite's unixOpen() makes assumptions about the structure of
189 // |file_name|. Do not pass a local copy, here, only the passed-in value.
190 int rc = wrapped_vfs->xOpen(wrapped_vfs,
191 file_name, wrapped_file,
192 desired_flags, used_flags);
193 if (rc != SQLITE_OK) {
194 sqlite3_free(wrapped_file);
197 // NOTE(shess): Any early exit from here needs to call xClose() on
200 #if defined(OS_MACOSX) && !defined(OS_IOS)
201 // When opening journal files, propagate time-machine exclusion from db.
202 static int kJournalFlags =
203 SQLITE_OPEN_MAIN_JOURNAL | SQLITE_OPEN_TEMP_JOURNAL |
204 SQLITE_OPEN_SUBJOURNAL | SQLITE_OPEN_MASTER_JOURNAL;
205 if (file_name && (desired_flags & kJournalFlags)) {
206 // https://www.sqlite.org/c3ref/vfs.html indicates that the journal path
207 // will have a "-suffix".
208 size_t dash_index = base::StringPiece(file_name).rfind('-');
209 if (dash_index != base::StringPiece::npos) {
210 std::string db_name(file_name, dash_index);
211 base::ScopedCFTypeRef<CFURLRef> db_url(CFURLRefForPath(db_name.c_str()));
212 if (CSBackupIsItemExcluded(db_url, nullptr)) {
213 base::ScopedCFTypeRef<CFURLRef> journal_url(CFURLRefForPath(file_name));
214 CSBackupSetItemExcluded(journal_url, TRUE, FALSE);
220 // |iVersion| determines what methods SQLite may call on the instance.
221 // Having the methods which can't be proxied return an error may cause SQLite
222 // to operate differently than if it didn't call those methods at all. To be
223 // on the safe side, the wrapper sqlite3_io_methods version perfectly matches
224 // the version of the wrapped files.
226 // At a first glance, it might be tempting to simplify the code by
227 // restricting wrapping support to VFS version 3. However, this would fail
228 // on Fuchsia and might fail on Mac.
230 // On Mac, SQLite built with SQLITE_ENABLE_LOCKING_STYLE ends up using a VFS
231 // that dynamically dispatches between a few variants of sqlite3_io_methods,
232 // based on whether the opened database is on a local or on a remote (AFS,
233 // NFS) filesystem. Some variants return a VFS version 1 structure.
235 // Fuchsia doesn't implement POSIX locking, so it always uses dot-style
236 // locking, which returns VFS version 1 files.
237 VfsFile* file = AsVfsFile(wrapper_file);
238 file->wrapped_file = wrapped_file;
239 if (wrapped_file->pMethods->iVersion == 1) {
240 static const sqlite3_io_methods io_methods = {
253 DeviceCharacteristics,
255 file->methods = &io_methods;
256 } else if (wrapped_file->pMethods->iVersion == 2) {
257 static const sqlite3_io_methods io_methods = {
270 DeviceCharacteristics,
271 // Methods above are valid for version 1.
277 file->methods = &io_methods;
279 static const sqlite3_io_methods io_methods = {
292 DeviceCharacteristics,
293 // Methods above are valid for version 1.
298 // Methods above are valid for version 2.
302 file->methods = &io_methods;
307 int Delete(sqlite3_vfs* vfs, const char* file_name, int sync_dir) {
308 sqlite3_vfs* wrapped_vfs = GetWrappedVfs(vfs);
309 return wrapped_vfs->xDelete(wrapped_vfs, file_name, sync_dir);
312 int Access(sqlite3_vfs* vfs, const char* file_name, int flag, int* res) {
313 sqlite3_vfs* wrapped_vfs = GetWrappedVfs(vfs);
314 return wrapped_vfs->xAccess(wrapped_vfs, file_name, flag, res);
317 int FullPathname(sqlite3_vfs* vfs, const char* relative_path,
318 int buf_size, char* absolute_path) {
319 sqlite3_vfs* wrapped_vfs = GetWrappedVfs(vfs);
320 return wrapped_vfs->xFullPathname(
321 wrapped_vfs, relative_path, buf_size, absolute_path);
324 void* DlOpen(sqlite3_vfs* vfs, const char* filename) {
325 sqlite3_vfs* wrapped_vfs = GetWrappedVfs(vfs);
326 return wrapped_vfs->xDlOpen(wrapped_vfs, filename);
329 void DlError(sqlite3_vfs* vfs, int buf_size, char* error_buffer) {
330 sqlite3_vfs* wrapped_vfs = GetWrappedVfs(vfs);
331 wrapped_vfs->xDlError(wrapped_vfs, buf_size, error_buffer);
334 void(*DlSym(sqlite3_vfs* vfs, void* handle, const char* sym))(void) {
335 sqlite3_vfs* wrapped_vfs = GetWrappedVfs(vfs);
336 return wrapped_vfs->xDlSym(wrapped_vfs, handle, sym);
339 void DlClose(sqlite3_vfs* vfs, void* handle) {
340 sqlite3_vfs* wrapped_vfs = GetWrappedVfs(vfs);
341 wrapped_vfs->xDlClose(wrapped_vfs, handle);
344 int Randomness(sqlite3_vfs* vfs, int buf_size, char* buffer) {
345 sqlite3_vfs* wrapped_vfs = GetWrappedVfs(vfs);
346 return wrapped_vfs->xRandomness(wrapped_vfs, buf_size, buffer);
349 int Sleep(sqlite3_vfs* vfs, int microseconds) {
350 sqlite3_vfs* wrapped_vfs = GetWrappedVfs(vfs);
351 return wrapped_vfs->xSleep(wrapped_vfs, microseconds);
354 int CurrentTime(sqlite3_vfs* vfs, double* now) {
355 sqlite3_vfs* wrapped_vfs = GetWrappedVfs(vfs);
356 return wrapped_vfs->xCurrentTime(wrapped_vfs, now);
359 int GetLastError(sqlite3_vfs* vfs, int e, char* s) {
360 sqlite3_vfs* wrapped_vfs = GetWrappedVfs(vfs);
361 return wrapped_vfs->xGetLastError(wrapped_vfs, e, s);
364 int CurrentTimeInt64(sqlite3_vfs* vfs, sqlite3_int64* now) {
365 sqlite3_vfs* wrapped_vfs = GetWrappedVfs(vfs);
366 return wrapped_vfs->xCurrentTimeInt64(wrapped_vfs, now);
369 int SetSystemCall(sqlite3_vfs* vfs, const char* name,
370 sqlite3_syscall_ptr func) {
371 sqlite3_vfs* wrapped_vfs = GetWrappedVfs(vfs);
372 return wrapped_vfs->xSetSystemCall(wrapped_vfs, name, func);
375 sqlite3_syscall_ptr GetSystemCall(sqlite3_vfs* vfs, const char* name) {
376 sqlite3_vfs* wrapped_vfs = GetWrappedVfs(vfs);
377 return wrapped_vfs->xGetSystemCall(wrapped_vfs, name);
380 const char* NextSystemCall(sqlite3_vfs* vfs, const char* name) {
381 sqlite3_vfs* wrapped_vfs = GetWrappedVfs(vfs);
382 return wrapped_vfs->xNextSystemCall(wrapped_vfs, name);
387 sqlite3_vfs* VFSWrapper() {
388 const char* kVFSName = "VFSWrapper";
390 // Return existing version if already registered.
392 sqlite3_vfs* vfs = sqlite3_vfs_find(kVFSName);
397 // Get the default VFS for this platform. If no default VFS, give up.
398 sqlite3_vfs* wrapped_vfs = sqlite3_vfs_find(nullptr);
402 std::unique_ptr<sqlite3_vfs, std::function<void(sqlite3_vfs*)>> wrapper_vfs(
403 static_cast<sqlite3_vfs*>(sqlite3_malloc(sizeof(sqlite3_vfs))),
407 memset(wrapper_vfs.get(), '\0', sizeof(sqlite3_vfs));
409 // VFS implementations should always work with a SQLite that only knows about
411 wrapper_vfs->iVersion = std::min(wrapped_vfs->iVersion, 3);
413 // Caller of xOpen() allocates this much space.
414 wrapper_vfs->szOsFile = sizeof(VfsFile);
416 wrapper_vfs->mxPathname = wrapped_vfs->mxPathname;
417 wrapper_vfs->pNext = nullptr;
418 wrapper_vfs->zName = kVFSName;
420 // Keep a reference to the wrapped vfs for use in methods.
421 wrapper_vfs->pAppData = wrapped_vfs;
424 wrapper_vfs->xOpen = &Open;
425 wrapper_vfs->xDelete = &Delete;
426 wrapper_vfs->xAccess = &Access;
427 wrapper_vfs->xFullPathname = &FullPathname;
428 wrapper_vfs->xDlOpen = &DlOpen;
429 wrapper_vfs->xDlError = &DlError;
430 wrapper_vfs->xDlSym = &DlSym;
431 wrapper_vfs->xDlClose = &DlClose;
432 wrapper_vfs->xRandomness = &Randomness;
433 wrapper_vfs->xSleep = &Sleep;
434 // |xCurrentTime| is null when SQLite is built with SQLITE_OMIT_DEPRECATED.
435 wrapper_vfs->xCurrentTime =
436 (wrapped_vfs->xCurrentTime ? &CurrentTime : nullptr);
437 wrapper_vfs->xGetLastError = &GetLastError;
438 // The methods above are in version 1 of sqlite_vfs.
439 DCHECK(wrapped_vfs->xCurrentTimeInt64);
440 wrapper_vfs->xCurrentTimeInt64 = &CurrentTimeInt64;
441 // The methods above are in version 2 of sqlite_vfs.
442 DCHECK(wrapped_vfs->xSetSystemCall);
443 wrapper_vfs->xSetSystemCall = &SetSystemCall;
444 DCHECK(wrapped_vfs->xGetSystemCall);
445 wrapper_vfs->xGetSystemCall = &GetSystemCall;
446 DCHECK(wrapped_vfs->xNextSystemCall);
447 wrapper_vfs->xNextSystemCall = &NextSystemCall;
448 // The methods above are in version 3 of sqlite_vfs.
450 if (SQLITE_OK == sqlite3_vfs_register(wrapper_vfs.get(), 0)) {
451 ANNOTATE_LEAKING_OBJECT_PTR(wrapper_vfs.get());
452 wrapper_vfs.release();
455 return sqlite3_vfs_find(kVFSName);