1 From 67024286d2719aec049605896ef0afec6932f184 Mon Sep 17 00:00:00 2001
2 From: dumi <dumi@chromium.org>
3 Date: Mon, 20 Jul 2009 23:40:51 +0000
4 Subject: [PATCH 05/16] Modify default VFS to support WebDatabase.
6 The renderer WebDatabase implementation needs to broker certain requests
7 to the browser. This modifies SQLite to allow monkey-patching the VFS
10 NOTE(shess): This patch relies on core SQLite implementation details
11 remaining unchanged. When importing a new version of SQLite, pay very
12 close attention to whether the change is still doing what is intended.
15 https://codereview.chromium.org/159044
16 https://codereview.chromium.org/384075
17 https://codereview.chromium.org/377039
18 [Possibly not a complete list.]
20 third_party/sqlite/src/src/os_unix.c | 95 ++++++++++++++++++++++++++++++------
21 third_party/sqlite/src/src/os_win.c | 7 +++
22 2 files changed, 86 insertions(+), 16 deletions(-)
24 diff --git a/third_party/sqlite/src/src/os_unix.c b/third_party/sqlite/src/src/os_unix.c
25 index a9344ee..1624f6a 100644
26 --- a/third_party/sqlite/src/src/os_unix.c
27 +++ b/third_party/sqlite/src/src/os_unix.c
28 @@ -1321,6 +1321,12 @@ static int fileHasMoved(unixFile *pFile){
29 return pFile->pInode!=0 && pFile->pId!=pFile->pInode->fileId.pId;
33 + /* TODO(shess): This check doesn't work when the Chromium's WebDB code is
34 + ** running in the sandbox.
38 return pFile->pInode!=0 &&
39 (osStat(pFile->zPath, &buf)!=0 || buf.st_ino!=pFile->pInode->fileId.ino);
41 @@ -5615,6 +5621,73 @@ static int findCreateFileMode(
45 +** Initializes a unixFile structure with zeros.
47 +void chromium_sqlite3_initialize_unix_sqlite3_file(sqlite3_file* file) {
48 + memset(file, 0, sizeof(unixFile));
51 +int chromium_sqlite3_fill_in_unix_sqlite3_file(sqlite3_vfs* vfs,
55 + const char* fileName,
57 + int ctrlFlags = (noLock ? UNIXFILE_NOLOCK : 0);
58 + return fillInUnixFile(vfs, fd, file, fileName, ctrlFlags);
62 +** Search for an unused file descriptor that was opened on the database file.
63 +** If a suitable file descriptor if found, then it is stored in *fd; otherwise,
64 +** *fd is not modified.
66 +** If a reusable file descriptor is not found, and a new UnixUnusedFd cannot
67 +** be allocated, SQLITE_NOMEM is returned. Otherwise, SQLITE_OK is returned.
69 +int chromium_sqlite3_get_reusable_file_handle(sqlite3_file* file,
70 + const char* fileName,
73 + unixFile* unixSQLite3File = (unixFile*)file;
74 + int fileType = flags & 0xFFFFFF00;
75 + if (fileType == SQLITE_OPEN_MAIN_DB) {
76 + UnixUnusedFd *unusedFd = findReusableFd(fileName, flags);
80 + unusedFd = sqlite3_malloc(sizeof(*unusedFd));
82 + return SQLITE_NOMEM;
85 + unixSQLite3File->pUnused = unusedFd;
91 +** Marks 'fd' as the unused file descriptor for 'pFile'.
93 +void chromium_sqlite3_update_reusable_file_handle(sqlite3_file* file,
96 + unixFile* unixSQLite3File = (unixFile*)file;
97 + if (unixSQLite3File->pUnused) {
98 + unixSQLite3File->pUnused->fd = fd;
99 + unixSQLite3File->pUnused->flags = flags;
104 +** Destroys pFile's field that keeps track of the unused file descriptor.
106 +void chromium_sqlite3_destroy_reusable_file_handle(sqlite3_file* file) {
107 + unixFile* unixSQLite3File = (unixFile*)file;
108 + sqlite3_free(unixSQLite3File->pUnused);
112 ** Open the file zPath.
114 ** Previously, the SQLite OS layer used three functions in place of this
115 @@ -5715,20 +5788,13 @@ static int unixOpen(
116 sqlite3_randomness(0,0);
119 - memset(p, 0, sizeof(unixFile));
120 + chromium_sqlite3_initialize_unix_sqlite3_file(pFile);
122 if( eType==SQLITE_OPEN_MAIN_DB ){
123 - UnixUnusedFd *pUnused;
124 - pUnused = findReusableFd(zName, flags);
128 - pUnused = sqlite3_malloc(sizeof(*pUnused));
130 - return SQLITE_NOMEM;
132 + rc = chromium_sqlite3_get_reusable_file_handle(pFile, zName, flags, &fd);
133 + if( rc!=SQLITE_OK ){
136 - p->pUnused = pUnused;
138 /* Database filenames are double-zero terminated if they are not
139 ** URIs with parameters. Hence, they can always be passed into
140 @@ -5798,10 +5864,7 @@ static int unixOpen(
145 - p->pUnused->fd = fd;
146 - p->pUnused->flags = flags;
148 + chromium_sqlite3_update_reusable_file_handle(pFile, fd, flags);
152 @@ -5893,7 +5956,7 @@ static int unixOpen(
156 - sqlite3_free(p->pUnused);
157 + chromium_sqlite3_destroy_reusable_file_handle(pFile);
161 diff --git a/third_party/sqlite/src/src/os_win.c b/third_party/sqlite/src/src/os_win.c
162 index 8ca2107..5b0a296 100644
163 --- a/third_party/sqlite/src/src/os_win.c
164 +++ b/third_party/sqlite/src/src/os_win.c
165 @@ -5546,4 +5546,11 @@ int sqlite3_os_end(void){
169 +void chromium_sqlite3_initialize_win_sqlite3_file(sqlite3_file* file, HANDLE handle) {
170 + winFile* winSQLite3File = (winFile*)file;
171 + memset(file, 0, sizeof(*file));
172 + winSQLite3File->pMethod = &winIoMethod;
173 + winSQLite3File->h = handle;
176 #endif /* SQLITE_OS_WIN */