Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(231)

Side by Side Diff: sql/vfs_wrapper.cc

Issue 2623083002: [sql] Move time-machine support from third_party/sqlite to sql/ (Closed)
Patch Set: disable TimeMachine test, sigh Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « sql/vfs_wrapper.h ('k') | third_party/sqlite/amalgamation/sqlite3.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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.
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_util.h"
15 #include "base/strings/stringprintf.h"
16
17 #if defined(OS_MACOSX) && !defined(OS_IOS)
18 #include <CoreFoundation/CoreFoundation.h>
19 #include <CoreServices/CoreServices.h>
20
21 #include "base/mac/mac_util.h"
22 #include "base/mac/scoped_cftyperef.h"
23 #endif
24
25 namespace sql {
26 namespace {
27
28 // https://www.sqlite.org/vfs.html - documents the overall VFS system.
29 //
30 // https://www.sqlite.org/c3ref/vfs.html - VFS methods. This code tucks the
31 // wrapped VFS pointer into the wrapper's pAppData pointer.
32 //
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.
37
38 sqlite3_vfs* GetWrappedVfs(sqlite3_vfs* wrapped_vfs) {
39 return static_cast<sqlite3_vfs*>(wrapped_vfs->pAppData);
40 }
41
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().
45 struct VfsFile {
46 const sqlite3_io_methods* methods;
47 sqlite3_file* wrapped_file;
48 };
49
50 VfsFile* AsVfsFile(sqlite3_file* wrapper_file) {
51 return reinterpret_cast<VfsFile*>(wrapper_file);
52 }
53
54 sqlite3_file* GetWrappedFile(sqlite3_file* wrapper_file) {
55 return AsVfsFile(wrapper_file)->wrapped_file;
56 }
57
58 int Close(sqlite3_file* sqlite_file)
59 {
60 VfsFile* file = AsVfsFile(sqlite_file);
61
62 int r = file->wrapped_file->pMethods->xClose(file->wrapped_file);
63 sqlite3_free(file->wrapped_file);
64 memset(file, 0, sizeof(*file));
65 return r;
66 }
67
68 int Read(sqlite3_file* sqlite_file, void* buf, int amt, sqlite3_int64 ofs)
69 {
70 sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file);
71 return wrapped_file->pMethods->xRead(wrapped_file, buf, amt, ofs);
72 }
73
74 int Write(sqlite3_file* sqlite_file, const void* buf, int amt,
75 sqlite3_int64 ofs)
76 {
77 sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file);
78 return wrapped_file->pMethods->xWrite(wrapped_file, buf, amt, ofs);
79 }
80
81 int Truncate(sqlite3_file* sqlite_file, sqlite3_int64 size)
82 {
83 sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file);
84 return wrapped_file->pMethods->xTruncate(wrapped_file, size);
85 }
86
87 int Sync(sqlite3_file* sqlite_file, int flags)
88 {
89 sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file);
90 return wrapped_file->pMethods->xSync(wrapped_file, flags);
91 }
92
93 int FileSize(sqlite3_file* sqlite_file, sqlite3_int64* size)
94 {
95 sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file);
96 return wrapped_file->pMethods->xFileSize(wrapped_file, size);
97 }
98
99 int Lock(sqlite3_file* sqlite_file, int file_lock)
100 {
101 sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file);
102 return wrapped_file->pMethods->xLock(wrapped_file, file_lock);
103 }
104
105 int Unlock(sqlite3_file* sqlite_file, int file_lock)
106 {
107 sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file);
108 return wrapped_file->pMethods->xUnlock(wrapped_file, file_lock);
109 }
110
111 int CheckReservedLock(sqlite3_file* sqlite_file, int* result)
112 {
113 sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file);
114 return wrapped_file->pMethods->xCheckReservedLock(wrapped_file, result);
115 }
116
117 int FileControl(sqlite3_file* sqlite_file, int op, void* arg)
118 {
119 sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file);
120 return wrapped_file->pMethods->xFileControl(wrapped_file, op, arg);
121 }
122
123 int SectorSize(sqlite3_file* sqlite_file)
124 {
125 sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file);
126 return wrapped_file->pMethods->xSectorSize(wrapped_file);
127 }
128
129 int DeviceCharacteristics(sqlite3_file* sqlite_file)
130 {
131 sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file);
132 return wrapped_file->pMethods->xDeviceCharacteristics(wrapped_file);
133 }
134
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);
140 }
141
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);
145 }
146
147 void ShmBarrier(sqlite3_file *sqlite_file) {
148 sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file);
149 wrapped_file->pMethods->xShmBarrier(wrapped_file);
150 }
151
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);
155 }
156
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);
160 }
161
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);
165 }
166
167 #if defined(OS_MACOSX) && !defined(OS_IOS)
168 // Helper to convert a POSIX path into a CoreFoundation path. Returns a bare
169 // ref which must be released.
170 CFURLRef CFURLRefForPath(const char* path){
Marijn Kruisselbrink 2017/01/23 19:20:21 nit: wouldn't it be safer if this returns a Scoped
Scott Hess - ex-Googler 2017/01/23 21:18:50 Done.
171 base::ScopedCFTypeRef<CFStringRef> urlString(
172 CFStringCreateWithFileSystemRepresentation(kCFAllocatorDefault, path));
173 return CFURLCreateWithFileSystemPath(kCFAllocatorDefault, urlString,
174 kCFURLPOSIXPathStyle, FALSE);
175 }
176 #endif
177
178 int Open(sqlite3_vfs* vfs, const char* file_name, sqlite3_file* id,
Marijn Kruisselbrink 2017/01/23 19:20:21 nit: why is the sqlite3_file parameter called "id"
Scott Hess - ex-Googler 2017/01/23 21:18:50 Probably historical reasons related to which exper
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 char* dash = rindex(file_name, '-');
Marijn Kruisselbrink 2017/01/23 19:20:21 nit: I think strrchr() is the more common/actually
Scott Hess - ex-Googler 2017/01/23 21:18:50 Done. You might catch me dropping bzero() here an
208 if (dash) {
209 std::string db_name(file_name, dash - file_name);
210 base::ScopedCFTypeRef<CFURLRef> db_url(CFURLRefForPath(db_name.c_str()));
211 if (CSBackupIsItemExcluded(db_url, NULL)) {
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(id);
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
300 int 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
305 int 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
310 int 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
317 void* DlOpen(sqlite3_vfs* vfs, const char* filename) {
318 sqlite3_vfs* wrapped_vfs = GetWrappedVfs(vfs);
319 return wrapped_vfs->xDlOpen(wrapped_vfs, filename);
320 }
321
322 void 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
327 void(*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
332 void DlClose(sqlite3_vfs* vfs, void* handle) {
333 sqlite3_vfs* wrapped_vfs = GetWrappedVfs(vfs);
334 wrapped_vfs->xDlClose(wrapped_vfs, handle);
335 }
336
337 int 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
342 int Sleep(sqlite3_vfs* vfs, int microseconds) {
343 sqlite3_vfs* wrapped_vfs = GetWrappedVfs(vfs);
344 return wrapped_vfs->xSleep(wrapped_vfs, microseconds);
345 }
346
347 int CurrentTime(sqlite3_vfs* vfs, double* now) {
348 sqlite3_vfs* wrapped_vfs = GetWrappedVfs(vfs);
349 return wrapped_vfs->xCurrentTime(wrapped_vfs, now);
350 }
351
352 int 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
357 int CurrentTimeInt64(sqlite3_vfs* vfs, sqlite3_int64* now) {
358 sqlite3_vfs* wrapped_vfs = GetWrappedVfs(vfs);
359 return wrapped_vfs->xCurrentTimeInt64(wrapped_vfs, now);
360 }
361
362 int 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
368 sqlite3_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
373 const 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
380 sqlite3_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));
Marijn Kruisselbrink 2017/01/23 19:20:21 nit: maybe be consistent with what zero you pass t
Scott Hess - ex-Googler 2017/01/23 21:18:51 Done. That's weird, I've had a decades-long habit
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 = NULL;
Marijn Kruisselbrink 2017/01/23 19:20:21 nullptr
Scott Hess - ex-Googler 2017/01/23 21:18:50 Done.
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 NULL for |xCurrentTimeInt64|.
431 wrapper_vfs->xCurrentTimeInt64 =
432 (wrapped_vfs->xCurrentTimeInt64 ? &CurrentTimeInt64 : NULL);
Marijn Kruisselbrink 2017/01/23 19:20:21 nullptr
Scott Hess - ex-Googler 2017/01/23 21:18:50 Done.
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
OLDNEW
« no previous file with comments | « sql/vfs_wrapper.h ('k') | third_party/sqlite/amalgamation/sqlite3.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698