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 20 matching lines...) Expand all Loading... |
31 #include "config.h" | 31 #include "config.h" |
32 #include "modules/webdatabase/sqlite/SQLiteFileSystem.h" | 32 #include "modules/webdatabase/sqlite/SQLiteFileSystem.h" |
33 | 33 |
34 #include <sqlite3.h> | 34 #include <sqlite3.h> |
35 #include "public/platform/Platform.h" | 35 #include "public/platform/Platform.h" |
36 | 36 |
37 #include <fcntl.h> | 37 #include <fcntl.h> |
38 #include <string.h> | 38 #include <string.h> |
39 #include <unistd.h> | 39 #include <unistd.h> |
40 | 40 |
41 using namespace blink; | |
42 | |
43 // Defined in Chromium's codebase in third_party/sqlite/src/os_unix.c | 41 // Defined in Chromium's codebase in third_party/sqlite/src/os_unix.c |
44 extern "C" { | 42 extern "C" { |
45 void chromium_sqlite3_initialize_unix_sqlite3_file(sqlite3_file* file); | 43 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 dir
fd, sqlite3_file* file, const char* fileName, int noLock); | 44 int chromium_sqlite3_fill_in_unix_sqlite3_file(sqlite3_vfs* vfs, int fd, int dir
fd, sqlite3_file* file, const char* fileName, int noLock); |
47 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); |
48 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); |
49 void chromium_sqlite3_destroy_reusable_file_handle(sqlite3_file* file); | 47 void chromium_sqlite3_destroy_reusable_file_handle(sqlite3_file* file); |
50 } | 48 } |
51 | 49 |
| 50 namespace blink { |
| 51 |
52 // Chromium's Posix implementation of SQLite VFS | 52 // Chromium's Posix implementation of SQLite VFS |
53 namespace { | 53 namespace { |
54 | 54 |
55 // Opens a file. | 55 // Opens a file. |
56 // | 56 // |
57 // vfs - pointer to the sqlite3_vfs object. | 57 // vfs - pointer to the sqlite3_vfs object. |
58 // fileName - the name of the file. | 58 // fileName - the name of the file. |
59 // id - the structure that will manipulate the newly opened file. | 59 // id - the structure that will manipulate the newly opened file. |
60 // desiredFlags - the desired open mode flags. | 60 // desiredFlags - the desired open mode flags. |
61 // usedFlags - the actual open mode flags that were used. | 61 // usedFlags - the actual open mode flags that were used. |
62 int chromiumOpen(sqlite3_vfs* vfs, const char* fileName, | 62 int chromiumOpen(sqlite3_vfs* vfs, const char* fileName, |
63 sqlite3_file* id, int desiredFlags, int* usedFlags) | 63 sqlite3_file* id, int desiredFlags, int* usedFlags) |
64 { | 64 { |
65 chromium_sqlite3_initialize_unix_sqlite3_file(id); | 65 chromium_sqlite3_initialize_unix_sqlite3_file(id); |
66 int fd = -1; | 66 int fd = -1; |
67 int result = chromium_sqlite3_get_reusable_file_handle(id, fileName, desired
Flags, &fd); | 67 int result = chromium_sqlite3_get_reusable_file_handle(id, fileName, desired
Flags, &fd); |
68 if (result != SQLITE_OK) | 68 if (result != SQLITE_OK) |
69 return result; | 69 return result; |
70 | 70 |
71 if (fd < 0) { | 71 if (fd < 0) { |
72 fd = blink::Platform::current()->databaseOpenFile(String(fileName), desi
redFlags); | 72 fd = Platform::current()->databaseOpenFile(String(fileName), desiredFlag
s); |
73 if ((fd < 0) && (desiredFlags & SQLITE_OPEN_READWRITE)) { | 73 if ((fd < 0) && (desiredFlags & SQLITE_OPEN_READWRITE)) { |
74 int newFlags = (desiredFlags & ~(SQLITE_OPEN_READWRITE | SQLITE_OPEN
_CREATE)) | SQLITE_OPEN_READONLY; | 74 int newFlags = (desiredFlags & ~(SQLITE_OPEN_READWRITE | SQLITE_OPEN
_CREATE)) | SQLITE_OPEN_READONLY; |
75 fd = blink::Platform::current()->databaseOpenFile(String(fileName),
newFlags); | 75 fd = Platform::current()->databaseOpenFile(String(fileName), newFlag
s); |
76 } | 76 } |
77 } | 77 } |
78 if (fd < 0) { | 78 if (fd < 0) { |
79 chromium_sqlite3_destroy_reusable_file_handle(id); | 79 chromium_sqlite3_destroy_reusable_file_handle(id); |
80 return SQLITE_CANTOPEN; | 80 return SQLITE_CANTOPEN; |
81 } | 81 } |
82 | 82 |
83 if (usedFlags) | 83 if (usedFlags) |
84 *usedFlags = desiredFlags; | 84 *usedFlags = desiredFlags; |
85 chromium_sqlite3_update_reusable_file_handle(id, fd, desiredFlags); | 85 chromium_sqlite3_update_reusable_file_handle(id, fd, desiredFlags); |
(...skipping 10 matching lines...) Expand all Loading... |
96 } | 96 } |
97 | 97 |
98 // Deletes the given file. | 98 // Deletes the given file. |
99 // | 99 // |
100 // vfs - pointer to the sqlite3_vfs object. | 100 // vfs - pointer to the sqlite3_vfs object. |
101 // fileName - the name of the file. | 101 // fileName - the name of the file. |
102 // syncDir - determines if the directory to which this file belongs | 102 // syncDir - determines if the directory to which this file belongs |
103 // should be synched after the file is deleted. | 103 // should be synched after the file is deleted. |
104 int chromiumDelete(sqlite3_vfs*, const char* fileName, int syncDir) | 104 int chromiumDelete(sqlite3_vfs*, const char* fileName, int syncDir) |
105 { | 105 { |
106 return blink::Platform::current()->databaseDeleteFile(String(fileName), sync
Dir); | 106 return Platform::current()->databaseDeleteFile(String(fileName), syncDir); |
107 } | 107 } |
108 | 108 |
109 // Check the existance and status of the given file. | 109 // Check the existance and status of the given file. |
110 // | 110 // |
111 // vfs - pointer to the sqlite3_vfs object. | 111 // vfs - pointer to the sqlite3_vfs object. |
112 // fileName - the name of the file. | 112 // fileName - the name of the file. |
113 // flag - the type of test to make on this file. | 113 // flag - the type of test to make on this file. |
114 // res - the result. | 114 // res - the result. |
115 int chromiumAccess(sqlite3_vfs*, const char* fileName, int flag, int* res) | 115 int chromiumAccess(sqlite3_vfs*, const char* fileName, int flag, int* res) |
116 { | 116 { |
117 int attr = static_cast<int>(blink::Platform::current()->databaseGetFileAttri
butes(String(fileName))); | 117 int attr = static_cast<int>(Platform::current()->databaseGetFileAttributes(S
tring(fileName))); |
118 if (attr < 0) { | 118 if (attr < 0) { |
119 *res = 0; | 119 *res = 0; |
120 return SQLITE_OK; | 120 return SQLITE_OK; |
121 } | 121 } |
122 | 122 |
123 switch (flag) { | 123 switch (flag) { |
124 case SQLITE_ACCESS_EXISTS: | 124 case SQLITE_ACCESS_EXISTS: |
125 *res = 1; // if the file doesn't exist, attr < 0 | 125 *res = 1; // if the file doesn't exist, attr < 0 |
126 break; | 126 break; |
127 case SQLITE_ACCESS_READWRITE: | 127 case SQLITE_ACCESS_READWRITE: |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
159 void* chromiumDlOpen(sqlite3_vfs*, const char*) | 159 void* chromiumDlOpen(sqlite3_vfs*, const char*) |
160 { | 160 { |
161 return 0; | 161 return 0; |
162 } | 162 } |
163 #else | 163 #else |
164 #define chromiumDlOpen 0 | 164 #define chromiumDlOpen 0 |
165 #endif // SQLITE_OMIT_LOAD_EXTENSION | 165 #endif // SQLITE_OMIT_LOAD_EXTENSION |
166 | 166 |
167 } // namespace | 167 } // namespace |
168 | 168 |
169 namespace blink { | |
170 | |
171 void SQLiteFileSystem::registerSQLiteVFS() | 169 void SQLiteFileSystem::registerSQLiteVFS() |
172 { | 170 { |
173 sqlite3_vfs* unix_vfs = sqlite3_vfs_find("unix"); | 171 sqlite3_vfs* unix_vfs = sqlite3_vfs_find("unix"); |
174 static sqlite3_vfs chromium_vfs = { | 172 static sqlite3_vfs chromium_vfs = { |
175 1, | 173 1, |
176 unix_vfs->szOsFile, | 174 unix_vfs->szOsFile, |
177 unix_vfs->mxPathname, | 175 unix_vfs->mxPathname, |
178 0, | 176 0, |
179 "chromium_vfs", | 177 "chromium_vfs", |
180 unix_vfs->pAppData, | 178 unix_vfs->pAppData, |
181 chromiumOpen, | 179 chromiumOpen, |
182 chromiumDelete, | 180 chromiumDelete, |
183 chromiumAccess, | 181 chromiumAccess, |
184 chromiumFullPathname, | 182 chromiumFullPathname, |
185 chromiumDlOpen, | 183 chromiumDlOpen, |
186 unix_vfs->xDlError, | 184 unix_vfs->xDlError, |
187 unix_vfs->xDlSym, | 185 unix_vfs->xDlSym, |
188 unix_vfs->xDlClose, | 186 unix_vfs->xDlClose, |
189 unix_vfs->xRandomness, | 187 unix_vfs->xRandomness, |
190 unix_vfs->xSleep, | 188 unix_vfs->xSleep, |
191 unix_vfs->xCurrentTime, | 189 unix_vfs->xCurrentTime, |
192 unix_vfs->xGetLastError | 190 unix_vfs->xGetLastError |
193 }; | 191 }; |
194 sqlite3_vfs_register(&chromium_vfs, 0); | 192 sqlite3_vfs_register(&chromium_vfs, 0); |
195 } | 193 } |
196 | 194 |
197 } // namespace blink | 195 } // namespace blink |
OLD | NEW |