/* -*- 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/. */
/* ** Close a QuotaFile.
*/ int QuotaClose(sqlite3_file* pFile) {
QuotaFile* p = (QuotaFile*)pFile; int rc;
rc = p->pReal->pMethods->xClose(p->pReal); if (rc == SQLITE_OK) { delete p->base.pMethods;
p->base.pMethods = nullptr;
p->quotaObject = nullptr; #ifdef DEBUG
p->fileChunkSize = 0; #endif
} return rc;
}
/* ** Read data from a QuotaFile.
*/ int QuotaRead(sqlite3_file* pFile, void* zBuf, int iAmt, sqlite_int64 iOfst) {
QuotaFile* p = (QuotaFile*)pFile; int rc;
rc = p->pReal->pMethods->xRead(p->pReal, zBuf, iAmt, iOfst); return rc;
}
/* ** Return the current file-size of a QuotaFile.
*/ int QuotaFileSize(sqlite3_file* pFile, sqlite_int64* pSize) {
QuotaFile* p = (QuotaFile*)pFile; int rc;
rc = p->pReal->pMethods->xFileSize(p->pReal, pSize); return rc;
}
/* ** Write data to a QuotaFile.
*/ int QuotaWrite(sqlite3_file* pFile, constvoid* zBuf, int iAmt,
sqlite_int64 iOfst) {
QuotaFile* p = (QuotaFile*)pFile; int rc; if (p->quotaObject) {
MOZ_ASSERT(INT64_MAX - iOfst >= iAmt); if (!p->quotaObject->MaybeUpdateSize(iOfst + iAmt, /* aTruncate */ false)) { return SQLITE_FULL;
}
}
rc = p->pReal->pMethods->xWrite(p->pReal, zBuf, iAmt, iOfst); if (p->quotaObject && rc != SQLITE_OK) {
NS_WARNING( "xWrite failed on a quota-controlled file, attempting to " "update its current size...");
sqlite_int64 currentSize; if (QuotaFileSize(pFile, ¤tSize) == SQLITE_OK) {
DebugOnly<bool> res =
p->quotaObject->MaybeUpdateSize(currentSize, /* aTruncate */ true);
MOZ_ASSERT(res);
}
} return rc;
}
/* ** Truncate a QuotaFile.
*/ int QuotaTruncate(sqlite3_file* pFile, sqlite_int64 size) {
QuotaFile* p = (QuotaFile*)pFile; int rc; if (p->quotaObject) { if (p->fileChunkSize > 0) { // Round up to the smallest multiple of the chunk size that will hold all // the data.
size =
((size + p->fileChunkSize - 1) / p->fileChunkSize) * p->fileChunkSize;
} if (!p->quotaObject->MaybeUpdateSize(size, /* aTruncate */ true)) { return SQLITE_FULL;
}
}
rc = p->pReal->pMethods->xTruncate(p->pReal, size); if (p->quotaObject) { if (rc == SQLITE_OK) { #ifdef DEBUG // Make sure xTruncate set the size exactly as we calculated above.
sqlite_int64 newSize;
MOZ_ASSERT(QuotaFileSize(pFile, &newSize) == SQLITE_OK);
MOZ_ASSERT(newSize == size); #endif
} else {
NS_WARNING( "xTruncate failed on a quota-controlled file, attempting to " "update its current size..."); if (QuotaFileSize(pFile, &size) == SQLITE_OK) {
DebugOnly<bool> res =
p->quotaObject->MaybeUpdateSize(size, /* aTruncate */ true);
MOZ_ASSERT(res);
}
}
} return rc;
}
/* ** Sync a QuotaFile.
*/ int QuotaSync(sqlite3_file* pFile, int flags) {
QuotaFile* p = (QuotaFile*)pFile; return p->pReal->pMethods->xSync(p->pReal, flags);
}
/* ** Lock a QuotaFile.
*/ int QuotaLock(sqlite3_file* pFile, int eLock) {
QuotaFile* p = (QuotaFile*)pFile; int rc;
rc = p->pReal->pMethods->xLock(p->pReal, eLock); return rc;
}
/* ** Unlock a QuotaFile.
*/ int QuotaUnlock(sqlite3_file* pFile, int eLock) {
QuotaFile* p = (QuotaFile*)pFile; int rc;
rc = p->pReal->pMethods->xUnlock(p->pReal, eLock); return rc;
}
/* ** Check if another file-handle holds a RESERVED lock on a QuotaFile.
*/ int QuotaCheckReservedLock(sqlite3_file* pFile, int* pResOut) {
QuotaFile* p = (QuotaFile*)pFile; int rc = p->pReal->pMethods->xCheckReservedLock(p->pReal, pResOut); return rc;
}
/* ** File control method. For custom operations on a QuotaFile.
*/ int QuotaFileControl(sqlite3_file* pFile, int op, void* pArg) {
QuotaFile* p = (QuotaFile*)pFile; int rc; // Hook SQLITE_FCNTL_SIZE_HINT for quota-controlled files and do the necessary // work before passing to the SQLite VFS. if (op == SQLITE_FCNTL_SIZE_HINT && p->quotaObject) {
sqlite3_int64 hintSize = *static_cast<sqlite3_int64*>(pArg);
sqlite3_int64 currentSize;
rc = QuotaFileSize(pFile, ¤tSize); if (rc != SQLITE_OK) { return rc;
} if (hintSize > currentSize) {
rc = QuotaTruncate(pFile, hintSize); if (rc != SQLITE_OK) { return rc;
}
}
}
rc = p->pReal->pMethods->xFileControl(p->pReal, op, pArg); // Grab the file chunk size after the SQLite VFS has approved. if (op == SQLITE_FCNTL_CHUNK_SIZE && rc == SQLITE_OK) {
p->fileChunkSize = *static_cast<int*>(pArg);
} #ifdef DEBUG if (op == SQLITE_FCNTL_SIZE_HINT && p->quotaObject && rc == SQLITE_OK) {
sqlite3_int64 hintSize = *static_cast<sqlite3_int64*>(pArg); if (p->fileChunkSize > 0) {
hintSize = ((hintSize + p->fileChunkSize - 1) / p->fileChunkSize) *
p->fileChunkSize;
}
sqlite3_int64 currentSize;
MOZ_ASSERT(QuotaFileSize(pFile, ¤tSize) == SQLITE_OK);
MOZ_ASSERT(currentSize >= hintSize);
} #endif return rc;
}
/* ** Return the sector-size in bytes for a QuotaFile.
*/ int QuotaSectorSize(sqlite3_file* pFile) {
QuotaFile* p = (QuotaFile*)pFile; int rc;
rc = p->pReal->pMethods->xSectorSize(p->pReal); return rc;
}
/* ** Return the device characteristic flags supported by a QuotaFile.
*/ int QuotaDeviceCharacteristics(sqlite3_file* pFile) {
QuotaFile* p = (QuotaFile*)pFile; int rc;
rc = p->pReal->pMethods->xDeviceCharacteristics(p->pReal); return rc;
}
/* ** Shared-memory operations.
*/ int QuotaShmLock(sqlite3_file* pFile, int ofst, int n, int flags) {
QuotaFile* p = (QuotaFile*)pFile; return p->pReal->pMethods->xShmLock(p->pReal, ofst, n, flags);
}
int QuotaShmMap(sqlite3_file* pFile, int iRegion, int szRegion, int isWrite, voidvolatile** pp) {
QuotaFile* p = (QuotaFile*)pFile; int rc;
rc = p->pReal->pMethods->xShmMap(p->pReal, iRegion, szRegion, isWrite, pp); return rc;
}
void QuotaShmBarrier(sqlite3_file* pFile) {
QuotaFile* p = (QuotaFile*)pFile;
p->pReal->pMethods->xShmBarrier(p->pReal);
}
int QuotaShmUnmap(sqlite3_file* pFile, int delFlag) {
QuotaFile* p = (QuotaFile*)pFile; int rc;
rc = p->pReal->pMethods->xShmUnmap(p->pReal, delFlag); return rc;
}
int QuotaFetch(sqlite3_file* pFile, sqlite3_int64 iOff, int iAmt, void** pp) {
QuotaFile* p = (QuotaFile*)pFile;
MOZ_ASSERT(p->pReal->pMethods->iVersion >= 3); return p->pReal->pMethods->xFetch(p->pReal, iOff, iAmt, pp);
}
int QuotaUnfetch(sqlite3_file* pFile, sqlite3_int64 iOff, void* pResOut) {
QuotaFile* p = (QuotaFile*)pFile;
MOZ_ASSERT(p->pReal->pMethods->iVersion >= 3); return p->pReal->pMethods->xUnfetch(p->pReal, iOff, pResOut);
}
int QuotaOpen(sqlite3_vfs* vfs, constchar* zName, sqlite3_file* pFile, int flags, int* pOutFlags) {
sqlite3_vfs* orig_vfs = static_cast<sqlite3_vfs*>(vfs->pAppData); int rc;
QuotaFile* p = (QuotaFile*)pFile;
MaybeEstablishQuotaControl(zName, p, flags);
rc = orig_vfs->xOpen(orig_vfs, zName, p->pReal, flags, pOutFlags); if (rc != SQLITE_OK) return rc; if (p->pReal->pMethods) {
sqlite3_io_methods* pNew = new sqlite3_io_methods; const sqlite3_io_methods* pSub = p->pReal->pMethods;
memset(pNew, 0, sizeof(*pNew)); // 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.
pNew->iVersion = pSub->iVersion;
MOZ_ASSERT(pNew->iVersion <= LAST_KNOWN_IOMETHODS_VERSION);
pNew->xClose = QuotaClose;
pNew->xRead = QuotaRead;
pNew->xWrite = QuotaWrite;
pNew->xTruncate = QuotaTruncate;
pNew->xSync = QuotaSync;
pNew->xFileSize = QuotaFileSize;
pNew->xLock = QuotaLock;
pNew->xUnlock = QuotaUnlock;
pNew->xCheckReservedLock = QuotaCheckReservedLock;
pNew->xFileControl = QuotaFileControl;
pNew->xSectorSize = QuotaSectorSize;
pNew->xDeviceCharacteristics = QuotaDeviceCharacteristics; if (pNew->iVersion >= 2) { // Methods added in version 2.
pNew->xShmMap = pSub->xShmMap ? QuotaShmMap : nullptr;
pNew->xShmLock = pSub->xShmLock ? QuotaShmLock : nullptr;
pNew->xShmBarrier = pSub->xShmBarrier ? QuotaShmBarrier : nullptr;
pNew->xShmUnmap = pSub->xShmUnmap ? QuotaShmUnmap : nullptr;
} if (pNew->iVersion >= 3) { // Methods added in version 3. // SQLite 3.7.17 calls these methods without checking for nullptr first, // so we always define them. Verify that we're not going to call // nullptrs, though.
MOZ_ASSERT(pSub->xFetch);
pNew->xFetch = QuotaFetch;
MOZ_ASSERT(pSub->xUnfetch);
pNew->xUnfetch = QuotaUnfetch;
}
pFile->pMethods = pNew;
} return rc;
}
int QuotaDelete(sqlite3_vfs* vfs, constchar* zName, int syncDir) {
sqlite3_vfs* orig_vfs = static_cast<sqlite3_vfs*>(vfs->pAppData); int rc;
RefPtr<QuotaObject> quotaObject;
if (StringEndsWith(nsDependentCString(zName), "-wal"_ns)) {
quotaObject = GetQuotaObjectFromName(zName);
}
int QuotaAccess(sqlite3_vfs* vfs, constchar* zName, int flags, int* pResOut) {
sqlite3_vfs* orig_vfs = static_cast<sqlite3_vfs*>(vfs->pAppData); return orig_vfs->xAccess(orig_vfs, zName, flags, pResOut);
}
int QuotaFullPathname(sqlite3_vfs* vfs, constchar* zName, int nOut, char* zOut) { #ifdefined(XP_WIN) // SQLite uses GetFullPathnameW which also normailizes file path. If a file // component ends with a dot, it would be removed. However, it's not desired. // // And that would result SQLite uses wrong database and quotaObject. // Note that we are safe to avoid the GetFullPathnameW call for \\?\ prefixed // paths. // And note that this hack will be removed once the issue is fixed directly in // SQLite.
// zName that starts with "//?/" is the case when a file URI was passed and // zName that starts with "\\?\" is the case when a normal path was passed // (not file URI). if (StaticPrefs::dom_quotaManager_overrideXFullPathname() &&
((zName[0] == '/' && zName[1] == '/' && zName[2] == '?' &&
zName[3] == '/') ||
(zName[0] == '\\' && zName[1] == '\\' && zName[2] == '?' &&
zName[3] == '\\'))) {
MOZ_ASSERT(nOut >= vfs->mxPathname);
MOZ_ASSERT(static_cast<size_t>(nOut) > strlen(zName));
size_t index = 0; while (zName[index] != '\0') { if (zName[index] == '/') {
zOut[index] = '\\';
} else {
zOut[index] = zName[index];
}
index++;
}
zOut[index] = '\0';
return SQLITE_OK;
} #elifdefined(XP_UNIX) // SQLite canonicalizes (resolves path components) file paths on Unix which // doesn't work well with file path sanity checks in quota manager. This is // especially a problem on mac where /var is a symlink to /private/var. // Since QuotaVFS is used only by quota clients which never access databases // outside of PROFILE/storage, we override Unix xFullPathname with own // implementation that doesn't do any canonicalization.
if (StaticPrefs::dom_quotaManager_overrideXFullPathnameUnix()) { if (nOut < 0) { // Match the return code used by SQLite's xFullPathname implementation // here and below. return SQLITE_CANTOPEN;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.