From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: dumi <dumi@chromium.org>
Date: Mon, 20 Jul 2009 23:40:51 +0000
Subject: [PATCH 2/6] Modify default VFS to support WebDatabase.

The renderer WebDatabase implementation needs to broker certain requests
to the browser.  This modifies SQLite to allow monkey-patching the VFS
to support this.

NOTE(shess): This patch relies on core SQLite implementation details
remaining unchanged.  When importing a new version of SQLite, pay very
close attention to whether the change is still doing what is intended.

Original review URLs:
https://codereview.chromium.org/159044
https://codereview.chromium.org/384075
https://codereview.chromium.org/377039
[Possibly not a complete list.]
---
 third_party/sqlite/src/src/os_unix.c   | 51 ++++++++++++++++++++++++++
 third_party/sqlite/src/src/os_win.c    |  8 ++++
 third_party/sqlite/src/src/sqlite.h.in | 23 ++++++++++++
 3 files changed, 82 insertions(+)

diff --git a/third_party/sqlite/src/src/os_unix.c b/third_party/sqlite/src/src/os_unix.c
index 52ef64116444..d0e1c39bc4b8 100644
--- a/third_party/sqlite/src/src/os_unix.c
+++ b/third_party/sqlite/src/src/os_unix.c
@@ -1437,6 +1437,12 @@ static int fileHasMoved(unixFile *pFile){
   return pFile->pInode!=0 && pFile->pId!=pFile->pInode->fileId.pId;
 #else
   struct stat buf;
+
+  /* TODO(shess): This check doesn't work when the Chromium's WebDB code is
+  ** running in the sandbox.
+  */
+  return 0;
+
   return pFile->pInode!=0 &&
       (osStat(pFile->zPath, &buf)!=0
          || (u64)buf.st_ino!=pFile->pInode->fileId.ino);
@@ -5879,6 +5885,45 @@ static int findCreateFileMode(
   return rc;
 }

+/*
+** Initialize |unixFile| internals of |file| on behalf of chromiumOpen() in
+** WebDatabase SQLiteFileSystemPosix.cpp.  Function is a subset of unixOpen(),
+** each duplicated piece is marked by "Duplicated in" comment in unixOpen().
+*/
+CHROMIUM_SQLITE_API
+int chromium_sqlite3_fill_in_unix_sqlite3_file(sqlite3_vfs* pVfs,
+                                               int fd,
+                                               sqlite3_file* pFile,
+                                               const char* zPath,
+                                               int noLock,
+                                               int flags) {
+  unixFile *p = (unixFile *)pFile;
+  const int eType = flags&0xFFFFFF00;  /* Type of file to open */
+  const int ctrlFlags = (noLock ? UNIXFILE_NOLOCK : 0);
+  int rc;
+
+  memset(p, 0, sizeof(unixFile));
+
+  /* osStat() will not work in the sandbox, so findReusableFd() will always
+  ** fail, so directly include the failure-case setup then initialize
+  ** pPreallocatedUnused.
+  */
+  if( eType==SQLITE_OPEN_MAIN_DB ){
+    p->pPreallocatedUnused = sqlite3_malloc(sizeof(*p->pPreallocatedUnused));
+    if (!p->pPreallocatedUnused) {
+      return SQLITE_NOMEM_BKPT;
+    }
+    p->pPreallocatedUnused->fd = fd;
+    p->pPreallocatedUnused->flags = flags;
+  }
+
+  rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags);
+  if( rc!=SQLITE_OK ){
+    sqlite3_free(p->pPreallocatedUnused);
+  }
+  return rc;
+}
+
 /*
 ** Open the file zPath.
 **
@@ -5979,6 +6024,8 @@ static int unixOpen(
     randomnessPid = osGetpid(0);
     sqlite3_randomness(0,0);
   }
+
+  /* Duplicated in chromium_sqlite3_fill_in_unix_sqlite3_file(). */
   memset(p, 0, sizeof(unixFile));

   if( eType==SQLITE_OPEN_MAIN_DB ){
@@ -5987,6 +6034,7 @@ static int unixOpen(
     if( pUnused ){
       fd = pUnused->fd;
     }else{
+      /* Duplicated in chromium_sqlite3_fill_in_unix_sqlite3_file(). */
       pUnused = sqlite3_malloc64(sizeof(*pUnused));
       if( !pUnused ){
         return SQLITE_NOMEM_BKPT;
@@ -6071,6 +6119,7 @@ static int unixOpen(
   }

   if( p->pPreallocatedUnused ){
+    /* Duplicated in chromium_sqlite3_fill_in_unix_sqlite3_file(). */
     p->pPreallocatedUnused->fd = fd;
     p->pPreallocatedUnused->flags = flags;
   }
@@ -6152,10 +6201,12 @@ static int unixOpen(
   assert( zPath==0 || zPath[0]=='/'
       || eType==SQLITE_OPEN_MASTER_JOURNAL || eType==SQLITE_OPEN_MAIN_JOURNAL
   );
+  /* Duplicated in chromium_sqlite3_fill_in_unix_sqlite3_file(). */
   rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags);

 open_finished:
   if( rc!=SQLITE_OK ){
+    /* Duplicated in chromium_sqlite3_fill_in_unix_sqlite3_file(). */
     sqlite3_free(p->pPreallocatedUnused);
   }
   return rc;
diff --git a/third_party/sqlite/src/src/os_win.c b/third_party/sqlite/src/src/os_win.c
index aafc89f7d2d5..76743781a019 100644
--- a/third_party/sqlite/src/src/os_win.c
+++ b/third_party/sqlite/src/src/os_win.c
@@ -6130,4 +6130,12 @@ int sqlite3_os_end(void){
   return SQLITE_OK;
 }

+CHROMIUM_SQLITE_API
+void chromium_sqlite3_initialize_win_sqlite3_file(sqlite3_file* file, HANDLE handle) {
+  winFile* winSQLite3File = (winFile*)file;
+  memset(file, 0, sizeof(*file));
+  winSQLite3File->pMethod = &winIoMethod;
+  winSQLite3File->h = handle;
+}
+
 #endif /* SQLITE_OS_WIN */
diff --git a/third_party/sqlite/src/src/sqlite.h.in b/third_party/sqlite/src/src/sqlite.h.in
index cf17bc015fa7..11622a49697f 100644
--- a/third_party/sqlite/src/src/sqlite.h.in
+++ b/third_party/sqlite/src/src/sqlite.h.in
@@ -8378,6 +8378,29 @@ int sqlite3_strnicmp(const char *, const char *, int);
 */
 int sqlite3_strglob(const char *zGlob, const char *zStr);

+/* Begin WebDatabase patch for Chromium */
+/* Expose some SQLite internals for the WebDatabase vfs.
+** DO NOT EXTEND THE USE OF THIS.
+*/
+#ifndef CHROMIUM_SQLITE_API
+#define CHROMIUM_SQLITE_API SQLITE_API
+#endif
+#if defined(CHROMIUM_SQLITE_INTERNALS)
+#ifdef _WIN32
+CHROMIUM_SQLITE_API
+void chromium_sqlite3_initialize_win_sqlite3_file(sqlite3_file* file, HANDLE handle);
+#else  /* _WIN32 */
+CHROMIUM_SQLITE_API
+int chromium_sqlite3_fill_in_unix_sqlite3_file(sqlite3_vfs* pVfs,
+                                               int fd,
+                                               sqlite3_file* pFile,
+                                               const char* zPath,
+                                               int noLock,
+                                               int flags);
+#endif  /* _WIN32 */
+#endif  /* CHROMIUM_SQLITE_INTERNALS */
+/* End WebDatabase patch for Chromium */
+
 /*
 ** CAPI3REF: String LIKE Matching
 *
--
2.18.0

