2014-02-18 19:05:07 +04:00
|
|
|
/* 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 "CacheIndex.h"
|
|
|
|
|
|
|
|
#include "CacheLog.h"
|
|
|
|
#include "CacheFileIOManager.h"
|
|
|
|
#include "CacheFileMetadata.h"
|
2014-04-10 14:47:20 +04:00
|
|
|
#include "CacheIndexIterator.h"
|
|
|
|
#include "CacheIndexContextIterator.h"
|
2014-02-18 19:05:07 +04:00
|
|
|
#include "nsThreadUtils.h"
|
2014-02-27 03:11:40 +04:00
|
|
|
#include "nsISizeOf.h"
|
2014-02-18 19:05:07 +04:00
|
|
|
#include "nsPrintfCString.h"
|
|
|
|
#include "mozilla/DebugOnly.h"
|
|
|
|
#include "prinrval.h"
|
|
|
|
#include "nsIFile.h"
|
|
|
|
#include "nsITimer.h"
|
2014-03-07 15:22:59 +04:00
|
|
|
#include "mozilla/AutoRestore.h"
|
2014-02-18 19:05:07 +04:00
|
|
|
#include <algorithm>
|
2015-06-14 15:13:18 +03:00
|
|
|
#include "mozilla/Telemetry.h"
|
2016-08-23 07:09:32 +03:00
|
|
|
#include "mozilla/Unused.h"
|
2014-02-18 19:05:07 +04:00
|
|
|
|
|
|
|
#define kMinUnwrittenChanges 300
|
|
|
|
#define kMinDumpInterval 20000 // in milliseconds
|
|
|
|
#define kMaxBufSize 16384
|
2020-01-14 16:53:01 +03:00
|
|
|
#define kIndexVersion 0x00000009
|
2014-03-28 17:51:58 +04:00
|
|
|
#define kUpdateIndexStartDelay 50000 // in milliseconds
|
2019-04-25 15:21:17 +03:00
|
|
|
#define kTelemetryReportBytesLimit (2U * 1024U * 1024U * 1024U) // 2GB
|
2014-02-18 19:05:07 +04:00
|
|
|
|
2015-05-16 11:07:10 +03:00
|
|
|
#define INDEX_NAME "index"
|
|
|
|
#define TEMP_INDEX_NAME "index.tmp"
|
|
|
|
#define JOURNAL_NAME "index.log"
|
2014-02-18 19:05:07 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace net {
|
|
|
|
|
2015-08-18 01:18:59 +03:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class FrecencyComparator {
|
|
|
|
public:
|
|
|
|
bool Equals(CacheIndexRecord* a, CacheIndexRecord* b) const {
|
2016-10-06 12:23:52 +03:00
|
|
|
if (!a || !b) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-08-18 01:18:59 +03:00
|
|
|
return a->mFrecency == b->mFrecency;
|
|
|
|
}
|
|
|
|
bool LessThan(CacheIndexRecord* a, CacheIndexRecord* b) const {
|
2016-10-06 12:23:52 +03:00
|
|
|
// Removed (=null) entries must be at the end of the array.
|
|
|
|
if (!a) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!b) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Place entries with frecency 0 at the end of the non-removed entries.
|
2015-08-18 01:18:59 +03:00
|
|
|
if (a->mFrecency == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (b->mFrecency == 0) {
|
|
|
|
return true;
|
|
|
|
}
|
2016-10-06 12:23:52 +03:00
|
|
|
|
2015-08-18 01:18:59 +03:00
|
|
|
return a->mFrecency < b->mFrecency;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2014-02-18 19:05:07 +04:00
|
|
|
/**
|
2015-03-03 09:58:00 +03:00
|
|
|
* This helper class is responsible for keeping CacheIndex::mIndexStats and
|
|
|
|
* CacheIndex::mFrecencyArray up to date.
|
2014-02-18 19:05:07 +04:00
|
|
|
*/
|
|
|
|
class CacheIndexEntryAutoManage {
|
|
|
|
public:
|
|
|
|
CacheIndexEntryAutoManage(const SHA1Sum::Hash* aHash, CacheIndex* aIndex)
|
|
|
|
: mIndex(aIndex),
|
|
|
|
mOldRecord(nullptr),
|
2016-02-17 13:47:47 +03:00
|
|
|
mOldFrecency(0),
|
2014-02-18 19:05:07 +04:00
|
|
|
mDoNotSearchInIndex(false),
|
|
|
|
mDoNotSearchInUpdates(false) {
|
2015-11-12 13:07:48 +03:00
|
|
|
CacheIndex::sLock.AssertCurrentThreadOwns();
|
2014-02-18 21:26:48 +04:00
|
|
|
|
2014-02-18 19:05:07 +04:00
|
|
|
mHash = aHash;
|
2014-10-28 17:59:53 +03:00
|
|
|
const CacheIndexEntry* entry = FindEntry();
|
2014-02-18 19:05:07 +04:00
|
|
|
mIndex->mIndexStats.BeforeChange(entry);
|
|
|
|
if (entry && entry->IsInitialized() && !entry->IsRemoved()) {
|
2020-02-11 19:20:08 +03:00
|
|
|
mOldRecord = entry->mRec.get();
|
2016-02-17 13:47:47 +03:00
|
|
|
mOldFrecency = entry->mRec->mFrecency;
|
2014-02-18 19:05:07 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
~CacheIndexEntryAutoManage() {
|
2015-11-12 13:07:48 +03:00
|
|
|
CacheIndex::sLock.AssertCurrentThreadOwns();
|
2014-02-18 21:26:48 +04:00
|
|
|
|
2014-10-28 17:59:53 +03:00
|
|
|
const CacheIndexEntry* entry = FindEntry();
|
2014-02-18 19:05:07 +04:00
|
|
|
mIndex->mIndexStats.AfterChange(entry);
|
|
|
|
if (!entry || !entry->IsInitialized() || entry->IsRemoved()) {
|
|
|
|
entry = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (entry && !mOldRecord) {
|
2020-02-11 19:20:08 +03:00
|
|
|
mIndex->mFrecencyArray.AppendRecord(entry->mRec.get());
|
|
|
|
mIndex->AddRecordToIterators(entry->mRec.get());
|
2014-02-18 19:05:07 +04:00
|
|
|
} else if (!entry && mOldRecord) {
|
2016-10-06 12:23:52 +03:00
|
|
|
mIndex->mFrecencyArray.RemoveRecord(mOldRecord);
|
2014-04-10 14:47:20 +04:00
|
|
|
mIndex->RemoveRecordFromIterators(mOldRecord);
|
2014-02-18 19:05:07 +04:00
|
|
|
} else if (entry && mOldRecord) {
|
2020-02-11 19:20:08 +03:00
|
|
|
auto rec = entry->mRec.get();
|
|
|
|
if (rec != mOldRecord) {
|
2014-02-18 19:05:07 +04:00
|
|
|
// record has a different address, we have to replace it
|
2020-02-11 19:20:08 +03:00
|
|
|
mIndex->ReplaceRecordInIterators(mOldRecord, rec);
|
2016-10-06 12:23:52 +03:00
|
|
|
|
|
|
|
if (entry->mRec->mFrecency == mOldFrecency) {
|
|
|
|
// If frecency hasn't changed simply replace the pointer
|
2020-02-11 19:20:08 +03:00
|
|
|
mIndex->mFrecencyArray.ReplaceRecord(mOldRecord, rec);
|
2016-10-06 12:23:52 +03:00
|
|
|
} else {
|
|
|
|
// Remove old pointer and insert the new one at the end of the array
|
|
|
|
mIndex->mFrecencyArray.RemoveRecord(mOldRecord);
|
2020-02-11 19:20:08 +03:00
|
|
|
mIndex->mFrecencyArray.AppendRecord(rec);
|
2016-10-06 12:23:52 +03:00
|
|
|
}
|
2016-02-17 13:47:47 +03:00
|
|
|
} else if (entry->mRec->mFrecency != mOldFrecency) {
|
2016-10-06 12:23:52 +03:00
|
|
|
// Move the element at the end of the array
|
2020-02-11 19:20:08 +03:00
|
|
|
mIndex->mFrecencyArray.RemoveRecord(rec);
|
|
|
|
mIndex->mFrecencyArray.AppendRecord(rec);
|
2014-02-18 19:05:07 +04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// both entries were removed or not initialized, do nothing
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-04 01:00:02 +03:00
|
|
|
// We cannot rely on nsTHashtable::GetEntry() in case we are removing entries
|
|
|
|
// while iterating. Destructor is called before the entry is removed. Caller
|
|
|
|
// must call one of following methods to skip lookup in the hashtable.
|
2014-02-18 19:05:07 +04:00
|
|
|
void DoNotSearchInIndex() { mDoNotSearchInIndex = true; }
|
|
|
|
void DoNotSearchInUpdates() { mDoNotSearchInUpdates = true; }
|
|
|
|
|
|
|
|
private:
|
2014-10-28 17:59:53 +03:00
|
|
|
const CacheIndexEntry* FindEntry() {
|
|
|
|
const CacheIndexEntry* entry = nullptr;
|
2014-02-18 19:05:07 +04:00
|
|
|
|
|
|
|
switch (mIndex->mState) {
|
|
|
|
case CacheIndex::READING:
|
|
|
|
case CacheIndex::WRITING:
|
|
|
|
if (!mDoNotSearchInUpdates) {
|
|
|
|
entry = mIndex->mPendingUpdates.GetEntry(*mHash);
|
|
|
|
}
|
2019-12-20 10:16:43 +03:00
|
|
|
[[fallthrough]];
|
2014-02-18 19:05:07 +04:00
|
|
|
case CacheIndex::BUILDING:
|
|
|
|
case CacheIndex::UPDATING:
|
|
|
|
case CacheIndex::READY:
|
|
|
|
if (!entry && !mDoNotSearchInIndex) {
|
|
|
|
entry = mIndex->mIndex.GetEntry(*mHash);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CacheIndex::INITIAL:
|
|
|
|
case CacheIndex::SHUTDOWN:
|
|
|
|
default:
|
|
|
|
MOZ_ASSERT(false, "Unexpected state!");
|
|
|
|
}
|
|
|
|
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
const SHA1Sum::Hash* mHash;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CacheIndex> mIndex;
|
2014-02-18 19:05:07 +04:00
|
|
|
CacheIndexRecord* mOldRecord;
|
2016-02-17 13:47:47 +03:00
|
|
|
uint32_t mOldFrecency;
|
2014-02-18 19:05:07 +04:00
|
|
|
bool mDoNotSearchInIndex;
|
|
|
|
bool mDoNotSearchInUpdates;
|
|
|
|
};
|
|
|
|
|
2018-03-09 17:37:18 +03:00
|
|
|
class FileOpenHelper final : public CacheFileIOListener {
|
2014-03-07 15:22:59 +04:00
|
|
|
public:
|
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
|
|
|
|
2014-08-05 17:20:24 +04:00
|
|
|
explicit FileOpenHelper(CacheIndex* aIndex)
|
2014-03-07 15:22:59 +04:00
|
|
|
: mIndex(aIndex), mCanceled(false) {}
|
|
|
|
|
|
|
|
void Cancel() {
|
2015-11-12 13:07:48 +03:00
|
|
|
CacheIndex::sLock.AssertCurrentThreadOwns();
|
2014-03-07 15:22:59 +04:00
|
|
|
mCanceled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2018-04-30 19:46:04 +03:00
|
|
|
virtual ~FileOpenHelper() = default;
|
2014-06-24 20:36:44 +04:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
NS_IMETHOD OnFileOpened(CacheFileHandle* aHandle, nsresult aResult) override;
|
2014-03-07 15:22:59 +04:00
|
|
|
NS_IMETHOD OnDataWritten(CacheFileHandle* aHandle, const char* aBuf,
|
2015-03-21 19:28:04 +03:00
|
|
|
nsresult aResult) override {
|
2014-03-07 15:22:59 +04:00
|
|
|
MOZ_CRASH("FileOpenHelper::OnDataWritten should not be called!");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
NS_IMETHOD OnDataRead(CacheFileHandle* aHandle, char* aBuf,
|
2015-03-21 19:28:04 +03:00
|
|
|
nsresult aResult) override {
|
2014-03-07 15:22:59 +04:00
|
|
|
MOZ_CRASH("FileOpenHelper::OnDataRead should not be called!");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
2015-03-21 19:28:04 +03:00
|
|
|
NS_IMETHOD OnFileDoomed(CacheFileHandle* aHandle, nsresult aResult) override {
|
2014-03-07 15:22:59 +04:00
|
|
|
MOZ_CRASH("FileOpenHelper::OnFileDoomed should not be called!");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
2015-03-21 19:28:04 +03:00
|
|
|
NS_IMETHOD OnEOFSet(CacheFileHandle* aHandle, nsresult aResult) override {
|
2014-03-07 15:22:59 +04:00
|
|
|
MOZ_CRASH("FileOpenHelper::OnEOFSet should not be called!");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
2015-03-21 19:28:04 +03:00
|
|
|
NS_IMETHOD OnFileRenamed(CacheFileHandle* aHandle,
|
|
|
|
nsresult aResult) override {
|
2014-03-07 15:22:59 +04:00
|
|
|
MOZ_CRASH("FileOpenHelper::OnFileRenamed should not be called!");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CacheIndex> mIndex;
|
2014-03-07 15:22:59 +04:00
|
|
|
bool mCanceled;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMETHODIMP FileOpenHelper::OnFileOpened(CacheFileHandle* aHandle,
|
|
|
|
nsresult aResult) {
|
2015-11-12 13:07:48 +03:00
|
|
|
StaticMutexAutoLock lock(CacheIndex::sLock);
|
2014-03-07 15:22:59 +04:00
|
|
|
|
|
|
|
if (mCanceled) {
|
|
|
|
if (aHandle) {
|
|
|
|
CacheFileIOManager::DoomFile(aHandle, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
mIndex->OnFileOpenedInternal(this, aHandle, aResult);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(FileOpenHelper, CacheFileIOListener);
|
2014-03-07 15:22:59 +04:00
|
|
|
|
2016-07-21 19:54:17 +03:00
|
|
|
StaticRefPtr<CacheIndex> CacheIndex::gInstance;
|
2015-11-12 13:07:48 +03:00
|
|
|
StaticMutex CacheIndex::sLock;
|
2014-02-18 19:05:07 +04:00
|
|
|
|
|
|
|
NS_IMPL_ADDREF(CacheIndex)
|
|
|
|
NS_IMPL_RELEASE(CacheIndex)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN(CacheIndex)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(mozilla::net::CacheFileIOListener)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIRunnable)
|
2018-02-12 22:36:46 +03:00
|
|
|
NS_INTERFACE_MAP_END
|
2014-02-18 19:05:07 +04:00
|
|
|
|
|
|
|
CacheIndex::CacheIndex()
|
2015-11-12 13:07:48 +03:00
|
|
|
: mState(INITIAL),
|
2014-02-18 19:05:07 +04:00
|
|
|
mShuttingDown(false),
|
|
|
|
mIndexNeedsUpdate(false),
|
2014-03-07 15:22:59 +04:00
|
|
|
mRemovingAll(false),
|
2014-02-18 19:05:07 +04:00
|
|
|
mIndexOnDiskIsValid(false),
|
|
|
|
mDontMarkIndexClean(false),
|
|
|
|
mIndexTimeStamp(0),
|
2014-03-07 15:22:59 +04:00
|
|
|
mUpdateEventPending(false),
|
2014-02-18 19:05:07 +04:00
|
|
|
mSkipEntries(0),
|
|
|
|
mProcessEntries(0),
|
|
|
|
mRWBuf(nullptr),
|
|
|
|
mRWBufSize(0),
|
|
|
|
mRWBufPos(0),
|
2016-05-11 11:48:28 +03:00
|
|
|
mRWPending(false),
|
2014-02-18 19:05:07 +04:00
|
|
|
mJournalReadSuccessfully(false),
|
2019-04-25 15:21:17 +03:00
|
|
|
mAsyncGetDiskConsumptionBlocked(false),
|
|
|
|
mTotalBytesWritten(0) {
|
2015-11-12 13:07:48 +03:00
|
|
|
sLock.AssertCurrentThreadOwns();
|
2014-02-18 19:05:07 +04:00
|
|
|
LOG(("CacheIndex::CacheIndex [this=%p]", this));
|
|
|
|
MOZ_ASSERT(!gInstance, "multiple CacheIndex instances!");
|
|
|
|
}
|
|
|
|
|
|
|
|
CacheIndex::~CacheIndex() {
|
2016-05-11 11:48:28 +03:00
|
|
|
sLock.AssertCurrentThreadOwns();
|
2014-02-18 19:05:07 +04:00
|
|
|
LOG(("CacheIndex::~CacheIndex [this=%p]", this));
|
|
|
|
|
|
|
|
ReleaseBuffer();
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
nsresult CacheIndex::Init(nsIFile* aCacheDirectory) {
|
|
|
|
LOG(("CacheIndex::Init()"));
|
|
|
|
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2015-11-12 13:07:48 +03:00
|
|
|
StaticMutexAutoLock lock(sLock);
|
|
|
|
|
2014-02-18 19:05:07 +04:00
|
|
|
if (gInstance) {
|
|
|
|
return NS_ERROR_ALREADY_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CacheIndex> idx = new CacheIndex();
|
2014-02-18 19:05:07 +04:00
|
|
|
|
|
|
|
nsresult rv = idx->InitInternal(aCacheDirectory);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2020-02-13 17:38:48 +03:00
|
|
|
gInstance = std::move(idx);
|
2014-02-18 19:05:07 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult CacheIndex::InitInternal(nsIFile* aCacheDirectory) {
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
rv = aCacheDirectory->Clone(getter_AddRefs(mCacheDirectory));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
mStartTime = TimeStamp::NowLoRes();
|
|
|
|
|
2019-04-25 15:21:17 +03:00
|
|
|
mTotalBytesWritten = CacheObserver::CacheAmountWritten();
|
|
|
|
mTotalBytesWritten <<= 10;
|
|
|
|
|
2014-03-07 15:22:59 +04:00
|
|
|
ReadIndexFromDisk();
|
2014-02-18 19:05:07 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
nsresult CacheIndex::PreShutdown() {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2015-11-12 13:07:48 +03:00
|
|
|
StaticMutexAutoLock lock(sLock);
|
|
|
|
|
2016-07-21 19:54:17 +03:00
|
|
|
LOG(("CacheIndex::PreShutdown() [gInstance=%p]", gInstance.get()));
|
2015-11-12 13:07:48 +03:00
|
|
|
|
2014-02-18 19:05:07 +04:00
|
|
|
nsresult rv;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CacheIndex> index = gInstance;
|
2014-02-18 19:05:07 +04:00
|
|
|
|
|
|
|
if (!index) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG(
|
|
|
|
("CacheIndex::PreShutdown() - [state=%d, indexOnDiskIsValid=%d, "
|
|
|
|
"dontMarkIndexClean=%d]",
|
|
|
|
index->mState, index->mIndexOnDiskIsValid, index->mDontMarkIndexClean));
|
|
|
|
|
2014-04-10 14:47:20 +04:00
|
|
|
LOG(("CacheIndex::PreShutdown() - Closing iterators."));
|
|
|
|
for (uint32_t i = 0; i < index->mIterators.Length();) {
|
|
|
|
rv = index->mIterators[i]->CloseInternal(NS_ERROR_FAILURE);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// CacheIndexIterator::CloseInternal() removes itself from mIteratos iff
|
|
|
|
// it returns success.
|
|
|
|
LOG(
|
|
|
|
("CacheIndex::PreShutdown() - Failed to remove iterator %p. "
|
2016-12-16 06:16:31 +03:00
|
|
|
"[rv=0x%08" PRIx32 "]",
|
|
|
|
index->mIterators[i], static_cast<uint32_t>(rv)));
|
2014-04-10 14:47:20 +04:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-18 19:05:07 +04:00
|
|
|
index->mShuttingDown = true;
|
|
|
|
|
|
|
|
if (index->mState == READY) {
|
|
|
|
return NS_OK; // nothing to do
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIRunnable> event;
|
2017-06-12 22:34:10 +03:00
|
|
|
event = NewRunnableMethod("net::CacheIndex::PreShutdownInternal", index,
|
|
|
|
&CacheIndex::PreShutdownInternal);
|
2014-02-18 19:05:07 +04:00
|
|
|
|
2016-05-11 11:48:28 +03:00
|
|
|
nsCOMPtr<nsIEventTarget> ioTarget = CacheFileIOManager::IOTarget();
|
|
|
|
MOZ_ASSERT(ioTarget);
|
2016-02-18 13:43:20 +03:00
|
|
|
|
2016-05-11 11:48:28 +03:00
|
|
|
// PreShutdownInternal() will be executed before any queued event on INDEX
|
|
|
|
// level. That's OK since we don't want to wait for any operation in progess.
|
|
|
|
rv = ioTarget->Dispatch(event, nsIEventTarget::DISPATCH_NORMAL);
|
2014-02-18 19:05:07 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("CacheIndex::PreShutdown() - Can't dispatch event");
|
|
|
|
LOG(("CacheIndex::PreShutdown() - Can't dispatch event"));
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CacheIndex::PreShutdownInternal() {
|
2015-11-12 13:07:48 +03:00
|
|
|
StaticMutexAutoLock lock(sLock);
|
2014-02-18 19:05:07 +04:00
|
|
|
|
|
|
|
LOG(
|
|
|
|
("CacheIndex::PreShutdownInternal() - [state=%d, indexOnDiskIsValid=%d, "
|
|
|
|
"dontMarkIndexClean=%d]",
|
|
|
|
mState, mIndexOnDiskIsValid, mDontMarkIndexClean));
|
|
|
|
|
|
|
|
MOZ_ASSERT(mShuttingDown);
|
|
|
|
|
2014-03-07 15:22:59 +04:00
|
|
|
if (mUpdateTimer) {
|
2017-07-07 11:58:00 +03:00
|
|
|
mUpdateTimer->Cancel();
|
2014-03-07 15:22:59 +04:00
|
|
|
mUpdateTimer = nullptr;
|
2014-02-18 19:05:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (mState) {
|
|
|
|
case WRITING:
|
|
|
|
FinishWrite(false);
|
|
|
|
break;
|
|
|
|
case READY:
|
|
|
|
// nothing to do, write the journal in Shutdown()
|
|
|
|
break;
|
|
|
|
case READING:
|
|
|
|
FinishRead(false);
|
|
|
|
break;
|
|
|
|
case BUILDING:
|
|
|
|
case UPDATING:
|
|
|
|
FinishUpdate(false);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
MOZ_ASSERT(false, "Implement me!");
|
|
|
|
}
|
|
|
|
|
|
|
|
// We should end up in READY state
|
|
|
|
MOZ_ASSERT(mState == READY);
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
nsresult CacheIndex::Shutdown() {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2015-11-12 13:07:48 +03:00
|
|
|
StaticMutexAutoLock lock(sLock);
|
|
|
|
|
2016-07-21 19:54:17 +03:00
|
|
|
LOG(("CacheIndex::Shutdown() [gInstance=%p]", gInstance.get()));
|
2015-11-12 13:07:48 +03:00
|
|
|
|
2016-07-21 19:54:17 +03:00
|
|
|
RefPtr<CacheIndex> index = gInstance.forget();
|
2014-02-18 19:05:07 +04:00
|
|
|
|
|
|
|
if (!index) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
2014-04-09 12:06:59 +04:00
|
|
|
bool sanitize = CacheObserver::ClearCacheOnShutdown();
|
|
|
|
|
2019-04-25 15:21:17 +03:00
|
|
|
CacheObserver::SetCacheAmountWritten(index->mTotalBytesWritten >> 10);
|
|
|
|
|
2014-02-18 19:05:07 +04:00
|
|
|
LOG(
|
|
|
|
("CacheIndex::Shutdown() - [state=%d, indexOnDiskIsValid=%d, "
|
2014-04-09 12:06:59 +04:00
|
|
|
"dontMarkIndexClean=%d, sanitize=%d]",
|
|
|
|
index->mState, index->mIndexOnDiskIsValid, index->mDontMarkIndexClean,
|
|
|
|
sanitize));
|
2014-02-18 19:05:07 +04:00
|
|
|
|
|
|
|
MOZ_ASSERT(index->mShuttingDown);
|
|
|
|
|
|
|
|
EState oldState = index->mState;
|
|
|
|
index->ChangeState(SHUTDOWN);
|
|
|
|
|
|
|
|
if (oldState != READY) {
|
|
|
|
LOG(
|
|
|
|
("CacheIndex::Shutdown() - Unexpected state. Did posting of "
|
|
|
|
"PreShutdownInternal() fail?"));
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (oldState) {
|
|
|
|
case WRITING:
|
|
|
|
index->FinishWrite(false);
|
2019-12-20 10:16:43 +03:00
|
|
|
[[fallthrough]];
|
2014-02-18 19:05:07 +04:00
|
|
|
case READY:
|
|
|
|
if (index->mIndexOnDiskIsValid && !index->mDontMarkIndexClean) {
|
2014-04-09 12:06:59 +04:00
|
|
|
if (!sanitize && NS_FAILED(index->WriteLogToDisk())) {
|
2016-09-04 03:39:49 +03:00
|
|
|
index->RemoveJournalAndTempFile();
|
2014-02-18 19:05:07 +04:00
|
|
|
}
|
|
|
|
} else {
|
2016-09-04 03:39:49 +03:00
|
|
|
index->RemoveJournalAndTempFile();
|
2014-02-18 19:05:07 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case READING:
|
|
|
|
index->FinishRead(false);
|
|
|
|
break;
|
|
|
|
case BUILDING:
|
|
|
|
case UPDATING:
|
|
|
|
index->FinishUpdate(false);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
MOZ_ASSERT(false, "Unexpected state!");
|
|
|
|
}
|
|
|
|
|
2014-04-09 12:06:59 +04:00
|
|
|
if (sanitize) {
|
2016-09-04 03:39:49 +03:00
|
|
|
index->RemoveAllIndexFiles();
|
2014-04-09 12:06:59 +04:00
|
|
|
}
|
|
|
|
|
2014-02-18 19:05:07 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
nsresult CacheIndex::AddEntry(const SHA1Sum::Hash* aHash) {
|
|
|
|
LOG(("CacheIndex::AddEntry() [hash=%08x%08x%08x%08x%08x]", LOGSHA1(aHash)));
|
|
|
|
|
2015-11-12 13:07:48 +03:00
|
|
|
MOZ_ASSERT(CacheFileIOManager::IsOnIOThread());
|
|
|
|
|
|
|
|
StaticMutexAutoLock lock(sLock);
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CacheIndex> index = gInstance;
|
2014-02-18 19:05:07 +04:00
|
|
|
|
|
|
|
if (!index) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!index->IsIndexUsable()) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Getters in CacheIndexStats assert when mStateLogged is true since the
|
|
|
|
// information is incomplete between calls to BeforeChange() and AfterChange()
|
|
|
|
// (i.e. while CacheIndexEntryAutoManage exists). We need to check whether
|
|
|
|
// non-fresh entries exists outside the scope of CacheIndexEntryAutoManage.
|
|
|
|
bool updateIfNonFreshEntriesExist = false;
|
|
|
|
|
|
|
|
{
|
|
|
|
CacheIndexEntryAutoManage entryMng(aHash, index);
|
|
|
|
|
|
|
|
CacheIndexEntry* entry = index->mIndex.GetEntry(*aHash);
|
|
|
|
bool entryRemoved = entry && entry->IsRemoved();
|
2014-10-28 17:59:53 +03:00
|
|
|
CacheIndexEntryUpdate* updated = nullptr;
|
2014-02-18 19:05:07 +04:00
|
|
|
|
|
|
|
if (index->mState == READY || index->mState == UPDATING ||
|
|
|
|
index->mState == BUILDING) {
|
|
|
|
MOZ_ASSERT(index->mPendingUpdates.Count() == 0);
|
|
|
|
|
|
|
|
if (entry && !entryRemoved) {
|
|
|
|
// Found entry in index that shouldn't exist.
|
|
|
|
|
|
|
|
if (entry->IsFresh()) {
|
|
|
|
// Someone removed the file on disk while FF is running. Update
|
|
|
|
// process can fix only non-fresh entries (i.e. entries that were not
|
|
|
|
// added within this session). Start update only if we have such
|
|
|
|
// entries.
|
|
|
|
//
|
|
|
|
// TODO: This should be very rare problem. If it turns out not to be
|
|
|
|
// true, change the update process so that it also iterates all
|
|
|
|
// initialized non-empty entries and checks whether the file exists.
|
|
|
|
|
|
|
|
LOG(
|
|
|
|
("CacheIndex::AddEntry() - Cache file was removed outside FF "
|
|
|
|
"process!"));
|
|
|
|
|
|
|
|
updateIfNonFreshEntriesExist = true;
|
|
|
|
} else if (index->mState == READY) {
|
|
|
|
// Index is outdated, update it.
|
|
|
|
LOG(
|
|
|
|
("CacheIndex::AddEntry() - Found entry that shouldn't exist, "
|
|
|
|
"update is needed"));
|
|
|
|
index->mIndexNeedsUpdate = true;
|
|
|
|
} else {
|
|
|
|
// We cannot be here when building index since all entries are fresh
|
|
|
|
// during building.
|
|
|
|
MOZ_ASSERT(index->mState == UPDATING);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!entry) {
|
|
|
|
entry = index->mIndex.PutEntry(*aHash);
|
|
|
|
}
|
|
|
|
} else { // WRITING, READING
|
2014-10-28 17:59:53 +03:00
|
|
|
updated = index->mPendingUpdates.GetEntry(*aHash);
|
2014-02-18 19:05:07 +04:00
|
|
|
bool updatedRemoved = updated && updated->IsRemoved();
|
|
|
|
|
|
|
|
if ((updated && !updatedRemoved) ||
|
|
|
|
(!updated && entry && !entryRemoved && entry->IsFresh())) {
|
|
|
|
// Fresh entry found, so the file was removed outside FF
|
|
|
|
LOG(
|
|
|
|
("CacheIndex::AddEntry() - Cache file was removed outside FF "
|
|
|
|
"process!"));
|
|
|
|
|
|
|
|
updateIfNonFreshEntriesExist = true;
|
|
|
|
} else if (!updated && entry && !entryRemoved) {
|
|
|
|
if (index->mState == WRITING) {
|
|
|
|
LOG(
|
|
|
|
("CacheIndex::AddEntry() - Found entry that shouldn't exist, "
|
|
|
|
"update is needed"));
|
|
|
|
index->mIndexNeedsUpdate = true;
|
|
|
|
}
|
|
|
|
// Ignore if state is READING since the index information is partial
|
|
|
|
}
|
|
|
|
|
|
|
|
updated = index->mPendingUpdates.PutEntry(*aHash);
|
|
|
|
}
|
|
|
|
|
2014-10-28 17:59:53 +03:00
|
|
|
if (updated) {
|
|
|
|
updated->InitNew();
|
|
|
|
updated->MarkDirty();
|
|
|
|
updated->MarkFresh();
|
|
|
|
} else {
|
|
|
|
entry->InitNew();
|
|
|
|
entry->MarkDirty();
|
|
|
|
entry->MarkFresh();
|
|
|
|
}
|
2014-02-18 19:05:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (updateIfNonFreshEntriesExist &&
|
|
|
|
index->mIndexStats.Count() != index->mIndexStats.Fresh()) {
|
|
|
|
index->mIndexNeedsUpdate = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
index->StartUpdatingIndexIfNeeded();
|
|
|
|
index->WriteIndexToDiskIfNeeded();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
nsresult CacheIndex::EnsureEntryExists(const SHA1Sum::Hash* aHash) {
|
|
|
|
LOG(("CacheIndex::EnsureEntryExists() [hash=%08x%08x%08x%08x%08x]",
|
|
|
|
LOGSHA1(aHash)));
|
|
|
|
|
2015-11-12 13:07:48 +03:00
|
|
|
MOZ_ASSERT(CacheFileIOManager::IsOnIOThread());
|
|
|
|
|
|
|
|
StaticMutexAutoLock lock(sLock);
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CacheIndex> index = gInstance;
|
2014-02-18 19:05:07 +04:00
|
|
|
|
|
|
|
if (!index) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!index->IsIndexUsable()) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
CacheIndexEntryAutoManage entryMng(aHash, index);
|
|
|
|
|
|
|
|
CacheIndexEntry* entry = index->mIndex.GetEntry(*aHash);
|
|
|
|
bool entryRemoved = entry && entry->IsRemoved();
|
|
|
|
|
|
|
|
if (index->mState == READY || index->mState == UPDATING ||
|
|
|
|
index->mState == BUILDING) {
|
|
|
|
MOZ_ASSERT(index->mPendingUpdates.Count() == 0);
|
|
|
|
|
|
|
|
if (!entry || entryRemoved) {
|
|
|
|
if (entryRemoved && entry->IsFresh()) {
|
|
|
|
// This could happen only if somebody copies files to the entries
|
|
|
|
// directory while FF is running.
|
|
|
|
LOG(
|
|
|
|
("CacheIndex::EnsureEntryExists() - Cache file was added outside "
|
|
|
|
"FF process! Update is needed."));
|
|
|
|
index->mIndexNeedsUpdate = true;
|
|
|
|
} else if (index->mState == READY ||
|
|
|
|
(entryRemoved && !entry->IsFresh())) {
|
|
|
|
// Removed non-fresh entries can be present as a result of
|
2015-07-27 06:15:36 +03:00
|
|
|
// MergeJournal()
|
2014-02-18 19:05:07 +04:00
|
|
|
LOG(
|
|
|
|
("CacheIndex::EnsureEntryExists() - Didn't find entry that should"
|
|
|
|
" exist, update is needed"));
|
|
|
|
index->mIndexNeedsUpdate = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!entry) {
|
|
|
|
entry = index->mIndex.PutEntry(*aHash);
|
|
|
|
}
|
|
|
|
entry->InitNew();
|
|
|
|
entry->MarkDirty();
|
|
|
|
}
|
|
|
|
entry->MarkFresh();
|
|
|
|
} else { // WRITING, READING
|
2014-10-28 17:59:53 +03:00
|
|
|
CacheIndexEntryUpdate* updated = index->mPendingUpdates.GetEntry(*aHash);
|
2014-02-18 19:05:07 +04:00
|
|
|
bool updatedRemoved = updated && updated->IsRemoved();
|
|
|
|
|
|
|
|
if (updatedRemoved || (!updated && entryRemoved && entry->IsFresh())) {
|
|
|
|
// Fresh information about missing entry found. This could happen only
|
|
|
|
// if somebody copies files to the entries directory while FF is
|
|
|
|
// running.
|
|
|
|
LOG(
|
|
|
|
("CacheIndex::EnsureEntryExists() - Cache file was added outside "
|
|
|
|
"FF process! Update is needed."));
|
|
|
|
index->mIndexNeedsUpdate = true;
|
|
|
|
} else if (!updated && (!entry || entryRemoved)) {
|
|
|
|
if (index->mState == WRITING) {
|
|
|
|
LOG(
|
|
|
|
("CacheIndex::EnsureEntryExists() - Didn't find entry that should"
|
|
|
|
" exist, update is needed"));
|
|
|
|
index->mIndexNeedsUpdate = true;
|
|
|
|
}
|
|
|
|
// Ignore if state is READING since the index information is partial
|
|
|
|
}
|
|
|
|
|
|
|
|
// We don't need entryRemoved and updatedRemoved info anymore
|
|
|
|
if (entryRemoved) entry = nullptr;
|
|
|
|
if (updatedRemoved) updated = nullptr;
|
|
|
|
|
|
|
|
if (updated) {
|
|
|
|
updated->MarkFresh();
|
|
|
|
} else {
|
|
|
|
if (!entry) {
|
|
|
|
// Create a new entry
|
|
|
|
updated = index->mPendingUpdates.PutEntry(*aHash);
|
|
|
|
updated->InitNew();
|
|
|
|
updated->MarkFresh();
|
|
|
|
updated->MarkDirty();
|
|
|
|
} else {
|
|
|
|
if (!entry->IsFresh()) {
|
|
|
|
// To mark the entry fresh we must make a copy of index entry
|
|
|
|
// since the index is read-only.
|
|
|
|
updated = index->mPendingUpdates.PutEntry(*aHash);
|
|
|
|
*updated = *entry;
|
|
|
|
updated->MarkFresh();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
index->StartUpdatingIndexIfNeeded();
|
|
|
|
index->WriteIndexToDiskIfNeeded();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
nsresult CacheIndex::InitEntry(const SHA1Sum::Hash* aHash,
|
2016-11-08 23:11:30 +03:00
|
|
|
OriginAttrsHash aOriginAttrsHash,
|
2014-02-18 19:05:07 +04:00
|
|
|
bool aAnonymous, bool aPinned) {
|
2016-11-08 23:11:30 +03:00
|
|
|
LOG(
|
|
|
|
("CacheIndex::InitEntry() [hash=%08x%08x%08x%08x%08x, "
|
2016-12-16 06:16:31 +03:00
|
|
|
"originAttrsHash=%" PRIx64 ", anonymous=%d, pinned=%d]",
|
2016-11-08 23:11:30 +03:00
|
|
|
LOGSHA1(aHash), aOriginAttrsHash, aAnonymous, aPinned));
|
2014-02-18 19:05:07 +04:00
|
|
|
|
2015-11-12 13:07:48 +03:00
|
|
|
MOZ_ASSERT(CacheFileIOManager::IsOnIOThread());
|
|
|
|
|
|
|
|
StaticMutexAutoLock lock(sLock);
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CacheIndex> index = gInstance;
|
2014-02-18 19:05:07 +04:00
|
|
|
|
|
|
|
if (!index) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!index->IsIndexUsable()) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
CacheIndexEntryAutoManage entryMng(aHash, index);
|
|
|
|
|
|
|
|
CacheIndexEntry* entry = index->mIndex.GetEntry(*aHash);
|
2014-10-28 17:59:53 +03:00
|
|
|
CacheIndexEntryUpdate* updated = nullptr;
|
2014-02-18 19:05:07 +04:00
|
|
|
bool reinitEntry = false;
|
|
|
|
|
|
|
|
if (entry && entry->IsRemoved()) {
|
|
|
|
entry = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (index->mState == READY || index->mState == UPDATING ||
|
|
|
|
index->mState == BUILDING) {
|
|
|
|
MOZ_ASSERT(index->mPendingUpdates.Count() == 0);
|
|
|
|
MOZ_ASSERT(entry);
|
|
|
|
MOZ_ASSERT(entry->IsFresh());
|
|
|
|
|
2017-04-15 11:49:00 +03:00
|
|
|
if (!entry) {
|
|
|
|
LOG(("CacheIndex::InitEntry() - Entry was not found in mIndex!"));
|
|
|
|
NS_WARNING(
|
|
|
|
("CacheIndex::InitEntry() - Entry was not found in mIndex!"));
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
2016-11-08 23:11:30 +03:00
|
|
|
if (IsCollision(entry, aOriginAttrsHash, aAnonymous)) {
|
2014-02-18 19:05:07 +04:00
|
|
|
index->mIndexNeedsUpdate =
|
|
|
|
true; // TODO Does this really help in case of collision?
|
|
|
|
reinitEntry = true;
|
|
|
|
} else {
|
|
|
|
if (entry->IsInitialized()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2014-10-28 17:59:53 +03:00
|
|
|
updated = index->mPendingUpdates.GetEntry(*aHash);
|
2014-02-18 19:05:07 +04:00
|
|
|
DebugOnly<bool> removed = updated && updated->IsRemoved();
|
|
|
|
|
|
|
|
MOZ_ASSERT(updated || !removed);
|
|
|
|
MOZ_ASSERT(updated || entry);
|
|
|
|
|
2017-04-15 11:49:00 +03:00
|
|
|
if (!updated && !entry) {
|
|
|
|
LOG(
|
|
|
|
("CacheIndex::InitEntry() - Entry was found neither in mIndex nor "
|
|
|
|
"in mPendingUpdates!"));
|
|
|
|
NS_WARNING(
|
|
|
|
("CacheIndex::InitEntry() - Entry was found neither in "
|
|
|
|
"mIndex nor in mPendingUpdates!"));
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
2014-02-18 19:05:07 +04:00
|
|
|
if (updated) {
|
|
|
|
MOZ_ASSERT(updated->IsFresh());
|
|
|
|
|
2016-11-08 23:11:30 +03:00
|
|
|
if (IsCollision(updated, aOriginAttrsHash, aAnonymous)) {
|
2014-02-18 19:05:07 +04:00
|
|
|
index->mIndexNeedsUpdate = true;
|
|
|
|
reinitEntry = true;
|
|
|
|
} else {
|
|
|
|
if (updated->IsInitialized()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(entry->IsFresh());
|
|
|
|
|
2016-11-08 23:11:30 +03:00
|
|
|
if (IsCollision(entry, aOriginAttrsHash, aAnonymous)) {
|
2014-02-18 19:05:07 +04:00
|
|
|
index->mIndexNeedsUpdate = true;
|
|
|
|
reinitEntry = true;
|
|
|
|
} else {
|
|
|
|
if (entry->IsInitialized()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// make a copy of a read-only entry
|
|
|
|
updated = index->mPendingUpdates.PutEntry(*aHash);
|
|
|
|
*updated = *entry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (reinitEntry) {
|
|
|
|
// There is a collision and we are going to rewrite this entry. Initialize
|
|
|
|
// it as a new entry.
|
2014-10-28 17:59:53 +03:00
|
|
|
if (updated) {
|
|
|
|
updated->InitNew();
|
|
|
|
updated->MarkFresh();
|
|
|
|
} else {
|
|
|
|
entry->InitNew();
|
|
|
|
entry->MarkFresh();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (updated) {
|
2016-11-08 23:11:30 +03:00
|
|
|
updated->Init(aOriginAttrsHash, aAnonymous, aPinned);
|
2014-10-28 17:59:53 +03:00
|
|
|
updated->MarkDirty();
|
|
|
|
} else {
|
2016-11-08 23:11:30 +03:00
|
|
|
entry->Init(aOriginAttrsHash, aAnonymous, aPinned);
|
2014-10-28 17:59:53 +03:00
|
|
|
entry->MarkDirty();
|
2014-02-18 19:05:07 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
index->StartUpdatingIndexIfNeeded();
|
|
|
|
index->WriteIndexToDiskIfNeeded();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
nsresult CacheIndex::RemoveEntry(const SHA1Sum::Hash* aHash) {
|
|
|
|
LOG(("CacheIndex::RemoveEntry() [hash=%08x%08x%08x%08x%08x]",
|
|
|
|
LOGSHA1(aHash)));
|
|
|
|
|
2015-11-12 13:07:48 +03:00
|
|
|
MOZ_ASSERT(CacheFileIOManager::IsOnIOThread());
|
|
|
|
|
|
|
|
StaticMutexAutoLock lock(sLock);
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CacheIndex> index = gInstance;
|
2014-02-18 19:05:07 +04:00
|
|
|
|
|
|
|
if (!index) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!index->IsIndexUsable()) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
CacheIndexEntryAutoManage entryMng(aHash, index);
|
|
|
|
|
|
|
|
CacheIndexEntry* entry = index->mIndex.GetEntry(*aHash);
|
|
|
|
bool entryRemoved = entry && entry->IsRemoved();
|
|
|
|
|
|
|
|
if (index->mState == READY || index->mState == UPDATING ||
|
|
|
|
index->mState == BUILDING) {
|
|
|
|
MOZ_ASSERT(index->mPendingUpdates.Count() == 0);
|
|
|
|
|
|
|
|
if (!entry || entryRemoved) {
|
|
|
|
if (entryRemoved && entry->IsFresh()) {
|
|
|
|
// This could happen only if somebody copies files to the entries
|
|
|
|
// directory while FF is running.
|
|
|
|
LOG(
|
|
|
|
("CacheIndex::RemoveEntry() - Cache file was added outside FF "
|
|
|
|
"process! Update is needed."));
|
|
|
|
index->mIndexNeedsUpdate = true;
|
|
|
|
} else if (index->mState == READY ||
|
|
|
|
(entryRemoved && !entry->IsFresh())) {
|
|
|
|
// Removed non-fresh entries can be present as a result of
|
2015-07-27 06:15:36 +03:00
|
|
|
// MergeJournal()
|
2014-02-18 19:05:07 +04:00
|
|
|
LOG(
|
|
|
|
("CacheIndex::RemoveEntry() - Didn't find entry that should exist"
|
|
|
|
", update is needed"));
|
|
|
|
index->mIndexNeedsUpdate = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (entry) {
|
|
|
|
if (!entry->IsDirty() && entry->IsFileEmpty()) {
|
2015-09-25 05:10:37 +03:00
|
|
|
index->mIndex.RemoveEntry(entry);
|
2014-02-18 19:05:07 +04:00
|
|
|
entry = nullptr;
|
|
|
|
} else {
|
|
|
|
entry->MarkRemoved();
|
|
|
|
entry->MarkDirty();
|
|
|
|
entry->MarkFresh();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else { // WRITING, READING
|
2014-10-28 17:59:53 +03:00
|
|
|
CacheIndexEntryUpdate* updated = index->mPendingUpdates.GetEntry(*aHash);
|
2014-02-18 19:05:07 +04:00
|
|
|
bool updatedRemoved = updated && updated->IsRemoved();
|
|
|
|
|
|
|
|
if (updatedRemoved || (!updated && entryRemoved && entry->IsFresh())) {
|
|
|
|
// Fresh information about missing entry found. This could happen only
|
|
|
|
// if somebody copies files to the entries directory while FF is
|
|
|
|
// running.
|
|
|
|
LOG(
|
|
|
|
("CacheIndex::RemoveEntry() - Cache file was added outside FF "
|
|
|
|
"process! Update is needed."));
|
|
|
|
index->mIndexNeedsUpdate = true;
|
|
|
|
} else if (!updated && (!entry || entryRemoved)) {
|
|
|
|
if (index->mState == WRITING) {
|
|
|
|
LOG(
|
|
|
|
("CacheIndex::RemoveEntry() - Didn't find entry that should exist"
|
|
|
|
", update is needed"));
|
|
|
|
index->mIndexNeedsUpdate = true;
|
|
|
|
}
|
|
|
|
// Ignore if state is READING since the index information is partial
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!updated) {
|
|
|
|
updated = index->mPendingUpdates.PutEntry(*aHash);
|
|
|
|
updated->InitNew();
|
|
|
|
}
|
|
|
|
|
|
|
|
updated->MarkRemoved();
|
|
|
|
updated->MarkDirty();
|
|
|
|
updated->MarkFresh();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
index->StartUpdatingIndexIfNeeded();
|
|
|
|
index->WriteIndexToDiskIfNeeded();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2019-04-25 15:21:17 +03:00
|
|
|
nsresult CacheIndex::UpdateEntry(const SHA1Sum::Hash* aHash,
|
|
|
|
const uint32_t* aFrecency,
|
|
|
|
const bool* aHasAltData,
|
|
|
|
const uint16_t* aOnStartTime,
|
|
|
|
const uint16_t* aOnStopTime,
|
|
|
|
const uint8_t* aContentType,
|
|
|
|
const uint32_t* aSize) {
|
2014-02-18 19:05:07 +04:00
|
|
|
LOG(
|
|
|
|
("CacheIndex::UpdateEntry() [hash=%08x%08x%08x%08x%08x, "
|
2019-03-27 17:32:12 +03:00
|
|
|
"frecency=%s, hasAltData=%s, onStartTime=%s, onStopTime=%s, "
|
2020-01-14 16:53:01 +03:00
|
|
|
"contentType=%s, size=%s]",
|
2014-02-18 19:05:07 +04:00
|
|
|
LOGSHA1(aHash), aFrecency ? nsPrintfCString("%u", *aFrecency).get() : "",
|
2017-03-06 13:21:59 +03:00
|
|
|
aHasAltData ? (*aHasAltData ? "true" : "false") : "",
|
2017-03-28 06:40:21 +03:00
|
|
|
aOnStartTime ? nsPrintfCString("%u", *aOnStartTime).get() : "",
|
|
|
|
aOnStopTime ? nsPrintfCString("%u", *aOnStopTime).get() : "",
|
2019-03-27 17:32:12 +03:00
|
|
|
aContentType ? nsPrintfCString("%u", *aContentType).get() : "",
|
2020-01-14 16:53:01 +03:00
|
|
|
aSize ? nsPrintfCString("%u", *aSize).get() : ""));
|
2014-02-18 19:05:07 +04:00
|
|
|
|
2015-11-12 13:07:48 +03:00
|
|
|
MOZ_ASSERT(CacheFileIOManager::IsOnIOThread());
|
|
|
|
|
|
|
|
StaticMutexAutoLock lock(sLock);
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CacheIndex> index = gInstance;
|
2014-02-18 19:05:07 +04:00
|
|
|
|
|
|
|
if (!index) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!index->IsIndexUsable()) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
CacheIndexEntryAutoManage entryMng(aHash, index);
|
|
|
|
|
|
|
|
CacheIndexEntry* entry = index->mIndex.GetEntry(*aHash);
|
|
|
|
|
|
|
|
if (entry && entry->IsRemoved()) {
|
|
|
|
entry = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (index->mState == READY || index->mState == UPDATING ||
|
|
|
|
index->mState == BUILDING) {
|
|
|
|
MOZ_ASSERT(index->mPendingUpdates.Count() == 0);
|
|
|
|
MOZ_ASSERT(entry);
|
|
|
|
|
2017-04-15 11:49:00 +03:00
|
|
|
if (!entry) {
|
|
|
|
LOG(("CacheIndex::UpdateEntry() - Entry was not found in mIndex!"));
|
|
|
|
NS_WARNING(
|
|
|
|
("CacheIndex::UpdateEntry() - Entry was not found in mIndex!"));
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
2019-04-25 15:21:17 +03:00
|
|
|
if (!HasEntryChanged(entry, aFrecency, aHasAltData, aOnStartTime,
|
2020-01-14 16:53:01 +03:00
|
|
|
aOnStopTime, aContentType, aSize)) {
|
2014-02-18 19:05:07 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2014-10-28 17:59:53 +03:00
|
|
|
|
|
|
|
MOZ_ASSERT(entry->IsFresh());
|
|
|
|
MOZ_ASSERT(entry->IsInitialized());
|
|
|
|
entry->MarkDirty();
|
|
|
|
|
|
|
|
if (aFrecency) {
|
|
|
|
entry->SetFrecency(*aFrecency);
|
|
|
|
}
|
|
|
|
|
2017-03-28 06:40:21 +03:00
|
|
|
if (aHasAltData) {
|
|
|
|
entry->SetHasAltData(*aHasAltData);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aOnStartTime) {
|
|
|
|
entry->SetOnStartTime(*aOnStartTime);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aOnStopTime) {
|
|
|
|
entry->SetOnStopTime(*aOnStopTime);
|
|
|
|
}
|
|
|
|
|
2019-03-27 17:32:12 +03:00
|
|
|
if (aContentType) {
|
|
|
|
entry->SetContentType(*aContentType);
|
|
|
|
}
|
|
|
|
|
2014-10-28 17:59:53 +03:00
|
|
|
if (aSize) {
|
|
|
|
entry->SetFileSize(*aSize);
|
|
|
|
}
|
2014-02-18 19:05:07 +04:00
|
|
|
} else {
|
2014-10-28 17:59:53 +03:00
|
|
|
CacheIndexEntryUpdate* updated = index->mPendingUpdates.GetEntry(*aHash);
|
2014-02-18 19:05:07 +04:00
|
|
|
DebugOnly<bool> removed = updated && updated->IsRemoved();
|
|
|
|
|
|
|
|
MOZ_ASSERT(updated || !removed);
|
|
|
|
MOZ_ASSERT(updated || entry);
|
|
|
|
|
|
|
|
if (!updated) {
|
2014-10-28 17:59:53 +03:00
|
|
|
if (!entry) {
|
|
|
|
LOG(
|
|
|
|
("CacheIndex::UpdateEntry() - Entry was found neither in mIndex "
|
|
|
|
"nor in mPendingUpdates!"));
|
|
|
|
NS_WARNING(
|
|
|
|
("CacheIndex::UpdateEntry() - Entry was found neither in "
|
|
|
|
"mIndex nor in mPendingUpdates!"));
|
2017-04-15 11:49:00 +03:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
2014-02-18 19:05:07 +04:00
|
|
|
}
|
2014-10-28 17:59:53 +03:00
|
|
|
|
|
|
|
// make a copy of a read-only entry
|
|
|
|
updated = index->mPendingUpdates.PutEntry(*aHash);
|
|
|
|
*updated = *entry;
|
2014-02-18 19:05:07 +04:00
|
|
|
}
|
|
|
|
|
2014-10-28 17:59:53 +03:00
|
|
|
MOZ_ASSERT(updated->IsFresh());
|
|
|
|
MOZ_ASSERT(updated->IsInitialized());
|
|
|
|
updated->MarkDirty();
|
2014-02-18 19:05:07 +04:00
|
|
|
|
2014-10-28 17:59:53 +03:00
|
|
|
if (aFrecency) {
|
|
|
|
updated->SetFrecency(*aFrecency);
|
|
|
|
}
|
2014-02-18 19:05:07 +04:00
|
|
|
|
2017-03-28 06:40:21 +03:00
|
|
|
if (aHasAltData) {
|
|
|
|
updated->SetHasAltData(*aHasAltData);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aOnStartTime) {
|
|
|
|
updated->SetOnStartTime(*aOnStartTime);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aOnStopTime) {
|
|
|
|
updated->SetOnStopTime(*aOnStopTime);
|
|
|
|
}
|
|
|
|
|
2019-03-27 17:32:12 +03:00
|
|
|
if (aContentType) {
|
|
|
|
updated->SetContentType(*aContentType);
|
|
|
|
}
|
|
|
|
|
2014-10-28 17:59:53 +03:00
|
|
|
if (aSize) {
|
|
|
|
updated->SetFileSize(*aSize);
|
|
|
|
}
|
2014-02-18 19:05:07 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
index->WriteIndexToDiskIfNeeded();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-03-07 15:22:59 +04:00
|
|
|
// static
|
|
|
|
nsresult CacheIndex::RemoveAll() {
|
|
|
|
LOG(("CacheIndex::RemoveAll()"));
|
|
|
|
|
|
|
|
MOZ_ASSERT(CacheFileIOManager::IsOnIOThread());
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> file;
|
|
|
|
|
|
|
|
{
|
2015-11-12 13:07:48 +03:00
|
|
|
StaticMutexAutoLock lock(sLock);
|
|
|
|
|
|
|
|
RefPtr<CacheIndex> index = gInstance;
|
|
|
|
|
|
|
|
if (!index) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
2014-03-07 15:22:59 +04:00
|
|
|
|
|
|
|
MOZ_ASSERT(!index->mRemovingAll);
|
|
|
|
|
|
|
|
if (!index->IsIndexUsable()) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
AutoRestore<bool> saveRemovingAll(index->mRemovingAll);
|
|
|
|
index->mRemovingAll = true;
|
|
|
|
|
|
|
|
// Doom index and journal handles but don't null them out since this will be
|
|
|
|
// done in FinishWrite/FinishRead methods.
|
|
|
|
if (index->mIndexHandle) {
|
|
|
|
CacheFileIOManager::DoomFile(index->mIndexHandle, nullptr);
|
|
|
|
} else {
|
|
|
|
// We don't have a handle to index file, so get the file here, but delete
|
|
|
|
// it outside the lock. Ignore the result since this is not fatal.
|
2015-05-16 11:07:10 +03:00
|
|
|
index->GetFile(NS_LITERAL_CSTRING(INDEX_NAME), getter_AddRefs(file));
|
2014-03-07 15:22:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (index->mJournalHandle) {
|
|
|
|
CacheFileIOManager::DoomFile(index->mJournalHandle, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (index->mState) {
|
|
|
|
case WRITING:
|
|
|
|
index->FinishWrite(false);
|
|
|
|
break;
|
|
|
|
case READY:
|
|
|
|
// nothing to do
|
|
|
|
break;
|
|
|
|
case READING:
|
|
|
|
index->FinishRead(false);
|
|
|
|
break;
|
|
|
|
case BUILDING:
|
|
|
|
case UPDATING:
|
|
|
|
index->FinishUpdate(false);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
MOZ_ASSERT(false, "Unexpected state!");
|
|
|
|
}
|
|
|
|
|
|
|
|
// We should end up in READY state
|
|
|
|
MOZ_ASSERT(index->mState == READY);
|
|
|
|
|
|
|
|
// There should not be any handle
|
|
|
|
MOZ_ASSERT(!index->mIndexHandle);
|
|
|
|
MOZ_ASSERT(!index->mJournalHandle);
|
|
|
|
|
|
|
|
index->mIndexOnDiskIsValid = false;
|
|
|
|
index->mIndexNeedsUpdate = false;
|
|
|
|
|
|
|
|
index->mIndexStats.Clear();
|
|
|
|
index->mFrecencyArray.Clear();
|
|
|
|
index->mIndex.Clear();
|
2015-05-23 10:12:43 +03:00
|
|
|
|
|
|
|
for (uint32_t i = 0; i < index->mIterators.Length();) {
|
|
|
|
nsresult rv = index->mIterators[i]->CloseInternal(NS_ERROR_NOT_AVAILABLE);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// CacheIndexIterator::CloseInternal() removes itself from mIterators
|
|
|
|
// iff it returns success.
|
|
|
|
LOG(
|
|
|
|
("CacheIndex::RemoveAll() - Failed to remove iterator %p. "
|
2016-12-16 06:16:31 +03:00
|
|
|
"[rv=0x%08" PRIx32 "]",
|
|
|
|
index->mIterators[i], static_cast<uint32_t>(rv)));
|
2015-05-23 10:12:43 +03:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
2014-03-07 15:22:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (file) {
|
|
|
|
// Ignore the result. The file might not exist and the failure is not fatal.
|
|
|
|
file->Remove(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-02-18 19:05:07 +04:00
|
|
|
// static
|
2017-04-10 14:22:23 +03:00
|
|
|
nsresult CacheIndex::HasEntry(
|
|
|
|
const nsACString& aKey, EntryStatus* _retval,
|
|
|
|
const std::function<void(const CacheIndexEntry*)>& aCB) {
|
2014-02-18 19:05:07 +04:00
|
|
|
LOG(("CacheIndex::HasEntry() [key=%s]", PromiseFlatCString(aKey).get()));
|
|
|
|
|
2015-10-22 13:11:00 +03:00
|
|
|
SHA1Sum sum;
|
|
|
|
SHA1Sum::Hash hash;
|
|
|
|
sum.update(aKey.BeginReading(), aKey.Length());
|
|
|
|
sum.finish(hash);
|
|
|
|
|
2017-04-10 14:22:23 +03:00
|
|
|
return HasEntry(hash, _retval, aCB);
|
2015-10-22 13:11:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2017-04-10 14:22:23 +03:00
|
|
|
nsresult CacheIndex::HasEntry(
|
|
|
|
const SHA1Sum::Hash& hash, EntryStatus* _retval,
|
|
|
|
const std::function<void(const CacheIndexEntry*)>& aCB) {
|
2015-11-12 13:07:48 +03:00
|
|
|
StaticMutexAutoLock lock(sLock);
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CacheIndex> index = gInstance;
|
2014-02-18 19:05:07 +04:00
|
|
|
|
|
|
|
if (!index) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!index->IsIndexUsable()) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
2014-10-28 17:59:53 +03:00
|
|
|
const CacheIndexEntry* entry = nullptr;
|
2014-02-18 19:05:07 +04:00
|
|
|
|
|
|
|
switch (index->mState) {
|
|
|
|
case READING:
|
|
|
|
case WRITING:
|
|
|
|
entry = index->mPendingUpdates.GetEntry(hash);
|
2019-12-20 10:16:43 +03:00
|
|
|
[[fallthrough]];
|
2014-02-18 19:05:07 +04:00
|
|
|
case BUILDING:
|
|
|
|
case UPDATING:
|
|
|
|
case READY:
|
|
|
|
if (!entry) {
|
|
|
|
entry = index->mIndex.GetEntry(hash);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case INITIAL:
|
|
|
|
case SHUTDOWN:
|
|
|
|
MOZ_ASSERT(false, "Unexpected state!");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!entry) {
|
|
|
|
if (index->mState == READY || index->mState == WRITING) {
|
|
|
|
*_retval = DOES_NOT_EXIST;
|
|
|
|
} else {
|
|
|
|
*_retval = DO_NOT_KNOW;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (entry->IsRemoved()) {
|
|
|
|
if (entry->IsFresh()) {
|
|
|
|
*_retval = DOES_NOT_EXIST;
|
|
|
|
} else {
|
|
|
|
*_retval = DO_NOT_KNOW;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
*_retval = EXISTS;
|
2017-04-10 14:22:23 +03:00
|
|
|
if (aCB) {
|
|
|
|
aCB(entry);
|
2015-10-22 13:11:00 +03:00
|
|
|
}
|
2014-02-18 19:05:07 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG(("CacheIndex::HasEntry() - result is %u", *_retval));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-02-18 21:26:48 +04:00
|
|
|
// static
|
2015-02-12 13:28:50 +03:00
|
|
|
nsresult CacheIndex::GetEntryForEviction(bool aIgnoreEmptyEntries,
|
|
|
|
SHA1Sum::Hash* aHash, uint32_t* aCnt) {
|
2014-02-18 21:26:48 +04:00
|
|
|
LOG(("CacheIndex::GetEntryForEviction()"));
|
|
|
|
|
2015-11-12 13:07:48 +03:00
|
|
|
MOZ_ASSERT(CacheFileIOManager::IsOnIOThread());
|
|
|
|
|
|
|
|
StaticMutexAutoLock lock(sLock);
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CacheIndex> index = gInstance;
|
2014-02-18 21:26:48 +04:00
|
|
|
|
|
|
|
if (!index) return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
|
|
|
if (!index->IsIndexUsable()) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
2020-03-02 15:17:32 +03:00
|
|
|
if (index->mIndexStats.Size() == 0) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t mediaUsage =
|
|
|
|
round(static_cast<double>(index->mIndexStats.SizeByType(
|
|
|
|
nsICacheEntry::CONTENT_TYPE_MEDIA)) *
|
|
|
|
100.0 / static_cast<double>(index->mIndexStats.Size()));
|
|
|
|
int32_t mediaUsageLimit =
|
|
|
|
StaticPrefs::browser_cache_disk_content_type_media_limit();
|
|
|
|
bool evictMedia = false;
|
|
|
|
if (mediaUsage > mediaUsageLimit) {
|
|
|
|
LOG(
|
|
|
|
("CacheIndex::GetEntryForEviction() - media content type is over the "
|
|
|
|
"limit [mediaUsage=%d, mediaUsageLimit=%d]",
|
|
|
|
mediaUsage, mediaUsageLimit));
|
|
|
|
evictMedia = true;
|
|
|
|
}
|
|
|
|
|
2014-07-21 22:34:17 +04:00
|
|
|
SHA1Sum::Hash hash;
|
2016-10-06 12:23:52 +03:00
|
|
|
CacheIndexRecord* foundRecord = nullptr;
|
|
|
|
uint32_t skipped = 0;
|
2014-07-21 22:34:17 +04:00
|
|
|
|
2015-10-22 13:11:00 +03:00
|
|
|
// find first non-forced valid and unpinned entry with the lowest frecency
|
2016-10-06 12:23:52 +03:00
|
|
|
index->mFrecencyArray.SortIfNeeded();
|
|
|
|
|
|
|
|
for (auto iter = index->mFrecencyArray.Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
CacheIndexRecord* rec = iter.Get();
|
2016-02-17 13:47:47 +03:00
|
|
|
|
2016-10-06 12:23:52 +03:00
|
|
|
memcpy(&hash, rec->mHash, sizeof(SHA1Sum::Hash));
|
|
|
|
|
|
|
|
++skipped;
|
2014-07-21 22:34:17 +04:00
|
|
|
|
2020-03-02 15:17:32 +03:00
|
|
|
if (evictMedia && CacheIndexEntry::GetContentType(rec) !=
|
|
|
|
nsICacheEntry::CONTENT_TYPE_MEDIA) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-03-03 09:58:00 +03:00
|
|
|
if (IsForcedValidEntry(&hash)) {
|
|
|
|
continue;
|
2014-07-21 22:34:17 +04:00
|
|
|
}
|
|
|
|
|
2016-10-06 12:23:52 +03:00
|
|
|
if (CacheIndexEntry::IsPinned(rec)) {
|
2015-10-22 13:11:00 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-02-11 19:20:08 +03:00
|
|
|
if (aIgnoreEmptyEntries && !CacheIndexEntry::GetFileSize(*rec)) {
|
2015-03-03 09:58:00 +03:00
|
|
|
continue;
|
|
|
|
}
|
2014-07-21 22:34:17 +04:00
|
|
|
|
2016-10-06 12:23:52 +03:00
|
|
|
--skipped;
|
|
|
|
foundRecord = rec;
|
2015-03-03 09:58:00 +03:00
|
|
|
break;
|
2014-02-18 21:26:48 +04:00
|
|
|
}
|
2015-02-12 13:28:50 +03:00
|
|
|
|
2015-03-03 09:58:00 +03:00
|
|
|
if (!foundRecord) return NS_ERROR_NOT_AVAILABLE;
|
2014-07-21 22:34:17 +04:00
|
|
|
|
2016-10-06 12:23:52 +03:00
|
|
|
*aCnt = skipped;
|
2014-07-21 22:34:17 +04:00
|
|
|
|
2015-03-03 09:58:00 +03:00
|
|
|
LOG(
|
2020-03-02 15:17:32 +03:00
|
|
|
("CacheIndex::GetEntryForEviction() - returning entry "
|
|
|
|
"[hash=%08x%08x%08x%08x%08x, cnt=%u, frecency=%u, contentType=%u]",
|
|
|
|
LOGSHA1(&hash), *aCnt, foundRecord->mFrecency,
|
|
|
|
CacheIndexEntry::GetContentType(foundRecord)));
|
2014-02-18 21:26:48 +04:00
|
|
|
|
2014-07-21 22:34:17 +04:00
|
|
|
memcpy(aHash, &hash, sizeof(SHA1Sum::Hash));
|
|
|
|
|
2014-02-18 21:26:48 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-07-21 22:34:17 +04:00
|
|
|
// static
|
|
|
|
bool CacheIndex::IsForcedValidEntry(const SHA1Sum::Hash* aHash) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CacheFileHandle> handle;
|
2014-07-21 22:34:17 +04:00
|
|
|
|
|
|
|
CacheFileIOManager::gInstance->mHandles.GetHandle(aHash,
|
2015-02-24 19:49:46 +03:00
|
|
|
getter_AddRefs(handle));
|
2014-07-21 22:34:17 +04:00
|
|
|
|
|
|
|
if (!handle) return false;
|
|
|
|
|
|
|
|
nsCString hashKey = handle->Key();
|
|
|
|
return CacheStorageService::Self()->IsForcedValidEntry(hashKey);
|
|
|
|
}
|
|
|
|
|
2014-02-18 21:26:48 +04:00
|
|
|
// static
|
|
|
|
nsresult CacheIndex::GetCacheSize(uint32_t* _retval) {
|
|
|
|
LOG(("CacheIndex::GetCacheSize()"));
|
|
|
|
|
2015-11-12 13:07:48 +03:00
|
|
|
StaticMutexAutoLock lock(sLock);
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CacheIndex> index = gInstance;
|
2014-02-18 21:26:48 +04:00
|
|
|
|
|
|
|
if (!index) return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
|
|
|
if (!index->IsIndexUsable()) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
*_retval = index->mIndexStats.Size();
|
|
|
|
LOG(("CacheIndex::GetCacheSize() - returning %u", *_retval));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-02-23 14:26:06 +03:00
|
|
|
// static
|
|
|
|
nsresult CacheIndex::GetEntryFileCount(uint32_t* _retval) {
|
|
|
|
LOG(("CacheIndex::GetEntryFileCount()"));
|
|
|
|
|
2015-11-12 13:07:48 +03:00
|
|
|
StaticMutexAutoLock lock(sLock);
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CacheIndex> index = gInstance;
|
2015-02-23 14:26:06 +03:00
|
|
|
|
|
|
|
if (!index) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!index->IsIndexUsable()) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
*_retval = index->mIndexStats.ActiveEntriesCount();
|
|
|
|
LOG(("CacheIndex::GetEntryFileCount() - returning %u", *_retval));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-05-01 15:28:12 +04:00
|
|
|
// static
|
|
|
|
nsresult CacheIndex::GetCacheStats(nsILoadContextInfo* aInfo, uint32_t* aSize,
|
|
|
|
uint32_t* aCount) {
|
|
|
|
LOG(("CacheIndex::GetCacheStats() [info=%p]", aInfo));
|
|
|
|
|
2015-11-12 13:07:48 +03:00
|
|
|
StaticMutexAutoLock lock(sLock);
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CacheIndex> index = gInstance;
|
2014-05-01 15:28:12 +04:00
|
|
|
|
|
|
|
if (!index) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!index->IsIndexUsable()) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aSize = 0;
|
|
|
|
*aCount = 0;
|
|
|
|
|
2016-10-06 12:23:52 +03:00
|
|
|
for (auto iter = index->mFrecencyArray.Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
CacheIndexRecord* record = iter.Get();
|
2017-02-03 06:15:00 +03:00
|
|
|
if (aInfo && !CacheIndexEntry::RecordMatchesLoadContextInfo(record, aInfo))
|
2014-05-01 15:28:12 +04:00
|
|
|
continue;
|
|
|
|
|
2020-02-11 19:20:08 +03:00
|
|
|
*aSize += CacheIndexEntry::GetFileSize(*record);
|
2014-05-01 15:28:12 +04:00
|
|
|
++*aCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-04-06 22:44:40 +04:00
|
|
|
// static
|
|
|
|
nsresult CacheIndex::AsyncGetDiskConsumption(
|
|
|
|
nsICacheStorageConsumptionObserver* aObserver) {
|
|
|
|
LOG(("CacheIndex::AsyncGetDiskConsumption()"));
|
|
|
|
|
2015-11-12 13:07:48 +03:00
|
|
|
StaticMutexAutoLock lock(sLock);
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CacheIndex> index = gInstance;
|
2014-04-06 22:44:40 +04:00
|
|
|
|
|
|
|
if (!index) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!index->IsIndexUsable()) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<DiskConsumptionObserver> observer =
|
2014-04-06 22:44:40 +04:00
|
|
|
DiskConsumptionObserver::Init(aObserver);
|
|
|
|
|
|
|
|
NS_ENSURE_ARG(observer);
|
|
|
|
|
2016-04-05 06:59:00 +03:00
|
|
|
if ((index->mState == READY || index->mState == WRITING) &&
|
|
|
|
!index->mAsyncGetDiskConsumptionBlocked) {
|
2014-04-06 22:44:40 +04:00
|
|
|
LOG(("CacheIndex::AsyncGetDiskConsumption - calling immediately"));
|
|
|
|
// Safe to call the callback under the lock,
|
|
|
|
// we always post to the main thread.
|
|
|
|
observer->OnDiskConsumption(index->mIndexStats.Size() << 10);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG(("CacheIndex::AsyncGetDiskConsumption - remembering callback"));
|
|
|
|
// Will be called when the index get to the READY state.
|
|
|
|
index->mDiskConsumptionObservers.AppendElement(observer);
|
|
|
|
|
2016-05-30 05:48:00 +03:00
|
|
|
// Move forward with index re/building if it is pending
|
|
|
|
RefPtr<CacheIOThread> ioThread = CacheFileIOManager::IOThread();
|
|
|
|
if (ioThread) {
|
2017-06-12 22:34:10 +03:00
|
|
|
ioThread->Dispatch(
|
|
|
|
NS_NewRunnableFunction("net::CacheIndex::AsyncGetDiskConsumption",
|
|
|
|
[]() -> void {
|
|
|
|
StaticMutexAutoLock lock(sLock);
|
2016-05-30 05:48:00 +03:00
|
|
|
|
2017-06-12 22:34:10 +03:00
|
|
|
RefPtr<CacheIndex> index = gInstance;
|
|
|
|
if (index && index->mUpdateTimer) {
|
|
|
|
index->mUpdateTimer->Cancel();
|
|
|
|
index->DelayedUpdateLocked();
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
CacheIOThread::INDEX);
|
2016-05-30 05:48:00 +03:00
|
|
|
}
|
|
|
|
|
2014-04-06 22:44:40 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-04-10 14:47:20 +04:00
|
|
|
// static
|
|
|
|
nsresult CacheIndex::GetIterator(nsILoadContextInfo* aInfo, bool aAddNew,
|
|
|
|
CacheIndexIterator** _retval) {
|
|
|
|
LOG(("CacheIndex::GetIterator() [info=%p, addNew=%d]", aInfo, aAddNew));
|
|
|
|
|
2015-11-12 13:07:48 +03:00
|
|
|
StaticMutexAutoLock lock(sLock);
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CacheIndex> index = gInstance;
|
2014-04-10 14:47:20 +04:00
|
|
|
|
|
|
|
if (!index) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!index->IsIndexUsable()) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
2016-10-06 12:23:52 +03:00
|
|
|
RefPtr<CacheIndexIterator> idxIter;
|
2014-04-10 14:47:20 +04:00
|
|
|
if (aInfo) {
|
2016-10-06 12:23:52 +03:00
|
|
|
idxIter = new CacheIndexContextIterator(index, aAddNew, aInfo);
|
2014-04-10 14:47:20 +04:00
|
|
|
} else {
|
2016-10-06 12:23:52 +03:00
|
|
|
idxIter = new CacheIndexIterator(index, aAddNew);
|
2014-04-10 14:47:20 +04:00
|
|
|
}
|
|
|
|
|
2016-10-06 12:23:52 +03:00
|
|
|
index->mFrecencyArray.SortIfNeeded();
|
2016-02-17 13:47:47 +03:00
|
|
|
|
2016-10-06 12:23:52 +03:00
|
|
|
for (auto iter = index->mFrecencyArray.Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
idxIter->AddRecord(iter.Get());
|
|
|
|
}
|
2014-04-10 14:47:20 +04:00
|
|
|
|
2016-10-06 12:23:52 +03:00
|
|
|
index->mIterators.AppendElement(idxIter);
|
|
|
|
idxIter.swap(*_retval);
|
2014-04-10 14:47:20 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
nsresult CacheIndex::IsUpToDate(bool* _retval) {
|
|
|
|
LOG(("CacheIndex::IsUpToDate()"));
|
|
|
|
|
2015-11-12 13:07:48 +03:00
|
|
|
StaticMutexAutoLock lock(sLock);
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CacheIndex> index = gInstance;
|
2014-04-10 14:47:20 +04:00
|
|
|
|
|
|
|
if (!index) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!index->IsIndexUsable()) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
*_retval = (index->mState == READY || index->mState == WRITING) &&
|
|
|
|
!index->mIndexNeedsUpdate && !index->mShuttingDown;
|
|
|
|
|
2016-12-16 06:16:31 +03:00
|
|
|
LOG(("CacheIndex::IsUpToDate() - returning %d", *_retval));
|
2014-04-10 14:47:20 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-02-18 19:05:07 +04:00
|
|
|
bool CacheIndex::IsIndexUsable() {
|
|
|
|
MOZ_ASSERT(mState != INITIAL);
|
|
|
|
|
|
|
|
switch (mState) {
|
|
|
|
case INITIAL:
|
|
|
|
case SHUTDOWN:
|
|
|
|
return false;
|
|
|
|
|
|
|
|
case READING:
|
|
|
|
case WRITING:
|
|
|
|
case BUILDING:
|
|
|
|
case UPDATING:
|
|
|
|
case READY:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
bool CacheIndex::IsCollision(CacheIndexEntry* aEntry,
|
2016-11-08 23:11:30 +03:00
|
|
|
OriginAttrsHash aOriginAttrsHash,
|
|
|
|
bool aAnonymous) {
|
2014-02-18 19:05:07 +04:00
|
|
|
if (!aEntry->IsInitialized()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-11-08 23:11:30 +03:00
|
|
|
if (aEntry->Anonymous() != aAnonymous ||
|
|
|
|
aEntry->OriginAttrsHash() != aOriginAttrsHash) {
|
2014-02-18 19:05:07 +04:00
|
|
|
LOG(
|
|
|
|
("CacheIndex::IsCollision() - Collision detected for entry hash=%08x"
|
2016-12-16 06:16:31 +03:00
|
|
|
"%08x%08x%08x%08x, expected values: originAttrsHash=%" PRIu64 ", "
|
|
|
|
"anonymous=%d; actual values: originAttrsHash=%" PRIu64
|
|
|
|
", anonymous=%d]",
|
2016-11-08 23:11:30 +03:00
|
|
|
LOGSHA1(aEntry->Hash()), aOriginAttrsHash, aAnonymous,
|
|
|
|
aEntry->OriginAttrsHash(), aEntry->Anonymous()));
|
2014-02-18 19:05:07 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2019-03-27 17:32:12 +03:00
|
|
|
bool CacheIndex::HasEntryChanged(
|
|
|
|
CacheIndexEntry* aEntry, const uint32_t* aFrecency, const bool* aHasAltData,
|
|
|
|
const uint16_t* aOnStartTime, const uint16_t* aOnStopTime,
|
2020-01-14 16:53:01 +03:00
|
|
|
const uint8_t* aContentType, const uint32_t* aSize) {
|
2014-02-18 19:05:07 +04:00
|
|
|
if (aFrecency && *aFrecency != aEntry->GetFrecency()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-03-06 13:21:59 +03:00
|
|
|
if (aHasAltData && *aHasAltData != aEntry->GetHasAltData()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-03-28 06:40:21 +03:00
|
|
|
if (aOnStartTime && *aOnStartTime != aEntry->GetOnStartTime()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aOnStopTime && *aOnStopTime != aEntry->GetOnStopTime()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-03-27 17:32:12 +03:00
|
|
|
if (aContentType && *aContentType != aEntry->GetContentType()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-02-18 19:05:07 +04:00
|
|
|
if (aSize &&
|
|
|
|
(*aSize & CacheIndexEntry::kFileSizeMask) != aEntry->GetFileSize()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CacheIndex::ProcessPendingOperations() {
|
|
|
|
LOG(("CacheIndex::ProcessPendingOperations()"));
|
|
|
|
|
2015-11-12 13:07:48 +03:00
|
|
|
sLock.AssertCurrentThreadOwns();
|
2014-02-18 19:05:07 +04:00
|
|
|
|
2015-07-29 07:14:39 +03:00
|
|
|
for (auto iter = mPendingUpdates.Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
CacheIndexEntryUpdate* update = iter.Get();
|
2014-02-18 19:05:07 +04:00
|
|
|
|
2015-07-29 07:14:39 +03:00
|
|
|
LOG(("CacheIndex::ProcessPendingOperations() [hash=%08x%08x%08x%08x%08x]",
|
|
|
|
LOGSHA1(update->Hash())));
|
2014-02-18 19:05:07 +04:00
|
|
|
|
2015-07-29 07:14:39 +03:00
|
|
|
MOZ_ASSERT(update->IsFresh());
|
2014-02-18 19:05:07 +04:00
|
|
|
|
2015-07-29 07:14:39 +03:00
|
|
|
CacheIndexEntry* entry = mIndex.GetEntry(*update->Hash());
|
2014-02-18 19:05:07 +04:00
|
|
|
|
2015-07-29 07:14:39 +03:00
|
|
|
{
|
|
|
|
CacheIndexEntryAutoManage emng(update->Hash(), this);
|
|
|
|
emng.DoNotSearchInUpdates();
|
2015-08-07 02:30:47 +03:00
|
|
|
|
2015-07-29 07:14:39 +03:00
|
|
|
if (update->IsRemoved()) {
|
|
|
|
if (entry) {
|
|
|
|
if (entry->IsRemoved()) {
|
|
|
|
MOZ_ASSERT(entry->IsFresh());
|
|
|
|
MOZ_ASSERT(entry->IsDirty());
|
|
|
|
} else if (!entry->IsDirty() && entry->IsFileEmpty()) {
|
|
|
|
// Entries with empty file are not stored in index on disk. Just
|
|
|
|
// remove the entry, but only in case the entry is not dirty, i.e.
|
|
|
|
// the entry file was empty when we wrote the index.
|
2017-06-14 18:27:25 +03:00
|
|
|
mIndex.RemoveEntry(entry);
|
2015-07-29 07:14:39 +03:00
|
|
|
entry = nullptr;
|
|
|
|
} else {
|
|
|
|
entry->MarkRemoved();
|
|
|
|
entry->MarkDirty();
|
|
|
|
entry->MarkFresh();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (entry) {
|
|
|
|
// Some information in mIndex can be newer than in mPendingUpdates (see
|
|
|
|
// bug 1074832). This will copy just those values that were really
|
|
|
|
// updated.
|
|
|
|
update->ApplyUpdate(entry);
|
2014-02-18 19:05:07 +04:00
|
|
|
} else {
|
2015-07-29 07:14:39 +03:00
|
|
|
// There is no entry in mIndex, copy all information from
|
|
|
|
// mPendingUpdates to mIndex.
|
|
|
|
entry = mIndex.PutEntry(*update->Hash());
|
|
|
|
*entry = *update;
|
2014-02-18 19:05:07 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-29 07:14:39 +03:00
|
|
|
iter.Remove();
|
2014-02-18 19:05:07 +04:00
|
|
|
}
|
|
|
|
|
2015-07-29 07:14:39 +03:00
|
|
|
MOZ_ASSERT(mPendingUpdates.Count() == 0);
|
2014-02-18 19:05:07 +04:00
|
|
|
|
2015-07-29 07:14:39 +03:00
|
|
|
EnsureCorrectStats();
|
2014-02-18 19:05:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CacheIndex::WriteIndexToDiskIfNeeded() {
|
2016-05-11 11:48:28 +03:00
|
|
|
if (mState != READY || mShuttingDown || mRWPending) {
|
2014-02-18 19:05:07 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mLastDumpTime.IsNull() &&
|
|
|
|
(TimeStamp::NowLoRes() - mLastDumpTime).ToMilliseconds() <
|
|
|
|
kMinDumpInterval) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mIndexStats.Dirty() < kMinUnwrittenChanges) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
WriteIndexToDisk();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CacheIndex::WriteIndexToDisk() {
|
|
|
|
LOG(("CacheIndex::WriteIndexToDisk()"));
|
|
|
|
mIndexStats.Log();
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
2015-11-12 13:07:48 +03:00
|
|
|
sLock.AssertCurrentThreadOwns();
|
2014-02-18 19:05:07 +04:00
|
|
|
MOZ_ASSERT(mState == READY);
|
|
|
|
MOZ_ASSERT(!mRWBuf);
|
|
|
|
MOZ_ASSERT(!mRWHash);
|
2016-05-11 11:48:28 +03:00
|
|
|
MOZ_ASSERT(!mRWPending);
|
2014-02-18 19:05:07 +04:00
|
|
|
|
|
|
|
ChangeState(WRITING);
|
|
|
|
|
|
|
|
mProcessEntries = mIndexStats.ActiveEntriesCount();
|
|
|
|
|
2014-03-07 15:22:59 +04:00
|
|
|
mIndexFileOpener = new FileOpenHelper(this);
|
2015-05-16 11:07:10 +03:00
|
|
|
rv = CacheFileIOManager::OpenFile(
|
|
|
|
NS_LITERAL_CSTRING(TEMP_INDEX_NAME),
|
2014-02-18 19:05:07 +04:00
|
|
|
CacheFileIOManager::SPECIAL_FILE | CacheFileIOManager::CREATE,
|
2014-03-07 15:22:59 +04:00
|
|
|
mIndexFileOpener);
|
2014-02-18 19:05:07 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
2016-12-16 06:16:31 +03:00
|
|
|
LOG(("CacheIndex::WriteIndexToDisk() - Can't open file [rv=0x%08" PRIx32
|
|
|
|
"]",
|
|
|
|
static_cast<uint32_t>(rv)));
|
2014-02-18 19:05:07 +04:00
|
|
|
FinishWrite(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write index header to a buffer, it will be written to disk together with
|
|
|
|
// records in WriteRecords() once we open the file successfully.
|
|
|
|
AllocBuffer();
|
|
|
|
mRWHash = new CacheHash();
|
|
|
|
|
2016-12-02 18:21:35 +03:00
|
|
|
mRWBufPos = 0;
|
|
|
|
// index version
|
|
|
|
NetworkEndian::writeUint32(mRWBuf + mRWBufPos, kIndexVersion);
|
|
|
|
mRWBufPos += sizeof(uint32_t);
|
|
|
|
// timestamp
|
|
|
|
NetworkEndian::writeUint32(mRWBuf + mRWBufPos,
|
2014-02-18 19:05:07 +04:00
|
|
|
static_cast<uint32_t>(PR_Now() / PR_USEC_PER_SEC));
|
2016-12-02 18:21:35 +03:00
|
|
|
mRWBufPos += sizeof(uint32_t);
|
|
|
|
// dirty flag
|
|
|
|
NetworkEndian::writeUint32(mRWBuf + mRWBufPos, 1);
|
|
|
|
mRWBufPos += sizeof(uint32_t);
|
2014-02-18 19:05:07 +04:00
|
|
|
|
|
|
|
mSkipEntries = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CacheIndex::WriteRecords() {
|
|
|
|
LOG(("CacheIndex::WriteRecords()"));
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
2015-11-12 13:07:48 +03:00
|
|
|
sLock.AssertCurrentThreadOwns();
|
2014-02-18 19:05:07 +04:00
|
|
|
MOZ_ASSERT(mState == WRITING);
|
2016-05-11 11:48:28 +03:00
|
|
|
MOZ_ASSERT(!mRWPending);
|
2014-02-18 19:05:07 +04:00
|
|
|
|
|
|
|
int64_t fileOffset;
|
|
|
|
|
|
|
|
if (mSkipEntries) {
|
|
|
|
MOZ_ASSERT(mRWBufPos == 0);
|
|
|
|
fileOffset = sizeof(CacheIndexHeader);
|
|
|
|
fileOffset += sizeof(CacheIndexRecord) * mSkipEntries;
|
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(mRWBufPos == sizeof(CacheIndexHeader));
|
|
|
|
fileOffset = 0;
|
|
|
|
}
|
|
|
|
uint32_t hashOffset = mRWBufPos;
|
|
|
|
|
2015-07-27 06:15:36 +03:00
|
|
|
char* buf = mRWBuf + mRWBufPos;
|
|
|
|
uint32_t skip = mSkipEntries;
|
|
|
|
uint32_t processMax = (mRWBufSize - mRWBufPos) / sizeof(CacheIndexRecord);
|
|
|
|
MOZ_ASSERT(processMax != 0 ||
|
|
|
|
mProcessEntries ==
|
|
|
|
0); // TODO make sure we can write an empty index
|
|
|
|
uint32_t processed = 0;
|
2014-02-18 19:05:07 +04:00
|
|
|
#ifdef DEBUG
|
2015-07-27 06:15:36 +03:00
|
|
|
bool hasMore = false;
|
2014-02-18 19:05:07 +04:00
|
|
|
#endif
|
2015-07-27 06:15:36 +03:00
|
|
|
for (auto iter = mIndex.Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
CacheIndexEntry* entry = iter.Get();
|
|
|
|
if (entry->IsRemoved() || !entry->IsInitialized() || entry->IsFileEmpty()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (skip) {
|
|
|
|
skip--;
|
|
|
|
continue;
|
|
|
|
}
|
2015-07-27 06:15:36 +03:00
|
|
|
|
2015-07-27 06:15:36 +03:00
|
|
|
if (processed == processMax) {
|
|
|
|
#ifdef DEBUG
|
|
|
|
hasMore = true;
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
entry->WriteToBuf(buf);
|
|
|
|
buf += sizeof(CacheIndexRecord);
|
|
|
|
processed++;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(mRWBufPos != static_cast<uint32_t>(buf - mRWBuf) ||
|
2014-02-18 19:05:07 +04:00
|
|
|
mProcessEntries == 0);
|
2015-07-27 06:15:36 +03:00
|
|
|
mRWBufPos = buf - mRWBuf;
|
|
|
|
mSkipEntries += processed;
|
2014-02-18 19:05:07 +04:00
|
|
|
MOZ_ASSERT(mSkipEntries <= mProcessEntries);
|
|
|
|
|
|
|
|
mRWHash->Update(mRWBuf + hashOffset, mRWBufPos - hashOffset);
|
|
|
|
|
|
|
|
if (mSkipEntries == mProcessEntries) {
|
2015-07-27 06:15:36 +03:00
|
|
|
MOZ_ASSERT(!hasMore);
|
2014-02-18 19:05:07 +04:00
|
|
|
|
|
|
|
// We've processed all records
|
|
|
|
if (mRWBufPos + sizeof(CacheHash::Hash32_t) > mRWBufSize) {
|
|
|
|
// realloc buffer to spare another write cycle
|
|
|
|
mRWBufSize = mRWBufPos + sizeof(CacheHash::Hash32_t);
|
|
|
|
mRWBuf = static_cast<char*>(moz_xrealloc(mRWBuf, mRWBufSize));
|
|
|
|
}
|
|
|
|
|
|
|
|
NetworkEndian::writeUint32(mRWBuf + mRWBufPos, mRWHash->GetHash());
|
|
|
|
mRWBufPos += sizeof(CacheHash::Hash32_t);
|
|
|
|
} else {
|
2015-07-27 06:15:36 +03:00
|
|
|
MOZ_ASSERT(hasMore);
|
2014-02-18 19:05:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
rv = CacheFileIOManager::Write(mIndexHandle, fileOffset, mRWBuf, mRWBufPos,
|
2015-03-27 01:05:34 +03:00
|
|
|
mSkipEntries == mProcessEntries, false, this);
|
2014-02-18 19:05:07 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
LOG(
|
|
|
|
("CacheIndex::WriteRecords() - CacheFileIOManager::Write() failed "
|
2016-12-16 06:16:31 +03:00
|
|
|
"synchronously [rv=0x%08" PRIx32 "]",
|
|
|
|
static_cast<uint32_t>(rv)));
|
2014-02-18 19:05:07 +04:00
|
|
|
FinishWrite(false);
|
2016-05-11 11:48:28 +03:00
|
|
|
} else {
|
|
|
|
mRWPending = true;
|
2014-02-18 19:05:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
mRWBufPos = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CacheIndex::FinishWrite(bool aSucceeded) {
|
|
|
|
LOG(("CacheIndex::FinishWrite() [succeeded=%d]", aSucceeded));
|
|
|
|
|
|
|
|
MOZ_ASSERT((!aSucceeded && mState == SHUTDOWN) || mState == WRITING);
|
|
|
|
|
2015-11-12 13:07:48 +03:00
|
|
|
sLock.AssertCurrentThreadOwns();
|
2014-02-18 19:05:07 +04:00
|
|
|
|
2016-05-11 11:48:28 +03:00
|
|
|
// If there is write operation pending we must be cancelling writing of the
|
|
|
|
// index when shutting down or removing the whole index.
|
|
|
|
MOZ_ASSERT(!mRWPending || (!aSucceeded && (mShuttingDown || mRemovingAll)));
|
|
|
|
|
2014-02-18 19:05:07 +04:00
|
|
|
mIndexHandle = nullptr;
|
|
|
|
mRWHash = nullptr;
|
|
|
|
ReleaseBuffer();
|
|
|
|
|
|
|
|
if (aSucceeded) {
|
2014-03-07 15:22:59 +04:00
|
|
|
// Opening of the file must not be in progress if writing succeeded.
|
|
|
|
MOZ_ASSERT(!mIndexFileOpener);
|
|
|
|
|
2015-07-27 06:35:31 +03:00
|
|
|
for (auto iter = mIndex.Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
CacheIndexEntry* entry = iter.Get();
|
|
|
|
|
|
|
|
bool remove = false;
|
|
|
|
{
|
|
|
|
CacheIndexEntryAutoManage emng(entry->Hash(), this);
|
|
|
|
|
|
|
|
if (entry->IsRemoved()) {
|
|
|
|
emng.DoNotSearchInIndex();
|
|
|
|
remove = true;
|
|
|
|
} else if (entry->IsDirty()) {
|
|
|
|
entry->ClearDirty();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (remove) {
|
|
|
|
iter.Remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-18 19:05:07 +04:00
|
|
|
mIndexOnDiskIsValid = true;
|
2014-03-07 15:22:59 +04:00
|
|
|
} else {
|
|
|
|
if (mIndexFileOpener) {
|
|
|
|
// If opening of the file is still in progress (e.g. WRITE process was
|
|
|
|
// canceled by RemoveAll()) then we need to cancel the opener to make sure
|
|
|
|
// that OnFileOpenedInternal() won't be called.
|
|
|
|
mIndexFileOpener->Cancel();
|
|
|
|
mIndexFileOpener = nullptr;
|
|
|
|
}
|
2014-02-18 19:05:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
ProcessPendingOperations();
|
|
|
|
mIndexStats.Log();
|
|
|
|
|
|
|
|
if (mState == WRITING) {
|
|
|
|
ChangeState(READY);
|
|
|
|
mLastDumpTime = TimeStamp::NowLoRes();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult CacheIndex::GetFile(const nsACString& aName, nsIFile** _retval) {
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> file;
|
|
|
|
rv = mCacheDirectory->Clone(getter_AddRefs(file));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = file->AppendNative(aName);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
file.swap(*_retval);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2020-04-10 11:14:19 +03:00
|
|
|
void CacheIndex::RemoveFile(const nsACString& aName) {
|
2014-02-18 19:05:07 +04:00
|
|
|
MOZ_ASSERT(mState == SHUTDOWN);
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> file;
|
|
|
|
rv = GetFile(aName, getter_AddRefs(file));
|
2020-04-10 11:14:19 +03:00
|
|
|
NS_ENSURE_SUCCESS_VOID(rv);
|
2014-02-18 19:05:07 +04:00
|
|
|
|
2020-04-10 11:14:19 +03:00
|
|
|
rv = file->Remove(false);
|
|
|
|
if (NS_FAILED(rv) && rv != NS_ERROR_FILE_NOT_FOUND) {
|
|
|
|
LOG(
|
|
|
|
("CacheIndex::RemoveFile() - Cannot remove old entry file from disk "
|
|
|
|
"[rv=0x%08" PRIx32 ", name=%s]",
|
|
|
|
static_cast<uint32_t>(rv), PromiseFlatCString(aName).get()));
|
2014-02-18 19:05:07 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-04 03:39:49 +03:00
|
|
|
void CacheIndex::RemoveAllIndexFiles() {
|
|
|
|
LOG(("CacheIndex::RemoveAllIndexFiles()"));
|
2015-05-16 11:07:10 +03:00
|
|
|
RemoveFile(NS_LITERAL_CSTRING(INDEX_NAME));
|
2016-09-04 03:39:49 +03:00
|
|
|
RemoveJournalAndTempFile();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CacheIndex::RemoveJournalAndTempFile() {
|
|
|
|
LOG(("CacheIndex::RemoveJournalAndTempFile()"));
|
2015-05-16 11:07:10 +03:00
|
|
|
RemoveFile(NS_LITERAL_CSTRING(TEMP_INDEX_NAME));
|
|
|
|
RemoveFile(NS_LITERAL_CSTRING(JOURNAL_NAME));
|
2014-02-18 19:05:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
class WriteLogHelper {
|
|
|
|
public:
|
2014-08-05 17:20:24 +04:00
|
|
|
explicit WriteLogHelper(PRFileDesc* aFD)
|
2014-02-18 19:05:07 +04:00
|
|
|
: mFD(aFD), mBufSize(kMaxBufSize), mBufPos(0) {
|
|
|
|
mHash = new CacheHash();
|
|
|
|
mBuf = static_cast<char*>(moz_xmalloc(mBufSize));
|
|
|
|
}
|
|
|
|
|
|
|
|
~WriteLogHelper() { free(mBuf); }
|
|
|
|
|
|
|
|
nsresult AddEntry(CacheIndexEntry* aEntry);
|
|
|
|
nsresult Finish();
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsresult FlushBuffer();
|
|
|
|
|
2016-09-04 03:39:49 +03:00
|
|
|
PRFileDesc* mFD;
|
|
|
|
char* mBuf;
|
|
|
|
uint32_t mBufSize;
|
|
|
|
int32_t mBufPos;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CacheHash> mHash;
|
2014-02-18 19:05:07 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
nsresult WriteLogHelper::AddEntry(CacheIndexEntry* aEntry) {
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
if (mBufPos + sizeof(CacheIndexRecord) > mBufSize) {
|
|
|
|
mHash->Update(mBuf, mBufPos);
|
|
|
|
|
|
|
|
rv = FlushBuffer();
|
2016-09-04 03:39:49 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2014-02-18 19:05:07 +04:00
|
|
|
MOZ_ASSERT(mBufPos + sizeof(CacheIndexRecord) <= mBufSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
aEntry->WriteToBuf(mBuf + mBufPos);
|
|
|
|
mBufPos += sizeof(CacheIndexRecord);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult WriteLogHelper::Finish() {
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
mHash->Update(mBuf, mBufPos);
|
|
|
|
if (mBufPos + sizeof(CacheHash::Hash32_t) > mBufSize) {
|
|
|
|
rv = FlushBuffer();
|
2016-09-04 03:39:49 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2014-02-18 19:05:07 +04:00
|
|
|
MOZ_ASSERT(mBufPos + sizeof(CacheHash::Hash32_t) <= mBufSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
NetworkEndian::writeUint32(mBuf + mBufPos, mHash->GetHash());
|
|
|
|
mBufPos += sizeof(CacheHash::Hash32_t);
|
|
|
|
|
|
|
|
rv = FlushBuffer();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult WriteLogHelper::FlushBuffer() {
|
2016-09-04 03:39:49 +03:00
|
|
|
if (CacheObserver::IsPastShutdownIOLag()) {
|
|
|
|
LOG(("WriteLogHelper::FlushBuffer() - Interrupting writing journal."));
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2014-02-18 19:05:07 +04:00
|
|
|
|
|
|
|
int32_t bytesWritten = PR_Write(mFD, mBuf, mBufPos);
|
|
|
|
|
|
|
|
if (bytesWritten != mBufPos) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
mBufPos = 0;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult CacheIndex::WriteLogToDisk() {
|
|
|
|
LOG(("CacheIndex::WriteLogToDisk()"));
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
MOZ_ASSERT(mPendingUpdates.Count() == 0);
|
|
|
|
MOZ_ASSERT(mState == SHUTDOWN);
|
|
|
|
|
2016-09-04 03:39:49 +03:00
|
|
|
if (CacheObserver::IsPastShutdownIOLag()) {
|
|
|
|
LOG(("CacheIndex::WriteLogToDisk() - Skipping writing journal."));
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2015-05-16 11:07:10 +03:00
|
|
|
RemoveFile(NS_LITERAL_CSTRING(TEMP_INDEX_NAME));
|
2014-02-18 19:05:07 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> indexFile;
|
2015-05-16 11:07:10 +03:00
|
|
|
rv = GetFile(NS_LITERAL_CSTRING(INDEX_NAME), getter_AddRefs(indexFile));
|
2014-02-18 19:05:07 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> logFile;
|
2015-05-16 11:07:10 +03:00
|
|
|
rv = GetFile(NS_LITERAL_CSTRING(JOURNAL_NAME), getter_AddRefs(logFile));
|
2014-02-18 19:05:07 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
mIndexStats.Log();
|
|
|
|
|
|
|
|
PRFileDesc* fd = nullptr;
|
|
|
|
rv = logFile->OpenNSPRFileDesc(PR_RDWR | PR_CREATE_FILE | PR_TRUNCATE, 0600,
|
|
|
|
&fd);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
WriteLogHelper wlh(fd);
|
2015-07-29 07:14:39 +03:00
|
|
|
for (auto iter = mIndex.Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
CacheIndexEntry* entry = iter.Get();
|
|
|
|
if (entry->IsRemoved() || entry->IsDirty()) {
|
2016-09-04 03:39:49 +03:00
|
|
|
rv = wlh.AddEntry(entry);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
2015-07-29 07:14:39 +03:00
|
|
|
}
|
|
|
|
}
|
2014-02-18 19:05:07 +04:00
|
|
|
|
|
|
|
rv = wlh.Finish();
|
|
|
|
PR_Close(fd);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = indexFile->OpenNSPRFileDesc(PR_RDWR, 0600, &fd);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2016-12-02 18:21:35 +03:00
|
|
|
// Seek to dirty flag in the index header and clear it.
|
|
|
|
static_assert(2 * sizeof(uint32_t) == offsetof(CacheIndexHeader, mIsDirty),
|
|
|
|
"Unexpected offset of CacheIndexHeader::mIsDirty");
|
|
|
|
int64_t offset = PR_Seek64(fd, 2 * sizeof(uint32_t), PR_SEEK_SET);
|
2014-02-18 19:05:07 +04:00
|
|
|
if (offset == -1) {
|
|
|
|
PR_Close(fd);
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2016-12-02 18:21:35 +03:00
|
|
|
uint32_t isDirty = 0;
|
|
|
|
int32_t bytesWritten = PR_Write(fd, &isDirty, sizeof(isDirty));
|
2014-02-18 19:05:07 +04:00
|
|
|
PR_Close(fd);
|
2016-12-02 18:21:35 +03:00
|
|
|
if (bytesWritten != sizeof(isDirty)) {
|
2014-02-18 19:05:07 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CacheIndex::ReadIndexFromDisk() {
|
|
|
|
LOG(("CacheIndex::ReadIndexFromDisk()"));
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
2015-11-12 13:07:48 +03:00
|
|
|
sLock.AssertCurrentThreadOwns();
|
2014-03-07 15:22:59 +04:00
|
|
|
MOZ_ASSERT(mState == INITIAL);
|
2014-02-18 19:05:07 +04:00
|
|
|
|
2014-03-07 15:22:59 +04:00
|
|
|
ChangeState(READING);
|
2014-02-18 19:05:07 +04:00
|
|
|
|
2014-03-07 15:22:59 +04:00
|
|
|
mIndexFileOpener = new FileOpenHelper(this);
|
2015-05-16 11:07:10 +03:00
|
|
|
rv = CacheFileIOManager::OpenFile(
|
|
|
|
NS_LITERAL_CSTRING(INDEX_NAME),
|
2014-02-18 19:05:07 +04:00
|
|
|
CacheFileIOManager::SPECIAL_FILE | CacheFileIOManager::OPEN,
|
2014-03-07 15:22:59 +04:00
|
|
|
mIndexFileOpener);
|
2014-02-18 19:05:07 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
LOG(
|
|
|
|
("CacheIndex::ReadIndexFromDisk() - CacheFileIOManager::OpenFile() "
|
2016-12-16 06:16:31 +03:00
|
|
|
"failed [rv=0x%08" PRIx32 ", file=%s]",
|
|
|
|
static_cast<uint32_t>(rv), INDEX_NAME));
|
2014-03-07 15:22:59 +04:00
|
|
|
FinishRead(false);
|
|
|
|
return;
|
2014-02-18 19:05:07 +04:00
|
|
|
}
|
|
|
|
|
2014-03-07 15:22:59 +04:00
|
|
|
mJournalFileOpener = new FileOpenHelper(this);
|
2015-05-16 11:07:10 +03:00
|
|
|
rv = CacheFileIOManager::OpenFile(
|
|
|
|
NS_LITERAL_CSTRING(JOURNAL_NAME),
|
2014-02-18 19:05:07 +04:00
|
|
|
CacheFileIOManager::SPECIAL_FILE | CacheFileIOManager::OPEN,
|
2014-03-07 15:22:59 +04:00
|
|
|
mJournalFileOpener);
|
2014-02-18 19:05:07 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
LOG(
|
|
|
|
("CacheIndex::ReadIndexFromDisk() - CacheFileIOManager::OpenFile() "
|
2016-12-16 06:16:31 +03:00
|
|
|
"failed [rv=0x%08" PRIx32 ", file=%s]",
|
|
|
|
static_cast<uint32_t>(rv), JOURNAL_NAME));
|
2014-03-07 15:22:59 +04:00
|
|
|
FinishRead(false);
|
2014-02-18 19:05:07 +04:00
|
|
|
}
|
|
|
|
|
2014-03-07 15:22:59 +04:00
|
|
|
mTmpFileOpener = new FileOpenHelper(this);
|
2015-05-16 11:07:10 +03:00
|
|
|
rv = CacheFileIOManager::OpenFile(
|
|
|
|
NS_LITERAL_CSTRING(TEMP_INDEX_NAME),
|
2014-02-18 19:05:07 +04:00
|
|
|
CacheFileIOManager::SPECIAL_FILE | CacheFileIOManager::OPEN,
|
2014-03-07 15:22:59 +04:00
|
|
|
mTmpFileOpener);
|
2014-02-18 19:05:07 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
LOG(
|
|
|
|
("CacheIndex::ReadIndexFromDisk() - CacheFileIOManager::OpenFile() "
|
2016-12-16 06:16:31 +03:00
|
|
|
"failed [rv=0x%08" PRIx32 ", file=%s]",
|
|
|
|
static_cast<uint32_t>(rv), TEMP_INDEX_NAME));
|
2014-02-18 19:05:07 +04:00
|
|
|
FinishRead(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CacheIndex::StartReadingIndex() {
|
|
|
|
LOG(("CacheIndex::StartReadingIndex()"));
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
2015-11-12 13:07:48 +03:00
|
|
|
sLock.AssertCurrentThreadOwns();
|
2014-02-18 19:05:07 +04:00
|
|
|
|
|
|
|
MOZ_ASSERT(mIndexHandle);
|
|
|
|
MOZ_ASSERT(mState == READING);
|
|
|
|
MOZ_ASSERT(!mIndexOnDiskIsValid);
|
|
|
|
MOZ_ASSERT(!mDontMarkIndexClean);
|
|
|
|
MOZ_ASSERT(!mJournalReadSuccessfully);
|
|
|
|
MOZ_ASSERT(mIndexHandle->FileSize() >= 0);
|
2016-05-11 11:48:28 +03:00
|
|
|
MOZ_ASSERT(!mRWPending);
|
2014-02-18 19:05:07 +04:00
|
|
|
|
|
|
|
int64_t entriesSize = mIndexHandle->FileSize() - sizeof(CacheIndexHeader) -
|
|
|
|
sizeof(CacheHash::Hash32_t);
|
|
|
|
|
|
|
|
if (entriesSize < 0 || entriesSize % sizeof(CacheIndexRecord)) {
|
|
|
|
LOG(("CacheIndex::StartReadingIndex() - Index is corrupted"));
|
|
|
|
FinishRead(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
AllocBuffer();
|
|
|
|
mSkipEntries = 0;
|
|
|
|
mRWHash = new CacheHash();
|
|
|
|
|
|
|
|
mRWBufPos =
|
|
|
|
std::min(mRWBufSize, static_cast<uint32_t>(mIndexHandle->FileSize()));
|
|
|
|
|
2014-08-18 17:36:27 +04:00
|
|
|
rv = CacheFileIOManager::Read(mIndexHandle, 0, mRWBuf, mRWBufPos, this);
|
2014-02-18 19:05:07 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
LOG(
|
|
|
|
("CacheIndex::StartReadingIndex() - CacheFileIOManager::Read() failed "
|
2016-12-16 06:16:31 +03:00
|
|
|
"synchronously [rv=0x%08" PRIx32 "]",
|
|
|
|
static_cast<uint32_t>(rv)));
|
2014-02-18 19:05:07 +04:00
|
|
|
FinishRead(false);
|
2016-05-11 11:48:28 +03:00
|
|
|
} else {
|
|
|
|
mRWPending = true;
|
2014-02-18 19:05:07 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CacheIndex::ParseRecords() {
|
|
|
|
LOG(("CacheIndex::ParseRecords()"));
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
2015-11-12 13:07:48 +03:00
|
|
|
sLock.AssertCurrentThreadOwns();
|
2014-02-18 19:05:07 +04:00
|
|
|
|
2016-05-11 11:48:28 +03:00
|
|
|
MOZ_ASSERT(!mRWPending);
|
|
|
|
|
2014-02-18 19:05:07 +04:00
|
|
|
uint32_t entryCnt = (mIndexHandle->FileSize() - sizeof(CacheIndexHeader) -
|
|
|
|
sizeof(CacheHash::Hash32_t)) /
|
|
|
|
sizeof(CacheIndexRecord);
|
|
|
|
uint32_t pos = 0;
|
|
|
|
|
|
|
|
if (!mSkipEntries) {
|
2016-12-02 18:21:35 +03:00
|
|
|
if (NetworkEndian::readUint32(mRWBuf + pos) != kIndexVersion) {
|
2014-02-18 19:05:07 +04:00
|
|
|
FinishRead(false);
|
|
|
|
return;
|
|
|
|
}
|
2016-12-02 18:21:35 +03:00
|
|
|
pos += sizeof(uint32_t);
|
2014-02-18 19:05:07 +04:00
|
|
|
|
2016-12-02 18:21:35 +03:00
|
|
|
mIndexTimeStamp = NetworkEndian::readUint32(mRWBuf + pos);
|
|
|
|
pos += sizeof(uint32_t);
|
2014-02-18 19:05:07 +04:00
|
|
|
|
2016-12-02 18:21:35 +03:00
|
|
|
if (NetworkEndian::readUint32(mRWBuf + pos)) {
|
2014-02-18 19:05:07 +04:00
|
|
|
if (mJournalHandle) {
|
|
|
|
CacheFileIOManager::DoomFile(mJournalHandle, nullptr);
|
|
|
|
mJournalHandle = nullptr;
|
|
|
|
}
|
|
|
|
} else {
|
2016-12-02 18:21:35 +03:00
|
|
|
uint32_t* isDirty =
|
|
|
|
reinterpret_cast<uint32_t*>(moz_xmalloc(sizeof(uint32_t)));
|
|
|
|
NetworkEndian::writeUint32(isDirty, 1);
|
2014-02-18 19:05:07 +04:00
|
|
|
|
|
|
|
// Mark index dirty. The buffer is freed by CacheFileIOManager when
|
|
|
|
// nullptr is passed as the listener and the call doesn't fail
|
|
|
|
// synchronously.
|
2016-12-02 18:21:35 +03:00
|
|
|
rv = CacheFileIOManager::Write(mIndexHandle, 2 * sizeof(uint32_t),
|
|
|
|
reinterpret_cast<char*>(isDirty),
|
|
|
|
sizeof(uint32_t), true, false, nullptr);
|
2014-02-18 19:05:07 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// This is not fatal, just free the memory
|
2016-12-02 18:21:35 +03:00
|
|
|
free(isDirty);
|
2014-02-18 19:05:07 +04:00
|
|
|
}
|
|
|
|
}
|
2016-12-02 18:21:35 +03:00
|
|
|
pos += sizeof(uint32_t);
|
2014-02-18 19:05:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t hashOffset = pos;
|
|
|
|
|
|
|
|
while (pos + sizeof(CacheIndexRecord) <= mRWBufPos &&
|
|
|
|
mSkipEntries != entryCnt) {
|
|
|
|
CacheIndexRecord* rec = reinterpret_cast<CacheIndexRecord*>(mRWBuf + pos);
|
|
|
|
CacheIndexEntry tmpEntry(&rec->mHash);
|
|
|
|
tmpEntry.ReadFromBuf(mRWBuf + pos);
|
|
|
|
|
|
|
|
if (tmpEntry.IsDirty() || !tmpEntry.IsInitialized() ||
|
|
|
|
tmpEntry.IsFileEmpty() || tmpEntry.IsFresh() || tmpEntry.IsRemoved()) {
|
|
|
|
LOG(
|
|
|
|
("CacheIndex::ParseRecords() - Invalid entry found in index, removing"
|
|
|
|
" whole index [dirty=%d, initialized=%d, fileEmpty=%d, fresh=%d, "
|
|
|
|
"removed=%d]",
|
|
|
|
tmpEntry.IsDirty(), tmpEntry.IsInitialized(), tmpEntry.IsFileEmpty(),
|
|
|
|
tmpEntry.IsFresh(), tmpEntry.IsRemoved()));
|
|
|
|
FinishRead(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
CacheIndexEntryAutoManage emng(tmpEntry.Hash(), this);
|
|
|
|
|
|
|
|
CacheIndexEntry* entry = mIndex.PutEntry(*tmpEntry.Hash());
|
|
|
|
*entry = tmpEntry;
|
|
|
|
|
|
|
|
pos += sizeof(CacheIndexRecord);
|
|
|
|
mSkipEntries++;
|
|
|
|
}
|
|
|
|
|
|
|
|
mRWHash->Update(mRWBuf + hashOffset, pos - hashOffset);
|
|
|
|
|
|
|
|
if (pos != mRWBufPos) {
|
|
|
|
memmove(mRWBuf, mRWBuf + pos, mRWBufPos - pos);
|
|
|
|
}
|
|
|
|
|
2015-01-10 03:21:04 +03:00
|
|
|
mRWBufPos -= pos;
|
|
|
|
pos = 0;
|
|
|
|
|
2014-02-18 19:05:07 +04:00
|
|
|
int64_t fileOffset = sizeof(CacheIndexHeader) +
|
|
|
|
mSkipEntries * sizeof(CacheIndexRecord) + mRWBufPos;
|
|
|
|
|
|
|
|
MOZ_ASSERT(fileOffset <= mIndexHandle->FileSize());
|
|
|
|
if (fileOffset == mIndexHandle->FileSize()) {
|
2016-05-06 16:11:17 +03:00
|
|
|
uint32_t expectedHash = NetworkEndian::readUint32(mRWBuf);
|
|
|
|
if (mRWHash->GetHash() != expectedHash) {
|
2014-02-18 19:05:07 +04:00
|
|
|
LOG(("CacheIndex::ParseRecords() - Hash mismatch, [is %x, should be %x]",
|
2016-05-06 16:11:17 +03:00
|
|
|
mRWHash->GetHash(), expectedHash));
|
2014-02-18 19:05:07 +04:00
|
|
|
FinishRead(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mIndexOnDiskIsValid = true;
|
|
|
|
mJournalReadSuccessfully = false;
|
|
|
|
|
|
|
|
if (mJournalHandle) {
|
|
|
|
StartReadingJournal();
|
|
|
|
} else {
|
|
|
|
FinishRead(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
pos = mRWBufPos;
|
|
|
|
uint32_t toRead =
|
|
|
|
std::min(mRWBufSize - pos,
|
|
|
|
static_cast<uint32_t>(mIndexHandle->FileSize() - fileOffset));
|
|
|
|
mRWBufPos = pos + toRead;
|
|
|
|
|
|
|
|
rv = CacheFileIOManager::Read(mIndexHandle, fileOffset, mRWBuf + pos, toRead,
|
2014-08-18 17:36:27 +04:00
|
|
|
this);
|
2014-02-18 19:05:07 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
LOG(
|
|
|
|
("CacheIndex::ParseRecords() - CacheFileIOManager::Read() failed "
|
2016-12-16 06:16:31 +03:00
|
|
|
"synchronously [rv=0x%08" PRIx32 "]",
|
|
|
|
static_cast<uint32_t>(rv)));
|
2014-02-18 19:05:07 +04:00
|
|
|
FinishRead(false);
|
|
|
|
return;
|
|
|
|
}
|
2018-04-30 19:46:04 +03:00
|
|
|
mRWPending = true;
|
2014-02-18 19:05:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void CacheIndex::StartReadingJournal() {
|
|
|
|
LOG(("CacheIndex::StartReadingJournal()"));
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
2015-11-12 13:07:48 +03:00
|
|
|
sLock.AssertCurrentThreadOwns();
|
2014-02-18 19:05:07 +04:00
|
|
|
|
|
|
|
MOZ_ASSERT(mJournalHandle);
|
|
|
|
MOZ_ASSERT(mIndexOnDiskIsValid);
|
|
|
|
MOZ_ASSERT(mTmpJournal.Count() == 0);
|
|
|
|
MOZ_ASSERT(mJournalHandle->FileSize() >= 0);
|
2016-05-11 11:48:28 +03:00
|
|
|
MOZ_ASSERT(!mRWPending);
|
2014-02-18 19:05:07 +04:00
|
|
|
|
|
|
|
int64_t entriesSize =
|
|
|
|
mJournalHandle->FileSize() - sizeof(CacheHash::Hash32_t);
|
|
|
|
|
|
|
|
if (entriesSize < 0 || entriesSize % sizeof(CacheIndexRecord)) {
|
|
|
|
LOG(("CacheIndex::StartReadingJournal() - Journal is corrupted"));
|
|
|
|
FinishRead(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mSkipEntries = 0;
|
|
|
|
mRWHash = new CacheHash();
|
|
|
|
|
|
|
|
mRWBufPos =
|
|
|
|
std::min(mRWBufSize, static_cast<uint32_t>(mJournalHandle->FileSize()));
|
|
|
|
|
2014-08-18 17:36:27 +04:00
|
|
|
rv = CacheFileIOManager::Read(mJournalHandle, 0, mRWBuf, mRWBufPos, this);
|
2014-02-18 19:05:07 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
LOG(
|
|
|
|
("CacheIndex::StartReadingJournal() - CacheFileIOManager::Read() failed"
|
2016-12-16 06:16:31 +03:00
|
|
|
" synchronously [rv=0x%08" PRIx32 "]",
|
|
|
|
static_cast<uint32_t>(rv)));
|
2014-02-18 19:05:07 +04:00
|
|
|
FinishRead(false);
|
2016-05-11 11:48:28 +03:00
|
|
|
} else {
|
|
|
|
mRWPending = true;
|
2014-02-18 19:05:07 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CacheIndex::ParseJournal() {
|
2015-01-10 03:21:04 +03:00
|
|
|
LOG(("CacheIndex::ParseJournal()"));
|
2014-02-18 19:05:07 +04:00
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
2015-11-12 13:07:48 +03:00
|
|
|
sLock.AssertCurrentThreadOwns();
|
2014-02-18 19:05:07 +04:00
|
|
|
|
2016-05-11 11:48:28 +03:00
|
|
|
MOZ_ASSERT(!mRWPending);
|
|
|
|
|
2014-02-18 19:05:07 +04:00
|
|
|
uint32_t entryCnt =
|
|
|
|
(mJournalHandle->FileSize() - sizeof(CacheHash::Hash32_t)) /
|
|
|
|
sizeof(CacheIndexRecord);
|
|
|
|
|
|
|
|
uint32_t pos = 0;
|
|
|
|
|
|
|
|
while (pos + sizeof(CacheIndexRecord) <= mRWBufPos &&
|
|
|
|
mSkipEntries != entryCnt) {
|
2016-12-02 18:21:35 +03:00
|
|
|
CacheIndexEntry tmpEntry(reinterpret_cast<SHA1Sum::Hash*>(mRWBuf + pos));
|
2014-02-18 19:05:07 +04:00
|
|
|
tmpEntry.ReadFromBuf(mRWBuf + pos);
|
|
|
|
|
|
|
|
CacheIndexEntry* entry = mTmpJournal.PutEntry(*tmpEntry.Hash());
|
|
|
|
*entry = tmpEntry;
|
|
|
|
|
|
|
|
if (entry->IsDirty() || entry->IsFresh()) {
|
|
|
|
LOG(
|
|
|
|
("CacheIndex::ParseJournal() - Invalid entry found in journal, "
|
|
|
|
"ignoring whole journal [dirty=%d, fresh=%d]",
|
|
|
|
entry->IsDirty(), entry->IsFresh()));
|
|
|
|
FinishRead(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
pos += sizeof(CacheIndexRecord);
|
|
|
|
mSkipEntries++;
|
|
|
|
}
|
|
|
|
|
|
|
|
mRWHash->Update(mRWBuf, pos);
|
|
|
|
|
|
|
|
if (pos != mRWBufPos) {
|
|
|
|
memmove(mRWBuf, mRWBuf + pos, mRWBufPos - pos);
|
|
|
|
}
|
|
|
|
|
2015-01-10 03:21:04 +03:00
|
|
|
mRWBufPos -= pos;
|
|
|
|
pos = 0;
|
|
|
|
|
2014-02-18 19:05:07 +04:00
|
|
|
int64_t fileOffset = mSkipEntries * sizeof(CacheIndexRecord) + mRWBufPos;
|
|
|
|
|
|
|
|
MOZ_ASSERT(fileOffset <= mJournalHandle->FileSize());
|
|
|
|
if (fileOffset == mJournalHandle->FileSize()) {
|
2016-05-06 16:11:17 +03:00
|
|
|
uint32_t expectedHash = NetworkEndian::readUint32(mRWBuf);
|
|
|
|
if (mRWHash->GetHash() != expectedHash) {
|
2014-02-18 19:05:07 +04:00
|
|
|
LOG(("CacheIndex::ParseJournal() - Hash mismatch, [is %x, should be %x]",
|
2016-05-06 16:11:17 +03:00
|
|
|
mRWHash->GetHash(), expectedHash));
|
2014-02-18 19:05:07 +04:00
|
|
|
FinishRead(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mJournalReadSuccessfully = true;
|
|
|
|
FinishRead(true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
pos = mRWBufPos;
|
|
|
|
uint32_t toRead =
|
|
|
|
std::min(mRWBufSize - pos,
|
|
|
|
static_cast<uint32_t>(mJournalHandle->FileSize() - fileOffset));
|
|
|
|
mRWBufPos = pos + toRead;
|
|
|
|
|
|
|
|
rv = CacheFileIOManager::Read(mJournalHandle, fileOffset, mRWBuf + pos,
|
2014-08-18 17:36:27 +04:00
|
|
|
toRead, this);
|
2014-02-18 19:05:07 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
LOG(
|
|
|
|
("CacheIndex::ParseJournal() - CacheFileIOManager::Read() failed "
|
2016-12-16 06:16:31 +03:00
|
|
|
"synchronously [rv=0x%08" PRIx32 "]",
|
|
|
|
static_cast<uint32_t>(rv)));
|
2014-02-18 19:05:07 +04:00
|
|
|
FinishRead(false);
|
|
|
|
return;
|
|
|
|
}
|
2018-04-30 19:46:04 +03:00
|
|
|
mRWPending = true;
|
2014-02-18 19:05:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void CacheIndex::MergeJournal() {
|
|
|
|
LOG(("CacheIndex::MergeJournal()"));
|
|
|
|
|
2015-11-12 13:07:48 +03:00
|
|
|
sLock.AssertCurrentThreadOwns();
|
2014-02-18 19:05:07 +04:00
|
|
|
|
2015-07-27 06:15:36 +03:00
|
|
|
for (auto iter = mTmpJournal.Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
CacheIndexEntry* entry = iter.Get();
|
2014-02-18 19:05:07 +04:00
|
|
|
|
2015-07-27 06:15:36 +03:00
|
|
|
LOG(("CacheIndex::MergeJournal() [hash=%08x%08x%08x%08x%08x]",
|
|
|
|
LOGSHA1(entry->Hash())));
|
2014-02-18 19:05:07 +04:00
|
|
|
|
2015-07-27 06:15:36 +03:00
|
|
|
CacheIndexEntry* entry2 = mIndex.GetEntry(*entry->Hash());
|
|
|
|
{
|
|
|
|
CacheIndexEntryAutoManage emng(entry->Hash(), this);
|
|
|
|
if (entry->IsRemoved()) {
|
|
|
|
if (entry2) {
|
|
|
|
entry2->MarkRemoved();
|
|
|
|
entry2->MarkDirty();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!entry2) {
|
|
|
|
entry2 = mIndex.PutEntry(*entry->Hash());
|
|
|
|
}
|
2015-08-07 02:30:47 +03:00
|
|
|
|
2015-07-27 06:15:36 +03:00
|
|
|
*entry2 = *entry;
|
|
|
|
entry2->MarkDirty();
|
|
|
|
}
|
2014-02-18 19:05:07 +04:00
|
|
|
}
|
2015-07-27 06:15:36 +03:00
|
|
|
iter.Remove();
|
2014-02-18 19:05:07 +04:00
|
|
|
}
|
|
|
|
|
2015-07-27 06:15:36 +03:00
|
|
|
MOZ_ASSERT(mTmpJournal.Count() == 0);
|
2014-02-18 19:05:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void CacheIndex::EnsureNoFreshEntry() {
|
|
|
|
#ifdef DEBUG_STATS
|
|
|
|
CacheIndexStats debugStats;
|
|
|
|
debugStats.DisableLogging();
|
2015-07-29 07:14:39 +03:00
|
|
|
for (auto iter = mIndex.Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
debugStats.BeforeChange(nullptr);
|
|
|
|
debugStats.AfterChange(iter.Get());
|
|
|
|
}
|
2014-02-18 19:05:07 +04:00
|
|
|
MOZ_ASSERT(debugStats.Fresh() == 0);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void CacheIndex::EnsureCorrectStats() {
|
|
|
|
#ifdef DEBUG_STATS
|
|
|
|
MOZ_ASSERT(mPendingUpdates.Count() == 0);
|
|
|
|
CacheIndexStats debugStats;
|
|
|
|
debugStats.DisableLogging();
|
2015-07-29 07:14:39 +03:00
|
|
|
for (auto iter = mIndex.Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
debugStats.BeforeChange(nullptr);
|
|
|
|
debugStats.AfterChange(iter.Get());
|
|
|
|
}
|
2014-02-18 19:05:07 +04:00
|
|
|
MOZ_ASSERT(debugStats == mIndexStats);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void CacheIndex::FinishRead(bool aSucceeded) {
|
|
|
|
LOG(("CacheIndex::FinishRead() [succeeded=%d]", aSucceeded));
|
2015-11-12 13:07:48 +03:00
|
|
|
sLock.AssertCurrentThreadOwns();
|
2014-02-18 19:05:07 +04:00
|
|
|
|
|
|
|
MOZ_ASSERT((!aSucceeded && mState == SHUTDOWN) || mState == READING);
|
|
|
|
|
|
|
|
MOZ_ASSERT(
|
|
|
|
// -> rebuild
|
|
|
|
(!aSucceeded && !mIndexOnDiskIsValid && !mJournalReadSuccessfully) ||
|
|
|
|
// -> update
|
|
|
|
(!aSucceeded && mIndexOnDiskIsValid && !mJournalReadSuccessfully) ||
|
|
|
|
// -> ready
|
|
|
|
(aSucceeded && mIndexOnDiskIsValid && mJournalReadSuccessfully));
|
|
|
|
|
2016-05-11 11:48:28 +03:00
|
|
|
// If there is read operation pending we must be cancelling reading of the
|
|
|
|
// index when shutting down or removing the whole index.
|
|
|
|
MOZ_ASSERT(!mRWPending || (!aSucceeded && (mShuttingDown || mRemovingAll)));
|
|
|
|
|
2014-02-18 19:05:07 +04:00
|
|
|
if (mState == SHUTDOWN) {
|
2015-05-16 11:07:10 +03:00
|
|
|
RemoveFile(NS_LITERAL_CSTRING(TEMP_INDEX_NAME));
|
|
|
|
RemoveFile(NS_LITERAL_CSTRING(JOURNAL_NAME));
|
2014-02-18 19:05:07 +04:00
|
|
|
} else {
|
|
|
|
if (mIndexHandle && !mIndexOnDiskIsValid) {
|
|
|
|
CacheFileIOManager::DoomFile(mIndexHandle, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mJournalHandle) {
|
|
|
|
CacheFileIOManager::DoomFile(mJournalHandle, nullptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-07 15:22:59 +04:00
|
|
|
if (mIndexFileOpener) {
|
|
|
|
mIndexFileOpener->Cancel();
|
|
|
|
mIndexFileOpener = nullptr;
|
|
|
|
}
|
|
|
|
if (mJournalFileOpener) {
|
|
|
|
mJournalFileOpener->Cancel();
|
|
|
|
mJournalFileOpener = nullptr;
|
|
|
|
}
|
|
|
|
if (mTmpFileOpener) {
|
|
|
|
mTmpFileOpener->Cancel();
|
|
|
|
mTmpFileOpener = nullptr;
|
|
|
|
}
|
|
|
|
|
2014-02-18 19:05:07 +04:00
|
|
|
mIndexHandle = nullptr;
|
|
|
|
mJournalHandle = nullptr;
|
|
|
|
mRWHash = nullptr;
|
|
|
|
ReleaseBuffer();
|
|
|
|
|
|
|
|
if (mState == SHUTDOWN) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mIndexOnDiskIsValid) {
|
|
|
|
MOZ_ASSERT(mTmpJournal.Count() == 0);
|
|
|
|
EnsureNoFreshEntry();
|
|
|
|
ProcessPendingOperations();
|
|
|
|
// Remove all entries that we haven't seen during this session
|
2015-07-27 06:35:31 +03:00
|
|
|
RemoveNonFreshEntries();
|
2014-03-07 15:22:59 +04:00
|
|
|
StartUpdatingIndex(true);
|
2014-02-18 19:05:07 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mJournalReadSuccessfully) {
|
|
|
|
mTmpJournal.Clear();
|
|
|
|
EnsureNoFreshEntry();
|
|
|
|
ProcessPendingOperations();
|
2014-03-07 15:22:59 +04:00
|
|
|
StartUpdatingIndex(false);
|
2014-02-18 19:05:07 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MergeJournal();
|
|
|
|
EnsureNoFreshEntry();
|
|
|
|
ProcessPendingOperations();
|
|
|
|
mIndexStats.Log();
|
|
|
|
|
|
|
|
ChangeState(READY);
|
|
|
|
mLastDumpTime = TimeStamp::NowLoRes(); // Do not dump new index immediately
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2014-03-07 15:22:59 +04:00
|
|
|
void CacheIndex::DelayedUpdate(nsITimer* aTimer, void* aClosure) {
|
|
|
|
LOG(("CacheIndex::DelayedUpdate()"));
|
2014-02-18 19:05:07 +04:00
|
|
|
|
2015-11-12 13:07:48 +03:00
|
|
|
StaticMutexAutoLock lock(sLock);
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CacheIndex> index = gInstance;
|
2014-02-18 19:05:07 +04:00
|
|
|
|
|
|
|
if (!index) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-30 05:48:00 +03:00
|
|
|
index->DelayedUpdateLocked();
|
|
|
|
}
|
2014-02-18 19:05:07 +04:00
|
|
|
|
2016-05-30 05:48:00 +03:00
|
|
|
// static
|
|
|
|
void CacheIndex::DelayedUpdateLocked() {
|
|
|
|
LOG(("CacheIndex::DelayedUpdateLocked()"));
|
|
|
|
|
|
|
|
sLock.AssertCurrentThreadOwns();
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
mUpdateTimer = nullptr;
|
|
|
|
|
|
|
|
if (!IsIndexUsable()) {
|
2014-02-18 19:05:07 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-30 05:48:00 +03:00
|
|
|
if (mState == READY && mShuttingDown) {
|
2014-02-18 19:05:07 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-07 15:22:59 +04:00
|
|
|
// mUpdateEventPending must be false here since StartUpdatingIndex() won't
|
|
|
|
// schedule timer if it is true.
|
2016-05-30 05:48:00 +03:00
|
|
|
MOZ_ASSERT(!mUpdateEventPending);
|
|
|
|
if (mState != BUILDING && mState != UPDATING) {
|
|
|
|
LOG(("CacheIndex::DelayedUpdateLocked() - Update was canceled"));
|
2014-03-07 15:22:59 +04:00
|
|
|
return;
|
|
|
|
}
|
2014-02-18 19:05:07 +04:00
|
|
|
|
|
|
|
// We need to redispatch to run with lower priority
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CacheIOThread> ioThread = CacheFileIOManager::IOThread();
|
2014-02-18 19:05:07 +04:00
|
|
|
MOZ_ASSERT(ioThread);
|
|
|
|
|
2016-05-30 05:48:00 +03:00
|
|
|
mUpdateEventPending = true;
|
|
|
|
rv = ioThread->Dispatch(this, CacheIOThread::INDEX);
|
2014-02-18 19:05:07 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
2016-05-30 05:48:00 +03:00
|
|
|
mUpdateEventPending = false;
|
|
|
|
NS_WARNING("CacheIndex::DelayedUpdateLocked() - Can't dispatch event");
|
2014-03-07 15:22:59 +04:00
|
|
|
LOG(("CacheIndex::DelayedUpdate() - Can't dispatch event"));
|
2016-05-30 05:48:00 +03:00
|
|
|
FinishUpdate(false);
|
2014-02-18 19:05:07 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-07 15:22:59 +04:00
|
|
|
nsresult CacheIndex::ScheduleUpdateTimer(uint32_t aDelay) {
|
|
|
|
LOG(("CacheIndex::ScheduleUpdateTimer() [delay=%u]", aDelay));
|
2014-02-18 19:05:07 +04:00
|
|
|
|
2014-03-07 15:22:59 +04:00
|
|
|
MOZ_ASSERT(!mUpdateTimer);
|
2014-02-18 19:05:07 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIEventTarget> ioTarget = CacheFileIOManager::IOTarget();
|
|
|
|
MOZ_ASSERT(ioTarget);
|
|
|
|
|
2017-10-16 09:12:02 +03:00
|
|
|
return NS_NewTimerWithFuncCallback(
|
|
|
|
getter_AddRefs(mUpdateTimer), CacheIndex::DelayedUpdate, nullptr, aDelay,
|
|
|
|
nsITimer::TYPE_ONE_SHOT, "net::CacheIndex::ScheduleUpdateTimer",
|
|
|
|
ioTarget);
|
2014-02-18 19:05:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult CacheIndex::SetupDirectoryEnumerator() {
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(!mDirEnumerator);
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIFile> file;
|
|
|
|
|
|
|
|
rv = mCacheDirectory->Clone(getter_AddRefs(file));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2015-05-16 11:07:10 +03:00
|
|
|
rv = file->AppendNative(NS_LITERAL_CSTRING(ENTRIES_DIR));
|
2014-02-18 19:05:07 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
bool exists;
|
|
|
|
rv = file->Exists(&exists);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (!exists) {
|
|
|
|
NS_WARNING(
|
|
|
|
"CacheIndex::SetupDirectoryEnumerator() - Entries directory "
|
|
|
|
"doesn't exist!");
|
|
|
|
LOG(
|
|
|
|
("CacheIndex::SetupDirectoryEnumerator() - Entries directory doesn't "
|
|
|
|
"exist!"));
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
2018-05-20 06:17:45 +03:00
|
|
|
rv = file->GetDirectoryEntries(getter_AddRefs(mDirEnumerator));
|
2014-02-18 19:05:07 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult CacheIndex::InitEntryFromDiskData(CacheIndexEntry* aEntry,
|
|
|
|
CacheFileMetadata* aMetaData,
|
|
|
|
int64_t aFileSize) {
|
2019-03-27 17:32:12 +03:00
|
|
|
nsresult rv;
|
|
|
|
|
2014-02-18 19:05:07 +04:00
|
|
|
aEntry->InitNew();
|
|
|
|
aEntry->MarkDirty();
|
|
|
|
aEntry->MarkFresh();
|
2015-10-11 19:13:09 +03:00
|
|
|
|
2016-11-08 23:11:30 +03:00
|
|
|
aEntry->Init(GetOriginAttrsHash(aMetaData->OriginAttributes()),
|
2015-10-11 19:13:09 +03:00
|
|
|
aMetaData->IsAnonymous(), aMetaData->Pinned());
|
2014-02-18 19:05:07 +04:00
|
|
|
|
2019-12-02 16:21:06 +03:00
|
|
|
aEntry->SetFrecency(aMetaData->GetFrecency());
|
2014-02-18 19:05:07 +04:00
|
|
|
|
2017-03-06 13:21:59 +03:00
|
|
|
const char* altData = aMetaData->GetElement(CacheFileUtils::kAltDataKey);
|
|
|
|
bool hasAltData = altData ? true : false;
|
|
|
|
if (hasAltData && NS_FAILED(CacheFileUtils::ParseAlternativeDataInfo(
|
|
|
|
altData, nullptr, nullptr))) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
aEntry->SetHasAltData(hasAltData);
|
|
|
|
|
2017-07-14 07:13:00 +03:00
|
|
|
static auto toUint16 = [](const char* aUint16String) -> uint16_t {
|
|
|
|
if (!aUint16String) {
|
|
|
|
return kIndexTimeNotAvailable;
|
2017-03-28 06:40:21 +03:00
|
|
|
}
|
2017-07-14 07:13:00 +03:00
|
|
|
nsresult rv;
|
2018-03-08 17:43:00 +03:00
|
|
|
uint64_t n64 = nsDependentCString(aUint16String).ToInteger64(&rv);
|
2017-07-14 07:13:00 +03:00
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
|
|
|
return n64 <= kIndexTimeOutOfBound ? n64 : kIndexTimeOutOfBound;
|
2017-03-28 06:40:21 +03:00
|
|
|
};
|
|
|
|
|
2017-07-14 07:13:00 +03:00
|
|
|
aEntry->SetOnStartTime(
|
|
|
|
toUint16(aMetaData->GetElement("net-response-time-onstart")));
|
|
|
|
aEntry->SetOnStopTime(
|
|
|
|
toUint16(aMetaData->GetElement("net-response-time-onstop")));
|
2017-03-28 06:40:21 +03:00
|
|
|
|
2019-03-27 17:32:12 +03:00
|
|
|
const char* contentTypeStr = aMetaData->GetElement("ctid");
|
|
|
|
uint8_t contentType = nsICacheEntry::CONTENT_TYPE_UNKNOWN;
|
|
|
|
if (contentTypeStr) {
|
|
|
|
int64_t n64 = nsDependentCString(contentTypeStr).ToInteger64(&rv);
|
|
|
|
if (NS_FAILED(rv) || n64 < nsICacheEntry::CONTENT_TYPE_UNKNOWN ||
|
|
|
|
n64 >= nsICacheEntry::CONTENT_TYPE_LAST) {
|
|
|
|
n64 = nsICacheEntry::CONTENT_TYPE_UNKNOWN;
|
|
|
|
}
|
|
|
|
contentType = n64;
|
|
|
|
}
|
|
|
|
aEntry->SetContentType(contentType);
|
|
|
|
|
2014-02-18 19:05:07 +04:00
|
|
|
aEntry->SetFileSize(static_cast<uint32_t>(std::min(
|
|
|
|
static_cast<int64_t>(PR_UINT32_MAX), (aFileSize + 0x3FF) >> 10)));
|
2017-03-06 13:21:59 +03:00
|
|
|
return NS_OK;
|
2014-02-18 19:05:07 +04:00
|
|
|
}
|
|
|
|
|
2014-03-07 15:22:59 +04:00
|
|
|
bool CacheIndex::IsUpdatePending() {
|
2015-11-12 13:07:48 +03:00
|
|
|
sLock.AssertCurrentThreadOwns();
|
2014-02-18 19:05:07 +04:00
|
|
|
|
2014-03-07 15:22:59 +04:00
|
|
|
if (mUpdateTimer || mUpdateEventPending) {
|
|
|
|
return true;
|
2014-02-18 19:05:07 +04:00
|
|
|
}
|
|
|
|
|
2014-03-07 15:22:59 +04:00
|
|
|
return false;
|
2014-02-18 19:05:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void CacheIndex::BuildIndex() {
|
|
|
|
LOG(("CacheIndex::BuildIndex()"));
|
|
|
|
|
2015-11-12 13:07:48 +03:00
|
|
|
sLock.AssertCurrentThreadOwns();
|
2014-02-18 19:05:07 +04:00
|
|
|
|
|
|
|
MOZ_ASSERT(mPendingUpdates.Count() == 0);
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
if (!mDirEnumerator) {
|
|
|
|
{
|
|
|
|
// Do not do IO under the lock.
|
2015-11-12 13:07:48 +03:00
|
|
|
StaticMutexAutoUnlock unlock(sLock);
|
2014-02-18 19:05:07 +04:00
|
|
|
rv = SetupDirectoryEnumerator();
|
|
|
|
}
|
|
|
|
if (mState == SHUTDOWN) {
|
2014-03-07 15:22:59 +04:00
|
|
|
// The index was shut down while we released the lock. FinishUpdate() was
|
2014-02-18 19:05:07 +04:00
|
|
|
// already called from Shutdown(), so just simply return here.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
2014-03-07 15:22:59 +04:00
|
|
|
FinishUpdate(false);
|
2014-02-18 19:05:07 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while (true) {
|
2014-02-27 03:11:42 +04:00
|
|
|
if (CacheIOThread::YieldAndRerun()) {
|
|
|
|
LOG((
|
|
|
|
"CacheIndex::BuildIndex() - Breaking loop for higher level events."));
|
2014-03-07 15:22:59 +04:00
|
|
|
mUpdateEventPending = true;
|
2014-02-27 03:11:42 +04:00
|
|
|
return;
|
2014-02-18 19:05:07 +04:00
|
|
|
}
|
|
|
|
|
2017-08-11 13:30:59 +03:00
|
|
|
bool fileExists = false;
|
2014-02-18 19:05:07 +04:00
|
|
|
nsCOMPtr<nsIFile> file;
|
|
|
|
{
|
|
|
|
// Do not do IO under the lock.
|
2015-11-12 13:07:48 +03:00
|
|
|
StaticMutexAutoUnlock unlock(sLock);
|
2014-02-18 19:05:07 +04:00
|
|
|
rv = mDirEnumerator->GetNextFile(getter_AddRefs(file));
|
2017-08-11 13:30:59 +03:00
|
|
|
|
|
|
|
if (file) {
|
|
|
|
file->Exists(&fileExists);
|
|
|
|
}
|
2014-02-18 19:05:07 +04:00
|
|
|
}
|
|
|
|
if (mState == SHUTDOWN) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!file) {
|
2014-03-07 15:22:59 +04:00
|
|
|
FinishUpdate(NS_SUCCEEDED(rv));
|
2014-02-18 19:05:07 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoCString leaf;
|
|
|
|
rv = file->GetNativeLeafName(leaf);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
LOG(
|
|
|
|
("CacheIndex::BuildIndex() - GetNativeLeafName() failed! Skipping "
|
|
|
|
"file."));
|
|
|
|
mDontMarkIndexClean = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-08-11 13:30:59 +03:00
|
|
|
if (!fileExists) {
|
|
|
|
LOG(
|
|
|
|
("CacheIndex::BuildIndex() - File returned by the iterator was "
|
|
|
|
"removed in the meantime [name=%s]",
|
|
|
|
leaf.get()));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-02-18 19:05:07 +04:00
|
|
|
SHA1Sum::Hash hash;
|
|
|
|
rv = CacheFileIOManager::StrToHash(leaf, &hash);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
LOG(
|
|
|
|
("CacheIndex::BuildIndex() - Filename is not a hash, removing file. "
|
|
|
|
"[name=%s]",
|
|
|
|
leaf.get()));
|
|
|
|
file->Remove(false);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
CacheIndexEntry* entry = mIndex.GetEntry(hash);
|
|
|
|
if (entry && entry->IsRemoved()) {
|
|
|
|
LOG(
|
|
|
|
("CacheIndex::BuildIndex() - Found file that should not exist. "
|
|
|
|
"[name=%s]",
|
|
|
|
leaf.get()));
|
|
|
|
entry->Log();
|
|
|
|
MOZ_ASSERT(entry->IsFresh());
|
|
|
|
entry = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CacheFileHandle> handle;
|
2015-02-24 19:49:46 +03:00
|
|
|
CacheFileIOManager::gInstance->mHandles.GetHandle(&hash,
|
2014-02-18 19:05:07 +04:00
|
|
|
getter_AddRefs(handle));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (entry) {
|
|
|
|
// the entry is up to date
|
|
|
|
LOG(
|
|
|
|
("CacheIndex::BuildIndex() - Skipping file because the entry is up to"
|
|
|
|
" date. [name=%s]",
|
|
|
|
leaf.get()));
|
|
|
|
entry->Log();
|
|
|
|
MOZ_ASSERT(entry->IsFresh()); // The entry must be from this session
|
|
|
|
// there must be an active CacheFile if the entry is not initialized
|
|
|
|
MOZ_ASSERT(entry->IsInitialized() || handle);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(!handle);
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CacheFileMetadata> meta = new CacheFileMetadata();
|
2014-02-18 19:05:07 +04:00
|
|
|
int64_t size = 0;
|
|
|
|
|
|
|
|
{
|
|
|
|
// Do not do IO under the lock.
|
2015-11-12 13:07:48 +03:00
|
|
|
StaticMutexAutoUnlock unlock(sLock);
|
2014-02-18 19:05:07 +04:00
|
|
|
rv = meta->SyncReadMetadata(file);
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = file->GetFileSize(&size);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
LOG(
|
|
|
|
("CacheIndex::BuildIndex() - Cannot get filesize of file that was"
|
|
|
|
" successfully parsed. [name=%s]",
|
|
|
|
leaf.get()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (mState == SHUTDOWN) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nobody could add the entry while the lock was released since we modify
|
|
|
|
// the index only on IO thread and this loop is executed on IO thread too.
|
|
|
|
entry = mIndex.GetEntry(hash);
|
|
|
|
MOZ_ASSERT(!entry || entry->IsRemoved());
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
LOG(
|
|
|
|
("CacheIndex::BuildIndex() - CacheFileMetadata::SyncReadMetadata() "
|
|
|
|
"failed, removing file. [name=%s]",
|
|
|
|
leaf.get()));
|
|
|
|
file->Remove(false);
|
|
|
|
} else {
|
|
|
|
CacheIndexEntryAutoManage entryMng(&hash, this);
|
|
|
|
entry = mIndex.PutEntry(hash);
|
2017-03-06 13:21:59 +03:00
|
|
|
if (NS_FAILED(InitEntryFromDiskData(entry, meta, size))) {
|
|
|
|
LOG(
|
|
|
|
("CacheIndex::BuildIndex() - CacheFile::InitEntryFromDiskData() "
|
|
|
|
"failed, removing file. [name=%s]",
|
|
|
|
leaf.get()));
|
|
|
|
file->Remove(false);
|
2017-08-11 13:30:59 +03:00
|
|
|
entry->MarkRemoved();
|
2017-03-06 13:21:59 +03:00
|
|
|
} else {
|
|
|
|
LOG(("CacheIndex::BuildIndex() - Added entry to index. [name=%s]",
|
|
|
|
leaf.get()));
|
|
|
|
entry->Log();
|
|
|
|
}
|
2014-02-18 19:05:07 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-18 08:43:11 +03:00
|
|
|
MOZ_ASSERT_UNREACHABLE("We should never get here");
|
2014-02-18 19:05:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CacheIndex::StartUpdatingIndexIfNeeded(bool aSwitchingToReadyState) {
|
|
|
|
// Start updating process when we are in or we are switching to READY state
|
2014-03-07 15:22:59 +04:00
|
|
|
// and index needs update, but not during shutdown or when removing all
|
|
|
|
// entries.
|
2014-02-18 19:05:07 +04:00
|
|
|
if ((mState == READY || aSwitchingToReadyState) && mIndexNeedsUpdate &&
|
2014-03-07 15:22:59 +04:00
|
|
|
!mShuttingDown && !mRemovingAll) {
|
2014-02-18 19:05:07 +04:00
|
|
|
LOG(("CacheIndex::StartUpdatingIndexIfNeeded() - starting update process"));
|
|
|
|
mIndexNeedsUpdate = false;
|
2014-03-07 15:22:59 +04:00
|
|
|
StartUpdatingIndex(false);
|
2014-02-18 19:05:07 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-03-07 15:22:59 +04:00
|
|
|
void CacheIndex::StartUpdatingIndex(bool aRebuild) {
|
|
|
|
LOG(("CacheIndex::StartUpdatingIndex() [rebuild=%d]", aRebuild));
|
|
|
|
|
2015-11-12 13:07:48 +03:00
|
|
|
sLock.AssertCurrentThreadOwns();
|
2014-02-18 19:05:07 +04:00
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
mIndexStats.Log();
|
|
|
|
|
2014-03-07 15:22:59 +04:00
|
|
|
ChangeState(aRebuild ? BUILDING : UPDATING);
|
2014-02-18 19:05:07 +04:00
|
|
|
mDontMarkIndexClean = false;
|
|
|
|
|
2014-03-07 15:22:59 +04:00
|
|
|
if (mShuttingDown || mRemovingAll) {
|
2014-02-18 19:05:07 +04:00
|
|
|
FinishUpdate(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-07 15:22:59 +04:00
|
|
|
if (IsUpdatePending()) {
|
|
|
|
LOG(("CacheIndex::StartUpdatingIndex() - Update is already pending"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-18 19:05:07 +04:00
|
|
|
uint32_t elapsed = (TimeStamp::NowLoRes() - mStartTime).ToMilliseconds();
|
|
|
|
if (elapsed < kUpdateIndexStartDelay) {
|
2014-03-28 17:51:58 +04:00
|
|
|
LOG(
|
|
|
|
("CacheIndex::StartUpdatingIndex() - %u ms elapsed since startup, "
|
|
|
|
"scheduling timer to fire in %u ms.",
|
|
|
|
elapsed, kUpdateIndexStartDelay - elapsed));
|
2014-03-07 15:22:59 +04:00
|
|
|
rv = ScheduleUpdateTimer(kUpdateIndexStartDelay - elapsed);
|
2014-02-18 19:05:07 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-07 15:22:59 +04:00
|
|
|
LOG(
|
|
|
|
("CacheIndex::StartUpdatingIndex() - ScheduleUpdateTimer() failed. "
|
|
|
|
"Starting update immediately."));
|
2014-03-28 17:51:58 +04:00
|
|
|
} else {
|
|
|
|
LOG(
|
|
|
|
("CacheIndex::StartUpdatingIndex() - %u ms elapsed since startup, "
|
|
|
|
"starting update now.",
|
|
|
|
elapsed));
|
2014-02-18 19:05:07 +04:00
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CacheIOThread> ioThread = CacheFileIOManager::IOThread();
|
2014-02-18 19:05:07 +04:00
|
|
|
MOZ_ASSERT(ioThread);
|
|
|
|
|
|
|
|
// We need to dispatch an event even if we are on IO thread since we need to
|
|
|
|
// update the index with the correct priority.
|
2014-03-07 15:22:59 +04:00
|
|
|
mUpdateEventPending = true;
|
|
|
|
rv = ioThread->Dispatch(this, CacheIOThread::INDEX);
|
2014-02-18 19:05:07 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
2014-03-07 15:22:59 +04:00
|
|
|
mUpdateEventPending = false;
|
2014-02-18 19:05:07 +04:00
|
|
|
NS_WARNING("CacheIndex::StartUpdatingIndex() - Can't dispatch event");
|
|
|
|
LOG(("CacheIndex::StartUpdatingIndex() - Can't dispatch event"));
|
|
|
|
FinishUpdate(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CacheIndex::UpdateIndex() {
|
|
|
|
LOG(("CacheIndex::UpdateIndex()"));
|
|
|
|
|
2015-11-12 13:07:48 +03:00
|
|
|
sLock.AssertCurrentThreadOwns();
|
2014-02-18 19:05:07 +04:00
|
|
|
|
|
|
|
MOZ_ASSERT(mPendingUpdates.Count() == 0);
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
if (!mDirEnumerator) {
|
|
|
|
{
|
|
|
|
// Do not do IO under the lock.
|
2015-11-12 13:07:48 +03:00
|
|
|
StaticMutexAutoUnlock unlock(sLock);
|
2014-02-18 19:05:07 +04:00
|
|
|
rv = SetupDirectoryEnumerator();
|
|
|
|
}
|
|
|
|
if (mState == SHUTDOWN) {
|
2014-03-07 15:22:59 +04:00
|
|
|
// The index was shut down while we released the lock. FinishUpdate() was
|
2014-02-18 19:05:07 +04:00
|
|
|
// already called from Shutdown(), so just simply return here.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
FinishUpdate(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while (true) {
|
2014-02-27 03:11:42 +04:00
|
|
|
if (CacheIOThread::YieldAndRerun()) {
|
|
|
|
LOG(
|
|
|
|
("CacheIndex::UpdateIndex() - Breaking loop for higher level "
|
|
|
|
"events."));
|
2014-03-07 15:22:59 +04:00
|
|
|
mUpdateEventPending = true;
|
2014-02-27 03:11:42 +04:00
|
|
|
return;
|
2014-02-18 19:05:07 +04:00
|
|
|
}
|
|
|
|
|
2017-08-11 13:30:59 +03:00
|
|
|
bool fileExists = false;
|
2014-02-18 19:05:07 +04:00
|
|
|
nsCOMPtr<nsIFile> file;
|
|
|
|
{
|
|
|
|
// Do not do IO under the lock.
|
2015-11-12 13:07:48 +03:00
|
|
|
StaticMutexAutoUnlock unlock(sLock);
|
2014-02-18 19:05:07 +04:00
|
|
|
rv = mDirEnumerator->GetNextFile(getter_AddRefs(file));
|
2017-08-11 13:30:59 +03:00
|
|
|
|
|
|
|
if (file) {
|
|
|
|
file->Exists(&fileExists);
|
|
|
|
}
|
2014-02-18 19:05:07 +04:00
|
|
|
}
|
|
|
|
if (mState == SHUTDOWN) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!file) {
|
|
|
|
FinishUpdate(NS_SUCCEEDED(rv));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoCString leaf;
|
|
|
|
rv = file->GetNativeLeafName(leaf);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
LOG(
|
|
|
|
("CacheIndex::UpdateIndex() - GetNativeLeafName() failed! Skipping "
|
|
|
|
"file."));
|
|
|
|
mDontMarkIndexClean = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-08-11 13:30:59 +03:00
|
|
|
if (!fileExists) {
|
|
|
|
LOG(
|
|
|
|
("CacheIndex::UpdateIndex() - File returned by the iterator was "
|
|
|
|
"removed in the meantime [name=%s]",
|
|
|
|
leaf.get()));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-02-18 19:05:07 +04:00
|
|
|
SHA1Sum::Hash hash;
|
|
|
|
rv = CacheFileIOManager::StrToHash(leaf, &hash);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
LOG(
|
|
|
|
("CacheIndex::UpdateIndex() - Filename is not a hash, removing file. "
|
|
|
|
"[name=%s]",
|
|
|
|
leaf.get()));
|
|
|
|
file->Remove(false);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
CacheIndexEntry* entry = mIndex.GetEntry(hash);
|
|
|
|
if (entry && entry->IsRemoved()) {
|
|
|
|
if (entry->IsFresh()) {
|
|
|
|
LOG(
|
|
|
|
("CacheIndex::UpdateIndex() - Found file that should not exist. "
|
|
|
|
"[name=%s]",
|
|
|
|
leaf.get()));
|
|
|
|
entry->Log();
|
|
|
|
}
|
|
|
|
entry = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CacheFileHandle> handle;
|
2015-02-24 19:49:46 +03:00
|
|
|
CacheFileIOManager::gInstance->mHandles.GetHandle(&hash,
|
2014-02-18 19:05:07 +04:00
|
|
|
getter_AddRefs(handle));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (entry && entry->IsFresh()) {
|
|
|
|
// the entry is up to date
|
|
|
|
LOG(
|
|
|
|
("CacheIndex::UpdateIndex() - Skipping file because the entry is up "
|
|
|
|
" to date. [name=%s]",
|
|
|
|
leaf.get()));
|
|
|
|
entry->Log();
|
|
|
|
// there must be an active CacheFile if the entry is not initialized
|
|
|
|
MOZ_ASSERT(entry->IsInitialized() || handle);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(!handle);
|
|
|
|
|
|
|
|
if (entry) {
|
|
|
|
PRTime lastModifiedTime;
|
|
|
|
{
|
|
|
|
// Do not do IO under the lock.
|
2015-11-12 13:07:48 +03:00
|
|
|
StaticMutexAutoUnlock unlock(sLock);
|
2014-02-18 19:05:07 +04:00
|
|
|
rv = file->GetLastModifiedTime(&lastModifiedTime);
|
|
|
|
}
|
|
|
|
if (mState == SHUTDOWN) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
LOG(
|
|
|
|
("CacheIndex::UpdateIndex() - Cannot get lastModifiedTime. "
|
|
|
|
"[name=%s]",
|
|
|
|
leaf.get()));
|
|
|
|
// Assume the file is newer than index
|
|
|
|
} else {
|
|
|
|
if (mIndexTimeStamp > (lastModifiedTime / PR_MSEC_PER_SEC)) {
|
|
|
|
LOG(
|
|
|
|
("CacheIndex::UpdateIndex() - Skipping file because of last "
|
2016-12-16 06:16:31 +03:00
|
|
|
"modified time. [name=%s, indexTimeStamp=%" PRIu32 ", "
|
|
|
|
"lastModifiedTime=%" PRId64 "]",
|
|
|
|
leaf.get(), mIndexTimeStamp,
|
2014-02-18 19:05:07 +04:00
|
|
|
lastModifiedTime / PR_MSEC_PER_SEC));
|
|
|
|
|
|
|
|
CacheIndexEntryAutoManage entryMng(&hash, this);
|
|
|
|
entry->MarkFresh();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CacheFileMetadata> meta = new CacheFileMetadata();
|
2014-02-18 19:05:07 +04:00
|
|
|
int64_t size = 0;
|
|
|
|
|
|
|
|
{
|
|
|
|
// Do not do IO under the lock.
|
2015-11-12 13:07:48 +03:00
|
|
|
StaticMutexAutoUnlock unlock(sLock);
|
2014-02-18 19:05:07 +04:00
|
|
|
rv = meta->SyncReadMetadata(file);
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = file->GetFileSize(&size);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
LOG(
|
|
|
|
("CacheIndex::UpdateIndex() - Cannot get filesize of file that "
|
|
|
|
"was successfully parsed. [name=%s]",
|
|
|
|
leaf.get()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (mState == SHUTDOWN) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nobody could add the entry while the lock was released since we modify
|
|
|
|
// the index only on IO thread and this loop is executed on IO thread too.
|
|
|
|
entry = mIndex.GetEntry(hash);
|
|
|
|
MOZ_ASSERT(!entry || !entry->IsFresh());
|
|
|
|
|
|
|
|
CacheIndexEntryAutoManage entryMng(&hash, this);
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
LOG(
|
|
|
|
("CacheIndex::UpdateIndex() - CacheFileMetadata::SyncReadMetadata() "
|
|
|
|
"failed, removing file. [name=%s]",
|
|
|
|
leaf.get()));
|
2017-03-06 13:21:59 +03:00
|
|
|
} else {
|
|
|
|
entry = mIndex.PutEntry(hash);
|
|
|
|
rv = InitEntryFromDiskData(entry, meta, size);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
LOG(
|
|
|
|
("CacheIndex::UpdateIndex() - CacheIndex::InitEntryFromDiskData "
|
|
|
|
"failed, removing file. [name=%s]",
|
|
|
|
leaf.get()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
2014-02-18 19:05:07 +04:00
|
|
|
file->Remove(false);
|
|
|
|
if (entry) {
|
|
|
|
entry->MarkRemoved();
|
|
|
|
entry->MarkFresh();
|
|
|
|
entry->MarkDirty();
|
|
|
|
}
|
|
|
|
} else {
|
2017-03-06 13:21:59 +03:00
|
|
|
LOG(
|
|
|
|
("CacheIndex::UpdateIndex() - Added/updated entry to/in index. "
|
|
|
|
"[name=%s]",
|
|
|
|
leaf.get()));
|
|
|
|
entry->Log();
|
2014-02-18 19:05:07 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-18 08:43:11 +03:00
|
|
|
MOZ_ASSERT_UNREACHABLE("We should never get here");
|
2014-02-18 19:05:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void CacheIndex::FinishUpdate(bool aSucceeded) {
|
|
|
|
LOG(("CacheIndex::FinishUpdate() [succeeded=%d]", aSucceeded));
|
|
|
|
|
2014-03-07 15:22:59 +04:00
|
|
|
MOZ_ASSERT(mState == UPDATING || mState == BUILDING ||
|
|
|
|
(!aSucceeded && mState == SHUTDOWN));
|
2014-02-18 19:05:07 +04:00
|
|
|
|
2015-11-12 13:07:48 +03:00
|
|
|
sLock.AssertCurrentThreadOwns();
|
2014-02-18 19:05:07 +04:00
|
|
|
|
|
|
|
if (mDirEnumerator) {
|
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
LOG(
|
|
|
|
("CacheIndex::FinishUpdate() - posting of PreShutdownInternal failed?"
|
|
|
|
" Cannot safely release mDirEnumerator, leaking it!"));
|
|
|
|
NS_WARNING(("CacheIndex::FinishUpdate() - Leaking mDirEnumerator!"));
|
|
|
|
// This can happen only in case dispatching event to IO thread failed in
|
|
|
|
// CacheIndex::PreShutdown().
|
2015-11-02 08:53:26 +03:00
|
|
|
Unused << mDirEnumerator.forget(); // Leak it since dir enumerator is not
|
|
|
|
// threadsafe
|
2014-02-18 19:05:07 +04:00
|
|
|
} else {
|
|
|
|
mDirEnumerator->Close();
|
|
|
|
mDirEnumerator = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!aSucceeded) {
|
|
|
|
mDontMarkIndexClean = true;
|
|
|
|
}
|
|
|
|
|
2014-03-07 15:22:59 +04:00
|
|
|
if (mState == SHUTDOWN) {
|
|
|
|
return;
|
|
|
|
}
|
2014-02-18 19:05:07 +04:00
|
|
|
|
2014-03-07 15:22:59 +04:00
|
|
|
if (mState == UPDATING && aSucceeded) {
|
|
|
|
// If we've iterated over all entries successfully then all entries that
|
|
|
|
// really exist on the disk are now marked as fresh. All non-fresh entries
|
|
|
|
// don't exist anymore and must be removed from the index.
|
2015-07-27 06:35:31 +03:00
|
|
|
RemoveNonFreshEntries();
|
2014-02-18 19:05:07 +04:00
|
|
|
}
|
2014-03-07 15:22:59 +04:00
|
|
|
|
|
|
|
// Make sure we won't start update. If the build or update failed, there is no
|
|
|
|
// reason to believe that it will succeed next time.
|
|
|
|
mIndexNeedsUpdate = false;
|
|
|
|
|
|
|
|
ChangeState(READY);
|
|
|
|
mLastDumpTime = TimeStamp::NowLoRes(); // Do not dump new index immediately
|
2014-02-18 19:05:07 +04:00
|
|
|
}
|
|
|
|
|
2015-07-27 06:35:31 +03:00
|
|
|
void CacheIndex::RemoveNonFreshEntries() {
|
|
|
|
for (auto iter = mIndex.Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
CacheIndexEntry* entry = iter.Get();
|
|
|
|
if (entry->IsFresh()) {
|
|
|
|
continue;
|
|
|
|
}
|
2014-02-18 19:05:07 +04:00
|
|
|
|
2015-07-27 06:35:31 +03:00
|
|
|
LOG(
|
|
|
|
("CacheIndex::RemoveNonFreshEntries() - Removing entry. "
|
|
|
|
"[hash=%08x%08x%08x%08x%08x]",
|
|
|
|
LOGSHA1(entry->Hash())));
|
2014-02-18 19:05:07 +04:00
|
|
|
|
2015-07-27 06:35:31 +03:00
|
|
|
{
|
|
|
|
CacheIndexEntryAutoManage emng(entry->Hash(), this);
|
|
|
|
emng.DoNotSearchInIndex();
|
|
|
|
}
|
2015-08-07 02:30:47 +03:00
|
|
|
|
2015-07-27 06:35:31 +03:00
|
|
|
iter.Remove();
|
|
|
|
}
|
2014-02-18 19:05:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
char const* CacheIndex::StateString(EState aState) {
|
|
|
|
switch (aState) {
|
|
|
|
case INITIAL:
|
|
|
|
return "INITIAL";
|
|
|
|
case READING:
|
|
|
|
return "READING";
|
|
|
|
case WRITING:
|
|
|
|
return "WRITING";
|
|
|
|
case BUILDING:
|
|
|
|
return "BUILDING";
|
|
|
|
case UPDATING:
|
|
|
|
return "UPDATING";
|
|
|
|
case READY:
|
|
|
|
return "READY";
|
|
|
|
case SHUTDOWN:
|
|
|
|
return "SHUTDOWN";
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(false, "Unexpected state!");
|
|
|
|
return "?";
|
|
|
|
}
|
|
|
|
|
|
|
|
void CacheIndex::ChangeState(EState aNewState) {
|
|
|
|
LOG(("CacheIndex::ChangeState() changing state %s -> %s", StateString(mState),
|
|
|
|
StateString(aNewState)));
|
|
|
|
|
|
|
|
// All pending updates should be processed before changing state
|
|
|
|
MOZ_ASSERT(mPendingUpdates.Count() == 0);
|
|
|
|
|
|
|
|
// PreShutdownInternal() should change the state to READY from every state. It
|
|
|
|
// may go through different states, but once we are in READY state the only
|
|
|
|
// possible transition is to SHUTDOWN state.
|
|
|
|
MOZ_ASSERT(!mShuttingDown || mState != READY || aNewState == SHUTDOWN);
|
|
|
|
|
|
|
|
// Start updating process when switching to READY state if needed
|
|
|
|
if (aNewState == READY && StartUpdatingIndexIfNeeded(true)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-06-14 15:13:18 +03:00
|
|
|
if ((mState == READING || mState == BUILDING || mState == UPDATING) &&
|
|
|
|
aNewState == READY) {
|
|
|
|
ReportHashStats();
|
|
|
|
}
|
|
|
|
|
2014-02-18 21:26:48 +04:00
|
|
|
// Try to evict entries over limit everytime we're leaving state READING,
|
2014-03-07 15:22:59 +04:00
|
|
|
// BUILDING or UPDATING, but not during shutdown or when removing all
|
|
|
|
// entries.
|
|
|
|
if (!mShuttingDown && !mRemovingAll && aNewState != SHUTDOWN &&
|
2014-02-18 21:26:48 +04:00
|
|
|
(mState == READING || mState == BUILDING || mState == UPDATING)) {
|
|
|
|
CacheFileIOManager::EvictIfOverLimit();
|
|
|
|
}
|
|
|
|
|
2014-02-18 19:05:07 +04:00
|
|
|
mState = aNewState;
|
2014-04-06 22:44:40 +04:00
|
|
|
|
2014-04-10 14:47:20 +04:00
|
|
|
if (mState != SHUTDOWN) {
|
|
|
|
CacheFileIOManager::CacheIndexStateChanged();
|
|
|
|
}
|
|
|
|
|
2016-04-05 06:59:00 +03:00
|
|
|
NotifyAsyncGetDiskConsumptionCallbacks();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CacheIndex::NotifyAsyncGetDiskConsumptionCallbacks() {
|
2016-05-12 14:57:00 +03:00
|
|
|
if ((mState == READY || mState == WRITING) &&
|
|
|
|
!mAsyncGetDiskConsumptionBlocked && mDiskConsumptionObservers.Length()) {
|
2014-04-06 22:44:40 +04:00
|
|
|
for (uint32_t i = 0; i < mDiskConsumptionObservers.Length(); ++i) {
|
|
|
|
DiskConsumptionObserver* o = mDiskConsumptionObservers[i];
|
|
|
|
// Safe to call under the lock. We always post to the main thread.
|
|
|
|
o->OnDiskConsumption(mIndexStats.Size() << 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
mDiskConsumptionObservers.Clear();
|
|
|
|
}
|
2014-02-18 19:05:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void CacheIndex::AllocBuffer() {
|
|
|
|
switch (mState) {
|
|
|
|
case WRITING:
|
|
|
|
mRWBufSize = sizeof(CacheIndexHeader) + sizeof(CacheHash::Hash32_t) +
|
|
|
|
mProcessEntries * sizeof(CacheIndexRecord);
|
|
|
|
if (mRWBufSize > kMaxBufSize) {
|
|
|
|
mRWBufSize = kMaxBufSize;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case READING:
|
|
|
|
mRWBufSize = kMaxBufSize;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
MOZ_ASSERT(false, "Unexpected state!");
|
|
|
|
}
|
|
|
|
|
|
|
|
mRWBuf = static_cast<char*>(moz_xmalloc(mRWBufSize));
|
|
|
|
}
|
|
|
|
|
|
|
|
void CacheIndex::ReleaseBuffer() {
|
2016-05-11 11:48:28 +03:00
|
|
|
sLock.AssertCurrentThreadOwns();
|
|
|
|
|
|
|
|
if (!mRWBuf || mRWPending) {
|
2014-02-18 19:05:07 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-11 11:48:28 +03:00
|
|
|
LOG(("CacheIndex::ReleaseBuffer() releasing buffer"));
|
|
|
|
|
2014-02-18 19:05:07 +04:00
|
|
|
free(mRWBuf);
|
|
|
|
mRWBuf = nullptr;
|
|
|
|
mRWBufSize = 0;
|
|
|
|
mRWBufPos = 0;
|
|
|
|
}
|
|
|
|
|
2016-10-06 12:23:52 +03:00
|
|
|
void CacheIndex::FrecencyArray::AppendRecord(CacheIndexRecord* aRecord) {
|
|
|
|
LOG(
|
|
|
|
("CacheIndex::FrecencyArray::AppendRecord() [record=%p, hash=%08x%08x%08x"
|
2014-02-18 19:05:07 +04:00
|
|
|
"%08x%08x]",
|
|
|
|
aRecord, LOGSHA1(aRecord->mHash)));
|
|
|
|
|
2016-10-06 12:23:52 +03:00
|
|
|
MOZ_ASSERT(!mRecs.Contains(aRecord));
|
|
|
|
mRecs.AppendElement(aRecord);
|
|
|
|
|
|
|
|
// If the new frecency is 0, the element should be at the end of the array,
|
|
|
|
// i.e. this change doesn't affect order of the array
|
|
|
|
if (aRecord->mFrecency != 0) {
|
|
|
|
++mUnsortedElements;
|
|
|
|
}
|
2014-02-18 19:05:07 +04:00
|
|
|
}
|
|
|
|
|
2016-10-06 12:23:52 +03:00
|
|
|
void CacheIndex::FrecencyArray::RemoveRecord(CacheIndexRecord* aRecord) {
|
|
|
|
LOG(("CacheIndex::FrecencyArray::RemoveRecord() [record=%p]", aRecord));
|
2014-02-18 19:05:07 +04:00
|
|
|
|
2016-10-06 12:23:52 +03:00
|
|
|
decltype(mRecs)::index_type idx;
|
|
|
|
idx = mRecs.IndexOf(aRecord);
|
|
|
|
MOZ_RELEASE_ASSERT(idx != mRecs.NoIndex);
|
|
|
|
mRecs[idx] = nullptr;
|
|
|
|
++mRemovedElements;
|
|
|
|
|
|
|
|
// Calling SortIfNeeded ensures that we get rid of removed elements in the
|
|
|
|
// array once we hit the limit.
|
|
|
|
SortIfNeeded();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CacheIndex::FrecencyArray::ReplaceRecord(CacheIndexRecord* aOldRecord,
|
|
|
|
CacheIndexRecord* aNewRecord) {
|
|
|
|
LOG(
|
|
|
|
("CacheIndex::FrecencyArray::ReplaceRecord() [oldRecord=%p, "
|
|
|
|
"newRecord=%p]",
|
|
|
|
aOldRecord, aNewRecord));
|
|
|
|
|
|
|
|
decltype(mRecs)::index_type idx;
|
|
|
|
idx = mRecs.IndexOf(aOldRecord);
|
|
|
|
MOZ_RELEASE_ASSERT(idx != mRecs.NoIndex);
|
|
|
|
mRecs[idx] = aNewRecord;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CacheIndex::FrecencyArray::SortIfNeeded() {
|
|
|
|
const uint32_t kMaxUnsortedCount = 512;
|
|
|
|
const uint32_t kMaxUnsortedPercent = 10;
|
|
|
|
const uint32_t kMaxRemovedCount = 512;
|
|
|
|
|
|
|
|
uint32_t unsortedLimit = std::min<uint32_t>(
|
|
|
|
kMaxUnsortedCount, Length() * kMaxUnsortedPercent / 100);
|
|
|
|
|
|
|
|
if (mUnsortedElements > unsortedLimit ||
|
|
|
|
mRemovedElements > kMaxRemovedCount) {
|
|
|
|
LOG(
|
|
|
|
("CacheIndex::FrecencyArray::SortIfNeeded() - Sorting array "
|
|
|
|
"[unsortedElements=%u, unsortedLimit=%u, removedElements=%u, "
|
|
|
|
"maxRemovedCount=%u]",
|
|
|
|
mUnsortedElements, unsortedLimit, mRemovedElements, kMaxRemovedCount));
|
|
|
|
|
|
|
|
mRecs.Sort(FrecencyComparator());
|
|
|
|
mUnsortedElements = 0;
|
|
|
|
if (mRemovedElements) {
|
|
|
|
#ifdef DEBUG
|
|
|
|
for (uint32_t i = Length(); i < mRecs.Length(); ++i) {
|
|
|
|
MOZ_ASSERT(!mRecs[i]);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
// Removed elements are at the end after sorting.
|
|
|
|
mRecs.RemoveElementsAt(Length(), mRemovedElements);
|
|
|
|
mRemovedElements = 0;
|
|
|
|
}
|
|
|
|
}
|
2014-02-18 19:05:07 +04:00
|
|
|
}
|
|
|
|
|
2014-04-10 14:47:20 +04:00
|
|
|
void CacheIndex::AddRecordToIterators(CacheIndexRecord* aRecord) {
|
2015-11-12 13:07:48 +03:00
|
|
|
sLock.AssertCurrentThreadOwns();
|
2014-04-10 14:47:20 +04:00
|
|
|
|
|
|
|
for (uint32_t i = 0; i < mIterators.Length(); ++i) {
|
|
|
|
// Add a new record only when iterator is supposed to be updated.
|
|
|
|
if (mIterators[i]->ShouldBeNewAdded()) {
|
|
|
|
mIterators[i]->AddRecord(aRecord);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CacheIndex::RemoveRecordFromIterators(CacheIndexRecord* aRecord) {
|
2015-11-12 13:07:48 +03:00
|
|
|
sLock.AssertCurrentThreadOwns();
|
2014-04-10 14:47:20 +04:00
|
|
|
|
|
|
|
for (uint32_t i = 0; i < mIterators.Length(); ++i) {
|
|
|
|
// Remove the record from iterator always, it makes no sence to return
|
|
|
|
// non-existing entries. Also the pointer to the record is no longer valid
|
|
|
|
// once the entry is removed from index.
|
|
|
|
mIterators[i]->RemoveRecord(aRecord);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CacheIndex::ReplaceRecordInIterators(CacheIndexRecord* aOldRecord,
|
|
|
|
CacheIndexRecord* aNewRecord) {
|
2015-11-12 13:07:48 +03:00
|
|
|
sLock.AssertCurrentThreadOwns();
|
2014-04-10 14:47:20 +04:00
|
|
|
|
|
|
|
for (uint32_t i = 0; i < mIterators.Length(); ++i) {
|
|
|
|
// We have to replace the record always since the pointer is no longer
|
|
|
|
// valid after this point. NOTE: Replacing the record doesn't mean that
|
|
|
|
// a new entry was added, it just means that the data in the entry was
|
|
|
|
// changed (e.g. a file size) and we had to track this change in
|
|
|
|
// mPendingUpdates since mIndex was read-only.
|
|
|
|
mIterators[i]->ReplaceRecord(aOldRecord, aNewRecord);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-18 19:05:07 +04:00
|
|
|
nsresult CacheIndex::Run() {
|
|
|
|
LOG(("CacheIndex::Run()"));
|
|
|
|
|
2015-11-12 13:07:48 +03:00
|
|
|
StaticMutexAutoLock lock(sLock);
|
2014-02-18 19:05:07 +04:00
|
|
|
|
|
|
|
if (!IsIndexUsable()) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mState == READY && mShuttingDown) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-03-07 15:22:59 +04:00
|
|
|
mUpdateEventPending = false;
|
|
|
|
|
2014-02-18 19:05:07 +04:00
|
|
|
switch (mState) {
|
|
|
|
case BUILDING:
|
|
|
|
BuildIndex();
|
|
|
|
break;
|
|
|
|
case UPDATING:
|
|
|
|
UpdateIndex();
|
|
|
|
break;
|
|
|
|
default:
|
2014-03-07 15:22:59 +04:00
|
|
|
LOG(("CacheIndex::Run() - Update/Build was canceled"));
|
2014-02-18 19:05:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-12-02 16:21:06 +03:00
|
|
|
void CacheIndex::OnFileOpenedInternal(FileOpenHelper* aOpener,
|
|
|
|
CacheFileHandle* aHandle,
|
|
|
|
nsresult aResult) {
|
2014-03-07 15:22:59 +04:00
|
|
|
LOG(
|
|
|
|
("CacheIndex::OnFileOpenedInternal() [opener=%p, handle=%p, "
|
2016-12-16 06:16:31 +03:00
|
|
|
"result=0x%08" PRIx32 "]",
|
|
|
|
aOpener, aHandle, static_cast<uint32_t>(aResult)));
|
2016-05-11 11:48:28 +03:00
|
|
|
MOZ_ASSERT(CacheFileIOManager::IsOnIOThread());
|
|
|
|
|
2014-02-18 19:05:07 +04:00
|
|
|
nsresult rv;
|
|
|
|
|
2015-11-12 13:07:48 +03:00
|
|
|
sLock.AssertCurrentThreadOwns();
|
2014-02-18 19:05:07 +04:00
|
|
|
|
2016-05-11 11:48:28 +03:00
|
|
|
MOZ_RELEASE_ASSERT(IsIndexUsable());
|
2014-02-18 19:05:07 +04:00
|
|
|
|
|
|
|
if (mState == READY && mShuttingDown) {
|
2019-12-02 16:21:06 +03:00
|
|
|
return;
|
2014-02-18 19:05:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (mState) {
|
|
|
|
case WRITING:
|
2014-03-07 15:22:59 +04:00
|
|
|
MOZ_ASSERT(aOpener == mIndexFileOpener);
|
|
|
|
mIndexFileOpener = nullptr;
|
|
|
|
|
2014-02-18 19:05:07 +04:00
|
|
|
if (NS_FAILED(aResult)) {
|
2014-03-07 15:22:59 +04:00
|
|
|
LOG(
|
|
|
|
("CacheIndex::OnFileOpenedInternal() - Can't open index file for "
|
2016-12-16 06:16:31 +03:00
|
|
|
"writing [rv=0x%08" PRIx32 "]",
|
|
|
|
static_cast<uint32_t>(aResult)));
|
2014-02-18 19:05:07 +04:00
|
|
|
FinishWrite(false);
|
|
|
|
} else {
|
|
|
|
mIndexHandle = aHandle;
|
|
|
|
WriteRecords();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case READING:
|
2014-03-07 15:22:59 +04:00
|
|
|
if (aOpener == mIndexFileOpener) {
|
|
|
|
mIndexFileOpener = nullptr;
|
2014-02-18 19:05:07 +04:00
|
|
|
|
|
|
|
if (NS_SUCCEEDED(aResult)) {
|
2014-03-07 15:22:59 +04:00
|
|
|
if (aHandle->FileSize() == 0) {
|
|
|
|
FinishRead(false);
|
|
|
|
CacheFileIOManager::DoomFile(aHandle, nullptr);
|
|
|
|
break;
|
|
|
|
}
|
2018-04-30 19:46:04 +03:00
|
|
|
mIndexHandle = aHandle;
|
2014-03-07 15:22:59 +04:00
|
|
|
} else {
|
2014-02-18 19:05:07 +04:00
|
|
|
FinishRead(false);
|
2014-03-07 15:22:59 +04:00
|
|
|
break;
|
2014-02-18 19:05:07 +04:00
|
|
|
}
|
2014-03-07 15:22:59 +04:00
|
|
|
} else if (aOpener == mJournalFileOpener) {
|
|
|
|
mJournalFileOpener = nullptr;
|
|
|
|
mJournalHandle = aHandle;
|
|
|
|
} else if (aOpener == mTmpFileOpener) {
|
|
|
|
mTmpFileOpener = nullptr;
|
|
|
|
mTmpHandle = aHandle;
|
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(false, "Unexpected state!");
|
|
|
|
}
|
2014-02-18 19:05:07 +04:00
|
|
|
|
2014-03-07 15:22:59 +04:00
|
|
|
if (mIndexFileOpener || mJournalFileOpener || mTmpFileOpener) {
|
|
|
|
// Some opener still didn't finish
|
|
|
|
break;
|
2014-02-18 19:05:07 +04:00
|
|
|
}
|
|
|
|
|
2014-03-07 15:22:59 +04:00
|
|
|
// We fail and cancel all other openers when we opening index file fails.
|
|
|
|
MOZ_ASSERT(mIndexHandle);
|
2014-02-18 19:05:07 +04:00
|
|
|
|
2014-03-07 15:22:59 +04:00
|
|
|
if (mTmpHandle) {
|
|
|
|
CacheFileIOManager::DoomFile(mTmpHandle, nullptr);
|
|
|
|
mTmpHandle = nullptr;
|
2014-02-18 19:05:07 +04:00
|
|
|
|
2014-03-07 15:22:59 +04:00
|
|
|
if (mJournalHandle) { // this shouldn't normally happen
|
|
|
|
LOG(
|
|
|
|
("CacheIndex::OnFileOpenedInternal() - Unexpected state, all "
|
|
|
|
"files [%s, %s, %s] should never exist. Removing whole index.",
|
2015-05-16 11:07:10 +03:00
|
|
|
INDEX_NAME, JOURNAL_NAME, TEMP_INDEX_NAME));
|
2014-03-07 15:22:59 +04:00
|
|
|
FinishRead(false);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-02-18 19:05:07 +04:00
|
|
|
|
2014-03-07 15:22:59 +04:00
|
|
|
if (mJournalHandle) {
|
|
|
|
// Rename journal to make sure we update index on next start in case
|
|
|
|
// firefox crashes
|
|
|
|
rv = CacheFileIOManager::RenameFile(
|
2015-05-16 11:07:10 +03:00
|
|
|
mJournalHandle, NS_LITERAL_CSTRING(TEMP_INDEX_NAME), this);
|
2014-03-07 15:22:59 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
LOG(
|
|
|
|
("CacheIndex::OnFileOpenedInternal() - CacheFileIOManager::"
|
2016-12-16 06:16:31 +03:00
|
|
|
"RenameFile() failed synchronously [rv=0x%08" PRIx32 "]",
|
|
|
|
static_cast<uint32_t>(rv)));
|
2014-03-07 15:22:59 +04:00
|
|
|
FinishRead(false);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
StartReadingIndex();
|
2014-02-18 19:05:07 +04:00
|
|
|
}
|
2014-03-07 15:22:59 +04:00
|
|
|
|
2014-02-18 19:05:07 +04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
MOZ_ASSERT(false, "Unexpected state!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-07 15:22:59 +04:00
|
|
|
nsresult CacheIndex::OnFileOpened(CacheFileHandle* aHandle, nsresult aResult) {
|
|
|
|
MOZ_CRASH("CacheIndex::OnFileOpened should not be called!");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
2014-02-18 19:05:07 +04:00
|
|
|
nsresult CacheIndex::OnDataWritten(CacheFileHandle* aHandle, const char* aBuf,
|
|
|
|
nsresult aResult) {
|
2016-12-16 06:16:31 +03:00
|
|
|
LOG(("CacheIndex::OnDataWritten() [handle=%p, result=0x%08" PRIx32 "]",
|
|
|
|
aHandle, static_cast<uint32_t>(aResult)));
|
2014-02-18 19:05:07 +04:00
|
|
|
|
2016-05-11 11:48:28 +03:00
|
|
|
MOZ_ASSERT(CacheFileIOManager::IsOnIOThread());
|
|
|
|
|
2014-02-18 19:05:07 +04:00
|
|
|
nsresult rv;
|
|
|
|
|
2015-11-12 13:07:48 +03:00
|
|
|
StaticMutexAutoLock lock(sLock);
|
2014-02-18 19:05:07 +04:00
|
|
|
|
2016-05-11 11:48:28 +03:00
|
|
|
MOZ_RELEASE_ASSERT(IsIndexUsable());
|
|
|
|
MOZ_RELEASE_ASSERT(mRWPending);
|
|
|
|
mRWPending = false;
|
2014-02-18 19:05:07 +04:00
|
|
|
|
|
|
|
if (mState == READY && mShuttingDown) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (mState) {
|
|
|
|
case WRITING:
|
2016-05-11 11:48:28 +03:00
|
|
|
MOZ_ASSERT(mIndexHandle == aHandle);
|
2014-03-07 15:22:59 +04:00
|
|
|
|
2014-02-18 19:05:07 +04:00
|
|
|
if (NS_FAILED(aResult)) {
|
|
|
|
FinishWrite(false);
|
|
|
|
} else {
|
|
|
|
if (mSkipEntries == mProcessEntries) {
|
|
|
|
rv = CacheFileIOManager::RenameFile(
|
2015-05-16 11:07:10 +03:00
|
|
|
mIndexHandle, NS_LITERAL_CSTRING(INDEX_NAME), this);
|
2014-02-18 19:05:07 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
LOG(
|
|
|
|
("CacheIndex::OnDataWritten() - CacheFileIOManager::"
|
2016-12-16 06:16:31 +03:00
|
|
|
"RenameFile() failed synchronously [rv=0x%08" PRIx32 "]",
|
|
|
|
static_cast<uint32_t>(rv)));
|
2014-02-18 19:05:07 +04:00
|
|
|
FinishWrite(false);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
WriteRecords();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2014-03-07 15:22:59 +04:00
|
|
|
// Writing was canceled.
|
|
|
|
LOG(
|
|
|
|
("CacheIndex::OnDataWritten() - ignoring notification since the "
|
|
|
|
"operation was previously canceled [state=%d]",
|
|
|
|
mState));
|
2016-05-11 11:48:28 +03:00
|
|
|
ReleaseBuffer();
|
2014-02-18 19:05:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult CacheIndex::OnDataRead(CacheFileHandle* aHandle, char* aBuf,
|
|
|
|
nsresult aResult) {
|
2016-12-16 06:16:31 +03:00
|
|
|
LOG(("CacheIndex::OnDataRead() [handle=%p, result=0x%08" PRIx32 "]", aHandle,
|
|
|
|
static_cast<uint32_t>(aResult)));
|
2014-02-18 19:05:07 +04:00
|
|
|
|
2016-05-11 11:48:28 +03:00
|
|
|
MOZ_ASSERT(CacheFileIOManager::IsOnIOThread());
|
|
|
|
|
2015-11-12 13:07:48 +03:00
|
|
|
StaticMutexAutoLock lock(sLock);
|
2014-02-18 19:05:07 +04:00
|
|
|
|
2016-05-11 11:48:28 +03:00
|
|
|
MOZ_RELEASE_ASSERT(IsIndexUsable());
|
|
|
|
MOZ_RELEASE_ASSERT(mRWPending);
|
|
|
|
mRWPending = false;
|
2014-02-18 19:05:07 +04:00
|
|
|
|
|
|
|
switch (mState) {
|
|
|
|
case READING:
|
2014-03-07 15:22:59 +04:00
|
|
|
MOZ_ASSERT(mIndexHandle == aHandle || mJournalHandle == aHandle);
|
|
|
|
|
2014-02-18 19:05:07 +04:00
|
|
|
if (NS_FAILED(aResult)) {
|
|
|
|
FinishRead(false);
|
|
|
|
} else {
|
|
|
|
if (!mIndexOnDiskIsValid) {
|
|
|
|
ParseRecords();
|
|
|
|
} else {
|
|
|
|
ParseJournal();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2014-03-07 15:22:59 +04:00
|
|
|
// Reading was canceled.
|
|
|
|
LOG(
|
|
|
|
("CacheIndex::OnDataRead() - ignoring notification since the "
|
|
|
|
"operation was previously canceled [state=%d]",
|
|
|
|
mState));
|
2016-05-11 11:48:28 +03:00
|
|
|
ReleaseBuffer();
|
2014-02-18 19:05:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult CacheIndex::OnFileDoomed(CacheFileHandle* aHandle, nsresult aResult) {
|
|
|
|
MOZ_CRASH("CacheIndex::OnFileDoomed should not be called!");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult CacheIndex::OnEOFSet(CacheFileHandle* aHandle, nsresult aResult) {
|
|
|
|
MOZ_CRASH("CacheIndex::OnEOFSet should not be called!");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult CacheIndex::OnFileRenamed(CacheFileHandle* aHandle, nsresult aResult) {
|
2016-12-16 06:16:31 +03:00
|
|
|
LOG(("CacheIndex::OnFileRenamed() [handle=%p, result=0x%08" PRIx32 "]",
|
|
|
|
aHandle, static_cast<uint32_t>(aResult)));
|
2014-02-18 19:05:07 +04:00
|
|
|
|
2016-05-11 11:48:28 +03:00
|
|
|
MOZ_ASSERT(CacheFileIOManager::IsOnIOThread());
|
|
|
|
|
2015-11-12 13:07:48 +03:00
|
|
|
StaticMutexAutoLock lock(sLock);
|
2014-02-18 19:05:07 +04:00
|
|
|
|
2016-05-11 11:48:28 +03:00
|
|
|
MOZ_RELEASE_ASSERT(IsIndexUsable());
|
2014-02-18 19:05:07 +04:00
|
|
|
|
|
|
|
if (mState == READY && mShuttingDown) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (mState) {
|
|
|
|
case WRITING:
|
|
|
|
// This is a result of renaming the new index written to tmpfile to index
|
|
|
|
// file. This is the last step when writing the index and the whole
|
|
|
|
// writing process is successful iff renaming was successful.
|
2014-03-07 15:22:59 +04:00
|
|
|
|
|
|
|
if (mIndexHandle != aHandle) {
|
|
|
|
LOG(
|
|
|
|
("CacheIndex::OnFileRenamed() - ignoring notification since it "
|
|
|
|
"belongs to previously canceled operation [state=%d]",
|
|
|
|
mState));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-02-18 19:05:07 +04:00
|
|
|
FinishWrite(NS_SUCCEEDED(aResult));
|
|
|
|
break;
|
|
|
|
case READING:
|
|
|
|
// This is a result of renaming journal file to tmpfile. It is renamed
|
|
|
|
// before we start reading index and journal file and it should normally
|
|
|
|
// succeed. If it fails give up reading of index.
|
2014-03-07 15:22:59 +04:00
|
|
|
|
|
|
|
if (mJournalHandle != aHandle) {
|
|
|
|
LOG(
|
|
|
|
("CacheIndex::OnFileRenamed() - ignoring notification since it "
|
|
|
|
"belongs to previously canceled operation [state=%d]",
|
|
|
|
mState));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-02-18 19:05:07 +04:00
|
|
|
if (NS_FAILED(aResult)) {
|
|
|
|
FinishRead(false);
|
|
|
|
} else {
|
|
|
|
StartReadingIndex();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2014-03-07 15:22:59 +04:00
|
|
|
// Reading/writing was canceled.
|
|
|
|
LOG(
|
|
|
|
("CacheIndex::OnFileRenamed() - ignoring notification since the "
|
|
|
|
"operation was previously canceled [state=%d]",
|
|
|
|
mState));
|
2014-02-18 19:05:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-02-27 03:11:40 +04:00
|
|
|
// Memory reporting
|
|
|
|
|
|
|
|
size_t CacheIndex::SizeOfExcludingThisInternal(
|
|
|
|
mozilla::MallocSizeOf mallocSizeOf) const {
|
2015-11-12 13:07:48 +03:00
|
|
|
sLock.AssertCurrentThreadOwns();
|
2014-02-27 03:11:40 +04:00
|
|
|
|
|
|
|
size_t n = 0;
|
|
|
|
nsCOMPtr<nsISizeOf> sizeOf;
|
|
|
|
|
|
|
|
// mIndexHandle and mJournalHandle are reported via SizeOfHandlesRunnable
|
|
|
|
// in CacheFileIOManager::SizeOfExcludingThisInternal as part of special
|
|
|
|
// handles array.
|
|
|
|
|
|
|
|
sizeOf = do_QueryInterface(mCacheDirectory);
|
|
|
|
if (sizeOf) {
|
|
|
|
n += sizeOf->SizeOfIncludingThis(mallocSizeOf);
|
|
|
|
}
|
|
|
|
|
2014-03-07 15:22:59 +04:00
|
|
|
sizeOf = do_QueryInterface(mUpdateTimer);
|
2014-02-27 03:11:40 +04:00
|
|
|
if (sizeOf) {
|
|
|
|
n += sizeOf->SizeOfIncludingThis(mallocSizeOf);
|
|
|
|
}
|
|
|
|
|
|
|
|
n += mallocSizeOf(mRWBuf);
|
|
|
|
n += mallocSizeOf(mRWHash);
|
|
|
|
|
2014-08-06 00:27:41 +04:00
|
|
|
n += mIndex.SizeOfExcludingThis(mallocSizeOf);
|
|
|
|
n += mPendingUpdates.SizeOfExcludingThis(mallocSizeOf);
|
|
|
|
n += mTmpJournal.SizeOfExcludingThis(mallocSizeOf);
|
2014-02-27 03:11:40 +04:00
|
|
|
|
2015-03-03 09:58:00 +03:00
|
|
|
// mFrecencyArray items are reported by mIndex/mPendingUpdates
|
2016-10-06 12:23:52 +03:00
|
|
|
n += mFrecencyArray.mRecs.ShallowSizeOfExcludingThis(mallocSizeOf);
|
2015-07-29 09:24:24 +03:00
|
|
|
n += mDiskConsumptionObservers.ShallowSizeOfExcludingThis(mallocSizeOf);
|
2014-02-27 03:11:40 +04:00
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
size_t CacheIndex::SizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) {
|
2015-11-12 13:07:48 +03:00
|
|
|
sLock.AssertCurrentThreadOwns();
|
|
|
|
|
2014-02-27 03:11:40 +04:00
|
|
|
if (!gInstance) return 0;
|
|
|
|
|
|
|
|
return gInstance->SizeOfExcludingThisInternal(mallocSizeOf);
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
size_t CacheIndex::SizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf) {
|
2015-11-12 13:07:48 +03:00
|
|
|
StaticMutexAutoLock lock(sLock);
|
|
|
|
|
2014-02-27 03:11:40 +04:00
|
|
|
return mallocSizeOf(gInstance) + SizeOfExcludingThis(mallocSizeOf);
|
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
namespace {
|
2015-06-14 15:13:18 +03:00
|
|
|
|
|
|
|
class HashComparator {
|
|
|
|
public:
|
|
|
|
bool Equals(CacheIndexRecord* a, CacheIndexRecord* b) const {
|
|
|
|
return memcmp(&a->mHash, &b->mHash, sizeof(SHA1Sum::Hash)) == 0;
|
|
|
|
}
|
|
|
|
bool LessThan(CacheIndexRecord* a, CacheIndexRecord* b) const {
|
|
|
|
return memcmp(&a->mHash, &b->mHash, sizeof(SHA1Sum::Hash)) < 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void ReportHashSizeMatch(const SHA1Sum::Hash* aHash1,
|
|
|
|
const SHA1Sum::Hash* aHash2) {
|
|
|
|
const uint32_t* h1 = reinterpret_cast<const uint32_t*>(aHash1);
|
|
|
|
const uint32_t* h2 = reinterpret_cast<const uint32_t*>(aHash2);
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < 5; ++i) {
|
|
|
|
if (h1[i] != h2[i]) {
|
|
|
|
uint32_t bitsDiff = h1[i] ^ h2[i];
|
|
|
|
bitsDiff = NetworkEndian::readUint32(&bitsDiff);
|
|
|
|
|
|
|
|
// count leading zeros in bitsDiff
|
|
|
|
static const uint8_t debruijn32[32] = {
|
|
|
|
0, 31, 9, 30, 3, 8, 13, 29, 2, 5, 7, 21, 12, 24, 28, 19,
|
|
|
|
1, 10, 4, 14, 6, 22, 25, 20, 11, 15, 23, 26, 16, 27, 17, 18};
|
|
|
|
|
|
|
|
bitsDiff |= bitsDiff >> 1;
|
|
|
|
bitsDiff |= bitsDiff >> 2;
|
|
|
|
bitsDiff |= bitsDiff >> 4;
|
|
|
|
bitsDiff |= bitsDiff >> 8;
|
|
|
|
bitsDiff |= bitsDiff >> 16;
|
|
|
|
bitsDiff++;
|
|
|
|
|
|
|
|
uint8_t hashSizeMatch =
|
|
|
|
debruijn32[bitsDiff * 0x076be629 >> 27] + (i << 5);
|
|
|
|
Telemetry::Accumulate(Telemetry::NETWORK_CACHE_HASH_STATS, hashSizeMatch);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(false, "Found a collision in the index!");
|
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
2015-06-14 15:13:18 +03:00
|
|
|
|
|
|
|
void CacheIndex::ReportHashStats() {
|
|
|
|
// We're gathering the hash stats only once, exclude too small caches.
|
|
|
|
if (CacheObserver::HashStatsReported() || mFrecencyArray.Length() < 15000) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsTArray<CacheIndexRecord*> records;
|
2016-10-06 12:23:52 +03:00
|
|
|
for (auto iter = mFrecencyArray.Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
records.AppendElement(iter.Get());
|
|
|
|
}
|
2015-06-14 15:13:18 +03:00
|
|
|
|
|
|
|
records.Sort(HashComparator());
|
|
|
|
|
|
|
|
for (uint32_t i = 1; i < records.Length(); i++) {
|
|
|
|
ReportHashSizeMatch(&records[i - 1]->mHash, &records[i]->mHash);
|
|
|
|
}
|
|
|
|
|
|
|
|
CacheObserver::SetHashStatsReported();
|
|
|
|
}
|
|
|
|
|
2016-04-05 06:59:00 +03:00
|
|
|
// static
|
2019-04-25 15:21:17 +03:00
|
|
|
void CacheIndex::UpdateTotalBytesWritten(uint32_t aBytesWritten) {
|
|
|
|
StaticMutexAutoLock lock(sLock);
|
|
|
|
|
2016-04-05 06:59:00 +03:00
|
|
|
RefPtr<CacheIndex> index = gInstance;
|
|
|
|
if (!index) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-25 15:21:17 +03:00
|
|
|
index->mTotalBytesWritten += aBytesWritten;
|
|
|
|
|
|
|
|
// Do telemetry report if enough data has been written and the index is
|
|
|
|
// in READY state. The data is available also in WRITING state, but we would
|
|
|
|
// need to deal with pending updates.
|
|
|
|
if (index->mTotalBytesWritten >= kTelemetryReportBytesLimit &&
|
|
|
|
index->mState == READY && !index->mIndexNeedsUpdate &&
|
|
|
|
!index->mShuttingDown) {
|
2019-05-08 16:15:40 +03:00
|
|
|
index->DoTelemetryReport();
|
2019-04-25 15:21:17 +03:00
|
|
|
|
|
|
|
index->mTotalBytesWritten = 0;
|
|
|
|
CacheObserver::SetCacheAmountWritten(0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t writtenKB = index->mTotalBytesWritten >> 10;
|
|
|
|
// Store number of written kilobytes to prefs after writing at least 10MB.
|
|
|
|
if ((writtenKB - CacheObserver::CacheAmountWritten()) > (10 * 1024)) {
|
|
|
|
CacheObserver::SetCacheAmountWritten(writtenKB);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-08 16:15:40 +03:00
|
|
|
void CacheIndex::DoTelemetryReport() {
|
2019-04-25 15:21:17 +03:00
|
|
|
static const nsLiteralCString
|
|
|
|
contentTypeNames[nsICacheEntry::CONTENT_TYPE_LAST] = {
|
|
|
|
NS_LITERAL_CSTRING("UNKNOWN"), NS_LITERAL_CSTRING("OTHER"),
|
|
|
|
NS_LITERAL_CSTRING("JAVASCRIPT"), NS_LITERAL_CSTRING("IMAGE"),
|
|
|
|
NS_LITERAL_CSTRING("MEDIA"), NS_LITERAL_CSTRING("STYLESHEET"),
|
|
|
|
NS_LITERAL_CSTRING("WASM")};
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < nsICacheEntry::CONTENT_TYPE_LAST; ++i) {
|
2020-02-24 12:09:54 +03:00
|
|
|
if (mIndexStats.Size() > 0) {
|
|
|
|
Telemetry::Accumulate(
|
|
|
|
Telemetry::NETWORK_CACHE_SIZE_SHARE, contentTypeNames[i],
|
|
|
|
round(static_cast<double>(mIndexStats.SizeByType(i)) * 100.0 /
|
|
|
|
static_cast<double>(mIndexStats.Size())));
|
2019-05-08 16:15:40 +03:00
|
|
|
}
|
|
|
|
|
2020-02-24 12:09:54 +03:00
|
|
|
if (mIndexStats.Count() > 0) {
|
|
|
|
Telemetry::Accumulate(
|
|
|
|
Telemetry::NETWORK_CACHE_ENTRY_COUNT_SHARE, contentTypeNames[i],
|
|
|
|
round(static_cast<double>(mIndexStats.CountByType(i)) * 100.0 /
|
|
|
|
static_cast<double>(mIndexStats.Count())));
|
2019-05-08 16:15:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCString probeKey;
|
|
|
|
if (CacheObserver::SmartCacheSizeEnabled()) {
|
|
|
|
probeKey = NS_LITERAL_CSTRING("SMARTSIZE");
|
|
|
|
} else {
|
|
|
|
probeKey = NS_LITERAL_CSTRING("USERDEFINEDSIZE");
|
2019-04-25 15:21:17 +03:00
|
|
|
}
|
2020-02-24 12:09:54 +03:00
|
|
|
Telemetry::Accumulate(Telemetry::NETWORK_CACHE_ENTRY_COUNT, probeKey,
|
|
|
|
mIndexStats.Count());
|
|
|
|
Telemetry::Accumulate(Telemetry::NETWORK_CACHE_SIZE, probeKey,
|
|
|
|
mIndexStats.Size() >> 10);
|
2019-04-25 15:21:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
void CacheIndex::OnAsyncEviction(bool aEvicting) {
|
2016-04-05 06:59:00 +03:00
|
|
|
StaticMutexAutoLock lock(sLock);
|
2019-04-25 15:21:17 +03:00
|
|
|
|
|
|
|
RefPtr<CacheIndex> index = gInstance;
|
|
|
|
if (!index) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-04-05 06:59:00 +03:00
|
|
|
index->mAsyncGetDiskConsumptionBlocked = aEvicting;
|
|
|
|
if (!aEvicting) {
|
|
|
|
index->NotifyAsyncGetDiskConsumptionCallbacks();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace net
|
|
|
|
} // namespace mozilla
|