vfs_wrapper.cc 16.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
// Copyright (c) 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "sql/vfs_wrapper.h"

#include <algorithm>
#include <string>
#include <vector>

#include "base/debug/leak_annotations.h"
#include "base/logging.h"
#include "base/memory/ptr_util.h"
14
#include "base/metrics/histogram_macros.h"
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
#include "base/strings/string_piece.h"

#if defined(OS_MACOSX) && !defined(OS_IOS)
#include <CoreFoundation/CoreFoundation.h>
#include <CoreServices/CoreServices.h>

#include "base/mac/mac_util.h"
#include "base/mac/scoped_cftyperef.h"
#endif

namespace sql {
namespace {

// https://www.sqlite.org/vfs.html - documents the overall VFS system.
//
// https://www.sqlite.org/c3ref/vfs.html - VFS methods.  This code tucks the
// wrapped VFS pointer into the wrapper's pAppData pointer.
//
// https://www.sqlite.org/c3ref/file.html - instance of an open file.  This code
// allocates a VfsFile for this, which contains a pointer to the wrapped file.
// Idiomatic SQLite would take the wrapped VFS szOsFile and increase it to store
// additional data as a prefix.

38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
// This enum must match the numbering from Sqlite.VfsEvents in histograms.xml.
// Do not reorder or remove items, only add new items before VFS_EVENT_MAX.
enum VfsEventType {
  // VFS method xOpen() call.
  VFS_OPEN = 0,

  // VFS method xDelete() call.
  VFS_DELETE,

  // VFS method xAccess() call.
  VFS_ACCESS,

  // VFS method xFullPathname() call.
  VFS_FULLPATHNAME,

  // I/O method xClose() call, should balance VFS_OPEN.
  VFS_IO_CLOSE,

  // I/O method xRead() call.
  VFS_IO_READ,

  // I/O method xWrite() call.
  VFS_IO_WRITE,

  // I/O method xTruncate() call.
  VFS_IO_TRUNCATE,

  // I/O method xSync() call.
  VFS_IO_SYNC,

  // I/O method xFileSize() call.
  VFS_IO_FILESIZE,

  // I/O method xFetch() call.  This is like xRead(), but when using
  // memory-mapping.
  VFS_IO_FETCH,

  // Add new items before this one, always keep this one at the end.
  VFS_EVENT_MAX
};

// TODO(shess): If the VFS was parameterized, then results could be binned by
// database.  It would require a separate VFS per database, though the variants
// could all use the same VFS functions.
void RecordVfsEvent(VfsEventType vfs_event) {
  UMA_HISTOGRAM_ENUMERATION("Sqlite.Vfs_Events", vfs_event, VFS_EVENT_MAX);
}

86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
sqlite3_vfs* GetWrappedVfs(sqlite3_vfs* wrapped_vfs) {
  return static_cast<sqlite3_vfs*>(wrapped_vfs->pAppData);
}

// NOTE(shess): This structure is allocated by SQLite using malloc.  Do not add
// C++ objects, they will not be correctly constructed and destructed.  Instead,
// manually manage a pointer to a C++ object in Open() and Close().
struct VfsFile {
  const sqlite3_io_methods* methods;
  sqlite3_file* wrapped_file;
};

VfsFile* AsVfsFile(sqlite3_file* wrapper_file) {
  return reinterpret_cast<VfsFile*>(wrapper_file);
}

sqlite3_file* GetWrappedFile(sqlite3_file* wrapper_file) {
  return AsVfsFile(wrapper_file)->wrapped_file;
}

int Close(sqlite3_file* sqlite_file)
{
108 109
  RecordVfsEvent(VFS_IO_CLOSE);

110 111 112 113 114 115 116 117 118 119
  VfsFile* file = AsVfsFile(sqlite_file);

  int r = file->wrapped_file->pMethods->xClose(file->wrapped_file);
  sqlite3_free(file->wrapped_file);
  memset(file, '\0', sizeof(*file));
  return r;
}

int Read(sqlite3_file* sqlite_file, void* buf, int amt, sqlite3_int64 ofs)
{
120 121 122
  RecordVfsEvent(VFS_IO_READ);
  UMA_HISTOGRAM_COUNTS("Sqlite.Vfs_Read", amt);

123 124 125 126 127 128 129
  sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file);
  return wrapped_file->pMethods->xRead(wrapped_file, buf, amt, ofs);
}

int Write(sqlite3_file* sqlite_file, const void* buf, int amt,
          sqlite3_int64 ofs)
{
130 131 132
  RecordVfsEvent(VFS_IO_WRITE);
  UMA_HISTOGRAM_COUNTS("Sqlite.Vfs_Write", amt);

133 134 135 136 137 138
  sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file);
  return wrapped_file->pMethods->xWrite(wrapped_file, buf, amt, ofs);
}

