2 * Copyright (C) 2009 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "SQLiteFileSystem.h"
34 #include "ChromiumBridge.h"
41 using namespace WebCore;
43 // Defined in Chromium's codebase in third_party/sqlite/src/os_unix.c
45 void chromium_sqlite3_initialize_unix_sqlite3_file(sqlite3_file* file);
46 int chromium_sqlite3_fill_in_unix_sqlite3_file(sqlite3_vfs* vfs, int fd, int dirfd, sqlite3_file* file, const char* fileName, int noLock);
47 int chromium_sqlite3_get_reusable_file_handle(sqlite3_file* file, const char* fileName, int flags, int* fd);
48 void chromium_sqlite3_update_reusable_file_handle(sqlite3_file* file, int fd, int flags);
49 void chromium_sqlite3_destroy_reusable_file_handle(sqlite3_file* file);
52 // Chromium's Posix implementation of SQLite VFS
57 // vfs - pointer to the sqlite3_vfs object.
58 // fileName - the name of the file.
59 // id - the structure that will manipulate the newly opened file.
60 // desiredFlags - the desired open mode flags.
61 // usedFlags - the actual open mode flags that were used.
62 int chromiumOpen(sqlite3_vfs* vfs, const char* fileName,
63 sqlite3_file* id, int desiredFlags, int* usedFlags)
65 chromium_sqlite3_initialize_unix_sqlite3_file(id);
68 int result = chromium_sqlite3_get_reusable_file_handle(id, fileName, desiredFlags, &fd);
69 if (result != SQLITE_OK)
73 fd = ChromiumBridge::databaseOpenFile(fileName, desiredFlags, &dirfd);
74 if ((fd < 0) && (desiredFlags & SQLITE_OPEN_READWRITE)) {
75 int newFlags = (desiredFlags & ~(SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE)) | SQLITE_OPEN_READONLY;
76 fd = ChromiumBridge::databaseOpenFile(fileName, newFlags, &dirfd);
80 chromium_sqlite3_destroy_reusable_file_handle(id);
81 return SQLITE_CANTOPEN;
85 *usedFlags = desiredFlags;
86 chromium_sqlite3_update_reusable_file_handle(id, fd, desiredFlags);
88 fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
90 fcntl(dirfd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
92 // The mask 0x00007F00 gives us the 7 bits that determine the type of the file SQLite is trying to open.
93 int fileType = desiredFlags & 0x00007F00;
94 int noLock = (fileType != SQLITE_OPEN_MAIN_DB);
95 result = chromium_sqlite3_fill_in_unix_sqlite3_file(vfs, fd, dirfd, id, fileName, noLock);
96 if (result != SQLITE_OK)
97 chromium_sqlite3_destroy_reusable_file_handle(id);
101 // Deletes the given file.
103 // vfs - pointer to the sqlite3_vfs object.
104 // fileName - the name of the file.
105 // syncDir - determines if the directory to which this file belongs
106 // should be synched after the file is deleted.
107 int chromiumDelete(sqlite3_vfs*, const char* fileName, int syncDir)
109 return ChromiumBridge::databaseDeleteFile(fileName, syncDir);
112 // Check the existance and status of the given file.
114 // vfs - pointer to the sqlite3_vfs object.
115 // fileName - the name of the file.
116 // flag - the type of test to make on this file.
118 int chromiumAccess(sqlite3_vfs*, const char* fileName, int flag, int* res)
120 int attr = static_cast<int>(ChromiumBridge::databaseGetFileAttributes(fileName));
127 case SQLITE_ACCESS_EXISTS:
128 *res = 1; // if the file doesn't exist, attr < 0
130 case SQLITE_ACCESS_READWRITE:
131 *res = (attr & W_OK) && (attr & R_OK);
133 case SQLITE_ACCESS_READ:
134 *res = (attr & R_OK);
143 // Turns a relative pathname into a full pathname.
145 // vfs - pointer to the sqlite3_vfs object.
146 // relativePath - the relative path.
147 // bufSize - the size of the output buffer in bytes.
148 // absolutePath - the output buffer where the absolute path will be stored.
149 int chromiumFullPathname(sqlite3_vfs* vfs, const char* relativePath,
150 int, char* absolutePath)
152 // The renderer process doesn't need to know the absolute path of the file
153 sqlite3_snprintf(vfs->mxPathname, absolutePath, "%s", relativePath);
157 #ifndef SQLITE_OMIT_LOAD_EXTENSION
158 // Returns NULL, thus disallowing loading libraries in the renderer process.
160 // vfs - pointer to the sqlite3_vfs object.
161 // fileName - the name of the shared library file.
162 void* chromiumDlOpen(sqlite3_vfs*, const char*)
167 #define chromiumDlOpen 0
168 #endif // SQLITE_OMIT_LOAD_EXTENSION
174 void SQLiteFileSystem::registerSQLiteVFS()
176 // FIXME: Make sure there aren't any unintended consequences when VFS code is called in the browser process.
177 if (!ChromiumBridge::sandboxEnabled()) {
178 ASSERT_NOT_REACHED();
182 sqlite3_vfs* unix_vfs = sqlite3_vfs_find("unix");
183 static sqlite3_vfs chromium_vfs = {
186 unix_vfs->mxPathname,
193 chromiumFullPathname,
198 unix_vfs->xRandomness,
200 unix_vfs->xCurrentTime,
201 unix_vfs->xGetLastError
203 sqlite3_vfs_register(&chromium_vfs, 1);
206 } // namespace WebCore