2013-01-30 01:38:37 +04:00
|
|
|
/* -*- 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 <string.h>
|
|
|
|
#include "mozilla/Telemetry.h"
|
|
|
|
#include "mozilla/Preferences.h"
|
|
|
|
#include "sqlite3.h"
|
|
|
|
#include "nsThreadUtils.h"
|
2013-09-11 08:18:36 +04:00
|
|
|
#include "mozilla/dom/quota/PersistenceType.h"
|
2013-01-30 01:38:37 +04:00
|
|
|
#include "mozilla/dom/quota/QuotaManager.h"
|
2013-03-26 15:13:17 +04:00
|
|
|
#include "mozilla/dom/quota/QuotaObject.h"
|
2018-06-12 23:22:50 +03:00
|
|
|
#include "mozilla/net/IOActivityMonitor.h"
|
2013-09-12 18:47:37 +04:00
|
|
|
#include "mozilla/IOInterposer.h"
|
2019-06-07 09:04:57 +03:00
|
|
|
#include "nsEscape.h"
|
2013-01-30 01:38:37 +04:00
|
|
|
|
2013-10-23 16:58:06 +04:00
|
|
|
// The last VFS version for which this file has been updated.
|
|
|
|
#define LAST_KNOWN_VFS_VERSION 3
|
|
|
|
|
|
|
|
// The last io_methods version for which this file has been updated.
|
|
|
|
#define LAST_KNOWN_IOMETHODS_VERSION 3
|
|
|
|
|
2013-01-30 01:38:37 +04:00
|
|
|
/**
|
2018-07-13 19:54:57 +03:00
|
|
|
* By default use the unix-excl VFS, for the following reasons:
|
|
|
|
* 1. It improves compatibility with NFS shares, whose implementation
|
|
|
|
* is incompatible with SQLite's locking requirements.
|
|
|
|
* Bug 433129 attempted to automatically identify such file-systems,
|
|
|
|
* but a reliable way was not found and the fallback locking is slower than
|
|
|
|
* POSIX locking, so we do not want to do it by default.
|
|
|
|
* 2. It allows wal mode to avoid the memory mapped -shm file, reducing the
|
|
|
|
* likelihood of SIGBUS failures when disk space is exhausted.
|
|
|
|
* 3. It provides some protection from third party database tampering while a
|
|
|
|
* connection is open.
|
|
|
|
* This preference allows to revert to the "unix" VFS, that is not exclusive,
|
|
|
|
* thus it can be used by developers to query a database through the Sqlite
|
|
|
|
* command line while it's already in use.
|
|
|
|
*/
|
|
|
|
#define PREF_MULTI_PROCESS_ACCESS "storage.multiProcessAccess.enabled"
|
2013-01-30 01:38:37 +04:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::dom::quota;
|
2018-06-12 23:22:50 +03:00
|
|
|
using namespace mozilla::net;
|
2013-01-30 01:38:37 +04:00
|
|
|
|
|
|
|
struct Histograms {
|
2019-05-01 11:47:10 +03:00
|
|
|
const char* name;
|
2017-02-15 22:15:15 +03:00
|
|
|
const Telemetry::HistogramID readB;
|
|
|
|
const Telemetry::HistogramID writeB;
|
|
|
|
const Telemetry::HistogramID readMS;
|
|
|
|
const Telemetry::HistogramID writeMS;
|
|
|
|
const Telemetry::HistogramID syncMS;
|
2013-01-30 01:38:37 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
#define SQLITE_TELEMETRY(FILENAME, HGRAM) \
|
|
|
|
{ \
|
|
|
|
FILENAME, Telemetry::MOZ_SQLITE_##HGRAM##_READ_B, \
|
|
|
|
Telemetry::MOZ_SQLITE_##HGRAM##_WRITE_B, \
|
|
|
|
Telemetry::MOZ_SQLITE_##HGRAM##_READ_MS, \
|
|
|
|
Telemetry::MOZ_SQLITE_##HGRAM##_WRITE_MS, \
|
|
|
|
Telemetry::MOZ_SQLITE_##HGRAM##_SYNC_MS \
|
|
|
|
}
|
|
|
|
|
|
|
|
Histograms gHistograms[] = {SQLITE_TELEMETRY("places.sqlite", PLACES),
|
|
|
|
SQLITE_TELEMETRY("cookies.sqlite", COOKIES),
|
|
|
|
SQLITE_TELEMETRY("webappsstore.sqlite", WEBAPPS),
|
2013-07-31 19:44:58 +04:00
|
|
|
SQLITE_TELEMETRY(nullptr, OTHER)};
|
2013-01-30 01:38:37 +04:00
|
|
|
#undef SQLITE_TELEMETRY
|
|
|
|
|
|
|
|
/** RAII class for measuring how long io takes on/off main thread
|
|
|
|
*/
|
|
|
|
class IOThreadAutoTimer {
|
|
|
|
public:
|
2017-07-06 15:00:35 +03:00
|
|
|
/**
|
2013-01-30 01:38:37 +04:00
|
|
|
* IOThreadAutoTimer measures time spent in IO. Additionally it
|
|
|
|
* automatically determines whether IO is happening on the main
|
|
|
|
* thread and picks an appropriate histogram.
|
|
|
|
*
|
|
|
|
* @param id takes a telemetry histogram id. The id+1 must be an
|
2017-07-06 15:00:35 +03:00
|
|
|
* equivalent histogram for the main thread. Eg, MOZ_SQLITE_OPEN_MS
|
2013-01-30 01:38:37 +04:00
|
|
|
* is followed by MOZ_SQLITE_OPEN_MAIN_THREAD_MS.
|
2013-06-14 22:01:02 +04:00
|
|
|
*
|
2013-09-12 18:47:37 +04:00
|
|
|
* @param aOp optionally takes an IO operation to report through the
|
|
|
|
* IOInterposer. Filename will be reported as NULL, and reference will be
|
|
|
|
* either "sqlite-mainthread" or "sqlite-otherthread".
|
2013-01-30 01:38:37 +04:00
|
|
|
*/
|
2017-02-15 22:15:15 +03:00
|
|
|
explicit IOThreadAutoTimer(
|
|
|
|
Telemetry::HistogramID aId,
|
2013-09-12 18:47:37 +04:00
|
|
|
IOInterposeObserver::Operation aOp = IOInterposeObserver::OpNone)
|
2013-01-30 01:38:37 +04:00
|
|
|
: start(TimeStamp::Now()),
|
2017-01-11 20:22:19 +03:00
|
|
|
id(aId)
|
2017-01-24 06:15:12 +03:00
|
|
|
#if defined(MOZ_GECKO_PROFILER) && !defined(XP_WIN)
|
2017-01-11 20:22:19 +03:00
|
|
|
,
|
|
|
|
op(aOp)
|
|
|
|
#endif
|
2013-01-30 01:38:37 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-10-01 23:53:19 +04:00
|
|
|
/**
|
|
|
|
* This constructor is for when we want to report an operation to
|
|
|
|
* IOInterposer but do not require a telemetry probe.
|
|
|
|
*
|
|
|
|
* @param aOp IO Operation to report through the IOInterposer.
|
|
|
|
*/
|
2014-08-08 03:47:28 +04:00
|
|
|
explicit IOThreadAutoTimer(IOInterposeObserver::Operation aOp)
|
2013-10-01 23:53:19 +04:00
|
|
|
: start(TimeStamp::Now()),
|
2017-01-11 20:22:19 +03:00
|
|
|
id(Telemetry::HistogramCount)
|
2017-01-24 06:15:12 +03:00
|
|
|
#if defined(MOZ_GECKO_PROFILER) && !defined(XP_WIN)
|
2017-01-11 20:22:19 +03:00
|
|
|
,
|
|
|
|
op(aOp)
|
|
|
|
#endif
|
2013-10-01 23:53:19 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~IOThreadAutoTimer() {
|
2013-06-14 22:01:02 +04:00
|
|
|
TimeStamp end(TimeStamp::Now());
|
2013-01-30 01:38:37 +04:00
|
|
|
uint32_t mainThread = NS_IsMainThread() ? 1 : 0;
|
2013-10-01 23:53:19 +04:00
|
|
|
if (id != Telemetry::HistogramCount) {
|
2017-02-15 22:15:15 +03:00
|
|
|
Telemetry::AccumulateTimeDelta(
|
|
|
|
static_cast<Telemetry::HistogramID>(id + mainThread), start, end);
|
2013-10-01 23:53:19 +04:00
|
|
|
}
|
2014-03-15 02:35:01 +04:00
|
|
|
// We don't report SQLite I/O on Windows because we have a comprehensive
|
|
|
|
// mechanism for intercepting I/O on that platform that captures a superset
|
|
|
|
// of the data captured here.
|
2017-01-24 06:15:12 +03:00
|
|
|
#if defined(MOZ_GECKO_PROFILER) && !defined(XP_WIN)
|
2013-09-12 18:47:37 +04:00
|
|
|
if (IOInterposer::IsObservedOperation(op)) {
|
2019-05-01 11:47:10 +03:00
|
|
|
const char* main_ref = "sqlite-mainthread";
|
|
|
|
const char* other_ref = "sqlite-otherthread";
|
2013-09-12 18:47:37 +04:00
|
|
|
|
|
|
|
// Create observation
|
|
|
|
IOInterposeObserver::Observation ob(op, start, end,
|
|
|
|
(mainThread ? main_ref : other_ref));
|
|
|
|
// Report observation
|
|
|
|
IOInterposer::Report(ob);
|
2013-06-14 22:01:02 +04:00
|
|
|
}
|
2017-01-24 06:15:12 +03:00
|
|
|
#endif /* defined(MOZ_GECKO_PROFILER) && !defined(XP_WIN) */
|
2013-01-30 01:38:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const TimeStamp start;
|
2017-02-15 22:15:15 +03:00
|
|
|
const Telemetry::HistogramID id;
|
2017-01-24 06:15:12 +03:00
|
|
|
#if defined(MOZ_GECKO_PROFILER) && !defined(XP_WIN)
|
2013-09-12 18:47:37 +04:00
|
|
|
IOInterposeObserver::Operation op;
|
2017-01-11 20:22:19 +03:00
|
|
|
#endif
|
2013-01-30 01:38:37 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
struct telemetry_file {
|
|
|
|
// Base class. Must be first
|
|
|
|
sqlite3_file base;
|
|
|
|
|
|
|
|
// histograms pertaining to this file
|
2019-05-01 11:47:10 +03:00
|
|
|
Histograms* histograms;
|
2013-01-30 01:38:37 +04:00
|
|
|
|
|
|
|
// quota object for this file
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<QuotaObject> quotaObject;
|
2013-01-30 01:38:37 +04:00
|
|
|
|
2015-02-16 20:48:14 +03:00
|
|
|
// The chunk size for this file. See the documentation for
|
|
|
|
// sqlite3_file_control() and FCNTL_CHUNK_SIZE.
|
|
|
|
int fileChunkSize;
|
|
|
|
|
2018-06-12 23:22:50 +03:00
|
|
|
// The filename
|
2019-05-01 11:47:10 +03:00
|
|
|
char* location;
|
2018-06-12 23:22:50 +03:00
|
|
|
|
2013-01-30 01:38:37 +04:00
|
|
|
// This contains the vfs that actually does work
|
|
|
|
sqlite3_file pReal[1];
|
|
|
|
};
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
const char* DatabasePathFromWALPath(const char* zWALName) {
|
2015-01-24 19:16:26 +03:00
|
|
|
/**
|
|
|
|
* Do some sketchy pointer arithmetic to find the parameter key. The WAL
|
|
|
|
* filename is in the middle of a big allocated block that contains:
|
|
|
|
*
|
|
|
|
* - Random Values
|
|
|
|
* - Main Database Path
|
|
|
|
* - \0
|
|
|
|
* - Multiple URI components consisting of:
|
|
|
|
* - Key
|
|
|
|
* - \0
|
|
|
|
* - Value
|
|
|
|
* - \0
|
|
|
|
* - \0
|
|
|
|
* - Journal Path
|
|
|
|
* - \0
|
|
|
|
* - WAL Path (zWALName)
|
|
|
|
* - \0
|
|
|
|
*
|
|
|
|
* Because the main database path is preceded by a random value we have to be
|
|
|
|
* careful when trying to figure out when we should terminate this loop.
|
|
|
|
*/
|
|
|
|
MOZ_ASSERT(zWALName);
|
|
|
|
|
|
|
|
nsDependentCSubstring dbPath(zWALName, strlen(zWALName));
|
|
|
|
|
|
|
|
// Chop off the "-wal" suffix.
|
|
|
|
NS_NAMED_LITERAL_CSTRING(kWALSuffix, "-wal");
|
|
|
|
MOZ_ASSERT(StringEndsWith(dbPath, kWALSuffix));
|
|
|
|
|
|
|
|
dbPath.Rebind(zWALName, dbPath.Length() - kWALSuffix.Length());
|
|
|
|
MOZ_ASSERT(!dbPath.IsEmpty());
|
|
|
|
|
|
|
|
// We want to scan to the end of the key/value URI pairs. Skip the preceding
|
|
|
|
// null and go to the last char of the journal path.
|
2019-05-01 11:47:10 +03:00
|
|
|
const char* cursor = zWALName - 2;
|
2015-01-24 19:16:26 +03:00
|
|
|
|
|
|
|
// Make sure we just skipped a null.
|
|
|
|
MOZ_ASSERT(!*(cursor + 1));
|
|
|
|
|
|
|
|
// Walk backwards over the journal path.
|
|
|
|
while (*cursor) {
|
|
|
|
cursor--;
|
|
|
|
}
|
|
|
|
|
|
|
|
// There should be another null here.
|
|
|
|
cursor--;
|
|
|
|
MOZ_ASSERT(!*cursor);
|
|
|
|
|
|
|
|
// Back up one more char to the last char of the previous string. It may be
|
|
|
|
// the database path or it may be a key/value URI pair.
|
|
|
|
cursor--;
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
{
|
|
|
|
// Verify that we just walked over the journal path. Account for the two
|
|
|
|
// nulls we just skipped.
|
2019-05-01 11:47:10 +03:00
|
|
|
const char* journalStart = cursor + 3;
|
2015-01-24 19:16:26 +03:00
|
|
|
|
|
|
|
nsDependentCSubstring journalPath(journalStart, strlen(journalStart));
|
|
|
|
|
|
|
|
// Chop off the "-journal" suffix.
|
|
|
|
NS_NAMED_LITERAL_CSTRING(kJournalSuffix, "-journal");
|
|
|
|
MOZ_ASSERT(StringEndsWith(journalPath, kJournalSuffix));
|
|
|
|
|
|
|
|
journalPath.Rebind(journalStart,
|
|
|
|
journalPath.Length() - kJournalSuffix.Length());
|
|
|
|
MOZ_ASSERT(!journalPath.IsEmpty());
|
|
|
|
|
|
|
|
// Make sure that the database name is a substring of the journal name.
|
|
|
|
MOZ_ASSERT(journalPath == dbPath);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Now we're either at the end of the key/value URI pairs or we're at the
|
|
|
|
// end of the database path. Carefully walk backwards one character at a
|
|
|
|
// time to do this safely without running past the beginning of the database
|
|
|
|
// path.
|
2019-05-01 11:47:10 +03:00
|
|
|
const char* const dbPathStart = dbPath.BeginReading();
|
|
|
|
const char* dbPathCursor = dbPath.EndReading() - 1;
|
2015-01-24 19:16:26 +03:00
|
|
|
bool isDBPath = true;
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
MOZ_ASSERT(*dbPathCursor, "dbPathCursor should never see a null char!");
|
|
|
|
|
|
|
|
if (isDBPath) {
|
|
|
|
isDBPath =
|
|
|
|
dbPathStart <= dbPathCursor && *dbPathCursor == *cursor && *cursor;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isDBPath) {
|
|
|
|
// This isn't the database path so it must be a value. Scan past it and
|
|
|
|
// the key also.
|
|
|
|
for (size_t stringCount = 0; stringCount < 2; stringCount++) {
|
|
|
|
// Scan past the string to the preceding null character.
|
|
|
|
while (*cursor) {
|
|
|
|
cursor--;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Back up one more char to the last char of preceding string.
|
|
|
|
cursor--;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset and start again.
|
|
|
|
dbPathCursor = dbPath.EndReading() - 1;
|
|
|
|
isDBPath = true;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(isDBPath);
|
|
|
|
MOZ_ASSERT(*cursor);
|
|
|
|
|
|
|
|
if (dbPathStart == dbPathCursor) {
|
|
|
|
// Found the full database path, we're all done.
|
|
|
|
MOZ_ASSERT(nsDependentCString(cursor) == dbPath);
|
|
|
|
return cursor;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Change the cursors and go through the loop again.
|
|
|
|
cursor--;
|
|
|
|
dbPathCursor--;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_CRASH("Should never get here!");
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<QuotaObject> GetQuotaObjectFromNameAndParameters(
|
2019-05-01 11:47:10 +03:00
|
|
|
const char* zName, const char* zURIParameterKey) {
|
2015-01-24 19:16:26 +03:00
|
|
|
MOZ_ASSERT(zName);
|
|
|
|
MOZ_ASSERT(zURIParameterKey);
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
const char* persistenceType =
|
2015-02-16 20:48:14 +03:00
|
|
|
sqlite3_uri_parameter(zURIParameterKey, "persistenceType");
|
2015-01-24 19:16:26 +03:00
|
|
|
if (!persistenceType) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
const char* group = sqlite3_uri_parameter(zURIParameterKey, "group");
|
2015-01-24 19:16:26 +03:00
|
|
|
if (!group) {
|
|
|
|
NS_WARNING("SQLite URI had 'persistenceType' but not 'group'?!");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
const char* origin = sqlite3_uri_parameter(zURIParameterKey, "origin");
|
2015-01-24 19:16:26 +03:00
|
|
|
if (!origin) {
|
|
|
|
NS_WARNING(
|
|
|
|
"SQLite URI had 'persistenceType' and 'group' but not "
|
|
|
|
"'origin'?!");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-08-22 16:06:48 +03:00
|
|
|
const char* clientType =
|
|
|
|
sqlite3_uri_parameter(zURIParameterKey, "clientType");
|
|
|
|
if (!clientType) {
|
|
|
|
NS_WARNING(
|
|
|
|
"SQLite URI had 'persistenceType', 'group' and 'origin' but not "
|
|
|
|
"'clientType'?!");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-06-07 09:04:57 +03:00
|
|
|
// Re-escape group and origin to make sure we get the right quota group and
|
|
|
|
// origin.
|
|
|
|
nsAutoCString escGroup;
|
|
|
|
nsresult rv =
|
|
|
|
NS_EscapeURL(nsDependentCString(group), esc_Query, escGroup, fallible);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoCString escOrigin;
|
|
|
|
rv = NS_EscapeURL(nsDependentCString(origin), esc_Query, escOrigin, fallible);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
QuotaManager* quotaManager = QuotaManager::Get();
|
2015-01-24 19:16:26 +03:00
|
|
|
MOZ_ASSERT(quotaManager);
|
|
|
|
|
|
|
|
return quotaManager->GetQuotaObject(
|
2019-06-07 09:04:57 +03:00
|
|
|
PersistenceTypeFromText(nsDependentCString(persistenceType)), escGroup,
|
2019-08-22 16:06:48 +03:00
|
|
|
escOrigin, Client::TypeFromText(nsDependentCString(clientType)),
|
|
|
|
NS_ConvertUTF8toUTF16(zName));
|
2015-01-24 19:16:26 +03:00
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
void MaybeEstablishQuotaControl(const char* zName, telemetry_file* pFile,
|
2015-01-24 19:16:26 +03:00
|
|
|
int flags) {
|
|
|
|
MOZ_ASSERT(pFile);
|
|
|
|
MOZ_ASSERT(!pFile->quotaObject);
|
|
|
|
|
|
|
|
if (!(flags & (SQLITE_OPEN_URI | SQLITE_OPEN_WAL))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(zName);
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
const char* zURIParameterKey =
|
2015-01-24 19:16:26 +03:00
|
|
|
(flags & SQLITE_OPEN_WAL) ? DatabasePathFromWALPath(zName) : zName;
|
|
|
|
|
|
|
|
MOZ_ASSERT(zURIParameterKey);
|
|
|
|
|
|
|
|
pFile->quotaObject =
|
|
|
|
GetQuotaObjectFromNameAndParameters(zName, zURIParameterKey);
|
|
|
|
}
|
|
|
|
|
2013-01-30 01:38:37 +04:00
|
|
|
/*
|
|
|
|
** Close a telemetry_file.
|
|
|
|
*/
|
2019-05-01 11:47:10 +03:00
|
|
|
int xClose(sqlite3_file* pFile) {
|
|
|
|
telemetry_file* p = (telemetry_file*)pFile;
|
2013-01-30 01:38:37 +04:00
|
|
|
int rc;
|
2013-10-01 23:53:19 +04:00
|
|
|
{ // Scope for IOThreadAutoTimer
|
|
|
|
IOThreadAutoTimer ioTimer(IOInterposeObserver::OpClose);
|
|
|
|
rc = p->pReal->pMethods->xClose(p->pReal);
|
|
|
|
}
|
2013-01-30 01:38:37 +04:00
|
|
|
if (rc == SQLITE_OK) {
|
|
|
|
delete p->base.pMethods;
|
2013-07-31 19:44:58 +04:00
|
|
|
p->base.pMethods = nullptr;
|
2013-01-30 01:38:37 +04:00
|
|
|
p->quotaObject = nullptr;
|
2018-06-12 23:22:50 +03:00
|
|
|
delete[] p->location;
|
2015-02-16 20:48:14 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
p->fileChunkSize = 0;
|
|
|
|
#endif
|
2013-01-30 01:38:37 +04:00
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Read data from a telemetry_file.
|
|
|
|
*/
|
2019-05-01 11:47:10 +03:00
|
|
|
int xRead(sqlite3_file* pFile, void* zBuf, int iAmt, sqlite_int64 iOfst) {
|
|
|
|
telemetry_file* p = (telemetry_file*)pFile;
|
2013-09-12 18:47:37 +04:00
|
|
|
IOThreadAutoTimer ioTimer(p->histograms->readMS, IOInterposeObserver::OpRead);
|
2013-01-30 01:38:37 +04:00
|
|
|
int rc;
|
|
|
|
rc = p->pReal->pMethods->xRead(p->pReal, zBuf, iAmt, iOfst);
|
2018-06-12 23:22:50 +03:00
|
|
|
if (rc == SQLITE_OK && IOActivityMonitor::IsActive()) {
|
|
|
|
IOActivityMonitor::Read(nsDependentCString(p->location), iAmt);
|
|
|
|
}
|
2013-01-30 01:38:37 +04:00
|
|
|
// sqlite likes to read from empty files, this is normal, ignore it.
|
|
|
|
if (rc != SQLITE_IOERR_SHORT_READ)
|
|
|
|
Telemetry::Accumulate(p->histograms->readB, rc == SQLITE_OK ? iAmt : 0);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2015-01-24 19:16:26 +03:00
|
|
|
/*
|
2015-02-16 20:48:14 +03:00
|
|
|
** Return the current file-size of a telemetry_file.
|
2015-01-24 19:16:26 +03:00
|
|
|
*/
|
2019-05-01 11:47:10 +03:00
|
|
|
int xFileSize(sqlite3_file* pFile, sqlite_int64* pSize) {
|
2015-02-16 20:48:14 +03:00
|
|
|
IOThreadAutoTimer ioTimer(IOInterposeObserver::OpStat);
|
2019-05-01 11:47:10 +03:00
|
|
|
telemetry_file* p = (telemetry_file*)pFile;
|
2015-01-24 19:16:26 +03:00
|
|
|
int rc;
|
2015-02-16 20:48:14 +03:00
|
|
|
rc = p->pReal->pMethods->xFileSize(p->pReal, pSize);
|
2015-01-24 19:16:26 +03:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2015-01-24 19:16:26 +03:00
|
|
|
/*
|
2015-02-16 20:48:14 +03:00
|
|
|
** Write data to a telemetry_file.
|
2015-01-24 19:16:26 +03:00
|
|
|
*/
|
2019-05-01 11:47:10 +03:00
|
|
|
int xWrite(sqlite3_file* pFile, const void* zBuf, int iAmt,
|
2015-02-16 20:48:14 +03:00
|
|
|
sqlite_int64 iOfst) {
|
2019-05-01 11:47:10 +03:00
|
|
|
telemetry_file* p = (telemetry_file*)pFile;
|
2015-02-16 20:48:14 +03:00
|
|
|
IOThreadAutoTimer ioTimer(p->histograms->writeMS,
|
|
|
|
IOInterposeObserver::OpWrite);
|
2015-01-24 19:16:26 +03:00
|
|
|
int rc;
|
2015-02-16 20:48:14 +03:00
|
|
|
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);
|
2018-06-12 23:22:50 +03:00
|
|
|
if (rc == SQLITE_OK && IOActivityMonitor::IsActive()) {
|
|
|
|
IOActivityMonitor::Write(nsDependentCString(p->location), iAmt);
|
|
|
|
}
|
|
|
|
|
2015-02-16 20:48:14 +03:00
|
|
|
Telemetry::Accumulate(p->histograms->writeB, rc == SQLITE_OK ? iAmt : 0);
|
|
|
|
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 (xFileSize(pFile, ¤tSize) == SQLITE_OK) {
|
|
|
|
p->quotaObject->MaybeUpdateSize(currentSize, /* aTruncate */ true);
|
|
|
|
}
|
|
|
|
}
|
2015-01-24 19:16:26 +03:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2013-01-30 01:38:37 +04:00
|
|
|
/*
|
|
|
|
** Truncate a telemetry_file.
|
|
|
|
*/
|
2019-05-01 11:47:10 +03:00
|
|
|
int xTruncate(sqlite3_file* pFile, sqlite_int64 size) {
|
2013-01-30 01:38:37 +04:00
|
|
|
IOThreadAutoTimer ioTimer(Telemetry::MOZ_SQLITE_TRUNCATE_MS);
|
2019-05-01 11:47:10 +03:00
|
|
|
telemetry_file* p = (telemetry_file*)pFile;
|
2013-01-30 01:38:37 +04:00
|
|
|
int rc;
|
|
|
|
Telemetry::AutoTimer<Telemetry::MOZ_SQLITE_TRUNCATE_MS> timer;
|
2015-02-16 20:48:14 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2013-01-30 01:38:37 +04:00
|
|
|
rc = p->pReal->pMethods->xTruncate(p->pReal, size);
|
2015-02-16 20:48:14 +03:00
|
|
|
if (p->quotaObject) {
|
2015-01-24 19:16:26 +03:00
|
|
|
if (rc == SQLITE_OK) {
|
2015-02-16 20:48:14 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
// Make sure xTruncate set the size exactly as we calculated above.
|
|
|
|
sqlite_int64 newSize;
|
|
|
|
MOZ_ASSERT(xFileSize(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 (xFileSize(pFile, &size) == SQLITE_OK) {
|
|
|
|
p->quotaObject->MaybeUpdateSize(size, /* aTruncate */ true);
|
|
|
|
}
|
2015-01-24 19:16:26 +03:00
|
|
|
}
|
2013-01-30 01:38:37 +04:00
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Sync a telemetry_file.
|
|
|
|
*/
|
2019-05-01 11:47:10 +03:00
|
|
|
int xSync(sqlite3_file* pFile, int flags) {
|
|
|
|
telemetry_file* p = (telemetry_file*)pFile;
|
2013-09-12 18:47:37 +04:00
|
|
|
IOThreadAutoTimer ioTimer(p->histograms->syncMS,
|
|
|
|
IOInterposeObserver::OpFSync);
|
2013-01-30 01:38:37 +04:00
|
|
|
return p->pReal->pMethods->xSync(p->pReal, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Lock a telemetry_file.
|
|
|
|
*/
|
2019-05-01 11:47:10 +03:00
|
|
|
int xLock(sqlite3_file* pFile, int eLock) {
|
|
|
|
telemetry_file* p = (telemetry_file*)pFile;
|
2013-01-30 01:38:37 +04:00
|
|
|
int rc;
|
|
|
|
rc = p->pReal->pMethods->xLock(p->pReal, eLock);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Unlock a telemetry_file.
|
|
|
|
*/
|
2019-05-01 11:47:10 +03:00
|
|
|
int xUnlock(sqlite3_file* pFile, int eLock) {
|
|
|
|
telemetry_file* p = (telemetry_file*)pFile;
|
2013-01-30 01:38:37 +04:00
|
|
|
int rc;
|
|
|
|
rc = p->pReal->pMethods->xUnlock(p->pReal, eLock);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Check if another file-handle holds a RESERVED lock on a telemetry_file.
|
|
|
|
*/
|
2019-05-01 11:47:10 +03:00
|
|
|
int xCheckReservedLock(sqlite3_file* pFile, int* pResOut) {
|
|
|
|
telemetry_file* p = (telemetry_file*)pFile;
|
2013-01-30 01:38:37 +04:00
|
|
|
int rc = p->pReal->pMethods->xCheckReservedLock(p->pReal, pResOut);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** File control method. For custom operations on a telemetry_file.
|
|
|
|
*/
|
2019-05-01 11:47:10 +03:00
|
|
|
int xFileControl(sqlite3_file* pFile, int op, void* pArg) {
|
|
|
|
telemetry_file* p = (telemetry_file*)pFile;
|
2015-02-16 20:48:14 +03:00
|
|
|
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) {
|
2019-05-01 11:47:10 +03:00
|
|
|
sqlite3_int64 hintSize = *static_cast<sqlite3_int64*>(pArg);
|
2015-02-16 20:48:14 +03:00
|
|
|
sqlite3_int64 currentSize;
|
|
|
|
rc = xFileSize(pFile, ¤tSize);
|
|
|
|
if (rc != SQLITE_OK) {
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
if (hintSize > currentSize) {
|
|
|
|
rc = xTruncate(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) {
|
2019-05-01 11:47:10 +03:00
|
|
|
p->fileChunkSize = *static_cast<int*>(pArg);
|
2015-02-16 20:48:14 +03:00
|
|
|
}
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (op == SQLITE_FCNTL_SIZE_HINT && p->quotaObject && rc == SQLITE_OK) {
|
2019-05-01 11:47:10 +03:00
|
|
|
sqlite3_int64 hintSize = *static_cast<sqlite3_int64*>(pArg);
|
2015-02-16 20:48:14 +03:00
|
|
|
if (p->fileChunkSize > 0) {
|
|
|
|
hintSize = ((hintSize + p->fileChunkSize - 1) / p->fileChunkSize) *
|
|
|
|
p->fileChunkSize;
|
|
|
|
}
|
|
|
|
sqlite3_int64 currentSize;
|
|
|
|
MOZ_ASSERT(xFileSize(pFile, ¤tSize) == SQLITE_OK);
|
|
|
|
MOZ_ASSERT(currentSize >= hintSize);
|
|
|
|
}
|
|
|
|
#endif
|
2013-01-30 01:38:37 +04:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Return the sector-size in bytes for a telemetry_file.
|
|
|
|
*/
|
2019-05-01 11:47:10 +03:00
|
|
|
int xSectorSize(sqlite3_file* pFile) {
|
|
|
|
telemetry_file* p = (telemetry_file*)pFile;
|
2013-01-30 01:38:37 +04:00
|
|
|
int rc;
|
|
|
|
rc = p->pReal->pMethods->xSectorSize(p->pReal);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Return the device characteristic flags supported by a telemetry_file.
|
|
|
|
*/
|
2019-05-01 11:47:10 +03:00
|
|
|
int xDeviceCharacteristics(sqlite3_file* pFile) {
|
|
|
|
telemetry_file* p = (telemetry_file*)pFile;
|
2013-01-30 01:38:37 +04:00
|
|
|
int rc;
|
|
|
|
rc = p->pReal->pMethods->xDeviceCharacteristics(p->pReal);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Shared-memory operations.
|
|
|
|
*/
|
2019-05-01 11:47:10 +03:00
|
|
|
int xShmLock(sqlite3_file* pFile, int ofst, int n, int flags) {
|
|
|
|
telemetry_file* p = (telemetry_file*)pFile;
|
2013-01-30 01:38:37 +04:00
|
|
|
return p->pReal->pMethods->xShmLock(p->pReal, ofst, n, flags);
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
int xShmMap(sqlite3_file* pFile, int iRegion, int szRegion, int isWrite,
|
|
|
|
void volatile** pp) {
|
|
|
|
telemetry_file* p = (telemetry_file*)pFile;
|
2013-01-30 01:38:37 +04:00
|
|
|
int rc;
|
|
|
|
rc = p->pReal->pMethods->xShmMap(p->pReal, iRegion, szRegion, isWrite, pp);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
void xShmBarrier(sqlite3_file* pFile) {
|
|
|
|
telemetry_file* p = (telemetry_file*)pFile;
|
2013-01-30 01:38:37 +04:00
|
|
|
p->pReal->pMethods->xShmBarrier(p->pReal);
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
int xShmUnmap(sqlite3_file* pFile, int delFlag) {
|
|
|
|
telemetry_file* p = (telemetry_file*)pFile;
|
2013-01-30 01:38:37 +04:00
|
|
|
int rc;
|
|
|
|
rc = p->pReal->pMethods->xShmUnmap(p->pReal, delFlag);
|
|
|
|
return rc;
|
|
|
|
}
|
2013-06-01 20:26:17 +04:00
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
int xFetch(sqlite3_file* pFile, sqlite3_int64 iOff, int iAmt, void** pp) {
|
|
|
|
telemetry_file* p = (telemetry_file*)pFile;
|
2013-06-01 20:26:17 +04:00
|
|
|
MOZ_ASSERT(p->pReal->pMethods->iVersion >= 3);
|
|
|
|
return p->pReal->pMethods->xFetch(p->pReal, iOff, iAmt, pp);
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
int xUnfetch(sqlite3_file* pFile, sqlite3_int64 iOff, void* pResOut) {
|
|
|
|
telemetry_file* p = (telemetry_file*)pFile;
|
2013-06-01 20:26:17 +04:00
|
|
|
MOZ_ASSERT(p->pReal->pMethods->iVersion >= 3);
|
|
|
|
return p->pReal->pMethods->xUnfetch(p->pReal, iOff, pResOut);
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
int xOpen(sqlite3_vfs* vfs, const char* zName, sqlite3_file* pFile, int flags,
|
|
|
|
int* pOutFlags) {
|
2013-10-01 23:53:19 +04:00
|
|
|
IOThreadAutoTimer ioTimer(Telemetry::MOZ_SQLITE_OPEN_MS,
|
|
|
|
IOInterposeObserver::OpCreateOrOpen);
|
2013-01-30 01:38:37 +04:00
|
|
|
Telemetry::AutoTimer<Telemetry::MOZ_SQLITE_OPEN_MS> timer;
|
2019-05-01 11:47:10 +03:00
|
|
|
sqlite3_vfs* orig_vfs = static_cast<sqlite3_vfs*>(vfs->pAppData);
|
2013-01-30 01:38:37 +04:00
|
|
|
int rc;
|
2019-05-01 11:47:10 +03:00
|
|
|
telemetry_file* p = (telemetry_file*)pFile;
|
|
|
|
Histograms* h = nullptr;
|
2013-01-30 01:38:37 +04:00
|
|
|
// check if the filename is one we are probing for
|
|
|
|
for (size_t i = 0; i < sizeof(gHistograms) / sizeof(gHistograms[0]); i++) {
|
|
|
|
h = &gHistograms[i];
|
|
|
|
// last probe is the fallback probe
|
|
|
|
if (!h->name) break;
|
|
|
|
if (!zName) continue;
|
2019-05-01 11:47:10 +03:00
|
|
|
const char* match = strstr(zName, h->name);
|
2013-01-30 01:38:37 +04:00
|
|
|
if (!match) continue;
|
|
|
|
char c = match[strlen(h->name)];
|
|
|
|
// include -wal/-journal too
|
|
|
|
if (!c || c == '-') break;
|
|
|
|
}
|
|
|
|
p->histograms = h;
|
|
|
|
|
2015-01-24 19:16:26 +03:00
|
|
|
MaybeEstablishQuotaControl(zName, p, flags);
|
2013-01-30 01:38:37 +04:00
|
|
|
|
|
|
|
rc = orig_vfs->xOpen(orig_vfs, zName, p->pReal, flags, pOutFlags);
|
|
|
|
if (rc != SQLITE_OK) return rc;
|
2018-06-12 23:22:50 +03:00
|
|
|
|
|
|
|
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://");
|
|
|
|
}
|
|
|
|
|
2013-01-30 01:38:37 +04:00
|
|
|
if (p->pReal->pMethods) {
|
2019-05-01 11:47:10 +03:00
|
|
|
sqlite3_io_methods* pNew = new sqlite3_io_methods;
|
|
|
|
const sqlite3_io_methods* pSub = p->pReal->pMethods;
|
2013-01-30 01:38:37 +04:00
|
|
|
memset(pNew, 0, sizeof(*pNew));
|
2013-10-23 16:58:06 +04:00
|
|
|
// 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);
|
2013-01-30 01:38:37 +04:00
|
|
|
pNew->xClose = xClose;
|
|
|
|
pNew->xRead = xRead;
|
|
|
|
pNew->xWrite = xWrite;
|
|
|
|
pNew->xTruncate = xTruncate;
|
|
|
|
pNew->xSync = xSync;
|
|
|
|
pNew->xFileSize = xFileSize;
|
|
|
|
pNew->xLock = xLock;
|
|
|
|
pNew->xUnlock = xUnlock;
|
|
|
|
pNew->xCheckReservedLock = xCheckReservedLock;
|
|
|
|
pNew->xFileControl = xFileControl;
|
|
|
|
pNew->xSectorSize = xSectorSize;
|
|
|
|
pNew->xDeviceCharacteristics = xDeviceCharacteristics;
|
2013-10-23 16:58:06 +04:00
|
|
|
if (pNew->iVersion >= 2) {
|
|
|
|
// Methods added in version 2.
|
|
|
|
pNew->xShmMap = pSub->xShmMap ? xShmMap : 0;
|
|
|
|
pNew->xShmLock = pSub->xShmLock ? xShmLock : 0;
|
|
|
|
pNew->xShmBarrier = pSub->xShmBarrier ? xShmBarrier : 0;
|
|
|
|
pNew->xShmUnmap = pSub->xShmUnmap ? xShmUnmap : 0;
|
|
|
|
}
|
|
|
|
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 = xFetch;
|
|
|
|
MOZ_ASSERT(pSub->xUnfetch);
|
|
|
|
pNew->xUnfetch = xUnfetch;
|
|
|
|
}
|
2013-01-30 01:38:37 +04:00
|
|
|
pFile->pMethods = pNew;
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
int xDelete(sqlite3_vfs* vfs, const char* zName, int syncDir) {
|
|
|
|
sqlite3_vfs* orig_vfs = static_cast<sqlite3_vfs*>(vfs->pAppData);
|
2015-01-24 19:16:26 +03:00
|
|
|
int rc;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<QuotaObject> quotaObject;
|
2015-01-24 19:16:26 +03:00
|
|
|
|
|
|
|
if (StringEndsWith(nsDependentCString(zName), NS_LITERAL_CSTRING("-wal"))) {
|
2019-05-01 11:47:10 +03:00
|
|
|
const char* zURIParameterKey = DatabasePathFromWALPath(zName);
|
2015-01-24 19:16:26 +03:00
|
|
|
MOZ_ASSERT(zURIParameterKey);
|
|
|
|
|
|
|
|
quotaObject = GetQuotaObjectFromNameAndParameters(zName, zURIParameterKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = orig_vfs->xDelete(orig_vfs, zName, syncDir);
|
|
|
|
if (rc == SQLITE_OK && quotaObject) {
|
2015-02-16 20:48:14 +03:00
|
|
|
MOZ_ALWAYS_TRUE(quotaObject->MaybeUpdateSize(0, /* aTruncate */ true));
|
2015-01-24 19:16:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
2013-01-30 01:38:37 +04:00
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
int xAccess(sqlite3_vfs* vfs, const char* zName, int flags, int* pResOut) {
|
|
|
|
sqlite3_vfs* orig_vfs = static_cast<sqlite3_vfs*>(vfs->pAppData);
|
2013-01-30 01:38:37 +04:00
|
|
|
return orig_vfs->xAccess(orig_vfs, zName, flags, pResOut);
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
int xFullPathname(sqlite3_vfs* vfs, const char* zName, int nOut, char* zOut) {
|
|
|
|
sqlite3_vfs* orig_vfs = static_cast<sqlite3_vfs*>(vfs->pAppData);
|
2013-01-30 01:38:37 +04:00
|
|
|
return orig_vfs->xFullPathname(orig_vfs, zName, nOut, zOut);
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
void* xDlOpen(sqlite3_vfs* vfs, const char* zFilename) {
|
|
|
|
sqlite3_vfs* orig_vfs = static_cast<sqlite3_vfs*>(vfs->pAppData);
|
2013-01-30 01:38:37 +04:00
|
|
|
return orig_vfs->xDlOpen(orig_vfs, zFilename);
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
void xDlError(sqlite3_vfs* vfs, int nByte, char* zErrMsg) {
|
|
|
|
sqlite3_vfs* orig_vfs = static_cast<sqlite3_vfs*>(vfs->pAppData);
|
2013-01-30 01:38:37 +04:00
|
|
|
orig_vfs->xDlError(orig_vfs, nByte, zErrMsg);
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
void (*xDlSym(sqlite3_vfs* vfs, void* pHdle, const char* zSym))(void) {
|
|
|
|
sqlite3_vfs* orig_vfs = static_cast<sqlite3_vfs*>(vfs->pAppData);
|
2013-01-30 01:38:37 +04:00
|
|
|
return orig_vfs->xDlSym(orig_vfs, pHdle, zSym);
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
void xDlClose(sqlite3_vfs* vfs, void* pHandle) {
|
|
|
|
sqlite3_vfs* orig_vfs = static_cast<sqlite3_vfs*>(vfs->pAppData);
|
2013-01-30 01:38:37 +04:00
|
|
|
orig_vfs->xDlClose(orig_vfs, pHandle);
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
int xRandomness(sqlite3_vfs* vfs, int nByte, char* zOut) {
|
|
|
|
sqlite3_vfs* orig_vfs = static_cast<sqlite3_vfs*>(vfs->pAppData);
|
2013-01-30 01:38:37 +04:00
|
|
|
return orig_vfs->xRandomness(orig_vfs, nByte, zOut);
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
int xSleep(sqlite3_vfs* vfs, int microseconds) {
|
|
|
|
sqlite3_vfs* orig_vfs = static_cast<sqlite3_vfs*>(vfs->pAppData);
|
2013-01-30 01:38:37 +04:00
|
|
|
return orig_vfs->xSleep(orig_vfs, microseconds);
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
int xCurrentTime(sqlite3_vfs* vfs, double* prNow) {
|
|
|
|
sqlite3_vfs* orig_vfs = static_cast<sqlite3_vfs*>(vfs->pAppData);
|
2013-01-30 01:38:37 +04:00
|
|
|
return orig_vfs->xCurrentTime(orig_vfs, prNow);
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
int xGetLastError(sqlite3_vfs* vfs, int nBuf, char* zBuf) {
|
|
|
|
sqlite3_vfs* orig_vfs = static_cast<sqlite3_vfs*>(vfs->pAppData);
|
2013-01-30 01:38:37 +04:00
|
|
|
return orig_vfs->xGetLastError(orig_vfs, nBuf, zBuf);
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
int xCurrentTimeInt64(sqlite3_vfs* vfs, sqlite3_int64* piNow) {
|
|
|
|
sqlite3_vfs* orig_vfs = static_cast<sqlite3_vfs*>(vfs->pAppData);
|
2013-01-30 01:38:37 +04:00
|
|
|
return orig_vfs->xCurrentTimeInt64(orig_vfs, piNow);
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
static int xSetSystemCall(sqlite3_vfs* vfs, const char* zName,
|
2013-01-30 01:38:37 +04:00
|
|
|
sqlite3_syscall_ptr pFunc) {
|
2019-05-01 11:47:10 +03:00
|
|
|
sqlite3_vfs* orig_vfs = static_cast<sqlite3_vfs*>(vfs->pAppData);
|
2013-01-30 01:38:37 +04:00
|
|
|
return orig_vfs->xSetSystemCall(orig_vfs, zName, pFunc);
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
static sqlite3_syscall_ptr xGetSystemCall(sqlite3_vfs* vfs, const char* zName) {
|
|
|
|
sqlite3_vfs* orig_vfs = static_cast<sqlite3_vfs*>(vfs->pAppData);
|
2013-01-30 01:38:37 +04:00
|
|
|
return orig_vfs->xGetSystemCall(orig_vfs, zName);
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
static const char* xNextSystemCall(sqlite3_vfs* vfs, const char* zName) {
|
|
|
|
sqlite3_vfs* orig_vfs = static_cast<sqlite3_vfs*>(vfs->pAppData);
|
2013-01-30 01:38:37 +04:00
|
|
|
return orig_vfs->xNextSystemCall(orig_vfs, zName);
|
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
2013-01-30 01:38:37 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace storage {
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
const char* GetVFSName() { return "telemetry-vfs"; }
|
2017-09-29 14:25:06 +03:00
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
sqlite3_vfs* ConstructTelemetryVFS() {
|
2013-01-30 01:38:37 +04:00
|
|
|
#if defined(XP_WIN)
|
2018-07-13 19:54:57 +03:00
|
|
|
# define EXPECTED_VFS "win32"
|
|
|
|
# define EXPECTED_VFS_EXCL "win32"
|
2013-01-30 01:38:37 +04:00
|
|
|
#else
|
2018-07-13 19:54:57 +03:00
|
|
|
# define EXPECTED_VFS "unix"
|
|
|
|
# define EXPECTED_VFS_EXCL "unix-excl"
|
2013-01-30 01:38:37 +04:00
|
|
|
#endif
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2013-01-30 01:38:37 +04:00
|
|
|
bool expected_vfs;
|
2019-05-01 11:47:10 +03:00
|
|
|
sqlite3_vfs* vfs;
|
2018-07-13 19:54:57 +03:00
|
|
|
if (Preferences::GetBool(PREF_MULTI_PROCESS_ACCESS, false)) {
|
|
|
|
// Use the non-exclusive VFS.
|
2013-07-31 19:44:58 +04:00
|
|
|
vfs = sqlite3_vfs_find(nullptr);
|
2013-01-30 01:38:37 +04:00
|
|
|
expected_vfs = vfs->zName && !strcmp(vfs->zName, EXPECTED_VFS);
|
2018-07-13 19:54:57 +03:00
|
|
|
} else {
|
|
|
|
vfs = sqlite3_vfs_find(EXPECTED_VFS_EXCL);
|
|
|
|
expected_vfs = (vfs != nullptr);
|
2013-01-30 01:38:37 +04:00
|
|
|
}
|
|
|
|
if (!expected_vfs) {
|
2013-07-31 19:44:58 +04:00
|
|
|
return nullptr;
|
2013-01-30 01:38:37 +04:00
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
sqlite3_vfs* tvfs = new ::sqlite3_vfs;
|
2013-01-30 01:38:37 +04:00
|
|
|
memset(tvfs, 0, sizeof(::sqlite3_vfs));
|
2013-10-23 16:58:06 +04:00
|
|
|
// 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.
|
|
|
|
tvfs->iVersion = vfs->iVersion;
|
|
|
|
MOZ_ASSERT(vfs->iVersion <= LAST_KNOWN_VFS_VERSION);
|
2013-01-30 01:38:37 +04:00
|
|
|
tvfs->szOsFile =
|
|
|
|
sizeof(telemetry_file) - sizeof(sqlite3_file) + vfs->szOsFile;
|
|
|
|
tvfs->mxPathname = vfs->mxPathname;
|
2017-09-29 14:25:06 +03:00
|
|
|
tvfs->zName = GetVFSName();
|
2013-01-30 01:38:37 +04:00
|
|
|
tvfs->pAppData = vfs;
|
|
|
|
tvfs->xOpen = xOpen;
|
|
|
|
tvfs->xDelete = xDelete;
|
|
|
|
tvfs->xAccess = xAccess;
|
|
|
|
tvfs->xFullPathname = xFullPathname;
|
|
|
|
tvfs->xDlOpen = xDlOpen;
|
|
|
|
tvfs->xDlError = xDlError;
|
|
|
|
tvfs->xDlSym = xDlSym;
|
|
|
|
tvfs->xDlClose = xDlClose;
|
|
|
|
tvfs->xRandomness = xRandomness;
|
|
|
|
tvfs->xSleep = xSleep;
|
|
|
|
tvfs->xCurrentTime = xCurrentTime;
|
|
|
|
tvfs->xGetLastError = xGetLastError;
|
2013-10-23 16:58:06 +04:00
|
|
|
if (tvfs->iVersion >= 2) {
|
|
|
|
// Methods added in version 2.
|
|
|
|
tvfs->xCurrentTimeInt64 = xCurrentTimeInt64;
|
|
|
|
}
|
|
|
|
if (tvfs->iVersion >= 3) {
|
|
|
|
// Methods added in version 3.
|
|
|
|
tvfs->xSetSystemCall = xSetSystemCall;
|
|
|
|
tvfs->xGetSystemCall = xGetSystemCall;
|
|
|
|
tvfs->xNextSystemCall = xNextSystemCall;
|
|
|
|
}
|
2013-01-30 01:38:37 +04:00
|
|
|
return tvfs;
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
already_AddRefed<QuotaObject> GetQuotaObjectForFile(sqlite3_file* pFile) {
|
2016-03-15 09:00:28 +03:00
|
|
|
MOZ_ASSERT(pFile);
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
telemetry_file* p = (telemetry_file*)pFile;
|
2016-03-15 09:00:28 +03:00
|
|
|
RefPtr<QuotaObject> result = p->quotaObject;
|
|
|
|
return result.forget();
|
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace storage
|
|
|
|
} // namespace mozilla
|