int Truncate(sqlite3_file* sqlite_file, sqlite3_int64 size)
{
139 140
  RecordVfsEvent(VFS_IO_TRUNCATE);

141 142 143 144 145 146
  sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file);
  return wrapped_file->pMethods->xTruncate(wrapped_file, size);
}

int Sync(sqlite3_file* sqlite_file, int flags)
{
147 148
  RecordVfsEvent(VFS_IO_SYNC);

149 150 151 152 153 154
  sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file);
  return wrapped_file->pMethods->xSync(wrapped_file, flags);
}

int FileSize(sqlite3_file* sqlite_file, sqlite3_int64* size)
{
155 156
  RecordVfsEvent(VFS_IO_FILESIZE);

157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
  sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file);
  return wrapped_file->pMethods->xFileSize(wrapped_file, size);
}

int Lock(sqlite3_file* sqlite_file, int file_lock)
{
  sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file);
  return wrapped_file->pMethods->xLock(wrapped_file, file_lock);
}

int Unlock(sqlite3_file* sqlite_file, int file_lock)
{
  sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file);
  return wrapped_file->pMethods->xUnlock(wrapped_file, file_lock);
}

int CheckReservedLock(sqlite3_file* sqlite_file, int* result)
{
  sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file);
  return wrapped_file->pMethods->xCheckReservedLock(wrapped_file, result);
}

int FileControl(sqlite3_file* sqlite_file, int op, void* arg)
{
  sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file);
  return wrapped_file->pMethods->xFileControl(wrapped_file, op, arg);
}

int SectorSize(sqlite3_file* sqlite_file)
{
  sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file);
  return wrapped_file->pMethods->xSectorSize(wrapped_file);
}

int DeviceCharacteristics(sqlite3_file* sqlite_file)
{
  sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file);
  return wrapped_file->pMethods->xDeviceCharacteristics(wrapped_file);
}

int ShmMap(sqlite3_file *sqlite_file, int region, int size,
           int extend, void volatile **pp) {
  sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file);
  return wrapped_file->pMethods->xShmMap(
      wrapped_file, region, size, extend, pp);
}

int ShmLock(sqlite3_file *sqlite_file, int ofst, int n, int flags) {
  sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file);
  return wrapped_file->pMethods->xShmLock(wrapped_file, ofst, n, flags);
}

void ShmBarrier(sqlite3_file *sqlite_file) {
  sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file);
  wrapped_file->pMethods->xShmBarrier(wrapped_file);
}

int ShmUnmap(sqlite3_file *sqlite_file, int del) {
  sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file);
  return wrapped_file->pMethods->xShmUnmap(wrapped_file, del);
}

int Fetch(sqlite3_file *sqlite_file, sqlite3_int64 off, int amt, void **pp) {
220 221 222
  RecordVfsEvent(VFS_IO_FETCH);
  UMA_HISTOGRAM_COUNTS("Sqlite.Vfs_Fetch", amt);

223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
  sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file);
  return wrapped_file->pMethods->xFetch(wrapped_file, off, amt, pp);
}

int Unfetch(sqlite3_file *sqlite_file, sqlite3_int64 off, void *p) {
  sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file);
  return wrapped_file->pMethods->xUnfetch(wrapped_file, off, p);
}

