1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
* vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "BaseVFS.h"
#include <string.h>
#include "sqlite3.h"
#include "mozilla/net/IOActivityMonitor.h"
namespace {
// The last VFS version for which this file has been updated.
constexpr int kLastKnowVfsVersion = 3;
// The last io_methods version for which this file has been updated.
constexpr int kLastKnownIOMethodsVersion = 3;
using namespace mozilla;
using namespace mozilla::net;
struct BaseFile {
// Base class. Must be first
sqlite3_file base;
// The filename
char* location;
// This points to the underlying sqlite3_file
sqlite3_file pReal[1];
};
int BaseClose(sqlite3_file* pFile) {
BaseFile* p = (BaseFile*)pFile;
delete[] p->location;
return p->pReal->pMethods->xClose(p->pReal);
}
int BaseRead(sqlite3_file* pFile, void* zBuf, int iAmt, sqlite_int64 iOfst) {
BaseFile* p = (BaseFile*)pFile;
int rc = p->pReal->pMethods->xRead(p->pReal, zBuf, iAmt, iOfst);
if (rc == SQLITE_OK && IOActivityMonitor::IsActive()) {
IOActivityMonitor::Read(nsDependentCString(p->location), iAmt);
}
return rc;
}
int BaseWrite(sqlite3_file* pFile, const void* zBuf, int iAmt,
sqlite_int64 iOfst) {
BaseFile* p = (BaseFile*)pFile;
int rc = p->pReal->pMethods->xWrite(p->pReal, zBuf, iAmt, iOfst);
if (rc == SQLITE_OK && IOActivityMonitor::IsActive()) {
IOActivityMonitor::Write(nsDependentCString(p->location), iAmt);
}
return rc;
}
int BaseTruncate(sqlite3_file* pFile, sqlite_int64 size) {
BaseFile* p = (BaseFile*)pFile;
return p->pReal->pMethods->xTruncate(p->pReal, size);
}
int BaseSync(sqlite3_file* pFile, int flags) {
BaseFile* p = (BaseFile*)pFile;
return p->pReal->pMethods->xSync(p->pReal, flags);
}
int BaseFileSize(sqlite3_file* pFile, sqlite_int64* pSize) {
BaseFile* p = (BaseFile*)pFile;
return p->pReal->pMethods->xFileSize(p->pReal, pSize);
}
int BaseLock(sqlite3_file* pFile, int eLock) {
BaseFile* p = (BaseFile*)pFile;
return p->pReal->pMethods->xLock(p->pReal, eLock);
}
int BaseUnlock(sqlite3_file* pFile, int eLock) {
BaseFile* p = (BaseFile*)pFile;
return p->pReal->pMethods->xUnlock(p->pReal, eLock);
}
int BaseCheckReservedLock(sqlite3_file* pFile, int* pResOut) {
BaseFile* p = (BaseFile*)pFile;
return p->pReal->pMethods->xCheckReservedLock(p->pReal, pResOut);
}
int BaseFileControl(sqlite3_file* pFile, int op, void* pArg) {
#if defined(MOZ_SQLITE_PERSIST_AUXILIARY_FILES)
// Persist auxiliary files (-shm and -wal) on disk, because creating and
// deleting them may be expensive on slow storage.
// Only do this when there is a journal size limit, so the journal is
// truncated instead of deleted on shutdown, that feels safer if the user
// moves a database file around without its auxiliary files.
MOZ_ASSERT(
::sqlite3_compileoption_used("DEFAULT_JOURNAL_SIZE_LIMIT"),
"A journal size limit ensures the journal is truncated on shutdown");
if (op == SQLITE_FCNTL_PERSIST_WAL) {
*static_cast<int*>(pArg) = 1;
return SQLITE_OK;
}
#endif
BaseFile* p = (BaseFile*)pFile;
return p->pReal->pMethods->xFileControl(p->pReal, op, pArg);
}
int BaseSectorSize(sqlite3_file* pFile) {
BaseFile* p = (BaseFile*)pFile;
return p->pReal->pMethods->xSectorSize(p->pReal);
}
int BaseDeviceCharacteristics(sqlite3_file* pFile) {
BaseFile* p = (BaseFile*)pFile;
return p->pReal->pMethods->xDeviceCharacteristics(p->pReal);
}
int BaseShmMap(sqlite3_file* pFile, int iPg, int pgsz, int bExtend,
void volatile** pp) {
BaseFile* p = (BaseFile*)pFile;
return p->pReal->pMethods->xShmMap(p->pReal, iPg, pgsz, bExtend, pp);
}
int BaseShmLock(sqlite3_file* pFile, int offset, int n, int flags) {
BaseFile* p = (BaseFile*)pFile;
return p->pReal->pMethods->xShmLock(p->pReal, offset, n, flags);
}
void BaseShmBarrier(sqlite3_file* pFile) {
BaseFile* p = (BaseFile*)pFile;
return p->pReal->pMethods->xShmBarrier(p->pReal);
}
int BaseShmUnmap(sqlite3_file* pFile, int deleteFlag) {
BaseFile* p = (BaseFile*)pFile;
return p->pReal->pMethods->xShmUnmap(p->pReal, deleteFlag);
}
int BaseFetch(sqlite3_file* pFile, sqlite3_int64 iOfst, int iAmt, void** pp) {
BaseFile* p = (BaseFile*)pFile;
return p->pReal->pMethods->xFetch(p->pReal, iOfst, iAmt, pp);
}
int BaseUnfetch(sqlite3_file* pFile, sqlite3_int64 iOfst, void* pPage) {
BaseFile* p = (BaseFile*)pFile;
return p->pReal->pMethods->xUnfetch(p->pReal, iOfst, pPage);
}
int BaseOpen(sqlite3_vfs* vfs, const char* zName, sqlite3_file* pFile,
int flags, int* pOutFlags) {
BaseFile* p = (BaseFile*)pFile;
if (zName) {
p->location = new char[7 + strlen(zName) + 1];
strcpy(p->location, "file://");
strcpy(p->location + 7, zName);
} else {
p->location = new char[8];
strcpy(p->location, "file://");
}
sqlite3_vfs* origVfs = (sqlite3_vfs*)(vfs->pAppData);
int rc = origVfs->xOpen(origVfs, zName, p->pReal, flags, pOutFlags);
if (rc) {
return rc;
}
if (p->pReal->pMethods) {
// If the io_methods version is higher than the last known one, you should
// update this VFS adding appropriate IO methods for any methods added in
// the version change.
MOZ_ASSERT(p->pReal->pMethods->iVersion == kLastKnownIOMethodsVersion);
static const sqlite3_io_methods IOmethods = {
kLastKnownIOMethodsVersion, /* iVersion */
BaseClose, /* xClose */
BaseRead, /* xRead */
BaseWrite, /* xWrite */
BaseTruncate, /* xTruncate */
BaseSync, /* xSync */
BaseFileSize, /* xFileSize */
BaseLock, /* xLock */
BaseUnlock, /* xUnlock */
BaseCheckReservedLock, /* xCheckReservedLock */
BaseFileControl, /* xFileControl */
BaseSectorSize, /* xSectorSize */
BaseDeviceCharacteristics, /* xDeviceCharacteristics */
BaseShmMap, /* xShmMap */
BaseShmLock, /* xShmLock */
BaseShmBarrier, /* xShmBarrier */
BaseShmUnmap, /* xShmUnmap */
BaseFetch, /* xFetch */
BaseUnfetch /* xUnfetch */
};
pFile->pMethods = &IOmethods;
}
return SQLITE_OK;
}
} // namespace
namespace mozilla::storage::basevfs {
const char* GetVFSName(bool exclusive) {
return exclusive ? "base-vfs-excl" : "base-vfs";
}
UniquePtr<sqlite3_vfs> ConstructVFS(bool exclusive) {
#if defined(XP_WIN)
# define EXPECTED_VFS "win32"
# define EXPECTED_VFS_EXCL "win32"
#else
# define EXPECTED_VFS "unix"
# define EXPECTED_VFS_EXCL "unix-excl"
#endif
if (sqlite3_vfs_find(GetVFSName(exclusive))) {
return nullptr;
}
bool found;
sqlite3_vfs* origVfs;
if (!exclusive) {
// Use the non-exclusive VFS.
origVfs = sqlite3_vfs_find(nullptr);
found = origVfs && origVfs->zName && !strcmp(origVfs->zName, EXPECTED_VFS);
} else {
origVfs = sqlite3_vfs_find(EXPECTED_VFS_EXCL);
found = (origVfs != nullptr);
}
if (!found) {
return nullptr;
}
// If the VFS version is higher than the last known one, you should update
// this VFS adding appropriate methods for any methods added in the version
// change.
MOZ_ASSERT(origVfs->iVersion == kLastKnowVfsVersion);
sqlite3_vfs vfs = {
kLastKnowVfsVersion, /* iVersion */
origVfs->szOsFile + static_cast<int>(sizeof(BaseFile)), /* szOsFile */
origVfs->mxPathname, /* mxPathname */
nullptr, /* pNext */
GetVFSName(exclusive), /* zName */
origVfs, /* pAppData */
BaseOpen, /* xOpen */
origVfs->xDelete, /* xDelete */
origVfs->xAccess, /* xAccess */
origVfs->xFullPathname, /* xFullPathname */
origVfs->xDlOpen, /* xDlOpen */
origVfs->xDlError, /* xDlError */
origVfs->xDlSym, /* xDlSym */
origVfs->xDlClose, /* xDlClose */
origVfs->xRandomness, /* xRandomness */
origVfs->xSleep, /* xSleep */
origVfs->xCurrentTime, /* xCurrentTime */
origVfs->xGetLastError, /* xGetLastError */
origVfs->xCurrentTimeInt64, /* xCurrentTimeInt64 */
origVfs->xSetSystemCall, /* xSetSystemCall */
origVfs->xGetSystemCall, /* xGetSystemCall */
origVfs->xNextSystemCall /* xNextSystemCall */
};
return MakeUnique<sqlite3_vfs>(vfs);
}
} // namespace mozilla::storage::basevfs
|