• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2009 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
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
13  * distribution.
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.
17  *
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.
29  */
30 
31 #include "config.h"
32 #include "SQLiteFileSystem.h"
33 
34 #include "ChromiumBridge.h"
35 #include <sqlite3.h>
36 
37 #include <fcntl.h>
38 #include <string.h>
39 #include <unistd.h>
40 
41 using namespace WebCore;
42 
43 // Defined in Chromium's codebase in third_party/sqlite/src/os_unix.c
44 extern "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);
50 }
51 
52 // Chromium's Posix implementation of SQLite VFS
53 namespace {
54 
55 // Opens a file.
56 //
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.
chromiumOpen(sqlite3_vfs * vfs,const char * fileName,sqlite3_file * id,int desiredFlags,int * usedFlags)62 int chromiumOpen(sqlite3_vfs* vfs, const char* fileName,
63                  sqlite3_file* id, int desiredFlags, int* usedFlags)
64 {
65     chromium_sqlite3_initialize_unix_sqlite3_file(id);
66     int fd = -1;
67     int dirfd = -1;
68     int result = chromium_sqlite3_get_reusable_file_handle(id, fileName, desiredFlags, &fd);
69     if (result != SQLITE_OK)
70         return result;
71 
72     if (fd < 0) {
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);
77         }
78     }
79     if (fd < 0) {
80         chromium_sqlite3_destroy_reusable_file_handle(id);
81         return SQLITE_CANTOPEN;
82     }
83 
84     if (usedFlags)
85         *usedFlags = desiredFlags;
86     chromium_sqlite3_update_reusable_file_handle(id, fd, desiredFlags);
87 
88     fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
89     if (dirfd >= 0)
90         fcntl(dirfd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
91 
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);
98     return result;
99 }
100 
101 // Deletes the given file.
102 //
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.
chromiumDelete(sqlite3_vfs *,const char * fileName,int syncDir)107 int chromiumDelete(sqlite3_vfs*, const char* fileName, int syncDir)
108 {
109     return ChromiumBridge::databaseDeleteFile(fileName, syncDir);
110 }
111 
112 // Check the existance and status of the given file.
113 //
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.
117 // res - the result.
chromiumAccess(sqlite3_vfs *,const char * fileName,int flag,int * res)118 int chromiumAccess(sqlite3_vfs*, const char* fileName, int flag, int* res)
119 {
120     int attr = static_cast<int>(ChromiumBridge::databaseGetFileAttributes(fileName));
121     if (attr < 0) {
122         *res = 0;
123         return SQLITE_OK;
124     }
125 
126     switch (flag) {
127     case SQLITE_ACCESS_EXISTS:
128         *res = 1;   // if the file doesn't exist, attr < 0
129         break;
130     case SQLITE_ACCESS_READWRITE:
131         *res = (attr & W_OK) && (attr & R_OK);
132         break;
133     case SQLITE_ACCESS_READ:
134         *res = (attr & R_OK);
135         break;
136     default:
137         return SQLITE_ERROR;
138     }
139 
140     return SQLITE_OK;
141 }
142 
143 // Turns a relative pathname into a full pathname.
144 //
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.
chromiumFullPathname(sqlite3_vfs * vfs,const char * relativePath,int,char * absolutePath)149 int chromiumFullPathname(sqlite3_vfs* vfs, const char* relativePath,
150                          int, char* absolutePath)
151 {
152     // The renderer process doesn't need to know the absolute path of the file
153     sqlite3_snprintf(vfs->mxPathname, absolutePath, "%s", relativePath);
154     return SQLITE_OK;
155 }
156 
157 #ifndef SQLITE_OMIT_LOAD_EXTENSION
158 // Returns NULL, thus disallowing loading libraries in the renderer process.
159 //
160 // vfs - pointer to the sqlite3_vfs object.
161 // fileName - the name of the shared library file.
chromiumDlOpen(sqlite3_vfs *,const char *)162 void* chromiumDlOpen(sqlite3_vfs*, const char*)
163 {
164     return 0;
165 }
166 #else
167 #define chromiumDlOpen 0
168 #endif // SQLITE_OMIT_LOAD_EXTENSION
169 
170 } // namespace
171 
172 namespace WebCore {
173 
registerSQLiteVFS()174 void SQLiteFileSystem::registerSQLiteVFS()
175 {
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();
179         return;
180     }
181 
182     sqlite3_vfs* unix_vfs = sqlite3_vfs_find("unix");
183     static sqlite3_vfs chromium_vfs = {
184         1,
185         unix_vfs->szOsFile,
186         unix_vfs->mxPathname,
187         0,
188         "chromium_vfs",
189         unix_vfs->pAppData,
190         chromiumOpen,
191         chromiumDelete,
192         chromiumAccess,
193         chromiumFullPathname,
194         chromiumDlOpen,
195         unix_vfs->xDlError,
196         unix_vfs->xDlSym,
197         unix_vfs->xDlClose,
198         unix_vfs->xRandomness,
199         unix_vfs->xSleep,
200         unix_vfs->xCurrentTime,
201         unix_vfs->xGetLastError
202     };
203     sqlite3_vfs_register(&chromium_vfs, 0);
204 }
205 
206 } // namespace WebCore
207