#if defined(OS_MACOSX) && !defined(OS_IOS)
// Helper to convert a POSIX path into a CoreFoundation path.
base::ScopedCFTypeRef<CFURLRef> CFURLRefForPath(const char* path){
  base::ScopedCFTypeRef<CFStringRef> urlString(
      CFStringCreateWithFileSystemRepresentation(kCFAllocatorDefault, path));
  base::ScopedCFTypeRef<CFURLRef> url(
      CFURLCreateWithFileSystemPath(kCFAllocatorDefault, urlString,
                                    kCFURLPOSIXPathStyle, FALSE));
  return url;
}
#endif

int Open(sqlite3_vfs* vfs, const char* file_name, sqlite3_file* wrapper_file,
         int desired_flags, int* used_flags) {
246 247
  RecordVfsEvent(VFS_OPEN);

248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
  sqlite3_vfs* wrapped_vfs = GetWrappedVfs(vfs);

  sqlite3_file* wrapped_file = static_cast<sqlite3_file*>(
      sqlite3_malloc(wrapped_vfs->szOsFile));
  if (!wrapped_file)
    return SQLITE_NOMEM;

  // NOTE(shess): SQLite's unixOpen() makes assumptions about the structure of
  // |file_name|.  Do not pass a local copy, here, only the passed-in value.
  int rc = wrapped_vfs->xOpen(wrapped_vfs,
                              file_name, wrapped_file,
                              desired_flags, used_flags);
  if (rc != SQLITE_OK) {
    sqlite3_free(wrapped_file);
    return rc;
  }
  // NOTE(shess): Any early exit from here needs to call xClose() on
  // |wrapped_file|.

#if defined(OS_MACOSX) && !defined(OS_IOS)
  // When opening journal files, propagate time-machine exclusion from db.
  static int kJournalFlags =
      SQLITE_OPEN_MAIN_JOURNAL | SQLITE_OPEN_TEMP_JOURNAL |
      SQLITE_OPEN_SUBJOURNAL | SQLITE_OPEN_MASTER_JOURNAL;
  if (file_name && (desired_flags & kJournalFlags)) {
    // https://www.sqlite.org/c3ref/vfs.html indicates that the journal path
    // will have a "-suffix".
    size_t dash_index = base::StringPiece(file_name).rfind('-');
    if (dash_index != base::StringPiece::npos) {
      std::string db_name(file_name, dash_index);
      base::ScopedCFTypeRef<CFURLRef> db_url(CFURLRefForPath(db_name.c_str()));
      if (CSBackupIsItemExcluded(db_url, nullptr)) {
        base::ScopedCFTypeRef<CFURLRef> journal_url(CFURLRefForPath(file_name));
        CSBackupSetItemExcluded(journal_url, TRUE, FALSE);
      }
    }
  }
#endif

  // The wrapper instances must support a specific |iVersion|, but there is no
  // explicit guarantee that the wrapped VFS will always vend instances with the
  // same |iVersion| (though I believe this is always the case in practice).
  // Vend a distinct set of IO methods for each version supported.
  //
  // |iVersion| determines what methods SQLite may call on the instance.  Having
  // the methods which can't be proxied return an error may cause SQLite to
  // operate differently than if it didn't call those methods at all.  Another
  // solution would be to fail if the wrapped file does not have the expected
  // version, which may cause problems on platforms which use the system SQLite
  // (iOS and some Linux distros).
  VfsFile* file = AsVfsFile(wrapper_file);
  file->wrapped_file = wrapped_file;
  if (wrapped_file->pMethods->iVersion == 1) {
    static const sqlite3_io_methods io_methods = {
      1,
      Close,
      Read,
      Write,
      Truncate,
      Sync,
      FileSize,
      Lock,
      Unlock,
      CheckReservedLock,
      FileControl,
      SectorSize,
      DeviceCharacteristics,
    };
    file->methods = &io_methods;
  } else if (wrapped_file->pMethods->iVersion == 2) {
    static const sqlite3_io_methods io_methods = {
      2,
      Close,
      Read,
      Write,
      Truncate,
      Sync,
      FileSize,
      Lock,
      Unlock,
      CheckReservedLock,
      FileControl,
      SectorSize,
      DeviceCharacteristics,
      // Methods above are valid for version 1.
      ShmMap,
      ShmLock,
      ShmBarrier,
      ShmUnmap,
    };
    file->methods = &io_methods;
  } else {
    static const sqlite3_io_methods io_methods = {
      3,
      Close,
      Read,
      Write,
      Truncate,
      Sync,
      FileSize,
      Lock,
      Unlock,
      CheckReservedLock,
      FileControl,
      SectorSize,
      DeviceCharacteristics,
      // Methods above are valid for version 1.
      ShmMap,
      ShmLock,
      ShmBarrier,
      ShmUnmap,
      // Methods above are valid for version 2.
      Fetch,
      Unfetch,
    };
    file->methods = &io_methods;
  }
  return SQLITE_OK;
}

