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