OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (C) 2009 Google Inc. All rights reserved. | 2 * Copyright (C) 2009 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
45 int chromium_sqlite3_get_reusable_file_handle(sqlite3_file* file, const char* fi leName, int flags, int* fd); | 45 int chromium_sqlite3_get_reusable_file_handle(sqlite3_file* file, const char* fi leName, int flags, int* fd); |
46 void chromium_sqlite3_update_reusable_file_handle(sqlite3_file* file, int fd, in t flags); | 46 void chromium_sqlite3_update_reusable_file_handle(sqlite3_file* file, int fd, in t flags); |
47 void chromium_sqlite3_destroy_reusable_file_handle(sqlite3_file* file); | 47 void chromium_sqlite3_destroy_reusable_file_handle(sqlite3_file* file); |
48 } | 48 } |
49 | 49 |
50 namespace blink { | 50 namespace blink { |
51 | 51 |
52 // Chromium's Posix implementation of SQLite VFS | 52 // Chromium's Posix implementation of SQLite VFS |
53 namespace { | 53 namespace { |
54 | 54 |
55 struct chromiumVfsFile { | |
56 sqlite3_io_methods* pMethods; | |
57 sqlite3_file* realFile; | |
58 char* fileName; | |
59 }; | |
60 | |
61 int chromiumClose(sqlite3_file* fi) | |
62 { | |
63 chromiumVfsFile* f = (chromiumVfsFile*)fi; | |
64 int r = f->realFile->pMethods->xClose(f->realFile); | |
65 sqlite3_free(f->fileName); | |
66 sqlite3_free(f->realFile); | |
67 memset(f, 0, sizeof(*f)); | |
68 return r; | |
69 } | |
70 | |
71 int chromiumRead(sqlite3_file* fi, void* pBuf, int iAmt, sqlite3_int64 iOfst) | |
72 { | |
73 chromiumVfsFile* f = (chromiumVfsFile*)fi; | |
74 return f->realFile->pMethods->xRead(f->realFile, pBuf, iAmt, iOfst); | |
75 } | |
76 | |
77 int chromiumWrite(sqlite3_file* fi, const void* pBuf, int iAmt, sqlite3_int64 iO fst) | |
78 { | |
79 chromiumVfsFile* f = (chromiumVfsFile*)fi; | |
80 return f->realFile->pMethods->xWrite(f->realFile, pBuf, iAmt, iOfst); | |
81 } | |
82 | |
83 int chromiumTruncate(sqlite3_file* fi, sqlite3_int64 size) | |
84 { | |
85 chromiumVfsFile* f = (chromiumVfsFile*)fi; | |
86 | |
87 // The OSX and Linux sandboxes block ftruncate(), proxy to the browser | |
88 // process. | |
89 if (Platform::current()->databaseSetFileSize(String(f->fileName), size)) | |
90 return SQLITE_OK; | |
91 return SQLITE_IOERR_TRUNCATE; | |
92 } | |
93 | |
94 int chromiumSync(sqlite3_file* fi, int flags) | |
95 { | |
96 chromiumVfsFile* f = (chromiumVfsFile*)fi; | |
97 return f->realFile->pMethods->xSync(f->realFile, flags); | |
98 } | |
99 | |
100 int chromiumFileSize(sqlite3_file* fi, sqlite3_int64 *pSize) | |
101 { | |
102 chromiumVfsFile* f = (chromiumVfsFile*)fi; | |
103 return f->realFile->pMethods->xFileSize(f->realFile, pSize); | |
104 } | |
105 | |
106 int chromiumLock(sqlite3_file* fi, int eFileLock) | |
107 { | |
108 chromiumVfsFile* f = (chromiumVfsFile*)fi; | |
109 return f->realFile->pMethods->xLock(f->realFile, eFileLock); | |
110 } | |
111 | |
112 int chromiumUnlock(sqlite3_file* fi, int eFileLock) | |
113 { | |
114 chromiumVfsFile* f = (chromiumVfsFile*)fi; | |
115 return f->realFile->pMethods->xUnlock(f->realFile, eFileLock); | |
116 } | |
117 | |
118 int chromiumCheckReservedLock(sqlite3_file* fi, int *pResOut) | |
119 { | |
120 chromiumVfsFile* f = (chromiumVfsFile*)fi; | |
121 return f->realFile->pMethods->xCheckReservedLock(f->realFile, pResOut); | |
122 } | |
123 | |
124 int chromiumFileControl(sqlite3_file* fi, int op, void *pArg) | |
125 { | |
126 chromiumVfsFile* f = (chromiumVfsFile*)fi; | |
127 return f->realFile->pMethods->xFileControl(f->realFile, op, pArg); | |
128 } | |
129 | |
130 int chromiumSectorSize(sqlite3_file* fi) | |
131 { | |
132 chromiumVfsFile* f = (chromiumVfsFile*)fi; | |
133 return f->realFile->pMethods->xSectorSize(f->realFile); | |
134 } | |
135 | |
136 int chromiumDeviceCharacteristics(sqlite3_file* fi) | |
137 { | |
138 chromiumVfsFile* f = (chromiumVfsFile*)fi; | |
139 return f->realFile->pMethods->xDeviceCharacteristics(f->realFile); | |
140 } | |
141 | |
55 // Opens a file. | 142 // Opens a file. |
56 // | 143 // |
57 // vfs - pointer to the sqlite3_vfs object. | 144 // vfs - pointer to the sqlite3_vfs object. |
58 // fileName - the name of the file. | 145 // fileName - the name of the file. |
59 // id - the structure that will manipulate the newly opened file. | 146 // id - the structure that will manipulate the newly opened file. |
60 // desiredFlags - the desired open mode flags. | 147 // desiredFlags - the desired open mode flags. |
61 // usedFlags - the actual open mode flags that were used. | 148 // usedFlags - the actual open mode flags that were used. |
62 int chromiumOpen(sqlite3_vfs* vfs, const char* fileName, | 149 int chromiumOpenInternal(sqlite3_vfs* vfs, const char* fileName, |
63 sqlite3_file* id, int desiredFlags, int* usedFlags) | 150 sqlite3_file* id, int desiredFlags, int* usedFlags) |
Scott Hess - ex-Googler
2015/03/18 01:45:46
Still figuring out why this indentation is throwin
michaeln
2015/03/19 00:14:04
probably it predates the tool and the checks
Scott Hess - ex-Googler
2015/03/25 20:19:55
Looks like I can just not wrap it...
| |
64 { | 151 { |
65 chromium_sqlite3_initialize_unix_sqlite3_file(id); | 152 chromium_sqlite3_initialize_unix_sqlite3_file(id); |
66 int fd = -1; | 153 int fd = -1; |
67 int result = chromium_sqlite3_get_reusable_file_handle(id, fileName, desired Flags, &fd); | 154 int result = chromium_sqlite3_get_reusable_file_handle(id, fileName, desired Flags, &fd); |
68 if (result != SQLITE_OK) | 155 if (result != SQLITE_OK) |
69 return result; | 156 return result; |
70 | 157 |
71 if (fd < 0) { | 158 if (fd < 0) { |
72 fd = Platform::current()->databaseOpenFile(String(fileName), desiredFlag s); | 159 fd = Platform::current()->databaseOpenFile(String(fileName), desiredFlag s); |
73 if ((fd < 0) && (desiredFlags & SQLITE_OPEN_READWRITE)) { | 160 if ((fd < 0) && (desiredFlags & SQLITE_OPEN_READWRITE)) { |
(...skipping 14 matching lines...) Expand all Loading... | |
88 | 175 |
89 // The mask 0x00007F00 gives us the 7 bits that determine the type of the fi le SQLite is trying to open. | 176 // The mask 0x00007F00 gives us the 7 bits that determine the type of the fi le SQLite is trying to open. |
90 int fileType = desiredFlags & 0x00007F00; | 177 int fileType = desiredFlags & 0x00007F00; |
91 int noLock = (fileType != SQLITE_OPEN_MAIN_DB); | 178 int noLock = (fileType != SQLITE_OPEN_MAIN_DB); |
92 result = chromium_sqlite3_fill_in_unix_sqlite3_file(vfs, fd, -1, id, fileNam e, noLock); | 179 result = chromium_sqlite3_fill_in_unix_sqlite3_file(vfs, fd, -1, id, fileNam e, noLock); |
93 if (result != SQLITE_OK) | 180 if (result != SQLITE_OK) |
94 chromium_sqlite3_destroy_reusable_file_handle(id); | 181 chromium_sqlite3_destroy_reusable_file_handle(id); |
95 return result; | 182 return result; |
96 } | 183 } |
97 | 184 |
185 // SQLite allocates a buffer using szOsFile then calls xOpen() on it. registerS QLiteVFS() caches the size of the "unix" | |
186 // vfs structure for use by chromiumOpen(). | |
187 static size_t szUnixFile = 0; | |
188 | |
189 int chromiumOpen(sqlite3_vfs* vfs, const char* fileName, | |
190 sqlite3_file* id, int desiredFlags, int* usedFlags) | |
Scott Hess - ex-Googler
2015/03/18 01:45:46
This indentation is throwing pre-submit complaints
| |
191 { | |
192 // registerSQLiteVFS() never happened. | |
193 if (!szUnixFile) | |
194 return SQLITE_ERROR; | |
195 | |
196 sqlite3_file* realFile = (sqlite3_file*)sqlite3_malloc(szUnixFile); | |
197 if (!realFile) | |
198 return SQLITE_NOMEM; | |
199 | |
200 // Make a local copy of the file name. SQLite's os_unix.c appears to be wri tten to allow caching the pointer passed | |
201 // in to this function, but that seems brittle. | |
202 char* realFileName = sqlite3_mprintf("%s", fileName); | |
203 if (!realFileName) { | |
204 sqlite3_free(realFile); | |
205 return SQLITE_NOMEM; | |
206 } | |
207 | |
208 // SQLite's unixOpen() makes assumptions about the structure of |fileName|. Our local copy may not answer those | |
209 // assumptions correctly. | |
210 int rc = chromiumOpenInternal(vfs, fileName, realFile, desiredFlags, usedFla gs); | |
211 if (rc != SQLITE_OK) { | |
212 sqlite3_free(realFileName); | |
213 sqlite3_free(realFile); | |
214 return rc; | |
215 } | |
216 | |
217 static sqlite3_io_methods chromiumIoMethods = { | |
218 1, | |
219 chromiumClose, | |
220 chromiumRead, | |
221 chromiumWrite, | |
222 chromiumTruncate, | |
223 chromiumSync, | |
224 chromiumFileSize, | |
225 chromiumLock, | |
226 chromiumUnlock, | |
227 chromiumCheckReservedLock, | |
228 chromiumFileControl, | |
229 chromiumSectorSize, | |
230 chromiumDeviceCharacteristics, | |
231 // Methods above are valid for version 1. | |
michaeln
2015/03/19 00:14:04
Looks like the default posix io methods provides t
Scott Hess - ex-Googler
2015/03/25 20:19:55
My understanding is that the version-2 bits are fo
| |
232 }; | |
233 chromiumVfsFile* f = (chromiumVfsFile*)id; | |
234 f->pMethods = &chromiumIoMethods; | |
235 f->realFile = realFile; | |
236 f->fileName = realFileName; | |
237 return SQLITE_OK; | |
238 } | |
239 | |
98 // Deletes the given file. | 240 // Deletes the given file. |
99 // | 241 // |
100 // vfs - pointer to the sqlite3_vfs object. | 242 // vfs - pointer to the sqlite3_vfs object. |
101 // fileName - the name of the file. | 243 // fileName - the name of the file. |
102 // syncDir - determines if the directory to which this file belongs | 244 // syncDir - determines if the directory to which this file belongs |
103 // should be synched after the file is deleted. | 245 // should be synched after the file is deleted. |
104 int chromiumDelete(sqlite3_vfs*, const char* fileName, int syncDir) | 246 int chromiumDelete(sqlite3_vfs*, const char* fileName, int syncDir) |
105 { | 247 { |
106 return Platform::current()->databaseDeleteFile(String(fileName), syncDir); | 248 return Platform::current()->databaseDeleteFile(String(fileName), syncDir); |
107 } | 249 } |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
162 } | 304 } |
163 #else | 305 #else |
164 #define chromiumDlOpen 0 | 306 #define chromiumDlOpen 0 |
165 #endif // SQLITE_OMIT_LOAD_EXTENSION | 307 #endif // SQLITE_OMIT_LOAD_EXTENSION |
166 | 308 |
167 } // namespace | 309 } // namespace |
168 | 310 |
169 void SQLiteFileSystem::registerSQLiteVFS() | 311 void SQLiteFileSystem::registerSQLiteVFS() |
170 { | 312 { |
171 sqlite3_vfs* unix_vfs = sqlite3_vfs_find("unix"); | 313 sqlite3_vfs* unix_vfs = sqlite3_vfs_find("unix"); |
314 szUnixFile = unix_vfs->szOsFile; | |
172 static sqlite3_vfs chromium_vfs = { | 315 static sqlite3_vfs chromium_vfs = { |
173 1, | 316 1, |
174 unix_vfs->szOsFile, | 317 sizeof(chromiumVfsFile), |
175 unix_vfs->mxPathname, | 318 unix_vfs->mxPathname, |
176 0, | 319 0, |
177 "chromium_vfs", | 320 "chromium_vfs", |
178 unix_vfs->pAppData, | 321 unix_vfs->pAppData, |
michaeln
2015/03/19 00:14:04
this func ptr is supposed to be a FINDER function
Scott Hess - ex-Googler
2015/03/25 20:19:55
pAppData is just a place you can tuck random infor
| |
179 chromiumOpen, | 322 chromiumOpen, |
180 chromiumDelete, | 323 chromiumDelete, |
181 chromiumAccess, | 324 chromiumAccess, |
182 chromiumFullPathname, | 325 chromiumFullPathname, |
183 chromiumDlOpen, | 326 chromiumDlOpen, |
184 unix_vfs->xDlError, | 327 unix_vfs->xDlError, |
185 unix_vfs->xDlSym, | 328 unix_vfs->xDlSym, |
186 unix_vfs->xDlClose, | 329 unix_vfs->xDlClose, |
187 unix_vfs->xRandomness, | 330 unix_vfs->xRandomness, |
188 unix_vfs->xSleep, | 331 unix_vfs->xSleep, |
189 unix_vfs->xCurrentTime, | 332 unix_vfs->xCurrentTime, |
190 unix_vfs->xGetLastError | 333 unix_vfs->xGetLastError |
191 }; | 334 }; |
192 sqlite3_vfs_register(&chromium_vfs, 0); | 335 sqlite3_vfs_register(&chromium_vfs, 0); |
193 } | 336 } |
194 | 337 |
195 } // namespace blink | 338 } // namespace blink |
OLD | NEW |