int Delete(sqlite3_vfs* vfs, const char* file_name, int sync_dir) {
369 370
  RecordVfsEvent(VFS_DELETE);

371 372 373 374 375
  sqlite3_vfs* wrapped_vfs = GetWrappedVfs(vfs);
  return wrapped_vfs->xDelete(wrapped_vfs, file_name, sync_dir);
}

int Access(sqlite3_vfs* vfs, const char* file_name, int flag, int* res) {
376 377
  RecordVfsEvent(VFS_ACCESS);

378 379 380 381 382 383
  sqlite3_vfs* wrapped_vfs = GetWrappedVfs(vfs);
  return wrapped_vfs->xAccess(wrapped_vfs, file_name, flag, res);
}

int FullPathname(sqlite3_vfs* vfs, const char* relative_path,
                 int buf_size, char* absolute_path) {
384 385
  RecordVfsEvent(VFS_FULLPATHNAME);

386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
  sqlite3_vfs* wrapped_vfs = GetWrappedVfs(vfs);
  return wrapped_vfs->xFullPathname(
      wrapped_vfs, relative_path, buf_size, absolute_path);
}

void* DlOpen(sqlite3_vfs* vfs, const char* filename) {
  sqlite3_vfs* wrapped_vfs = GetWrappedVfs(vfs);
  return wrapped_vfs->xDlOpen(wrapped_vfs, filename);
}

void DlError(sqlite3_vfs* vfs, int buf_size, char* error_buffer) {
  sqlite3_vfs* wrapped_vfs = GetWrappedVfs(vfs);
  wrapped_vfs->xDlError(wrapped_vfs, buf_size, error_buffer);
}

void(*DlSym(sqlite3_vfs* vfs, void* handle, const char* sym))(void) {
  sqlite3_vfs* wrapped_vfs = GetWrappedVfs(vfs);
  return wrapped_vfs->xDlSym(wrapped_vfs, handle, sym);
}

void DlClose(sqlite3_vfs* vfs, void* handle) {
  sqlite3_vfs* wrapped_vfs = GetWrappedVfs(vfs);
  wrapped_vfs->xDlClose(wrapped_vfs, handle);
}

int Randomness(sqlite3_vfs* vfs, int buf_size, char* buffer) {
  sqlite3_vfs* wrapped_vfs = GetWrappedVfs(vfs);
  return wrapped_vfs->xRandomness(wrapped_vfs, buf_size, buffer);
}

int Sleep(sqlite3_vfs* vfs, int microseconds) {
  sqlite3_vfs* wrapped_vfs = GetWrappedVfs(vfs);
  return wrapped_vfs->xSleep(wrapped_vfs, microseconds);
}

int CurrentTime(sqlite3_vfs* vfs, double* now) {
  sqlite3_vfs* wrapped_vfs = GetWrappedVfs(vfs);
  return wrapped_vfs->xCurrentTime(wrapped_vfs, now);
}

int GetLastError(sqlite3_vfs* vfs, int e, char* s) {
  sqlite3_vfs* wrapped_vfs = GetWrappedVfs(vfs);
  return wrapped_vfs->xGetLastError(wrapped_vfs, e, s);
}

int CurrentTimeInt64(sqlite3_vfs* vfs, sqlite3_int64* now) {
  sqlite3_vfs* wrapped_vfs = GetWrappedVfs(vfs);
  return wrapped_vfs->xCurrentTimeInt64(wrapped_vfs, now);
}

