blob: c522be8819283448e69a2c504f9377ed1a710a1b [file] [log] [blame]
shess5f2c3442017-01-24 02:15:101// 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.
4
5#include "sql/vfs_wrapper.h"
6
7#include <algorithm>
8#include <string>
9#include <vector>
10
11#include "base/debug/leak_annotations.h"
12#include "base/logging.h"
13#include "base/memory/ptr_util.h"
14#include "base/strings/string_piece.h"
15
16#if defined(OS_MACOSX) && !defined(OS_IOS)
17#include <CoreFoundation/CoreFoundation.h>
18#include <CoreServices/CoreServices.h>
19
20#include "base/mac/mac_util.h"
21#include "base/mac/scoped_cftyperef.h"
22#endif
23
24namespace sql {
25namespace {
26
27// https://www.sqlite.org/vfs.html - documents the overall VFS system.
28//
29// https://www.sqlite.org/c3ref/vfs.html - VFS methods. This code tucks the
30// wrapped VFS pointer into the wrapper's pAppData pointer.
31//
32// https://www.sqlite.org/c3ref/file.html - instance of an open file. This code
33// allocates a VfsFile for this, which contains a pointer to the wrapped file.
34// Idiomatic SQLite would take the wrapped VFS szOsFile and increase it to store
35// additional data as a prefix.
36
37sqlite3_vfs* GetWrappedVfs(sqlite3_vfs* wrapped_vfs) {
38 return static_cast<sqlite3_vfs*>(wrapped_vfs->pAppData);
39}
40
41// NOTE(shess): This structure is allocated by SQLite using malloc. Do not add
42// C++ objects, they will not be correctly constructed and destructed. Instead,
43// manually manage a pointer to a C++ object in Open() and Close().
44struct VfsFile {
45 const sqlite3_io_methods* methods;
46 sqlite3_file* wrapped_file;
47};
48
49VfsFile* AsVfsFile(sqlite3_file* wrapper_file) {
50 return reinterpret_cast<VfsFile*>(wrapper_file);
51}
52
53sqlite3_file* GetWrappedFile(sqlite3_file* wrapper_file) {
54 return AsVfsFile(wrapper_file)->wrapped_file;
55}
56
57int Close(sqlite3_file* sqlite_file)
58{
59 VfsFile* file = AsVfsFile(sqlite_file);
60
61 int r = file->wrapped_file->pMethods->xClose(file->wrapped_file);
62 sqlite3_free(file->wrapped_file);
63 memset(file, '\0', sizeof(*file));
64 return r;
65}
66
67int Read(sqlite3_file* sqlite_file, void* buf, int amt, sqlite3_int64 ofs)
68{
69 sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file);
70 return wrapped_file->pMethods->xRead(wrapped_file, buf, amt, ofs);
71}
72
73int Write(sqlite3_file* sqlite_file, const void* buf, int amt,
74 sqlite3_int64 ofs)
75{
76 sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file);
77 return wrapped_file->pMethods->xWrite(wrapped_file, buf, amt, ofs);
78}
79
80int Truncate(sqlite3_file* sqlite_file, sqlite3_int64 size)
81{
82 sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file);
83 return wrapped_file->pMethods->xTruncate(wrapped_file, size);
84}
85
86int Sync(sqlite3_file* sqlite_file, int flags)
87{
88 sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file);
89 return wrapped_file->pMethods->xSync(wrapped_file, flags);
90}
91
92int FileSize(sqlite3_file* sqlite_file, sqlite3_int64* size)
93{
94 sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file);
95 return wrapped_file->pMethods->xFileSize(wrapped_file, size);
96}
97
98int Lock(sqlite3_file* sqlite_file, int file_lock)
99{
100 sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file);
101 return wrapped_file->pMethods->xLock(wrapped_file, file_lock);
102}
103
104int Unlock(sqlite3_file* sqlite_file, int file_lock)
105{
106 sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file);
107 return wrapped_file->pMethods->xUnlock(wrapped_file, file_lock);
108}
109
110int CheckReservedLock(sqlite3_file* sqlite_file, int* result)
111{
112 sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file);
113 return wrapped_file->pMethods->xCheckReservedLock(wrapped_file, result);
114}
115
116int FileControl(sqlite3_file* sqlite_file, int op, void* arg)
117{
118 sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file);
119 return wrapped_file->pMethods->xFileControl(wrapped_file, op, arg);
120}
121
122int SectorSize(sqlite3_file* sqlite_file)
123{
124 sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file);
125 return wrapped_file->pMethods->xSectorSize(wrapped_file);
126}
127
128int DeviceCharacteristics(sqlite3_file* sqlite_file)
129{
130 sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file);
131 return wrapped_file->pMethods->xDeviceCharacteristics(wrapped_file);
132}
133
134int ShmMap(sqlite3_file *sqlite_file, int region, int size,
135 int extend, void volatile **pp) {
136 sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file);
137 return wrapped_file->pMethods->xShmMap(
138 wrapped_file, region, size, extend, pp);
139}
140
141int ShmLock(sqlite3_file *sqlite_file, int ofst, int n, int flags) {
142 sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file);
143 return wrapped_file->pMethods->xShmLock(wrapped_file, ofst, n, flags);
144}
145
146void ShmBarrier(sqlite3_file *sqlite_file) {
147 sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file);
148 wrapped_file->pMethods->xShmBarrier(wrapped_file);
149}
150
151int ShmUnmap(sqlite3_file *sqlite_file, int del) {
152 sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file);
153 return wrapped_file->pMethods->xShmUnmap(wrapped_file, del);
154}
155
156int Fetch(sqlite3_file *sqlite_file, sqlite3_int64 off, int amt, void **pp) {
157 sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file);
158 return wrapped_file->pMethods->xFetch(wrapped_file, off, amt, pp);
159}
160
161int Unfetch(sqlite3_file *sqlite_file, sqlite3_int64 off, void *p) {
162 sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file);
163 return wrapped_file->pMethods->xUnfetch(wrapped_file, off, p);
164}
165
166#if defined(OS_MACOSX) && !defined(OS_IOS)
167// Helper to convert a POSIX path into a CoreFoundation path.
168base::ScopedCFTypeRef<CFURLRef> CFURLRefForPath(const char* path){
169 base::ScopedCFTypeRef<CFStringRef> urlString(
170 CFStringCreateWithFileSystemRepresentation(kCFAllocatorDefault, path));
171 base::ScopedCFTypeRef<CFURLRef> url(
172 CFURLCreateWithFileSystemPath(kCFAllocatorDefault, urlString,
173 kCFURLPOSIXPathStyle, FALSE));
174 return url;
175}
176#endif
177
178int Open(sqlite3_vfs* vfs, const char* file_name, sqlite3_file* wrapper_file,
179 int desired_flags, int* used_flags) {
180 sqlite3_vfs* wrapped_vfs = GetWrappedVfs(vfs);
181
182 sqlite3_file* wrapped_file = static_cast<sqlite3_file*>(
183 sqlite3_malloc(wrapped_vfs->szOsFile));
184 if (!wrapped_file)
185 return SQLITE_NOMEM;
186
187 // NOTE(shess): SQLite's unixOpen() makes assumptions about the structure of
188 // |file_name|. Do not pass a local copy, here, only the passed-in value.
189 int rc = wrapped_vfs->xOpen(wrapped_vfs,
190 file_name, wrapped_file,
191 desired_flags, used_flags);
192 if (rc != SQLITE_OK) {
193 sqlite3_free(wrapped_file);
194 return rc;
195 }
196 // NOTE(shess): Any early exit from here needs to call xClose() on
197 // |wrapped_file|.
198
199#if defined(OS_MACOSX) && !defined(OS_IOS)
200 // When opening journal files, propagate time-machine exclusion from db.
201 static int kJournalFlags =
202 SQLITE_OPEN_MAIN_JOURNAL | SQLITE_OPEN_TEMP_JOURNAL |
203 SQLITE_OPEN_SUBJOURNAL | SQLITE_OPEN_MASTER_JOURNAL;
204 if (file_name && (desired_flags & kJournalFlags)) {
205 // https://www.sqlite.org/c3ref/vfs.html indicates that the journal path
206 // will have a "-suffix".
207 size_t dash_index = base::StringPiece(file_name).rfind('-');
208 if (dash_index != base::StringPiece::npos) {
209 std::string db_name(file_name, dash_index);
210 base::ScopedCFTypeRef<CFURLRef> db_url(CFURLRefForPath(db_name.c_str()));
211 if (CSBackupIsItemExcluded(db_url, nullptr)) {
212 base::ScopedCFTypeRef<CFURLRef> journal_url(CFURLRefForPath(file_name));
213 CSBackupSetItemExcluded(journal_url, TRUE, FALSE);
214 }
215 }
216 }
217#endif
218
219 // The wrapper instances must support a specific |iVersion|, but there is no
220 // explicit guarantee that the wrapped VFS will always vend instances with the
221 // same |iVersion| (though I believe this is always the case in practice).
222 // Vend a distinct set of IO methods for each version supported.
223 //
224 // |iVersion| determines what methods SQLite may call on the instance. Having
225 // the methods which can't be proxied return an error may cause SQLite to
226 // operate differently than if it didn't call those methods at all. Another
227 // solution would be to fail if the wrapped file does not have the expected
228 // version, which may cause problems on platforms which use the system SQLite
229 // (iOS and some Linux distros).
230 VfsFile* file = AsVfsFile(wrapper_file);
231 file->wrapped_file = wrapped_file;
232 if (wrapped_file->pMethods->iVersion == 1) {
233 static const sqlite3_io_methods io_methods = {
234 1,
235 Close,
236 Read,
237 Write,
238 Truncate,
239 Sync,
240 FileSize,
241 Lock,
242 Unlock,
243 CheckReservedLock,
244 FileControl,
245 SectorSize,
246 DeviceCharacteristics,
247 };
248 file->methods = &io_methods;
249 } else if (wrapped_file->pMethods->iVersion == 2) {
250 static const sqlite3_io_methods io_methods = {
251 2,
252 Close,
253 Read,
254 Write,
255 Truncate,
256 Sync,
257 FileSize,
258 Lock,
259 Unlock,
260 CheckReservedLock,
261 FileControl,
262 SectorSize,
263 DeviceCharacteristics,
264 // Methods above are valid for version 1.
265 ShmMap,
266 ShmLock,
267 ShmBarrier,
268 ShmUnmap,
269 };
270 file->methods = &io_methods;
271 } else {
272 static const sqlite3_io_methods io_methods = {
273 3,
274 Close,
275 Read,
276 Write,
277 Truncate,
278 Sync,
279 FileSize,
280 Lock,
281 Unlock,
282 CheckReservedLock,
283 FileControl,
284 SectorSize,
285 DeviceCharacteristics,
286 // Methods above are valid for version 1.
287 ShmMap,
288 ShmLock,
289 ShmBarrier,
290 ShmUnmap,
291 // Methods above are valid for version 2.
292 Fetch,
293 Unfetch,
294 };
295 file->methods = &io_methods;
296 }
297 return SQLITE_OK;
298}
299
300int Delete(sqlite3_vfs* vfs, const char* file_name, int sync_dir) {
301 sqlite3_vfs* wrapped_vfs = GetWrappedVfs(vfs);
302 return wrapped_vfs->xDelete(wrapped_vfs, file_name, sync_dir);
303}
304
305int Access(sqlite3_vfs* vfs, const char* file_name, int flag, int* res) {
306 sqlite3_vfs* wrapped_vfs = GetWrappedVfs(vfs);
307 return wrapped_vfs->xAccess(wrapped_vfs, file_name, flag, res);
308}
309
310int FullPathname(sqlite3_vfs* vfs, const char* relative_path,
311 int buf_size, char* absolute_path) {
312 sqlite3_vfs* wrapped_vfs = GetWrappedVfs(vfs);
313 return wrapped_vfs->xFullPathname(
314 wrapped_vfs, relative_path, buf_size, absolute_path);
315}
316
317void* DlOpen(sqlite3_vfs* vfs, const char* filename) {
318 sqlite3_vfs* wrapped_vfs = GetWrappedVfs(vfs);
319 return wrapped_vfs->xDlOpen(wrapped_vfs, filename);
320}
321
322void DlError(sqlite3_vfs* vfs, int buf_size, char* error_buffer) {
323 sqlite3_vfs* wrapped_vfs = GetWrappedVfs(vfs);
324 wrapped_vfs->xDlError(wrapped_vfs, buf_size, error_buffer);
325}
326
327void(*DlSym(sqlite3_vfs* vfs, void* handle, const char* sym))(void) {
328 sqlite3_vfs* wrapped_vfs = GetWrappedVfs(vfs);
329 return wrapped_vfs->xDlSym(wrapped_vfs, handle, sym);
330}
331
332void DlClose(sqlite3_vfs* vfs, void* handle) {
333 sqlite3_vfs* wrapped_vfs = GetWrappedVfs(vfs);
334 wrapped_vfs->xDlClose(wrapped_vfs, handle);
335}
336
337int Randomness(sqlite3_vfs* vfs, int buf_size, char* buffer) {
338 sqlite3_vfs* wrapped_vfs = GetWrappedVfs(vfs);
339 return wrapped_vfs->xRandomness(wrapped_vfs, buf_size, buffer);
340}
341
342int Sleep(sqlite3_vfs* vfs, int microseconds) {
343 sqlite3_vfs* wrapped_vfs = GetWrappedVfs(vfs);
344 return wrapped_vfs->xSleep(wrapped_vfs, microseconds);
345}
346
347int CurrentTime(sqlite3_vfs* vfs, double* now) {
348 sqlite3_vfs* wrapped_vfs = GetWrappedVfs(vfs);
349 return wrapped_vfs->xCurrentTime(wrapped_vfs, now);
350}
351
352int GetLastError(sqlite3_vfs* vfs, int e, char* s) {
353 sqlite3_vfs* wrapped_vfs = GetWrappedVfs(vfs);
354 return wrapped_vfs->xGetLastError(wrapped_vfs, e, s);
355}
356
357int CurrentTimeInt64(sqlite3_vfs* vfs, sqlite3_int64* now) {
358 sqlite3_vfs* wrapped_vfs = GetWrappedVfs(vfs);
359 return wrapped_vfs->xCurrentTimeInt64(wrapped_vfs, now);
360}
361
362int SetSystemCall(sqlite3_vfs* vfs, const char* name,
363 sqlite3_syscall_ptr func) {
364 sqlite3_vfs* wrapped_vfs = GetWrappedVfs(vfs);
365 return wrapped_vfs->xSetSystemCall(wrapped_vfs, name, func);
366}
367
368sqlite3_syscall_ptr GetSystemCall(sqlite3_vfs* vfs, const char* name) {
369 sqlite3_vfs* wrapped_vfs = GetWrappedVfs(vfs);
370 return wrapped_vfs->xGetSystemCall(wrapped_vfs, name);
371}
372
373const char* NextSystemCall(sqlite3_vfs* vfs, const char* name) {
374 sqlite3_vfs* wrapped_vfs = GetWrappedVfs(vfs);
375 return wrapped_vfs->xNextSystemCall(wrapped_vfs, name);
376}
377
378} // namespace
379
380sqlite3_vfs* VFSWrapper() {
381 const char* kVFSName = "VFSWrapper";
382
383 // Return existing version if already registered.
384 {
385 sqlite3_vfs* vfs = sqlite3_vfs_find(kVFSName);
386 if (vfs != nullptr)
387 return vfs;
388 }
389
390 // Get the default VFS for this platform. If no default VFS, give up.
391 sqlite3_vfs* wrapped_vfs = sqlite3_vfs_find(nullptr);
392 if (!wrapped_vfs)
393 return nullptr;
394
395 std::unique_ptr<sqlite3_vfs, std::function<void(sqlite3_vfs*)>> wrapper_vfs(
396 static_cast<sqlite3_vfs*>(sqlite3_malloc(sizeof(sqlite3_vfs))),
397 [](sqlite3_vfs* v) {
398 sqlite3_free(v);
399 });
400 memset(wrapper_vfs.get(), '\0', sizeof(sqlite3_vfs));
401
402 // VFS implementations should always work with a SQLite that only knows about
403 // earlier versions.
404 wrapper_vfs->iVersion = std::min(wrapped_vfs->iVersion, 3);
405
406 // Caller of xOpen() allocates this much space.
407 wrapper_vfs->szOsFile = sizeof(VfsFile);
408
409 wrapper_vfs->mxPathname = wrapped_vfs->mxPathname;
410 wrapper_vfs->pNext = nullptr;
411 wrapper_vfs->zName = kVFSName;
412
413 // Keep a reference to the wrapped vfs for use in methods.
414 wrapper_vfs->pAppData = wrapped_vfs;
415
416 // VFS methods.
417 wrapper_vfs->xOpen = &Open;
418 wrapper_vfs->xDelete = &Delete;
419 wrapper_vfs->xAccess = &Access;
420 wrapper_vfs->xFullPathname = &FullPathname;
421 wrapper_vfs->xDlOpen = &DlOpen;
422 wrapper_vfs->xDlError = &DlError;
423 wrapper_vfs->xDlSym = &DlSym;
424 wrapper_vfs->xDlClose = &DlClose;
425 wrapper_vfs->xRandomness = &Randomness;
426 wrapper_vfs->xSleep = &Sleep;
427 wrapper_vfs->xCurrentTime = &CurrentTime;
428 wrapper_vfs->xGetLastError = &GetLastError;
429 // The methods above are in version 1 of sqlite_vfs.
430 // There were VFS implementations with nullptr for |xCurrentTimeInt64|.
431 wrapper_vfs->xCurrentTimeInt64 =
432 (wrapped_vfs->xCurrentTimeInt64 ? &CurrentTimeInt64 : nullptr);
433 // The methods above are in version 2 of sqlite_vfs.
434 wrapper_vfs->xSetSystemCall = &SetSystemCall;
435 wrapper_vfs->xGetSystemCall = &GetSystemCall;
436 wrapper_vfs->xNextSystemCall = &NextSystemCall;
437 // The methods above are in version 3 of sqlite_vfs.
438
439 if (SQLITE_OK == sqlite3_vfs_register(wrapper_vfs.get(), 0)) {
440 ANNOTATE_LEAKING_OBJECT_PTR(wrapper_vfs.get());
441 wrapper_vfs.release();
442 }
443
444 return sqlite3_vfs_find(kVFSName);
445}
446
447} // namespace sql