shess | 5f2c344 | 2017-01-24 02:15:10 | [diff] [blame] | 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" |
Victor Costan | 7e74dce | 2019-01-28 20:15:25 | [diff] [blame] | 12 | #include "base/files/file_path.h" |
shess | 5f2c344 | 2017-01-24 02:15:10 | [diff] [blame] | 13 | #include "base/logging.h" |
| 14 | #include "base/memory/ptr_util.h" |
shess | 3998fbc | 2017-03-15 16:18:31 | [diff] [blame] | 15 | #include "base/metrics/histogram_macros.h" |
shess | 5f2c344 | 2017-01-24 02:15:10 | [diff] [blame] | 16 | #include "base/strings/string_piece.h" |
Sergey Ulanov | 7de7e9f | 2019-03-09 00:41:02 | [diff] [blame] | 17 | #include "build/build_config.h" |
shess | 5f2c344 | 2017-01-24 02:15:10 | [diff] [blame] | 18 | |
| 19 | #if defined(OS_MACOSX) && !defined(OS_IOS) |
shess | 5f2c344 | 2017-01-24 02:15:10 | [diff] [blame] | 20 | #include "base/mac/mac_util.h" |
shess | 5f2c344 | 2017-01-24 02:15:10 | [diff] [blame] | 21 | #endif |
| 22 | |
Sergey Ulanov | 7de7e9f | 2019-03-09 00:41:02 | [diff] [blame] | 23 | #if defined(OS_FUCHSIA) |
| 24 | #include "sql/vfs_wrapper_fuchsia.h" |
| 25 | #endif |
| 26 | |
shess | 5f2c344 | 2017-01-24 02:15:10 | [diff] [blame] | 27 | namespace sql { |
| 28 | namespace { |
| 29 | |
| 30 | // https://www.sqlite.org/vfs.html - documents the overall VFS system. |
| 31 | // |
| 32 | // https://www.sqlite.org/c3ref/vfs.html - VFS methods. This code tucks the |
| 33 | // wrapped VFS pointer into the wrapper's pAppData pointer. |
| 34 | // |
| 35 | // https://www.sqlite.org/c3ref/file.html - instance of an open file. This code |
| 36 | // allocates a VfsFile for this, which contains a pointer to the wrapped file. |
| 37 | // Idiomatic SQLite would take the wrapped VFS szOsFile and increase it to store |
| 38 | // additional data as a prefix. |
| 39 | |
| 40 | sqlite3_vfs* GetWrappedVfs(sqlite3_vfs* wrapped_vfs) { |
| 41 | return static_cast<sqlite3_vfs*>(wrapped_vfs->pAppData); |
| 42 | } |
| 43 | |
shess | 5f2c344 | 2017-01-24 02:15:10 | [diff] [blame] | 44 | VfsFile* AsVfsFile(sqlite3_file* wrapper_file) { |
| 45 | return reinterpret_cast<VfsFile*>(wrapper_file); |
| 46 | } |
| 47 | |
| 48 | sqlite3_file* GetWrappedFile(sqlite3_file* wrapper_file) { |
| 49 | return AsVfsFile(wrapper_file)->wrapped_file; |
| 50 | } |
| 51 | |
| 52 | int Close(sqlite3_file* sqlite_file) |
| 53 | { |
| 54 | VfsFile* file = AsVfsFile(sqlite_file); |
| 55 | |
Sergey Ulanov | 7de7e9f | 2019-03-09 00:41:02 | [diff] [blame] | 56 | #if defined(OS_FUCHSIA) |
| 57 | FuchsiaVfsUnlock(sqlite_file, SQLITE_LOCK_NONE); |
| 58 | #endif |
| 59 | |
shess | 5f2c344 | 2017-01-24 02:15:10 | [diff] [blame] | 60 | int r = file->wrapped_file->pMethods->xClose(file->wrapped_file); |
| 61 | sqlite3_free(file->wrapped_file); |
Sergey Ulanov | 7de7e9f | 2019-03-09 00:41:02 | [diff] [blame] | 62 | |
| 63 | // Memory will be freed with sqlite3_free(), so the destructor needs to be |
| 64 | // called explicitly. |
| 65 | file->~VfsFile(); |
shess | 5f2c344 | 2017-01-24 02:15:10 | [diff] [blame] | 66 | memset(file, '\0', sizeof(*file)); |
| 67 | return r; |
| 68 | } |
| 69 | |
| 70 | int Read(sqlite3_file* sqlite_file, void* buf, int amt, sqlite3_int64 ofs) |
| 71 | { |
| 72 | sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file); |
| 73 | return wrapped_file->pMethods->xRead(wrapped_file, buf, amt, ofs); |
| 74 | } |
| 75 | |
| 76 | int Write(sqlite3_file* sqlite_file, const void* buf, int amt, |
| 77 | sqlite3_int64 ofs) |
| 78 | { |
| 79 | sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file); |
| 80 | return wrapped_file->pMethods->xWrite(wrapped_file, buf, amt, ofs); |
| 81 | } |
| 82 | |
| 83 | int Truncate(sqlite3_file* sqlite_file, sqlite3_int64 size) |
| 84 | { |
| 85 | sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file); |
| 86 | return wrapped_file->pMethods->xTruncate(wrapped_file, size); |
| 87 | } |
| 88 | |
| 89 | int Sync(sqlite3_file* sqlite_file, int flags) |
| 90 | { |
| 91 | sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file); |
| 92 | return wrapped_file->pMethods->xSync(wrapped_file, flags); |
| 93 | } |
| 94 | |
| 95 | int FileSize(sqlite3_file* sqlite_file, sqlite3_int64* size) |
| 96 | { |
| 97 | sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file); |
| 98 | return wrapped_file->pMethods->xFileSize(wrapped_file, size); |
| 99 | } |
| 100 | |
Sergey Ulanov | 7de7e9f | 2019-03-09 00:41:02 | [diff] [blame] | 101 | #if !defined(OS_FUCHSIA) |
| 102 | |
shess | 5f2c344 | 2017-01-24 02:15:10 | [diff] [blame] | 103 | int Lock(sqlite3_file* sqlite_file, int file_lock) |
| 104 | { |
| 105 | sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file); |
| 106 | return wrapped_file->pMethods->xLock(wrapped_file, file_lock); |
| 107 | } |
| 108 | |
| 109 | int Unlock(sqlite3_file* sqlite_file, int file_lock) |
| 110 | { |
| 111 | sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file); |
| 112 | return wrapped_file->pMethods->xUnlock(wrapped_file, file_lock); |
| 113 | } |
| 114 | |
| 115 | int CheckReservedLock(sqlite3_file* sqlite_file, int* result) |
| 116 | { |
| 117 | sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file); |
| 118 | return wrapped_file->pMethods->xCheckReservedLock(wrapped_file, result); |
| 119 | } |
| 120 | |
Sergey Ulanov | 7de7e9f | 2019-03-09 00:41:02 | [diff] [blame] | 121 | #endif // !defined(OS_FUCHSIA) |
| 122 | |
shess | 5f2c344 | 2017-01-24 02:15:10 | [diff] [blame] | 123 | int FileControl(sqlite3_file* sqlite_file, int op, void* arg) |
| 124 | { |
| 125 | sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file); |
| 126 | return wrapped_file->pMethods->xFileControl(wrapped_file, op, arg); |
| 127 | } |
| 128 | |
| 129 | int SectorSize(sqlite3_file* sqlite_file) |
| 130 | { |
| 131 | sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file); |
| 132 | return wrapped_file->pMethods->xSectorSize(wrapped_file); |
| 133 | } |
| 134 | |
| 135 | int DeviceCharacteristics(sqlite3_file* sqlite_file) |
| 136 | { |
| 137 | sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file); |
| 138 | return wrapped_file->pMethods->xDeviceCharacteristics(wrapped_file); |
| 139 | } |
| 140 | |
| 141 | int ShmMap(sqlite3_file *sqlite_file, int region, int size, |
| 142 | int extend, void volatile **pp) { |
| 143 | sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file); |
| 144 | return wrapped_file->pMethods->xShmMap( |
| 145 | wrapped_file, region, size, extend, pp); |
| 146 | } |
| 147 | |
| 148 | int ShmLock(sqlite3_file *sqlite_file, int ofst, int n, int flags) { |
| 149 | sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file); |
| 150 | return wrapped_file->pMethods->xShmLock(wrapped_file, ofst, n, flags); |
| 151 | } |
| 152 | |
| 153 | void ShmBarrier(sqlite3_file *sqlite_file) { |
| 154 | sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file); |
| 155 | wrapped_file->pMethods->xShmBarrier(wrapped_file); |
| 156 | } |
| 157 | |
| 158 | int ShmUnmap(sqlite3_file *sqlite_file, int del) { |
| 159 | sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file); |
| 160 | return wrapped_file->pMethods->xShmUnmap(wrapped_file, del); |
| 161 | } |
| 162 | |
| 163 | int Fetch(sqlite3_file *sqlite_file, sqlite3_int64 off, int amt, void **pp) { |
| 164 | sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file); |
| 165 | return wrapped_file->pMethods->xFetch(wrapped_file, off, amt, pp); |
| 166 | } |
| 167 | |
| 168 | int Unfetch(sqlite3_file *sqlite_file, sqlite3_int64 off, void *p) { |
| 169 | sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file); |
| 170 | return wrapped_file->pMethods->xUnfetch(wrapped_file, off, p); |
| 171 | } |
| 172 | |
shess | 5f2c344 | 2017-01-24 02:15:10 | [diff] [blame] | 173 | int Open(sqlite3_vfs* vfs, const char* file_name, sqlite3_file* wrapper_file, |
| 174 | int desired_flags, int* used_flags) { |
| 175 | sqlite3_vfs* wrapped_vfs = GetWrappedVfs(vfs); |
| 176 | |
| 177 | sqlite3_file* wrapped_file = static_cast<sqlite3_file*>( |
| 178 | sqlite3_malloc(wrapped_vfs->szOsFile)); |
| 179 | if (!wrapped_file) |
| 180 | return SQLITE_NOMEM; |
| 181 | |
| 182 | // NOTE(shess): SQLite's unixOpen() makes assumptions about the structure of |
| 183 | // |file_name|. Do not pass a local copy, here, only the passed-in value. |
| 184 | int rc = wrapped_vfs->xOpen(wrapped_vfs, |
| 185 | file_name, wrapped_file, |
| 186 | desired_flags, used_flags); |
| 187 | if (rc != SQLITE_OK) { |
| 188 | sqlite3_free(wrapped_file); |
| 189 | return rc; |
| 190 | } |
| 191 | // NOTE(shess): Any early exit from here needs to call xClose() on |
| 192 | // |wrapped_file|. |
| 193 | |
| 194 | #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 195 | // When opening journal files, propagate time-machine exclusion from db. |
| 196 | static int kJournalFlags = |
| 197 | SQLITE_OPEN_MAIN_JOURNAL | SQLITE_OPEN_TEMP_JOURNAL | |
| 198 | SQLITE_OPEN_SUBJOURNAL | SQLITE_OPEN_MASTER_JOURNAL; |
| 199 | if (file_name && (desired_flags & kJournalFlags)) { |
| 200 | // https://www.sqlite.org/c3ref/vfs.html indicates that the journal path |
Victor Costan | 7e74dce | 2019-01-28 20:15:25 | [diff] [blame] | 201 | // will have a suffix separated by "-" from the main database file name. |
| 202 | base::StringPiece file_name_string_piece(file_name); |
| 203 | size_t dash_index = file_name_string_piece.rfind('-'); |
shess | 5f2c344 | 2017-01-24 02:15:10 | [diff] [blame] | 204 | if (dash_index != base::StringPiece::npos) { |
Victor Costan | 7e74dce | 2019-01-28 20:15:25 | [diff] [blame] | 205 | base::StringPiece db_name(file_name, dash_index); |
| 206 | if (base::mac::GetFileBackupExclusion(base::FilePath(db_name))) { |
| 207 | base::mac::SetFileBackupExclusion( |
| 208 | base::FilePath(file_name_string_piece)); |
shess | 5f2c344 | 2017-01-24 02:15:10 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | } |
| 212 | #endif |
| 213 | |
Victor Costan | 86ca431 | 2018-02-07 21:34:10 | [diff] [blame] | 214 | // |iVersion| determines what methods SQLite may call on the instance. |
| 215 | // Having the methods which can't be proxied return an error may cause SQLite |
| 216 | // to operate differently than if it didn't call those methods at all. To be |
| 217 | // on the safe side, the wrapper sqlite3_io_methods version perfectly matches |
| 218 | // the version of the wrapped files. |
shess | 5f2c344 | 2017-01-24 02:15:10 | [diff] [blame] | 219 | // |
Victor Costan | 86ca431 | 2018-02-07 21:34:10 | [diff] [blame] | 220 | // At a first glance, it might be tempting to simplify the code by |
Sergey Ulanov | 7de7e9f | 2019-03-09 00:41:02 | [diff] [blame] | 221 | // restricting wrapping support to VFS version 3. However, this might fail on |
| 222 | // Mac. |
Victor Costan | 86ca431 | 2018-02-07 21:34:10 | [diff] [blame] | 223 | // |
| 224 | // On Mac, SQLite built with SQLITE_ENABLE_LOCKING_STYLE ends up using a VFS |
| 225 | // that dynamically dispatches between a few variants of sqlite3_io_methods, |
| 226 | // based on whether the opened database is on a local or on a remote (AFS, |
| 227 | // NFS) filesystem. Some variants return a VFS version 1 structure. |
shess | 5f2c344 | 2017-01-24 02:15:10 | [diff] [blame] | 228 | VfsFile* file = AsVfsFile(wrapper_file); |
Sergey Ulanov | 7de7e9f | 2019-03-09 00:41:02 | [diff] [blame] | 229 | |
| 230 | // Call constructor explicitly since the memory is already allocated. |
| 231 | new (file) VfsFile(); |
| 232 | |
shess | 5f2c344 | 2017-01-24 02:15:10 | [diff] [blame] | 233 | file->wrapped_file = wrapped_file; |
Sergey Ulanov | 7de7e9f | 2019-03-09 00:41:02 | [diff] [blame] | 234 | |
| 235 | #if defined(OS_FUCHSIA) |
| 236 | file->file_name = file_name; |
| 237 | file->lock_level = SQLITE_LOCK_NONE; |
| 238 | #endif |
| 239 | |
shess | 5f2c344 | 2017-01-24 02:15:10 | [diff] [blame] | 240 | if (wrapped_file->pMethods->iVersion == 1) { |
| 241 | static const sqlite3_io_methods io_methods = { |
| 242 | 1, |
| 243 | Close, |
| 244 | Read, |
| 245 | Write, |
| 246 | Truncate, |
| 247 | Sync, |
| 248 | FileSize, |
Sergey Ulanov | 7de7e9f | 2019-03-09 00:41:02 | [diff] [blame] | 249 | #if !defined(OS_FUCHSIA) |
shess | 5f2c344 | 2017-01-24 02:15:10 | [diff] [blame] | 250 | Lock, |
| 251 | Unlock, |
| 252 | CheckReservedLock, |
Sergey Ulanov | 7de7e9f | 2019-03-09 00:41:02 | [diff] [blame] | 253 | #else |
| 254 | FuchsiaVfsLock, |
| 255 | FuchsiaVfsUnlock, |
| 256 | FuchsiaVfsCheckReservedLock, |
| 257 | #endif |
shess | 5f2c344 | 2017-01-24 02:15:10 | [diff] [blame] | 258 | FileControl, |
| 259 | SectorSize, |
| 260 | DeviceCharacteristics, |
| 261 | }; |
| 262 | file->methods = &io_methods; |
| 263 | } else if (wrapped_file->pMethods->iVersion == 2) { |
| 264 | static const sqlite3_io_methods io_methods = { |
| 265 | 2, |
| 266 | Close, |
| 267 | Read, |
| 268 | Write, |
| 269 | Truncate, |
| 270 | Sync, |
| 271 | FileSize, |
Sergey Ulanov | 7de7e9f | 2019-03-09 00:41:02 | [diff] [blame] | 272 | #if !defined(OS_FUCHSIA) |
shess | 5f2c344 | 2017-01-24 02:15:10 | [diff] [blame] | 273 | Lock, |
| 274 | Unlock, |
| 275 | CheckReservedLock, |
Sergey Ulanov | 7de7e9f | 2019-03-09 00:41:02 | [diff] [blame] | 276 | #else |
| 277 | FuchsiaVfsLock, |
| 278 | FuchsiaVfsUnlock, |
| 279 | FuchsiaVfsCheckReservedLock, |
| 280 | #endif |
shess | 5f2c344 | 2017-01-24 02:15:10 | [diff] [blame] | 281 | FileControl, |
| 282 | SectorSize, |
| 283 | DeviceCharacteristics, |
| 284 | // Methods above are valid for version 1. |
| 285 | ShmMap, |
| 286 | ShmLock, |
| 287 | ShmBarrier, |
| 288 | ShmUnmap, |
| 289 | }; |
| 290 | file->methods = &io_methods; |
| 291 | } else { |
| 292 | static const sqlite3_io_methods io_methods = { |
| 293 | 3, |
| 294 | Close, |
| 295 | Read, |
| 296 | Write, |
| 297 | Truncate, |
| 298 | Sync, |
| 299 | FileSize, |
Sergey Ulanov | 7de7e9f | 2019-03-09 00:41:02 | [diff] [blame] | 300 | #if !defined(OS_FUCHSIA) |
shess | 5f2c344 | 2017-01-24 02:15:10 | [diff] [blame] | 301 | Lock, |
| 302 | Unlock, |
| 303 | CheckReservedLock, |
Sergey Ulanov | 7de7e9f | 2019-03-09 00:41:02 | [diff] [blame] | 304 | #else |
| 305 | FuchsiaVfsLock, |
| 306 | FuchsiaVfsUnlock, |
| 307 | FuchsiaVfsCheckReservedLock, |
| 308 | #endif |
shess | 5f2c344 | 2017-01-24 02:15:10 | [diff] [blame] | 309 | FileControl, |
| 310 | SectorSize, |
| 311 | DeviceCharacteristics, |
| 312 | // Methods above are valid for version 1. |
| 313 | ShmMap, |
| 314 | ShmLock, |
| 315 | ShmBarrier, |
| 316 | ShmUnmap, |
| 317 | // Methods above are valid for version 2. |
| 318 | Fetch, |
| 319 | Unfetch, |
| 320 | }; |
| 321 | file->methods = &io_methods; |
| 322 | } |
| 323 | return SQLITE_OK; |
| 324 | } |
| 325 | |
| 326 | int Delete(sqlite3_vfs* vfs, const char* file_name, int sync_dir) { |
| 327 | sqlite3_vfs* wrapped_vfs = GetWrappedVfs(vfs); |
| 328 | return wrapped_vfs->xDelete(wrapped_vfs, file_name, sync_dir); |
| 329 | } |
| 330 | |
| 331 | int Access(sqlite3_vfs* vfs, const char* file_name, int flag, int* res) { |
| 332 | sqlite3_vfs* wrapped_vfs = GetWrappedVfs(vfs); |
| 333 | return wrapped_vfs->xAccess(wrapped_vfs, file_name, flag, res); |
| 334 | } |
| 335 | |
| 336 | int FullPathname(sqlite3_vfs* vfs, const char* relative_path, |
| 337 | int buf_size, char* absolute_path) { |
shess | 5f2c344 | 2017-01-24 02:15:10 | [diff] [blame] | 338 | sqlite3_vfs* wrapped_vfs = GetWrappedVfs(vfs); |
| 339 | return wrapped_vfs->xFullPathname( |
| 340 | wrapped_vfs, relative_path, buf_size, absolute_path); |
| 341 | } |
| 342 | |
shess | 5f2c344 | 2017-01-24 02:15:10 | [diff] [blame] | 343 | int Randomness(sqlite3_vfs* vfs, int buf_size, char* buffer) { |
| 344 | sqlite3_vfs* wrapped_vfs = GetWrappedVfs(vfs); |
| 345 | return wrapped_vfs->xRandomness(wrapped_vfs, buf_size, buffer); |
| 346 | } |
| 347 | |
| 348 | int Sleep(sqlite3_vfs* vfs, int microseconds) { |
| 349 | sqlite3_vfs* wrapped_vfs = GetWrappedVfs(vfs); |
| 350 | return wrapped_vfs->xSleep(wrapped_vfs, microseconds); |
| 351 | } |
| 352 | |
shess | 5f2c344 | 2017-01-24 02:15:10 | [diff] [blame] | 353 | int GetLastError(sqlite3_vfs* vfs, int e, char* s) { |
| 354 | sqlite3_vfs* wrapped_vfs = GetWrappedVfs(vfs); |
| 355 | return wrapped_vfs->xGetLastError(wrapped_vfs, e, s); |
| 356 | } |
| 357 | |
| 358 | int CurrentTimeInt64(sqlite3_vfs* vfs, sqlite3_int64* now) { |
| 359 | sqlite3_vfs* wrapped_vfs = GetWrappedVfs(vfs); |
| 360 | return wrapped_vfs->xCurrentTimeInt64(wrapped_vfs, now); |
| 361 | } |
| 362 | |
shess | 5f2c344 | 2017-01-24 02:15:10 | [diff] [blame] | 363 | } // namespace |
| 364 | |
| 365 | sqlite3_vfs* VFSWrapper() { |
| 366 | const char* kVFSName = "VFSWrapper"; |
| 367 | |
| 368 | // Return existing version if already registered. |
| 369 | { |
| 370 | sqlite3_vfs* vfs = sqlite3_vfs_find(kVFSName); |
Victor Costan | bd62311 | 2018-07-18 04:17:27 | [diff] [blame] | 371 | if (vfs) |
shess | 5f2c344 | 2017-01-24 02:15:10 | [diff] [blame] | 372 | return vfs; |
| 373 | } |
| 374 | |
Sergey Ulanov | 7de7e9f | 2019-03-09 00:41:02 | [diff] [blame] | 375 | // Get the default VFS on all platforms except Fuchsia. |
| 376 | const char* base_vfs_name = nullptr; |
| 377 | #if defined(OS_FUCHSIA) |
| 378 | base_vfs_name = "unix-none"; |
| 379 | #endif |
| 380 | sqlite3_vfs* wrapped_vfs = sqlite3_vfs_find(base_vfs_name); |
| 381 | |
| 382 | // Give up if there is no VFS implementation for the current platform. |
| 383 | if (!wrapped_vfs) { |
| 384 | NOTREACHED(); |
shess | 5f2c344 | 2017-01-24 02:15:10 | [diff] [blame] | 385 | return nullptr; |
Sergey Ulanov | 7de7e9f | 2019-03-09 00:41:02 | [diff] [blame] | 386 | } |
shess | 5f2c344 | 2017-01-24 02:15:10 | [diff] [blame] | 387 | |
| 388 | std::unique_ptr<sqlite3_vfs, std::function<void(sqlite3_vfs*)>> wrapper_vfs( |
| 389 | static_cast<sqlite3_vfs*>(sqlite3_malloc(sizeof(sqlite3_vfs))), |
| 390 | [](sqlite3_vfs* v) { |
| 391 | sqlite3_free(v); |
| 392 | }); |
| 393 | memset(wrapper_vfs.get(), '\0', sizeof(sqlite3_vfs)); |
| 394 | |
| 395 | // VFS implementations should always work with a SQLite that only knows about |
| 396 | // earlier versions. |
Victor Costan | be262328 | 2019-04-11 21:32:21 | [diff] [blame] | 397 | constexpr int kSqliteVfsApiVersion = 3; |
| 398 | wrapper_vfs->iVersion = kSqliteVfsApiVersion; |
| 399 | |
| 400 | // All the SQLite VFS implementations used by Chrome should support the |
| 401 | // version proxied here. |
| 402 | DCHECK_GE(wrapped_vfs->iVersion, kSqliteVfsApiVersion); |
shess | 5f2c344 | 2017-01-24 02:15:10 | [diff] [blame] | 403 | |
| 404 | // Caller of xOpen() allocates this much space. |
| 405 | wrapper_vfs->szOsFile = sizeof(VfsFile); |
| 406 | |
| 407 | wrapper_vfs->mxPathname = wrapped_vfs->mxPathname; |
Victor Costan | be262328 | 2019-04-11 21:32:21 | [diff] [blame] | 408 | wrapper_vfs->pNext = nullptr; // Field used by SQLite. |
shess | 5f2c344 | 2017-01-24 02:15:10 | [diff] [blame] | 409 | wrapper_vfs->zName = kVFSName; |
| 410 | |
| 411 | // Keep a reference to the wrapped vfs for use in methods. |
| 412 | wrapper_vfs->pAppData = wrapped_vfs; |
| 413 | |
| 414 | // VFS methods. |
| 415 | wrapper_vfs->xOpen = &Open; |
| 416 | wrapper_vfs->xDelete = &Delete; |
| 417 | wrapper_vfs->xAccess = &Access; |
| 418 | wrapper_vfs->xFullPathname = &FullPathname; |
Victor Costan | be262328 | 2019-04-11 21:32:21 | [diff] [blame] | 419 | |
| 420 | // SQLite's dynamic extension loading is disabled in Chrome. Not proxying |
| 421 | // these methods lets us ship less logic and provides a tiny bit of extra |
| 422 | // security, as we know for sure that SQLite will not dynamically load code. |
| 423 | wrapper_vfs->xDlOpen = nullptr; |
| 424 | wrapper_vfs->xDlError = nullptr; |
| 425 | wrapper_vfs->xDlSym = nullptr; |
| 426 | wrapper_vfs->xDlClose = nullptr; |
| 427 | |
shess | 5f2c344 | 2017-01-24 02:15:10 | [diff] [blame] | 428 | wrapper_vfs->xRandomness = &Randomness; |
| 429 | wrapper_vfs->xSleep = &Sleep; |
Victor Costan | be262328 | 2019-04-11 21:32:21 | [diff] [blame] | 430 | |
| 431 | // |xCurrentTime| is null when SQLite is built with SQLITE_OMIT_DEPRECATED, so |
| 432 | // it does not need to be proxied. |
| 433 | wrapper_vfs->xCurrentTime = nullptr; |
| 434 | |
shess | 5f2c344 | 2017-01-24 02:15:10 | [diff] [blame] | 435 | wrapper_vfs->xGetLastError = &GetLastError; |
Victor Costan | be262328 | 2019-04-11 21:32:21 | [diff] [blame] | 436 | |
| 437 | // The methods above are in version 1 of SQLite's VFS API. |
| 438 | |
| 439 | DCHECK(wrapped_vfs->xCurrentTimeInt64 != nullptr); |
Victor Costan | 86ca431 | 2018-02-07 21:34:10 | [diff] [blame] | 440 | wrapper_vfs->xCurrentTimeInt64 = &CurrentTimeInt64; |
Victor Costan | be262328 | 2019-04-11 21:32:21 | [diff] [blame] | 441 | |
| 442 | // The methods above are in version 2 of SQLite's VFS API. |
| 443 | |
| 444 | // The VFS system call interception API is intended for very low-level SQLite |
| 445 | // testing and tweaks. Proxying these methods is not necessary because Chrome |
| 446 | // does not do very low-level SQLite testing, and the VFS wrapper supports all |
| 447 | // the needed tweaks. |
| 448 | wrapper_vfs->xSetSystemCall = nullptr; |
| 449 | wrapper_vfs->xGetSystemCall = nullptr; |
| 450 | wrapper_vfs->xNextSystemCall = nullptr; |
| 451 | |
shess | 5f2c344 | 2017-01-24 02:15:10 | [diff] [blame] | 452 | // The methods above are in version 3 of sqlite_vfs. |
| 453 | |
| 454 | if (SQLITE_OK == sqlite3_vfs_register(wrapper_vfs.get(), 0)) { |
| 455 | ANNOTATE_LEAKING_OBJECT_PTR(wrapper_vfs.get()); |
| 456 | wrapper_vfs.release(); |
| 457 | } |
| 458 | |
| 459 | return sqlite3_vfs_find(kVFSName); |
| 460 | } |
| 461 | |
| 462 | } // namespace sql |