| OLD | NEW | 
|---|
|  | (Empty) | 
| 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 "core/platform/sql/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 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 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); |  | 
| 48 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); |  | 
| 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. |  | 
| 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, desired
     Flags, &fd); |  | 
| 68     if (result != SQLITE_OK) |  | 
| 69         return result; |  | 
| 70 |  | 
| 71     if (fd < 0) { |  | 
| 72         fd = WebKit::Platform::current()->databaseOpenFile(String(fileName), des
     iredFlags); |  | 
| 73         if ((fd < 0) && (desiredFlags & SQLITE_OPEN_READWRITE)) { |  | 
| 74             int newFlags = (desiredFlags & ~(SQLITE_OPEN_READWRITE | SQLITE_OPEN
     _CREATE)) | SQLITE_OPEN_READONLY; |  | 
| 75             fd = WebKit::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 fi
     le 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, fileNam
     e, 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. |  | 
| 104 int chromiumDelete(sqlite3_vfs*, const char* fileName, int syncDir) |  | 
| 105 { |  | 
| 106     return WebKit::Platform::current()->databaseDeleteFile(String(fileName), syn
     cDir); |  | 
| 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. |  | 
| 115 int chromiumAccess(sqlite3_vfs*, const char* fileName, int flag, int* res) |  | 
| 116 { |  | 
| 117     int attr = static_cast<int>(WebKit::Platform::current()->databaseGetFileAttr
     ibutes(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. |  | 
| 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. |  | 
| 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 |  | 
| 169 namespace WebCore { |  | 
| 170 |  | 
| 171 void SQLiteFileSystem::registerSQLiteVFS() |  | 
| 172 { |  | 
| 173     sqlite3_vfs* unix_vfs = sqlite3_vfs_find("unix"); |  | 
| 174     static sqlite3_vfs chromium_vfs = { |  | 
| 175         1, |  | 
| 176         unix_vfs->szOsFile, |  | 
| 177         unix_vfs->mxPathname, |  | 
| 178         0, |  | 
| 179         "chromium_vfs", |  | 
| 180         unix_vfs->pAppData, |  | 
| 181         chromiumOpen, |  | 
| 182         chromiumDelete, |  | 
| 183         chromiumAccess, |  | 
| 184         chromiumFullPathname, |  | 
| 185         chromiumDlOpen, |  | 
| 186         unix_vfs->xDlError, |  | 
| 187         unix_vfs->xDlSym, |  | 
| 188         unix_vfs->xDlClose, |  | 
| 189         unix_vfs->xRandomness, |  | 
| 190         unix_vfs->xSleep, |  | 
| 191         unix_vfs->xCurrentTime, |  | 
| 192         unix_vfs->xGetLastError |  | 
| 193     }; |  | 
| 194     sqlite3_vfs_register(&chromium_vfs, 0); |  | 
| 195 } |  | 
| 196 |  | 
| 197 } // namespace WebCore |  | 
| OLD | NEW | 
|---|