int SetSystemCall(sqlite3_vfs* vfs, const char* name,
                  sqlite3_syscall_ptr func) {
  sqlite3_vfs* wrapped_vfs = GetWrappedVfs(vfs);
  return wrapped_vfs->xSetSystemCall(wrapped_vfs, name, func);
}

sqlite3_syscall_ptr GetSystemCall(sqlite3_vfs* vfs, const char* name) {
  sqlite3_vfs* wrapped_vfs = GetWrappedVfs(vfs);
  return wrapped_vfs->xGetSystemCall(wrapped_vfs, name);
}

const char* NextSystemCall(sqlite3_vfs* vfs, const char* name) {
  sqlite3_vfs* wrapped_vfs = GetWrappedVfs(vfs);
  return wrapped_vfs->xNextSystemCall(wrapped_vfs, name);
}

}  // namespace

sqlite3_vfs* VFSWrapper() {
  const char* kVFSName = "VFSWrapper";

  // Return existing version if already registered.
  {
    sqlite3_vfs* vfs = sqlite3_vfs_find(kVFSName);
    if (vfs != nullptr)
      return vfs;
  }

  // Get the default VFS for this platform.  If no default VFS, give up.
  sqlite3_vfs* wrapped_vfs = sqlite3_vfs_find(nullptr);
  if (!wrapped_vfs)
    return nullptr;

  std::unique_ptr<sqlite3_vfs, std::function<void(sqlite3_vfs*)>> wrapper_vfs(
      static_cast<sqlite3_vfs*>(sqlite3_malloc(sizeof(sqlite3_vfs))),
      [](sqlite3_vfs* v) {
        sqlite3_free(v);
      });
  memset(wrapper_vfs.get(), '\0', sizeof(sqlite3_vfs));

  // VFS implementations should always work with a SQLite that only knows about
  // earlier versions.
  wrapper_vfs->iVersion = std::min(wrapped_vfs->iVersion, 3);

  // Caller of xOpen() allocates this much space.
  wrapper_vfs->szOsFile = sizeof(VfsFile);

  wrapper_vfs->mxPathname = wrapped_vfs->mxPathname;
  wrapper_vfs->pNext = nullptr;
  wrapper_vfs->zName = kVFSName;

  // Keep a reference to the wrapped vfs for use in methods.
  wrapper_vfs->pAppData = wrapped_vfs;

  // VFS methods.
  wrapper_vfs->xOpen = &Open;
  wrapper_vfs->xDelete = &Delete;
  wrapper_vfs->xAccess = &Access;
  wrapper_vfs->xFullPathname = &FullPathname;
  wrapper_vfs->xDlOpen = &DlOpen;
  wrapper_vfs->xDlError = &DlError;
  wrapper_vfs->xDlSym = &DlSym;
  wrapper_vfs->xDlClose = &DlClose;
  wrapper_vfs->xRandomness = &Randomness;
  wrapper_vfs->xSleep = &Sleep;
  wrapper_vfs->xCurrentTime = &CurrentTime;
  wrapper_vfs->xGetLastError = &GetLastError;
  // The methods above are in version 1 of sqlite_vfs.
  // There were VFS implementations with nullptr for |xCurrentTimeInt64|.
  wrapper_vfs->xCurrentTimeInt64 =
      (wrapped_vfs->xCurrentTimeInt64 ? &CurrentTimeInt64 : nullptr);
  // The methods above are in version 2 of sqlite_vfs.
  wrapper_vfs->xSetSystemCall = &SetSystemCall;
  wrapper_vfs->xGetSystemCall = &GetSystemCall;
  wrapper_vfs->xNextSystemCall = &NextSystemCall;
  // The methods above are in version 3 of sqlite_vfs.

  if (SQLITE_OK == sqlite3_vfs_register(wrapper_vfs.get(), 0)) {
    ANNOTATE_LEAKING_OBJECT_PTR(wrapper_vfs.get());
    wrapper_vfs.release();
  }

  return sqlite3_vfs_find(kVFSName);
}

}  // namespace sql