1 The WebDatabase implementation in the renderer users a custom vfs to
2 broker file open and other requests. This modifies the built-in vfs
3 implementation to let that code share much of the implementation
6 diff --git src/os_unix.c src/os_unix.c
7 index ef04a72..e5e1509 100644
10 @@ -3496,9 +3496,16 @@ typedef const sqlite3_io_methods *(*finder_type)(const char*,unixFile*);
14 +** Initializes a unixFile structure with zeros.
16 +void initUnixFile(sqlite3_file* file) {
17 + memset(file, 0, sizeof(unixFile));
21 ** Initialize the contents of the unixFile structure pointed to by pId.
23 -static int fillInUnixFile(
25 sqlite3_vfs *pVfs, /* Pointer to vfs object */
26 int h, /* Open file descriptor of file being opened */
27 int dirfd, /* Directory file descriptor */
28 @@ -3812,6 +3819,73 @@ static UnixUnusedFd *findReusableFd(const char *zPath, int flags){
32 +** Initializes a unixFile structure with zeros.
34 +void chromium_sqlite3_initialize_unix_sqlite3_file(sqlite3_file* file) {
35 + memset(file, 0, sizeof(unixFile));
38 +int chromium_sqlite3_fill_in_unix_sqlite3_file(sqlite3_vfs* vfs,
42 + const char* fileName,
45 + return fillInUnixFile(vfs, fd, dirfd, file, fileName, noLock, isDelete, 0);
49 +** Search for an unused file descriptor that was opened on the database file.
50 +** If a suitable file descriptor if found, then it is stored in *fd; otherwise,
51 +** *fd is not modified.
53 +** If a reusable file descriptor is not found, and a new UnixUnusedFd cannot
54 +** be allocated, SQLITE_NOMEM is returned. Otherwise, SQLITE_OK is returned.
56 +int chromium_sqlite3_get_reusable_file_handle(sqlite3_file* file,
57 + const char* fileName,
60 + unixFile* unixSQLite3File = (unixFile*)file;
61 + int fileType = flags & 0xFFFFFF00;
62 + if (fileType == SQLITE_OPEN_MAIN_DB) {
63 + UnixUnusedFd *unusedFd = findReusableFd(fileName, flags);
67 + unusedFd = sqlite3_malloc(sizeof(*unusedFd));
69 + return SQLITE_NOMEM;
72 + unixSQLite3File->pUnused = unusedFd;
78 +** Marks 'fd' as the unused file descriptor for 'pFile'.
80 +void chromium_sqlite3_update_reusable_file_handle(sqlite3_file* file,
83 + unixFile* unixSQLite3File = (unixFile*)file;
84 + if (unixSQLite3File->pUnused) {
85 + unixSQLite3File->pUnused->fd = fd;
86 + unixSQLite3File->pUnused->flags = flags;
91 +** Destroys pFile's field that keeps track of the unused file descriptor.
93 +void chromium_sqlite3_destroy_reusable_file_handle(sqlite3_file* file) {
94 + unixFile* unixSQLite3File = (unixFile*)file;
95 + sqlite3_free(unixSQLite3File->pUnused);
99 ** Open the file zPath.
101 ** Previously, the SQLite OS layer used three functions in place of this
102 @@ -3893,20 +3967,13 @@ static int unixOpen(
103 || eType==SQLITE_OPEN_TRANSIENT_DB
106 - memset(p, 0, sizeof(unixFile));
107 + chromium_sqlite3_initialize_unix_sqlite3_file(pFile);
109 if( eType==SQLITE_OPEN_MAIN_DB ){
110 - UnixUnusedFd *pUnused;
111 - pUnused = findReusableFd(zName, flags);
115 - pUnused = sqlite3_malloc(sizeof(*pUnused));
117 - return SQLITE_NOMEM;
119 + rc = chromium_sqlite3_get_reusable_file_handle(pFile, zName, flags, &fd);
120 + if( rc!=SQLITE_OK ){
123 - p->pUnused = pUnused;
125 /* If zName is NULL, the upper layer is requesting a temp file. */
126 assert(isDelete && !isOpenDirectory);
127 @@ -3949,10 +4016,7 @@ static int unixOpen(
132 - p->pUnused->fd = fd;
133 - p->pUnused->flags = flags;
135 + chromium_sqlite3_update_reusable_file_handle(pFile, fd, flags);
139 @@ -4028,7 +4092,7 @@ static int unixOpen(
140 rc = fillInUnixFile(pVfs, fd, dirfd, pFile, zPath, noLock, isDelete);
143 - sqlite3_free(p->pUnused);
144 + chromium_sqlite3_destroy_reusable_file_handle(pFile);
148 diff --git src/os_win.c src/os_win.c
149 index bc03a4b..06539d7 100644
152 @@ -1890,4 +1890,11 @@ int sqlite3_os_end(void){
156 +void chromium_sqlite3_initialize_win_sqlite3_file(sqlite3_file* file, HANDLE handle) {
157 + winFile* winSQLite3File = (winFile*)file;
158 + memset(file, 0, sizeof(*file));
159 + winSQLite3File->pMethod = &winIoMethod;
160 + winSQLite3File->h = handle;
163 #endif /* SQLITE_OS_WIN */