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 "modules/webdatabase/sqlite/SQLiteFileSystem.h"
33
34 #include <sqlite3.h>
35 #include "public/platform/Platform.h"
36
37 #include <fcntl.h>
38 #include <string.h>
39 #include <unistd.h>
40
41 // Defined in Chromium's codebase in third_party/sqlite/src/os_unix.c
42 extern "C" {
43 void chromium_sqlite3_initialize_unix_sqlite3_file(sqlite3_file* file);
44 int chromium_sqlite3_fill_in_unix_sqlite3_file(sqlite3_vfs* vfs, int fd, int dirfd, sqlite3_file* file, const char* fileName, int noLock);
45 int chromium_sqlite3_get_reusable_file_handle(sqlite3_file* file, const char* fileName, int flags, int* fd);
46 void chromium_sqlite3_update_reusable_file_handle(sqlite3_file* file, int fd, int flags);
47 void chromium_sqlite3_destroy_reusable_file_handle(sqlite3_file* file);
48 }
49
50 namespace blink {
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 result = chromium_sqlite3_get_reusable_file_handle(id, fileName, desiredFlags, &fd);
68 if (result != SQLITE_OK)
69 return result;
70
71 if (fd < 0) {
72 fd = Platform::current()->databaseOpenFile(String(fileName), desiredFlags);
73 if ((fd < 0) && (desiredFlags & SQLITE_OPEN_READWRITE)) {
74 int newFlags = (desiredFlags & ~(SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE)) | SQLITE_OPEN_READONLY;
75 fd = Platform::current()->databaseOpenFile(String(fileName), newFlags);
76 }
77 }
78 if (fd < 0) {
79 chromium_sqlite3_destroy_reusable_file_handle(id);
80 return SQLITE_CANTOPEN;
81 }
82
83 if (usedFlags)
84 *usedFlags = desiredFlags;
85 chromium_sqlite3_update_reusable_file_handle(id, fd, desiredFlags);
86
87 fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
88
89 // The mask 0x00007F00 gives us the 7 bits that determine the type of the file SQLite is trying to open.
90 int fileType = desiredFlags & 0x00007F00;
91 int noLock = (fileType != SQLITE_OPEN_MAIN_DB);
92 result = chromium_sqlite3_fill_in_unix_sqlite3_file(vfs, fd, -1, id, fileName, noLock);
93 if (result != SQLITE_OK)
94 chromium_sqlite3_destroy_reusable_file_handle(id);
95 return result;
96 }
97
98 // Deletes the given file.
99 //
100 // vfs - pointer to the sqlite3_vfs object.
101 // fileName - the name of the file.
102 // syncDir - determines if the directory to which this file belongs
103 // should be synched after the file is deleted.
chromiumDelete(sqlite3_vfs *,const char * fileName,int syncDir)104 int chromiumDelete(sqlite3_vfs*, const char* fileName, int syncDir)
105 {
106 return Platform::current()->databaseDeleteFile(String(fileName), syncDir);
107 }
108
109 // Check the existance and status of the given file.
110 //
111 // vfs - pointer to the sqlite3_vfs object.
112 // fileName - the name of the file.
113 // flag - the type of test to make on this file.
114 // res - the result.
chromiumAccess(sqlite3_vfs *,const char * fileName,int flag,int * res)115 int chromiumAccess(sqlite3_vfs*, const char* fileName, int flag, int* res)
116 {
117 int attr = static_cast<int>(Platform::current()->databaseGetFileAttributes(String(fileName)));
118 if (attr < 0) {
119 *res = 0;
120 return SQLITE_OK;
121 }
122
123 switch (flag) {
124 case SQLITE_ACCESS_EXISTS:
125 *res = 1; // if the file doesn't exist, attr < 0
126 break;
127 case SQLITE_ACCESS_READWRITE:
128 *res = (attr & W_OK) && (attr & R_OK);
129 break;
130 case SQLITE_ACCESS_READ:
131 *res = (attr & R_OK);
132 break;
133 default:
134 return SQLITE_ERROR;
135 }
136
137 return SQLITE_OK;
138 }
139
140 // Turns a relative pathname into a full pathname.
141 //
142 // vfs - pointer to the sqlite3_vfs object.
143 // relativePath - the relative path.
144 // bufSize - the size of the output buffer in bytes.
145 // absolutePath - the output buffer where the absolute path will be stored.
chromiumFullPathname(sqlite3_vfs * vfs,const char * relativePath,int,char * absolutePath)146 int chromiumFullPathname(sqlite3_vfs* vfs, const char* relativePath,
147 int, char* absolutePath)
148 {
149 // The renderer process doesn't need to know the absolute path of the file
150 sqlite3_snprintf(vfs->mxPathname, absolutePath, "%s", relativePath);
151 return SQLITE_OK;
152 }
153
154 #ifndef SQLITE_OMIT_LOAD_EXTENSION
155 // Returns NULL, thus disallowing loading libraries in the renderer process.
156 //
157 // vfs - pointer to the sqlite3_vfs object.
158 // fileName - the name of the shared library file.
chromiumDlOpen(sqlite3_vfs *,const char *)159 void* chromiumDlOpen(sqlite3_vfs*, const char*)
160 {
161 return 0;
162 }
163 #else
164 #define chromiumDlOpen 0
165 #endif // SQLITE_OMIT_LOAD_EXTENSION
166
167 } // namespace
168
registerSQLiteVFS()169 void SQLiteFileSystem::registerSQLiteVFS()
170 {
171 sqlite3_vfs* unix_vfs = sqlite3_vfs_find("unix");
172 static sqlite3_vfs chromium_vfs = {
173 1,
174 unix_vfs->szOsFile,
175 unix_vfs->mxPathname,
176 0,
177 "chromium_vfs",
178 unix_vfs->pAppData,
179 chromiumOpen,
180 chromiumDelete,
181 chromiumAccess,
182 chromiumFullPathname,
183 chromiumDlOpen,
184 unix_vfs->xDlError,
185 unix_vfs->xDlSym,
186 unix_vfs->xDlClose,
187 unix_vfs->xRandomness,
188 unix_vfs->xSleep,
189 unix_vfs->xCurrentTime,
190 unix_vfs->xGetLastError
191 };
192 sqlite3_vfs_register(&chromium_vfs, 0);
193 }
194
195 } // namespace blink
196