2005-06-30 00:47:45 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/* vim:set ts=4 sw=4 sts=4 cin et: */
|
2012-05-21 15:12:37 +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/. */
|
2001-03-25 19:49:31 +04:00
|
|
|
|
2012-11-22 05:14:00 +04:00
|
|
|
#include "nsCache.h"
|
2001-03-25 19:49:31 +04:00
|
|
|
#include "nsDiskCacheMap.h"
|
2001-05-19 03:24:09 +04:00
|
|
|
#include "nsDiskCacheBinding.h"
|
|
|
|
#include "nsDiskCacheEntry.h"
|
2012-08-24 06:23:10 +04:00
|
|
|
#include "nsDiskCacheDevice.h"
|
2012-08-20 16:08:46 +04:00
|
|
|
#include "nsCacheService.h"
|
2001-05-19 03:24:09 +04:00
|
|
|
|
2001-03-29 09:54:58 +04:00
|
|
|
#include <string.h>
|
2010-12-06 18:49:05 +03:00
|
|
|
#include "nsPrintfCString.h"
|
2001-03-25 19:49:31 +04:00
|
|
|
|
2008-01-06 04:07:16 +03:00
|
|
|
#include "nsISerializable.h"
|
|
|
|
#include "nsSerializationHelper.h"
|
2001-05-19 03:24:09 +04:00
|
|
|
|
2013-06-23 16:03:39 +04:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
2016-08-15 09:43:21 +03:00
|
|
|
#include "mozilla/Sprintf.h"
|
2011-11-08 08:41:52 +04:00
|
|
|
#include "mozilla/Telemetry.h"
|
2013-01-15 16:22:03 +04:00
|
|
|
#include <algorithm>
|
2011-11-08 08:41:52 +04:00
|
|
|
|
2012-08-20 16:08:46 +04:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2001-05-19 03:24:09 +04:00
|
|
|
/******************************************************************************
|
|
|
|
* nsDiskCacheMap
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* File operations
|
|
|
|
*/
|
|
|
|
|
|
|
|
nsresult
|
2012-07-21 03:47:52 +04:00
|
|
|
nsDiskCacheMap::Open(nsIFile * cacheDirectory,
|
2016-06-28 20:29:28 +03:00
|
|
|
nsDiskCache::CorruptCacheInfo * corruptInfo)
|
2001-03-29 09:54:58 +04:00
|
|
|
{
|
2012-07-21 03:47:52 +04:00
|
|
|
NS_ENSURE_ARG_POINTER(corruptInfo);
|
|
|
|
|
|
|
|
// Assume we have an unexpected error until we find otherwise.
|
|
|
|
*corruptInfo = nsDiskCache::kUnexpectedError;
|
2001-05-19 03:24:09 +04:00
|
|
|
NS_ENSURE_ARG_POINTER(cacheDirectory);
|
|
|
|
if (mMapFD) return NS_ERROR_ALREADY_INITIALIZED;
|
|
|
|
|
|
|
|
mCacheDirectory = cacheDirectory; // save a reference for ourselves
|
|
|
|
|
2012-06-06 06:08:30 +04:00
|
|
|
// create nsIFile for _CACHE_MAP_
|
2001-05-19 03:24:09 +04:00
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIFile> file;
|
|
|
|
rv = cacheDirectory->Clone(getter_AddRefs(file));
|
2012-06-06 06:08:30 +04:00
|
|
|
rv = file->AppendNative(NS_LITERAL_CSTRING("_CACHE_MAP_"));
|
2007-12-29 07:22:54 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2003-03-11 02:19:05 +03:00
|
|
|
|
2005-06-10 21:32:48 +04:00
|
|
|
// open the file - restricted to user, the data could be confidential
|
2012-06-06 06:08:30 +04:00
|
|
|
rv = file->OpenNSPRFileDesc(PR_RDWR | PR_CREATE_FILE, 00600, &mMapFD);
|
2012-07-21 03:47:52 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
*corruptInfo = nsDiskCache::kOpenCacheMapError;
|
|
|
|
NS_WARNING("Could not open cache map file");
|
|
|
|
return NS_ERROR_FILE_CORRUPTED;
|
|
|
|
}
|
2003-03-11 02:19:05 +03:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool cacheFilesExist = CacheFilesExist();
|
2003-03-11 02:19:05 +03:00
|
|
|
rv = NS_ERROR_FILE_CORRUPTED; // presume the worst
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mapSize = PR_Available(mMapFD);
|
2012-08-20 16:08:46 +04:00
|
|
|
|
2012-09-04 23:05:19 +04:00
|
|
|
if (NS_FAILED(InitCacheClean(cacheDirectory,
|
2016-06-28 20:29:28 +03:00
|
|
|
corruptInfo))) {
|
2012-08-20 16:08:46 +04:00
|
|
|
// corruptInfo is set in the call to InitCacheClean
|
|
|
|
goto error_exit;
|
|
|
|
}
|
2003-03-11 02:19:05 +03:00
|
|
|
|
2001-05-19 03:24:09 +04:00
|
|
|
// check size of map file
|
2003-03-11 02:19:05 +03:00
|
|
|
if (mapSize == 0) { // creating a new _CACHE_MAP_
|
|
|
|
|
|
|
|
// block files shouldn't exist if we're creating the _CACHE_MAP_
|
2012-07-21 03:47:52 +04:00
|
|
|
if (cacheFilesExist) {
|
|
|
|
*corruptInfo = nsDiskCache::kBlockFilesShouldNotExist;
|
|
|
|
goto error_exit;
|
|
|
|
}
|
2003-03-11 02:19:05 +03:00
|
|
|
|
2012-07-21 03:47:52 +04:00
|
|
|
if (NS_FAILED(CreateCacheSubDirectories())) {
|
|
|
|
*corruptInfo = nsDiskCache::kCreateCacheSubdirectories;
|
2010-12-06 18:49:05 +03:00
|
|
|
goto error_exit;
|
2012-07-21 03:47:52 +04:00
|
|
|
}
|
2010-12-06 18:49:05 +03:00
|
|
|
|
2001-05-19 03:24:09 +04:00
|
|
|
// create the file - initialize in memory
|
2005-06-30 00:47:45 +04:00
|
|
|
memset(&mHeader, 0, sizeof(nsDiskCacheHeader));
|
|
|
|
mHeader.mVersion = nsDiskCache::kCurrentVersion;
|
|
|
|
mHeader.mRecordCount = kMinRecordCount;
|
|
|
|
mRecordArray = (nsDiskCacheRecord *)
|
|
|
|
PR_CALLOC(mHeader.mRecordCount * sizeof(nsDiskCacheRecord));
|
|
|
|
if (!mRecordArray) {
|
2012-07-21 03:47:52 +04:00
|
|
|
*corruptInfo = nsDiskCache::kOutOfMemory;
|
2005-06-30 00:47:45 +04:00
|
|
|
rv = NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
goto error_exit;
|
2001-05-19 03:24:09 +04:00
|
|
|
}
|
2005-06-30 00:47:45 +04:00
|
|
|
} else if (mapSize >= sizeof(nsDiskCacheHeader)) { // read existing _CACHE_MAP_
|
2003-03-11 02:19:05 +03:00
|
|
|
|
|
|
|
// if _CACHE_MAP_ exists, so should the block files
|
2012-07-21 03:47:52 +04:00
|
|
|
if (!cacheFilesExist) {
|
|
|
|
*corruptInfo = nsDiskCache::kBlockFilesShouldExist;
|
2005-06-30 00:47:45 +04:00
|
|
|
goto error_exit;
|
2012-07-21 03:47:52 +04:00
|
|
|
}
|
2003-03-11 02:19:05 +03:00
|
|
|
|
2012-06-01 02:20:03 +04:00
|
|
|
CACHE_LOG_DEBUG(("CACHE: nsDiskCacheMap::Open [this=%p] reading map", this));
|
|
|
|
|
2005-06-30 00:47:45 +04:00
|
|
|
// read the header
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t bytesRead = PR_Read(mMapFD, &mHeader, sizeof(nsDiskCacheHeader));
|
2012-07-21 03:47:52 +04:00
|
|
|
if (sizeof(nsDiskCacheHeader) != bytesRead) {
|
|
|
|
*corruptInfo = nsDiskCache::kHeaderSizeNotRead;
|
|
|
|
goto error_exit;
|
|
|
|
}
|
2001-05-19 03:24:09 +04:00
|
|
|
mHeader.Unswap();
|
2005-06-30 00:47:45 +04:00
|
|
|
|
2012-07-21 03:47:52 +04:00
|
|
|
if (mHeader.mIsDirty) {
|
|
|
|
*corruptInfo = nsDiskCache::kHeaderIsDirty;
|
|
|
|
goto error_exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mHeader.mVersion != nsDiskCache::kCurrentVersion) {
|
|
|
|
*corruptInfo = nsDiskCache::kVersionMismatch;
|
2001-05-19 03:24:09 +04:00
|
|
|
goto error_exit;
|
2012-07-21 03:47:52 +04:00
|
|
|
}
|
2005-06-30 00:47:45 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t recordArraySize =
|
2005-06-30 00:47:45 +04:00
|
|
|
mHeader.mRecordCount * sizeof(nsDiskCacheRecord);
|
2012-07-21 03:47:52 +04:00
|
|
|
if (mapSize < recordArraySize + sizeof(nsDiskCacheHeader)) {
|
|
|
|
*corruptInfo = nsDiskCache::kRecordsIncomplete;
|
2005-06-30 00:47:45 +04:00
|
|
|
goto error_exit;
|
2012-07-21 03:47:52 +04:00
|
|
|
}
|
2005-06-30 00:47:45 +04:00
|
|
|
|
|
|
|
// Get the space for the records
|
|
|
|
mRecordArray = (nsDiskCacheRecord *) PR_MALLOC(recordArraySize);
|
|
|
|
if (!mRecordArray) {
|
2012-07-21 03:47:52 +04:00
|
|
|
*corruptInfo = nsDiskCache::kOutOfMemory;
|
2005-06-30 00:47:45 +04:00
|
|
|
rv = NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
goto error_exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the records
|
|
|
|
bytesRead = PR_Read(mMapFD, mRecordArray, recordArraySize);
|
2012-07-21 03:47:52 +04:00
|
|
|
if (bytesRead < recordArraySize) {
|
|
|
|
*corruptInfo = nsDiskCache::kNotEnoughToRead;
|
2005-06-30 00:47:45 +04:00
|
|
|
goto error_exit;
|
2012-07-21 03:47:52 +04:00
|
|
|
}
|
2005-06-30 00:47:45 +04:00
|
|
|
|
|
|
|
// Unswap each record
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t total = 0;
|
|
|
|
for (int32_t i = 0; i < mHeader.mRecordCount; ++i) {
|
2005-06-30 00:47:45 +04:00
|
|
|
if (mRecordArray[i].HashNumber()) {
|
|
|
|
#if defined(IS_LITTLE_ENDIAN)
|
|
|
|
mRecordArray[i].Unswap();
|
|
|
|
#endif
|
|
|
|
total ++;
|
|
|
|
}
|
2001-05-19 03:24:09 +04:00
|
|
|
}
|
|
|
|
|
2003-03-11 02:19:05 +03:00
|
|
|
// verify entry count
|
2012-07-21 03:47:52 +04:00
|
|
|
if (total != mHeader.mEntryCount) {
|
|
|
|
*corruptInfo = nsDiskCache::kEntryCountIncorrect;
|
2005-06-30 00:47:45 +04:00
|
|
|
goto error_exit;
|
2012-07-21 03:47:52 +04:00
|
|
|
}
|
2003-03-11 02:19:05 +03:00
|
|
|
|
2001-05-19 03:24:09 +04:00
|
|
|
} else {
|
2012-07-21 03:47:52 +04:00
|
|
|
*corruptInfo = nsDiskCache::kHeaderIncomplete;
|
2001-05-19 03:24:09 +04:00
|
|
|
goto error_exit;
|
2001-03-29 09:54:58 +04:00
|
|
|
}
|
2001-05-19 03:24:09 +04:00
|
|
|
|
2012-07-21 03:47:52 +04:00
|
|
|
rv = OpenBlockFiles(corruptInfo);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// corruptInfo is set in the call to OpenBlockFiles
|
|
|
|
goto error_exit;
|
|
|
|
}
|
2001-05-19 03:24:09 +04:00
|
|
|
|
|
|
|
// set dirty bit and flush header
|
2011-10-17 18:59:28 +04:00
|
|
|
mHeader.mIsDirty = true;
|
2001-05-19 03:24:09 +04:00
|
|
|
rv = FlushHeader();
|
2012-07-21 03:47:52 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
*corruptInfo = nsDiskCache::kFlushHeaderError;
|
|
|
|
goto error_exit;
|
|
|
|
}
|
2001-05-19 03:24:09 +04:00
|
|
|
|
2012-11-05 22:22:33 +04:00
|
|
|
Telemetry::Accumulate(Telemetry::HTTP_DISK_CACHE_OVERHEAD,
|
|
|
|
(uint32_t)SizeOfExcludingThis(moz_malloc_size_of));
|
2011-11-08 08:41:52 +04:00
|
|
|
|
2012-07-21 03:47:52 +04:00
|
|
|
*corruptInfo = nsDiskCache::kNotCorrupt;
|
2001-05-19 03:24:09 +04:00
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
error_exit:
|
2011-10-17 18:59:28 +04:00
|
|
|
(void) Close(false);
|
2005-06-30 00:47:45 +04:00
|
|
|
|
2001-05-19 03:24:09 +04:00
|
|
|
return rv;
|
2001-03-29 09:54:58 +04:00
|
|
|
}
|
|
|
|
|
2001-04-04 07:30:45 +04:00
|
|
|
|
2001-05-19 03:24:09 +04:00
|
|
|
nsresult
|
2011-09-29 10:19:26 +04:00
|
|
|
nsDiskCacheMap::Close(bool flush)
|
2001-03-25 19:49:31 +04:00
|
|
|
{
|
2012-08-24 06:22:14 +04:00
|
|
|
nsCacheService::AssertOwnsLock();
|
2005-06-30 00:47:45 +04:00
|
|
|
nsresult rv = NS_OK;
|
2001-05-19 03:24:09 +04:00
|
|
|
|
2012-08-20 16:08:46 +04:00
|
|
|
// Cancel any pending cache validation event, the FlushRecords call below
|
|
|
|
// will validate the cache.
|
|
|
|
if (mCleanCacheTimer) {
|
|
|
|
mCleanCacheTimer->Cancel();
|
|
|
|
}
|
|
|
|
|
2005-06-30 00:47:45 +04:00
|
|
|
// If cache map file and its block files are still open, close them
|
|
|
|
if (mMapFD) {
|
|
|
|
// close block files
|
|
|
|
rv = CloseBlockFiles(flush);
|
|
|
|
if (NS_SUCCEEDED(rv) && flush && mRecordArray) {
|
|
|
|
// write the map records
|
2011-10-17 18:59:28 +04:00
|
|
|
rv = FlushRecords(false); // don't bother swapping buckets back
|
2005-06-30 00:47:45 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
// clear dirty bit
|
2011-10-17 18:59:28 +04:00
|
|
|
mHeader.mIsDirty = false;
|
2005-06-30 00:47:45 +04:00
|
|
|
rv = FlushHeader();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ((PR_Close(mMapFD) != PR_SUCCESS) && (NS_SUCCEEDED(rv)))
|
|
|
|
rv = NS_ERROR_UNEXPECTED;
|
2001-05-19 03:24:09 +04:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
mMapFD = nullptr;
|
2003-03-11 02:19:05 +03:00
|
|
|
}
|
2012-08-20 16:08:46 +04:00
|
|
|
|
|
|
|
if (mCleanFD) {
|
|
|
|
PR_Close(mCleanFD);
|
|
|
|
mCleanFD = nullptr;
|
|
|
|
}
|
|
|
|
|
2005-06-30 00:47:45 +04:00
|
|
|
PR_FREEIF(mRecordArray);
|
2007-12-29 07:22:54 +03:00
|
|
|
PR_FREEIF(mBuffer);
|
|
|
|
mBufferSize = 0;
|
2005-06-30 00:47:45 +04:00
|
|
|
return rv;
|
2001-05-19 03:24:09 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-08-07 05:13:29 +04:00
|
|
|
nsresult
|
|
|
|
nsDiskCacheMap::Trim()
|
|
|
|
{
|
|
|
|
nsresult rv, rv2 = NS_OK;
|
2011-01-18 17:12:10 +03:00
|
|
|
for (int i=0; i < kNumBlockFiles; ++i) {
|
2002-08-07 05:13:29 +04:00
|
|
|
rv = mBlockFile[i].Trim();
|
|
|
|
if (NS_FAILED(rv)) rv2 = rv; // if one or more errors, report at least one
|
|
|
|
}
|
2005-06-30 00:47:45 +04:00
|
|
|
// Try to shrink the records array
|
|
|
|
rv = ShrinkRecords();
|
|
|
|
if (NS_FAILED(rv)) rv2 = rv; // if one or more errors, report at least one
|
2002-08-07 05:13:29 +04:00
|
|
|
return rv2;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-05-19 03:24:09 +04:00
|
|
|
nsresult
|
|
|
|
nsDiskCacheMap::FlushHeader()
|
|
|
|
{
|
|
|
|
if (!mMapFD) return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
|
|
|
// seek to beginning of cache map
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t filePos = PR_Seek(mMapFD, 0, PR_SEEK_SET);
|
2001-05-19 03:24:09 +04:00
|
|
|
if (filePos != 0) return NS_ERROR_UNEXPECTED;
|
|
|
|
|
|
|
|
// write the header
|
|
|
|
mHeader.Swap();
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t bytesWritten = PR_Write(mMapFD, &mHeader, sizeof(nsDiskCacheHeader));
|
2001-05-19 03:24:09 +04:00
|
|
|
mHeader.Unswap();
|
|
|
|
if (sizeof(nsDiskCacheHeader) != bytesWritten) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
2011-08-23 02:24:23 +04:00
|
|
|
|
|
|
|
PRStatus err = PR_Sync(mMapFD);
|
|
|
|
if (err != PR_SUCCESS) return NS_ERROR_UNEXPECTED;
|
|
|
|
|
2012-09-04 23:05:19 +04:00
|
|
|
// If we have a clean header then revalidate the cache clean file
|
|
|
|
if (!mHeader.mIsDirty) {
|
|
|
|
RevalidateCache();
|
|
|
|
}
|
|
|
|
|
2001-05-19 03:24:09 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
2011-09-29 10:19:26 +04:00
|
|
|
nsDiskCacheMap::FlushRecords(bool unswap)
|
2001-05-19 03:24:09 +04:00
|
|
|
{
|
|
|
|
if (!mMapFD) return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
|
|
|
// seek to beginning of buckets
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t filePos = PR_Seek(mMapFD, sizeof(nsDiskCacheHeader), PR_SEEK_SET);
|
2005-06-30 00:47:45 +04:00
|
|
|
if (filePos != sizeof(nsDiskCacheHeader))
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
2001-05-19 03:24:09 +04:00
|
|
|
|
2005-06-30 00:47:45 +04:00
|
|
|
#if defined(IS_LITTLE_ENDIAN)
|
|
|
|
// Swap each record
|
2012-08-22 19:56:38 +04:00
|
|
|
for (int32_t i = 0; i < mHeader.mRecordCount; ++i) {
|
2005-06-30 00:47:45 +04:00
|
|
|
if (mRecordArray[i].HashNumber())
|
|
|
|
mRecordArray[i].Swap();
|
2001-05-19 03:24:09 +04:00
|
|
|
}
|
2005-06-30 00:47:45 +04:00
|
|
|
#endif
|
2001-05-19 03:24:09 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t recordArraySize = sizeof(nsDiskCacheRecord) * mHeader.mRecordCount;
|
2001-05-19 03:24:09 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t bytesWritten = PR_Write(mMapFD, mRecordArray, recordArraySize);
|
2005-06-30 00:47:45 +04:00
|
|
|
if (bytesWritten != recordArraySize)
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
|
|
|
|
#if defined(IS_LITTLE_ENDIAN)
|
2001-05-19 03:24:09 +04:00
|
|
|
if (unswap) {
|
2005-06-30 00:47:45 +04:00
|
|
|
// Unswap each record
|
2012-08-22 19:56:38 +04:00
|
|
|
for (int32_t i = 0; i < mHeader.mRecordCount; ++i) {
|
2005-06-30 00:47:45 +04:00
|
|
|
if (mRecordArray[i].HashNumber())
|
|
|
|
mRecordArray[i].Unswap();
|
2001-03-25 19:49:31 +04:00
|
|
|
}
|
|
|
|
}
|
2005-06-30 00:47:45 +04:00
|
|
|
#endif
|
2001-03-25 19:49:31 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2001-05-19 03:24:09 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Record operations
|
|
|
|
*/
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t
|
|
|
|
nsDiskCacheMap::GetBucketRank(uint32_t bucketIndex, uint32_t targetRank)
|
2005-06-30 00:47:45 +04:00
|
|
|
{
|
|
|
|
nsDiskCacheRecord * records = GetFirstRecordInBucket(bucketIndex);
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t rank = 0;
|
2005-06-30 00:47:45 +04:00
|
|
|
|
|
|
|
for (int i = mHeader.mBucketUsage[bucketIndex]-1; i >= 0; i--) {
|
|
|
|
if ((rank < records[i].EvictionRank()) &&
|
|
|
|
((targetRank == 0) || (records[i].EvictionRank() < targetRank)))
|
|
|
|
rank = records[i].EvictionRank();
|
|
|
|
}
|
|
|
|
return rank;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsDiskCacheMap::GrowRecords()
|
|
|
|
{
|
2010-06-11 06:46:51 +04:00
|
|
|
if (mHeader.mRecordCount >= mMaxRecordCount)
|
2005-06-30 00:47:45 +04:00
|
|
|
return NS_OK;
|
2006-03-09 23:02:54 +03:00
|
|
|
CACHE_LOG_DEBUG(("CACHE: GrowRecords\n"));
|
2005-06-30 00:47:45 +04:00
|
|
|
|
|
|
|
// Resize the record array
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t newCount = mHeader.mRecordCount << 1;
|
2010-06-11 06:46:51 +04:00
|
|
|
if (newCount > mMaxRecordCount)
|
|
|
|
newCount = mMaxRecordCount;
|
2005-06-30 00:47:45 +04:00
|
|
|
nsDiskCacheRecord *newArray = (nsDiskCacheRecord *)
|
|
|
|
PR_REALLOC(mRecordArray, newCount * sizeof(nsDiskCacheRecord));
|
|
|
|
if (!newArray)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
// Space out the buckets
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t oldRecordsPerBucket = GetRecordsPerBucket();
|
|
|
|
uint32_t newRecordsPerBucket = newCount / kBuckets;
|
2005-06-30 00:47:45 +04:00
|
|
|
// Work from back to space out each bucket to the new array
|
|
|
|
for (int bucketIndex = kBuckets - 1; bucketIndex >= 0; --bucketIndex) {
|
|
|
|
// Move bucket
|
|
|
|
nsDiskCacheRecord *newRecords = newArray + bucketIndex * newRecordsPerBucket;
|
2012-08-22 19:56:38 +04:00
|
|
|
const uint32_t count = mHeader.mBucketUsage[bucketIndex];
|
2005-06-30 00:47:45 +04:00
|
|
|
memmove(newRecords,
|
|
|
|
newArray + bucketIndex * oldRecordsPerBucket,
|
|
|
|
count * sizeof(nsDiskCacheRecord));
|
2011-08-03 02:23:54 +04:00
|
|
|
// clear unused records
|
|
|
|
memset(newRecords + count, 0,
|
|
|
|
(newRecordsPerBucket - count) * sizeof(nsDiskCacheRecord));
|
2005-06-30 00:47:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set as the new record array
|
|
|
|
mRecordArray = newArray;
|
|
|
|
mHeader.mRecordCount = newCount;
|
2012-08-20 16:08:46 +04:00
|
|
|
|
|
|
|
InvalidateCache();
|
|
|
|
|
2005-06-30 00:47:45 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsDiskCacheMap::ShrinkRecords()
|
|
|
|
{
|
|
|
|
if (mHeader.mRecordCount <= kMinRecordCount)
|
|
|
|
return NS_OK;
|
2006-03-09 23:02:54 +03:00
|
|
|
CACHE_LOG_DEBUG(("CACHE: ShrinkRecords\n"));
|
2005-06-30 00:47:45 +04:00
|
|
|
|
|
|
|
// Verify if we can shrink the record array: all buckets must be less than
|
|
|
|
// 1/2 filled
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t maxUsage = 0, bucketIndex;
|
2005-06-30 00:47:45 +04:00
|
|
|
for (bucketIndex = 0; bucketIndex < kBuckets; ++bucketIndex) {
|
|
|
|
if (maxUsage < mHeader.mBucketUsage[bucketIndex])
|
|
|
|
maxUsage = mHeader.mBucketUsage[bucketIndex];
|
|
|
|
}
|
|
|
|
// Determine new bucket size, halve size until maxUsage
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t oldRecordsPerBucket = GetRecordsPerBucket();
|
|
|
|
uint32_t newRecordsPerBucket = oldRecordsPerBucket;
|
2005-06-30 00:47:45 +04:00
|
|
|
while (maxUsage < (newRecordsPerBucket >> 1))
|
|
|
|
newRecordsPerBucket >>= 1;
|
2012-08-08 14:23:15 +04:00
|
|
|
if (newRecordsPerBucket < (kMinRecordCount / kBuckets))
|
|
|
|
newRecordsPerBucket = (kMinRecordCount / kBuckets);
|
|
|
|
NS_ASSERTION(newRecordsPerBucket <= oldRecordsPerBucket,
|
|
|
|
"ShrinkRecords() can't grow records!");
|
2005-06-30 00:47:45 +04:00
|
|
|
if (newRecordsPerBucket == oldRecordsPerBucket)
|
|
|
|
return NS_OK;
|
|
|
|
// Move the buckets close to each other
|
2012-08-08 14:23:15 +04:00
|
|
|
for (bucketIndex = 1; bucketIndex < kBuckets; ++bucketIndex) {
|
2005-06-30 00:47:45 +04:00
|
|
|
// Move bucket
|
|
|
|
memmove(mRecordArray + bucketIndex * newRecordsPerBucket,
|
|
|
|
mRecordArray + bucketIndex * oldRecordsPerBucket,
|
2012-08-08 14:23:15 +04:00
|
|
|
newRecordsPerBucket * sizeof(nsDiskCacheRecord));
|
2005-06-30 00:47:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Shrink the record array memory block itself
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t newCount = newRecordsPerBucket * kBuckets;
|
2005-06-30 00:47:45 +04:00
|
|
|
nsDiskCacheRecord* newArray = (nsDiskCacheRecord *)
|
|
|
|
PR_REALLOC(mRecordArray, newCount * sizeof(nsDiskCacheRecord));
|
|
|
|
if (!newArray)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
// Set as the new record array
|
|
|
|
mRecordArray = newArray;
|
|
|
|
mHeader.mRecordCount = newCount;
|
2012-08-20 16:08:46 +04:00
|
|
|
|
|
|
|
InvalidateCache();
|
|
|
|
|
2005-06-30 00:47:45 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2001-05-19 03:24:09 +04:00
|
|
|
nsresult
|
|
|
|
nsDiskCacheMap::AddRecord( nsDiskCacheRecord * mapRecord,
|
|
|
|
nsDiskCacheRecord * oldRecord)
|
2001-03-25 19:49:31 +04:00
|
|
|
{
|
2006-03-09 23:02:54 +03:00
|
|
|
CACHE_LOG_DEBUG(("CACHE: AddRecord [%x]\n", mapRecord->HashNumber()));
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
const uint32_t hashNumber = mapRecord->HashNumber();
|
|
|
|
const uint32_t bucketIndex = GetBucketIndex(hashNumber);
|
|
|
|
const uint32_t count = mHeader.mBucketUsage[bucketIndex];
|
2001-05-19 03:24:09 +04:00
|
|
|
|
2003-03-11 02:19:05 +03:00
|
|
|
oldRecord->SetHashNumber(0); // signify no record
|
2001-05-19 03:24:09 +04:00
|
|
|
|
2005-06-30 00:47:45 +04:00
|
|
|
if (count == GetRecordsPerBucket()) {
|
|
|
|
// Ignore failure to grow the record space, we will then reuse old records
|
|
|
|
GrowRecords();
|
2001-05-19 03:24:09 +04:00
|
|
|
}
|
|
|
|
|
2005-06-30 00:47:45 +04:00
|
|
|
nsDiskCacheRecord * records = GetFirstRecordInBucket(bucketIndex);
|
|
|
|
if (count < GetRecordsPerBucket()) {
|
|
|
|
// stick the new record at the end
|
|
|
|
records[count] = *mapRecord;
|
|
|
|
mHeader.mEntryCount++;
|
|
|
|
mHeader.mBucketUsage[bucketIndex]++;
|
|
|
|
if (mHeader.mEvictionRank[bucketIndex] < mapRecord->EvictionRank())
|
|
|
|
mHeader.mEvictionRank[bucketIndex] = mapRecord->EvictionRank();
|
2012-08-20 16:08:46 +04:00
|
|
|
InvalidateCache();
|
2005-06-30 00:47:45 +04:00
|
|
|
} else {
|
|
|
|
// Find the record with the highest eviction rank
|
|
|
|
nsDiskCacheRecord * mostEvictable = &records[0];
|
|
|
|
for (int i = count-1; i > 0; i--) {
|
|
|
|
if (records[i].EvictionRank() > mostEvictable->EvictionRank())
|
|
|
|
mostEvictable = &records[i];
|
|
|
|
}
|
|
|
|
*oldRecord = *mostEvictable; // i == GetRecordsPerBucket(), so
|
|
|
|
// evict the mostEvictable
|
|
|
|
*mostEvictable = *mapRecord; // replace it with the new record
|
|
|
|
// check if we need to update mostEvictable entry in header
|
|
|
|
if (mHeader.mEvictionRank[bucketIndex] < mapRecord->EvictionRank())
|
|
|
|
mHeader.mEvictionRank[bucketIndex] = mapRecord->EvictionRank();
|
|
|
|
if (oldRecord->EvictionRank() >= mHeader.mEvictionRank[bucketIndex])
|
|
|
|
mHeader.mEvictionRank[bucketIndex] = GetBucketRank(bucketIndex, 0);
|
2012-08-20 16:08:46 +04:00
|
|
|
InvalidateCache();
|
2001-05-19 03:24:09 +04:00
|
|
|
}
|
|
|
|
|
2005-06-30 00:47:45 +04:00
|
|
|
NS_ASSERTION(mHeader.mEvictionRank[bucketIndex] == GetBucketRank(bucketIndex, 0),
|
|
|
|
"eviction rank out of sync");
|
2001-05-19 03:24:09 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsDiskCacheMap::UpdateRecord( nsDiskCacheRecord * mapRecord)
|
|
|
|
{
|
2006-03-09 23:02:54 +03:00
|
|
|
CACHE_LOG_DEBUG(("CACHE: UpdateRecord [%x]\n", mapRecord->HashNumber()));
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
const uint32_t hashNumber = mapRecord->HashNumber();
|
|
|
|
const uint32_t bucketIndex = GetBucketIndex(hashNumber);
|
2005-06-30 00:47:45 +04:00
|
|
|
nsDiskCacheRecord * records = GetFirstRecordInBucket(bucketIndex);
|
2001-05-19 03:24:09 +04:00
|
|
|
|
2005-06-30 00:47:45 +04:00
|
|
|
for (int i = mHeader.mBucketUsage[bucketIndex]-1; i >= 0; i--) {
|
|
|
|
if (records[i].HashNumber() == hashNumber) {
|
2012-08-22 19:56:38 +04:00
|
|
|
const uint32_t oldRank = records[i].EvictionRank();
|
2001-05-19 03:24:09 +04:00
|
|
|
|
|
|
|
// stick the new record here
|
2005-06-30 00:47:45 +04:00
|
|
|
records[i] = *mapRecord;
|
2001-05-19 03:24:09 +04:00
|
|
|
|
|
|
|
// update eviction rank in header if necessary
|
|
|
|
if (mHeader.mEvictionRank[bucketIndex] < mapRecord->EvictionRank())
|
|
|
|
mHeader.mEvictionRank[bucketIndex] = mapRecord->EvictionRank();
|
|
|
|
else if (mHeader.mEvictionRank[bucketIndex] == oldRank)
|
2005-06-30 00:47:45 +04:00
|
|
|
mHeader.mEvictionRank[bucketIndex] = GetBucketRank(bucketIndex, 0);
|
2001-05-19 03:24:09 +04:00
|
|
|
|
2012-08-20 16:08:46 +04:00
|
|
|
InvalidateCache();
|
|
|
|
|
2005-06-30 00:47:45 +04:00
|
|
|
NS_ASSERTION(mHeader.mEvictionRank[bucketIndex] == GetBucketRank(bucketIndex, 0),
|
2001-05-22 02:05:10 +04:00
|
|
|
"eviction rank out of sync");
|
2001-05-19 03:24:09 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
2006-03-09 23:02:54 +03:00
|
|
|
NS_NOTREACHED("record not found");
|
2001-05-19 03:24:09 +04:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
2012-08-22 19:56:38 +04:00
|
|
|
nsDiskCacheMap::FindRecord( uint32_t hashNumber, nsDiskCacheRecord * result)
|
2001-05-19 03:24:09 +04:00
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
const uint32_t bucketIndex = GetBucketIndex(hashNumber);
|
2005-06-30 00:47:45 +04:00
|
|
|
nsDiskCacheRecord * records = GetFirstRecordInBucket(bucketIndex);
|
|
|
|
|
|
|
|
for (int i = mHeader.mBucketUsage[bucketIndex]-1; i >= 0; i--) {
|
|
|
|
if (records[i].HashNumber() == hashNumber) {
|
|
|
|
*result = records[i]; // copy the record
|
2001-05-19 03:24:09 +04:00
|
|
|
NS_ASSERTION(result->ValidRecord(), "bad cache map record");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_ERROR_CACHE_KEY_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsDiskCacheMap::DeleteRecord( nsDiskCacheRecord * mapRecord)
|
|
|
|
{
|
2006-03-09 23:02:54 +03:00
|
|
|
CACHE_LOG_DEBUG(("CACHE: DeleteRecord [%x]\n", mapRecord->HashNumber()));
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
const uint32_t hashNumber = mapRecord->HashNumber();
|
|
|
|
const uint32_t bucketIndex = GetBucketIndex(hashNumber);
|
2005-06-30 00:47:45 +04:00
|
|
|
nsDiskCacheRecord * records = GetFirstRecordInBucket(bucketIndex);
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t last = mHeader.mBucketUsage[bucketIndex]-1;
|
2005-06-30 00:47:45 +04:00
|
|
|
|
|
|
|
for (int i = last; i >= 0; i--) {
|
|
|
|
if (records[i].HashNumber() == hashNumber) {
|
2001-05-19 03:24:09 +04:00
|
|
|
// found it, now delete it.
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t evictionRank = records[i].EvictionRank();
|
2003-03-11 02:19:05 +03:00
|
|
|
NS_ASSERTION(evictionRank == mapRecord->EvictionRank(),
|
|
|
|
"evictionRank out of sync");
|
2005-06-30 00:47:45 +04:00
|
|
|
// if not the last record, shift last record into opening
|
|
|
|
records[i] = records[last];
|
|
|
|
records[last].SetHashNumber(0); // clear last record
|
|
|
|
mHeader.mBucketUsage[bucketIndex] = last;
|
2001-05-19 03:24:09 +04:00
|
|
|
mHeader.mEntryCount--;
|
2005-06-30 00:47:45 +04:00
|
|
|
|
2001-05-19 03:24:09 +04:00
|
|
|
// update eviction rank
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t bucketIndex = GetBucketIndex(mapRecord->HashNumber());
|
2001-05-19 03:24:09 +04:00
|
|
|
if (mHeader.mEvictionRank[bucketIndex] <= evictionRank) {
|
2005-06-30 00:47:45 +04:00
|
|
|
mHeader.mEvictionRank[bucketIndex] = GetBucketRank(bucketIndex, 0);
|
2001-04-06 02:28:17 +04:00
|
|
|
}
|
2001-05-19 03:24:09 +04:00
|
|
|
|
2012-08-20 16:08:46 +04:00
|
|
|
InvalidateCache();
|
|
|
|
|
2003-03-11 02:19:05 +03:00
|
|
|
NS_ASSERTION(mHeader.mEvictionRank[bucketIndex] ==
|
2005-06-30 00:47:45 +04:00
|
|
|
GetBucketRank(bucketIndex, 0), "eviction rank out of sync");
|
2001-05-19 03:24:09 +04:00
|
|
|
return NS_OK;
|
2001-04-06 02:28:17 +04:00
|
|
|
}
|
|
|
|
}
|
2001-05-19 03:24:09 +04:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t
|
|
|
|
nsDiskCacheMap::VisitEachRecord(uint32_t bucketIndex,
|
2005-06-30 00:47:45 +04:00
|
|
|
nsDiskCacheRecordVisitor * visitor,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t evictionRank)
|
2005-06-30 00:47:45 +04:00
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t rv = kVisitNextRecord;
|
|
|
|
uint32_t count = mHeader.mBucketUsage[bucketIndex];
|
2005-06-30 00:47:45 +04:00
|
|
|
nsDiskCacheRecord * records = GetFirstRecordInBucket(bucketIndex);
|
|
|
|
|
|
|
|
// call visitor for each entry (matching any eviction rank)
|
|
|
|
for (int i = count-1; i >= 0; i--) {
|
|
|
|
if (evictionRank > records[i].EvictionRank()) continue;
|
|
|
|
|
|
|
|
rv = visitor->VisitRecord(&records[i]);
|
|
|
|
if (rv == kStopVisitingRecords)
|
|
|
|
break; // Stop visiting records
|
|
|
|
|
|
|
|
if (rv == kDeleteRecordAndContinue) {
|
|
|
|
--count;
|
|
|
|
records[i] = records[count];
|
|
|
|
records[count].SetHashNumber(0);
|
2012-08-20 16:08:46 +04:00
|
|
|
InvalidateCache();
|
2005-06-30 00:47:45 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mHeader.mBucketUsage[bucketIndex] - count != 0) {
|
|
|
|
mHeader.mEntryCount -= mHeader.mBucketUsage[bucketIndex] - count;
|
|
|
|
mHeader.mBucketUsage[bucketIndex] = count;
|
|
|
|
// recalc eviction rank
|
|
|
|
mHeader.mEvictionRank[bucketIndex] = GetBucketRank(bucketIndex, 0);
|
|
|
|
}
|
|
|
|
NS_ASSERTION(mHeader.mEvictionRank[bucketIndex] ==
|
|
|
|
GetBucketRank(bucketIndex, 0), "eviction rank out of sync");
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-05-19 03:24:09 +04:00
|
|
|
/**
|
|
|
|
* VisitRecords
|
|
|
|
*
|
|
|
|
* Visit every record in cache map in the most convenient order
|
|
|
|
*/
|
|
|
|
nsresult
|
|
|
|
nsDiskCacheMap::VisitRecords( nsDiskCacheRecordVisitor * visitor)
|
|
|
|
{
|
2005-06-30 00:47:45 +04:00
|
|
|
for (int bucketIndex = 0; bucketIndex < kBuckets; ++bucketIndex) {
|
|
|
|
if (VisitEachRecord(bucketIndex, visitor, 0) == kStopVisitingRecords)
|
|
|
|
break;
|
|
|
|
}
|
2001-05-19 03:24:09 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* EvictRecords
|
|
|
|
*
|
|
|
|
* Just like VisitRecords, but visits the records in order of their eviction rank
|
|
|
|
*/
|
|
|
|
nsresult
|
|
|
|
nsDiskCacheMap::EvictRecords( nsDiskCacheRecordVisitor * visitor)
|
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t tempRank[kBuckets];
|
2005-06-30 00:47:45 +04:00
|
|
|
int bucketIndex = 0;
|
2001-05-22 02:05:10 +04:00
|
|
|
|
|
|
|
// copy eviction rank array
|
2005-06-30 00:47:45 +04:00
|
|
|
for (bucketIndex = 0; bucketIndex < kBuckets; ++bucketIndex)
|
|
|
|
tempRank[bucketIndex] = mHeader.mEvictionRank[bucketIndex];
|
|
|
|
|
|
|
|
// Maximum number of iterations determined by number of records
|
2011-05-20 21:27:11 +04:00
|
|
|
// as a safety limiter for the loop. Use a copy of mHeader.mEntryCount since
|
|
|
|
// the value could decrease if some entry is evicted.
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t entryCount = mHeader.mEntryCount;
|
2011-05-20 21:27:11 +04:00
|
|
|
for (int n = 0; n < entryCount; ++n) {
|
2001-05-19 03:24:09 +04:00
|
|
|
|
|
|
|
// find bucket with highest eviction rank
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t rank = 0;
|
2005-06-30 00:47:45 +04:00
|
|
|
for (int i = 0; i < kBuckets; ++i) {
|
2001-05-22 02:05:10 +04:00
|
|
|
if (rank < tempRank[i]) {
|
|
|
|
rank = tempRank[i];
|
2005-06-30 00:47:45 +04:00
|
|
|
bucketIndex = i;
|
2001-04-06 02:28:17 +04:00
|
|
|
}
|
|
|
|
}
|
2001-05-19 03:24:09 +04:00
|
|
|
|
2005-06-30 00:47:45 +04:00
|
|
|
if (rank == 0) break; // we've examined all the records
|
|
|
|
|
2001-05-19 03:24:09 +04:00
|
|
|
// visit records in bucket with eviction ranks >= target eviction rank
|
2005-06-30 00:47:45 +04:00
|
|
|
if (VisitEachRecord(bucketIndex, visitor, rank) == kStopVisitingRecords)
|
|
|
|
break;
|
|
|
|
|
2001-05-22 02:05:10 +04:00
|
|
|
// find greatest rank less than 'rank'
|
2005-06-30 00:47:45 +04:00
|
|
|
tempRank[bucketIndex] = GetBucketRank(bucketIndex, rank);
|
2001-04-06 02:28:17 +04:00
|
|
|
}
|
2001-05-19 03:24:09 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2001-04-06 02:28:17 +04:00
|
|
|
|
2001-05-19 03:24:09 +04:00
|
|
|
|
|
|
|
nsresult
|
2012-07-21 03:47:52 +04:00
|
|
|
nsDiskCacheMap::OpenBlockFiles(nsDiskCache::CorruptCacheInfo * corruptInfo)
|
2001-05-19 03:24:09 +04:00
|
|
|
{
|
2012-07-21 03:47:52 +04:00
|
|
|
NS_ENSURE_ARG_POINTER(corruptInfo);
|
|
|
|
|
2012-06-06 06:08:30 +04:00
|
|
|
// create nsIFile for block file
|
|
|
|
nsCOMPtr<nsIFile> blockFile;
|
2007-12-29 07:22:54 +03:00
|
|
|
nsresult rv = NS_OK;
|
2012-07-21 03:47:52 +04:00
|
|
|
*corruptInfo = nsDiskCache::kUnexpectedError;
|
2001-05-19 03:24:09 +04:00
|
|
|
|
2011-01-18 17:12:10 +03:00
|
|
|
for (int i = 0; i < kNumBlockFiles; ++i) {
|
2001-05-19 03:24:09 +04:00
|
|
|
rv = GetBlockFileForIndex(i, getter_AddRefs(blockFile));
|
2012-07-21 03:47:52 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
*corruptInfo = nsDiskCache::kCouldNotGetBlockFileForIndex;
|
|
|
|
break;
|
|
|
|
}
|
2001-05-19 03:24:09 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t blockSize = GetBlockSizeForIndex(i+1); // +1 to match file selectors 1,2,3
|
|
|
|
uint32_t bitMapSize = GetBitMapSizeForIndex(i+1);
|
2012-07-21 03:47:52 +04:00
|
|
|
rv = mBlockFile[i].Open(blockFile, blockSize, bitMapSize, corruptInfo);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// corruptInfo was set inside the call to mBlockFile[i].Open
|
|
|
|
break;
|
|
|
|
}
|
2001-05-19 03:24:09 +04:00
|
|
|
}
|
2007-12-29 07:22:54 +03:00
|
|
|
// close all files in case of any error
|
2012-07-21 03:47:52 +04:00
|
|
|
if (NS_FAILED(rv))
|
2011-10-17 18:59:28 +04:00
|
|
|
(void)CloseBlockFiles(false); // we already have an error to report
|
2001-05-19 03:24:09 +04:00
|
|
|
|
2001-04-06 02:28:17 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2001-05-19 03:24:09 +04:00
|
|
|
|
|
|
|
nsresult
|
2011-09-29 10:19:26 +04:00
|
|
|
nsDiskCacheMap::CloseBlockFiles(bool flush)
|
2001-04-06 02:28:17 +04:00
|
|
|
{
|
2001-05-19 03:24:09 +04:00
|
|
|
nsresult rv, rv2 = NS_OK;
|
2011-01-18 17:12:10 +03:00
|
|
|
for (int i=0; i < kNumBlockFiles; ++i) {
|
2003-03-11 02:19:05 +03:00
|
|
|
rv = mBlockFile[i].Close(flush);
|
2001-05-19 03:24:09 +04:00
|
|
|
if (NS_FAILED(rv)) rv2 = rv; // if one or more errors, report at least one
|
|
|
|
}
|
|
|
|
return rv2;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2003-03-11 02:19:05 +03:00
|
|
|
nsDiskCacheMap::CacheFilesExist()
|
|
|
|
{
|
2012-06-06 06:08:30 +04:00
|
|
|
nsCOMPtr<nsIFile> blockFile;
|
2003-03-11 02:19:05 +03:00
|
|
|
nsresult rv;
|
|
|
|
|
2011-01-18 17:12:10 +03:00
|
|
|
for (int i = 0; i < kNumBlockFiles; ++i) {
|
2011-09-29 10:19:26 +04:00
|
|
|
bool exists;
|
2003-03-11 02:19:05 +03:00
|
|
|
rv = GetBlockFileForIndex(i, getter_AddRefs(blockFile));
|
2011-10-17 18:59:28 +04:00
|
|
|
if (NS_FAILED(rv)) return false;
|
2003-03-11 02:19:05 +03:00
|
|
|
|
|
|
|
rv = blockFile->Exists(&exists);
|
2011-10-17 18:59:28 +04:00
|
|
|
if (NS_FAILED(rv) || !exists) return false;
|
2003-03-11 02:19:05 +03:00
|
|
|
}
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2003-03-11 02:19:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-06 18:49:05 +03:00
|
|
|
nsresult
|
|
|
|
nsDiskCacheMap::CreateCacheSubDirectories()
|
|
|
|
{
|
|
|
|
if (!mCacheDirectory)
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (int32_t index = 0 ; index < 16 ; index++) {
|
2010-12-06 18:49:05 +03:00
|
|
|
nsCOMPtr<nsIFile> file;
|
|
|
|
nsresult rv = mCacheDirectory->Clone(getter_AddRefs(file));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
rv = file->AppendNative(nsPrintfCString("%X", index));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2012-06-06 06:08:30 +04:00
|
|
|
rv = file->Create(nsIFile::DIRECTORY_TYPE, 0700);
|
2010-12-06 18:49:05 +03:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-12-29 07:22:54 +03:00
|
|
|
nsDiskCacheEntry *
|
|
|
|
nsDiskCacheMap::ReadDiskCacheEntry(nsDiskCacheRecord * record)
|
2001-05-19 03:24:09 +04:00
|
|
|
{
|
2006-03-09 23:02:54 +03:00
|
|
|
CACHE_LOG_DEBUG(("CACHE: ReadDiskCacheEntry [%x]\n", record->HashNumber()));
|
|
|
|
|
2001-06-28 05:30:26 +04:00
|
|
|
nsresult rv = NS_ERROR_UNEXPECTED;
|
2012-07-30 18:20:58 +04:00
|
|
|
nsDiskCacheEntry * diskEntry = nullptr;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t metaFile = record->MetaFile();
|
|
|
|
int32_t bytesRead = 0;
|
2001-04-06 02:28:17 +04:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
if (!record->MetaLocationInitialized()) return nullptr;
|
2001-05-25 22:47:53 +04:00
|
|
|
|
2001-05-19 03:24:09 +04:00
|
|
|
if (metaFile == 0) { // entry/metadata stored in separate file
|
|
|
|
// open and read the file
|
2012-06-06 06:08:30 +04:00
|
|
|
nsCOMPtr<nsIFile> file;
|
2010-12-06 18:49:05 +03:00
|
|
|
rv = GetLocalFileForDiskCacheRecord(record,
|
|
|
|
nsDiskCache::kMetaData,
|
2011-10-17 18:59:28 +04:00
|
|
|
false,
|
2010-12-06 18:49:05 +03:00
|
|
|
getter_AddRefs(file));
|
2012-07-30 18:20:58 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
2001-04-06 02:28:17 +04:00
|
|
|
|
2012-06-01 02:20:03 +04:00
|
|
|
CACHE_LOG_DEBUG(("CACHE: nsDiskCacheMap::ReadDiskCacheEntry"
|
|
|
|
"[this=%p] reading disk cache entry", this));
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
PRFileDesc * fd = nullptr;
|
2012-06-01 02:20:03 +04:00
|
|
|
|
2005-06-10 21:32:48 +04:00
|
|
|
// open the file - restricted to user, the data could be confidential
|
|
|
|
rv = file->OpenNSPRFileDesc(PR_RDONLY, 00600, &fd);
|
2012-07-30 18:20:58 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
2001-05-19 03:24:09 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t fileSize = PR_Available(fd);
|
2001-05-19 03:24:09 +04:00
|
|
|
if (fileSize < 0) {
|
2002-08-07 05:13:29 +04:00
|
|
|
// an error occurred. We could call PR_GetError(), but how would that help?
|
2001-05-19 03:24:09 +04:00
|
|
|
rv = NS_ERROR_UNEXPECTED;
|
2007-12-29 07:22:54 +03:00
|
|
|
} else {
|
|
|
|
rv = EnsureBuffer(fileSize);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
bytesRead = PR_Read(fd, mBuffer, fileSize);
|
|
|
|
if (bytesRead < fileSize) {
|
|
|
|
rv = NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
}
|
2001-05-19 03:24:09 +04:00
|
|
|
}
|
2007-12-29 07:22:54 +03:00
|
|
|
PR_Close(fd);
|
2012-07-30 18:20:58 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
2001-05-19 03:24:09 +04:00
|
|
|
|
2011-01-18 17:12:10 +03:00
|
|
|
} else if (metaFile < (kNumBlockFiles + 1)) {
|
2001-05-19 03:24:09 +04:00
|
|
|
// entry/metadata stored in cache block file
|
|
|
|
|
|
|
|
// allocate buffer
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t blockCount = record->MetaBlockCount();
|
2006-05-26 22:21:12 +04:00
|
|
|
bytesRead = blockCount * GetBlockSizeForIndex(metaFile);
|
2007-12-29 07:22:54 +03:00
|
|
|
|
|
|
|
rv = EnsureBuffer(bytesRead);
|
2012-07-30 18:20:58 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
2001-05-19 03:24:09 +04:00
|
|
|
|
2006-05-26 22:21:12 +04:00
|
|
|
// read diskEntry, note when the blocks are at the end of file,
|
|
|
|
// bytesRead may be less than blockSize*blockCount.
|
|
|
|
// But the bytesRead should at least agree with the real disk entry size.
|
2007-12-29 07:22:54 +03:00
|
|
|
rv = mBlockFile[metaFile - 1].ReadBlocks(mBuffer,
|
2001-05-19 03:24:09 +04:00
|
|
|
record->MetaStartBlock(),
|
2006-05-26 22:21:12 +04:00
|
|
|
blockCount,
|
|
|
|
&bytesRead);
|
2012-07-30 18:20:58 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
2001-05-19 03:24:09 +04:00
|
|
|
}
|
2007-12-29 07:22:54 +03:00
|
|
|
diskEntry = (nsDiskCacheEntry *)mBuffer;
|
2001-05-19 03:24:09 +04:00
|
|
|
diskEntry->Unswap(); // disk to memory
|
2006-05-26 22:21:12 +04:00
|
|
|
// Check if calculated size agrees with bytesRead
|
2012-08-22 19:56:38 +04:00
|
|
|
if (bytesRead < 0 || (uint32_t)bytesRead < diskEntry->Size())
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2007-12-29 07:22:54 +03:00
|
|
|
|
|
|
|
// Return the buffer containing the diskEntry structure
|
|
|
|
return diskEntry;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* CreateDiskCacheEntry(nsCacheEntry * entry)
|
|
|
|
*
|
|
|
|
* Prepare an nsCacheEntry for writing to disk
|
|
|
|
*/
|
|
|
|
nsDiskCacheEntry *
|
|
|
|
nsDiskCacheMap::CreateDiskCacheEntry(nsDiskCacheBinding * binding,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t * aSize)
|
2007-12-29 07:22:54 +03:00
|
|
|
{
|
|
|
|
nsCacheEntry * entry = binding->mCacheEntry;
|
2012-07-30 18:20:58 +04:00
|
|
|
if (!entry) return nullptr;
|
2007-12-29 07:22:54 +03:00
|
|
|
|
2008-01-06 04:07:16 +03:00
|
|
|
// Store security info, if it is serializable
|
2012-03-30 13:21:01 +04:00
|
|
|
nsCOMPtr<nsISupports> infoObj = entry->SecurityInfo();
|
|
|
|
nsCOMPtr<nsISerializable> serializable = do_QueryInterface(infoObj);
|
2012-07-30 18:20:58 +04:00
|
|
|
if (infoObj && !serializable) return nullptr;
|
2008-01-06 04:07:16 +03:00
|
|
|
if (serializable) {
|
|
|
|
nsCString info;
|
2012-03-30 13:21:01 +04:00
|
|
|
nsresult rv = NS_SerializeToString(serializable, info);
|
2012-07-30 18:20:58 +04:00
|
|
|
if (NS_FAILED(rv)) return nullptr;
|
2012-03-30 13:21:01 +04:00
|
|
|
rv = entry->SetMetaDataElement("security-info", info.get());
|
2012-07-30 18:20:58 +04:00
|
|
|
if (NS_FAILED(rv)) return nullptr;
|
2008-01-06 04:07:16 +03:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t keySize = entry->Key()->Length() + 1;
|
|
|
|
uint32_t metaSize = entry->MetaDataSize();
|
|
|
|
uint32_t size = sizeof(nsDiskCacheEntry) + keySize + metaSize;
|
2007-12-29 07:22:54 +03:00
|
|
|
|
|
|
|
if (aSize) *aSize = size;
|
|
|
|
|
|
|
|
nsresult rv = EnsureBuffer(size);
|
2012-07-30 18:20:58 +04:00
|
|
|
if (NS_FAILED(rv)) return nullptr;
|
2007-12-29 07:22:54 +03:00
|
|
|
|
|
|
|
nsDiskCacheEntry *diskEntry = (nsDiskCacheEntry *)mBuffer;
|
|
|
|
diskEntry->mHeaderVersion = nsDiskCache::kCurrentVersion;
|
|
|
|
diskEntry->mMetaLocation = binding->mRecord.MetaLocation();
|
|
|
|
diskEntry->mFetchCount = entry->FetchCount();
|
|
|
|
diskEntry->mLastFetched = entry->LastFetched();
|
|
|
|
diskEntry->mLastModified = entry->LastModified();
|
|
|
|
diskEntry->mExpirationTime = entry->ExpirationTime();
|
|
|
|
diskEntry->mDataSize = entry->DataSize();
|
|
|
|
diskEntry->mKeySize = keySize;
|
|
|
|
diskEntry->mMetaDataSize = metaSize;
|
|
|
|
|
|
|
|
memcpy(diskEntry->Key(), entry->Key()->get(), keySize);
|
|
|
|
|
|
|
|
rv = entry->FlattenMetaData(diskEntry->MetaData(), metaSize);
|
2012-07-30 18:20:58 +04:00
|
|
|
if (NS_FAILED(rv)) return nullptr;
|
2007-12-29 07:22:54 +03:00
|
|
|
|
|
|
|
return diskEntry;
|
2001-05-19 03:24:09 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsDiskCacheMap::WriteDiskCacheEntry(nsDiskCacheBinding * binding)
|
|
|
|
{
|
2006-03-09 23:02:54 +03:00
|
|
|
CACHE_LOG_DEBUG(("CACHE: WriteDiskCacheEntry [%x]\n",
|
|
|
|
binding->mRecord.HashNumber()));
|
|
|
|
|
2001-05-19 03:24:09 +04:00
|
|
|
nsresult rv = NS_OK;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t size;
|
2006-05-26 22:21:12 +04:00
|
|
|
nsDiskCacheEntry * diskEntry = CreateDiskCacheEntry(binding, &size);
|
2001-05-19 03:24:09 +04:00
|
|
|
if (!diskEntry) return NS_ERROR_UNEXPECTED;
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t fileIndex = CalculateFileIndex(size);
|
2001-05-19 03:24:09 +04:00
|
|
|
|
|
|
|
// Deallocate old storage if necessary
|
|
|
|
if (binding->mRecord.MetaLocationInitialized()) {
|
|
|
|
// we have existing storage
|
|
|
|
|
2002-08-07 05:13:29 +04:00
|
|
|
if ((binding->mRecord.MetaFile() == 0) &&
|
|
|
|
(fileIndex == 0)) { // keeping the separate file
|
2001-05-19 03:24:09 +04:00
|
|
|
// just decrement total
|
2006-03-01 01:18:21 +03:00
|
|
|
DecrementTotalSize(binding->mRecord.MetaFileSize());
|
2001-05-19 03:24:09 +04:00
|
|
|
NS_ASSERTION(binding->mRecord.MetaFileGeneration() == binding->mGeneration,
|
|
|
|
"generations out of sync");
|
|
|
|
} else {
|
|
|
|
rv = DeleteStorage(&binding->mRecord, nsDiskCache::kMetaData);
|
2007-12-29 07:22:54 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2001-03-25 19:49:31 +04:00
|
|
|
}
|
|
|
|
}
|
2001-05-22 02:05:10 +04:00
|
|
|
|
|
|
|
binding->mRecord.SetEvictionRank(ULONG_MAX - SecondsFromPRTime(PR_Now()));
|
2011-01-18 17:12:10 +03:00
|
|
|
// write entry data to disk cache block file
|
|
|
|
diskEntry->Swap();
|
|
|
|
|
|
|
|
if (fileIndex != 0) {
|
|
|
|
while (1) {
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t blockSize = GetBlockSizeForIndex(fileIndex);
|
|
|
|
uint32_t blocks = ((size - 1) / blockSize) + 1;
|
2011-01-18 17:12:10 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t startBlock;
|
2011-01-18 17:12:10 +03:00
|
|
|
rv = mBlockFile[fileIndex - 1].WriteBlocks(diskEntry, size, blocks,
|
|
|
|
&startBlock);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
// update binding and cache map record
|
|
|
|
binding->mRecord.SetMetaBlocks(fileIndex, startBlock, blocks);
|
|
|
|
|
|
|
|
rv = UpdateRecord(&binding->mRecord);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// XXX we should probably write out bucket ourselves
|
|
|
|
|
|
|
|
IncrementTotalSize(blocks, blockSize);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fileIndex == kNumBlockFiles) {
|
|
|
|
fileIndex = 0; // write data to separate file
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// try next block file
|
|
|
|
fileIndex++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-05-19 03:24:09 +04:00
|
|
|
if (fileIndex == 0) {
|
|
|
|
// Write entry data to separate file
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t metaFileSizeK = ((size + 0x03FF) >> 10); // round up to nearest 1k
|
2011-05-20 21:27:11 +04:00
|
|
|
if (metaFileSizeK > kMaxDataSizeK)
|
|
|
|
metaFileSizeK = kMaxDataSizeK;
|
|
|
|
|
2001-05-19 03:24:09 +04:00
|
|
|
binding->mRecord.SetMetaFileGeneration(binding->mGeneration);
|
|
|
|
binding->mRecord.SetMetaFileSize(metaFileSizeK);
|
|
|
|
rv = UpdateRecord(&binding->mRecord);
|
2007-12-29 07:22:54 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2001-04-06 02:28:17 +04:00
|
|
|
|
2012-06-06 06:08:30 +04:00
|
|
|
nsCOMPtr<nsIFile> localFile;
|
2001-05-19 03:24:09 +04:00
|
|
|
rv = GetLocalFileForDiskCacheRecord(&binding->mRecord,
|
|
|
|
nsDiskCache::kMetaData,
|
2011-10-17 18:59:28 +04:00
|
|
|
true,
|
2001-05-19 03:24:09 +04:00
|
|
|
getter_AddRefs(localFile));
|
2007-12-29 07:22:54 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2001-05-19 03:24:09 +04:00
|
|
|
|
|
|
|
// open the file
|
|
|
|
PRFileDesc * fd;
|
2005-06-10 21:32:48 +04:00
|
|
|
// open the file - restricted to user, the data could be confidential
|
|
|
|
rv = localFile->OpenNSPRFileDesc(PR_RDWR | PR_TRUNCATE | PR_CREATE_FILE, 00600, &fd);
|
2007-12-29 07:22:54 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2001-05-19 03:24:09 +04:00
|
|
|
|
|
|
|
// write the file
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t bytesWritten = PR_Write(fd, diskEntry, size);
|
2001-05-19 03:24:09 +04:00
|
|
|
|
2006-08-01 19:18:00 +04:00
|
|
|
PRStatus err = PR_Close(fd);
|
2012-08-22 19:56:38 +04:00
|
|
|
if ((bytesWritten != (int32_t)size) || (err != PR_SUCCESS)) {
|
2007-12-29 07:22:54 +03:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
2001-03-25 19:49:31 +04:00
|
|
|
}
|
2011-05-20 21:27:11 +04:00
|
|
|
|
2006-03-01 01:18:21 +03:00
|
|
|
IncrementTotalSize(metaFileSizeK);
|
2001-03-25 19:49:31 +04:00
|
|
|
}
|
2001-03-29 09:54:58 +04:00
|
|
|
|
2001-05-19 03:24:09 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-08-07 05:13:29 +04:00
|
|
|
nsresult
|
2012-08-22 19:56:38 +04:00
|
|
|
nsDiskCacheMap::ReadDataCacheBlocks(nsDiskCacheBinding * binding, char * buffer, uint32_t size)
|
2002-08-07 05:13:29 +04:00
|
|
|
{
|
2006-03-09 23:02:54 +03:00
|
|
|
CACHE_LOG_DEBUG(("CACHE: ReadDataCacheBlocks [%x size=%u]\n",
|
|
|
|
binding->mRecord.HashNumber(), size));
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t fileIndex = binding->mRecord.DataFile();
|
|
|
|
int32_t readSize = size;
|
2002-08-07 05:13:29 +04:00
|
|
|
|
2006-05-26 22:21:12 +04:00
|
|
|
nsresult rv = mBlockFile[fileIndex - 1].ReadBlocks(buffer,
|
|
|
|
binding->mRecord.DataStartBlock(),
|
|
|
|
binding->mRecord.DataBlockCount(),
|
|
|
|
&readSize);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-08-22 19:56:38 +04:00
|
|
|
if (readSize < (int32_t)size) {
|
2006-05-26 22:21:12 +04:00
|
|
|
rv = NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
2002-08-07 05:13:29 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
2012-08-22 19:56:38 +04:00
|
|
|
nsDiskCacheMap::WriteDataCacheBlocks(nsDiskCacheBinding * binding, char * buffer, uint32_t size)
|
2002-08-07 05:13:29 +04:00
|
|
|
{
|
2006-03-09 23:02:54 +03:00
|
|
|
CACHE_LOG_DEBUG(("CACHE: WriteDataCacheBlocks [%x size=%u]\n",
|
|
|
|
binding->mRecord.HashNumber(), size));
|
|
|
|
|
2006-05-26 22:21:12 +04:00
|
|
|
nsresult rv = NS_OK;
|
2002-08-07 05:13:29 +04:00
|
|
|
|
|
|
|
// determine block file & number of blocks
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t fileIndex = CalculateFileIndex(size);
|
|
|
|
uint32_t blockCount = 0;
|
|
|
|
int32_t startBlock = 0;
|
2011-01-18 17:12:10 +03:00
|
|
|
|
2002-08-07 05:13:29 +04:00
|
|
|
if (size > 0) {
|
2013-04-11 11:32:39 +04:00
|
|
|
// if fileIndex is 0, bad things happen below, which makes gcc 4.7
|
|
|
|
// complain, but it's not supposed to happen. See bug 854105.
|
|
|
|
MOZ_ASSERT(fileIndex);
|
|
|
|
while (fileIndex) {
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t blockSize = GetBlockSizeForIndex(fileIndex);
|
2011-01-18 17:12:10 +03:00
|
|
|
blockCount = ((size - 1) / blockSize) + 1;
|
2002-08-07 05:13:29 +04:00
|
|
|
|
2011-01-18 17:12:10 +03:00
|
|
|
rv = mBlockFile[fileIndex - 1].WriteBlocks(buffer, size, blockCount,
|
|
|
|
&startBlock);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
IncrementTotalSize(blockCount, blockSize);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fileIndex == kNumBlockFiles)
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
fileIndex++;
|
|
|
|
}
|
2002-08-07 05:13:29 +04:00
|
|
|
}
|
2011-01-18 17:12:10 +03:00
|
|
|
|
2002-08-07 05:13:29 +04:00
|
|
|
// update binding and cache map record
|
|
|
|
binding->mRecord.SetDataBlocks(fileIndex, startBlock, blockCount);
|
2006-03-31 01:06:54 +04:00
|
|
|
if (!binding->mDoomed) {
|
|
|
|
rv = UpdateRecord(&binding->mRecord);
|
|
|
|
}
|
2006-05-26 22:21:12 +04:00
|
|
|
return rv;
|
2002-08-07 05:13:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-05-19 03:24:09 +04:00
|
|
|
nsresult
|
|
|
|
nsDiskCacheMap::DeleteStorage(nsDiskCacheRecord * record)
|
|
|
|
{
|
|
|
|
nsresult rv1 = DeleteStorage(record, nsDiskCache::kData);
|
|
|
|
nsresult rv2 = DeleteStorage(record, nsDiskCache::kMetaData);
|
|
|
|
return NS_FAILED(rv1) ? rv1 : rv2;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
2011-09-29 10:19:26 +04:00
|
|
|
nsDiskCacheMap::DeleteStorage(nsDiskCacheRecord * record, bool metaData)
|
2001-05-19 03:24:09 +04:00
|
|
|
{
|
2006-03-09 23:02:54 +03:00
|
|
|
CACHE_LOG_DEBUG(("CACHE: DeleteStorage [%x %u]\n", record->HashNumber(),
|
|
|
|
metaData));
|
|
|
|
|
2001-06-28 05:30:26 +04:00
|
|
|
nsresult rv = NS_ERROR_UNEXPECTED;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t fileIndex = metaData ? record->MetaFile() : record->DataFile();
|
2001-05-19 03:24:09 +04:00
|
|
|
nsCOMPtr<nsIFile> file;
|
|
|
|
|
|
|
|
if (fileIndex == 0) {
|
|
|
|
// delete the file
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t sizeK = metaData ? record->MetaFileSize() : record->DataFileSize();
|
2001-05-19 03:24:09 +04:00
|
|
|
// XXX if sizeK == USHRT_MAX, stat file for actual size
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
rv = GetFileForDiskCacheRecord(record, metaData, false, getter_AddRefs(file));
|
2001-05-19 03:24:09 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
rv = file->Remove(false); // false == non-recursive
|
2001-05-19 03:24:09 +04:00
|
|
|
}
|
2006-03-01 01:18:21 +03:00
|
|
|
DecrementTotalSize(sizeK);
|
2001-05-19 03:24:09 +04:00
|
|
|
|
2011-01-18 17:12:10 +03:00
|
|
|
} else if (fileIndex < (kNumBlockFiles + 1)) {
|
2001-05-19 03:24:09 +04:00
|
|
|
// deallocate blocks
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t startBlock = metaData ? record->MetaStartBlock() : record->DataStartBlock();
|
|
|
|
uint32_t blockCount = metaData ? record->MetaBlockCount() : record->DataBlockCount();
|
2001-05-19 03:24:09 +04:00
|
|
|
|
|
|
|
rv = mBlockFile[fileIndex - 1].DeallocateBlocks(startBlock, blockCount);
|
2006-03-01 01:18:21 +03:00
|
|
|
DecrementTotalSize(blockCount, GetBlockSizeForIndex(fileIndex));
|
2001-05-19 03:24:09 +04:00
|
|
|
}
|
2002-08-07 05:13:29 +04:00
|
|
|
if (metaData) record->ClearMetaLocation();
|
|
|
|
else record->ClearDataLocation();
|
2001-04-06 02:28:17 +04:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2001-05-19 03:24:09 +04:00
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsDiskCacheMap::GetFileForDiskCacheRecord(nsDiskCacheRecord * record,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool meta,
|
|
|
|
bool createPath,
|
2002-08-07 05:13:29 +04:00
|
|
|
nsIFile ** result)
|
2001-05-19 03:24:09 +04:00
|
|
|
{
|
|
|
|
if (!mCacheDirectory) return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> file;
|
|
|
|
nsresult rv = mCacheDirectory->Clone(getter_AddRefs(file));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
2010-12-06 18:49:05 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t hash = record->HashNumber();
|
2010-12-06 18:49:05 +03:00
|
|
|
|
|
|
|
// The file is stored under subdirectories according to the hash number:
|
|
|
|
// 0x01234567 -> 0/12/
|
|
|
|
rv = file->AppendNative(nsPrintfCString("%X", hash >> 28));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
rv = file->AppendNative(nsPrintfCString("%02X", (hash >> 20) & 0xFF));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool exists;
|
2010-12-06 18:49:05 +03:00
|
|
|
if (createPath && (NS_FAILED(file->Exists(&exists)) || !exists)) {
|
2012-06-06 06:08:30 +04:00
|
|
|
rv = file->Create(nsIFile::DIRECTORY_TYPE, 0700);
|
2010-12-06 18:49:05 +03:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int16_t generation = record->Generation();
|
2001-05-19 03:24:09 +04:00
|
|
|
char name[32];
|
2010-12-06 18:49:05 +03:00
|
|
|
// Cut the beginning of the hash that was used in the path
|
2016-08-15 09:44:00 +03:00
|
|
|
::SprintfLiteral(name, "%05X%c%02X", hash & 0xFFFFF, (meta ? 'm' : 'd'),
|
|
|
|
generation);
|
2002-04-27 09:33:09 +04:00
|
|
|
rv = file->AppendNative(nsDependentCString(name));
|
2001-05-19 03:24:09 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2001-04-06 02:28:17 +04:00
|
|
|
|
2001-05-19 03:24:09 +04:00
|
|
|
NS_IF_ADDREF(*result = file);
|
|
|
|
return rv;
|
|
|
|
}
|
2001-04-06 02:28:17 +04:00
|
|
|
|
2002-08-07 05:13:29 +04:00
|
|
|
|
2001-05-19 03:24:09 +04:00
|
|
|
nsresult
|
|
|
|
nsDiskCacheMap::GetLocalFileForDiskCacheRecord(nsDiskCacheRecord * record,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool meta,
|
|
|
|
bool createPath,
|
2012-06-06 06:08:30 +04:00
|
|
|
nsIFile ** result)
|
2001-05-19 03:24:09 +04:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIFile> file;
|
2010-12-06 18:49:05 +03:00
|
|
|
nsresult rv = GetFileForDiskCacheRecord(record,
|
|
|
|
meta,
|
|
|
|
createPath,
|
|
|
|
getter_AddRefs(file));
|
2001-05-19 03:24:09 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2012-06-06 06:08:30 +04:00
|
|
|
NS_IF_ADDREF(*result = file);
|
2001-05-19 03:24:09 +04:00
|
|
|
return rv;
|
|
|
|
}
|
2001-04-06 02:28:17 +04:00
|
|
|
|
2001-05-19 03:24:09 +04:00
|
|
|
|
|
|
|
nsresult
|
2012-08-22 19:56:38 +04:00
|
|
|
nsDiskCacheMap::GetBlockFileForIndex(uint32_t index, nsIFile ** result)
|
2001-05-19 03:24:09 +04:00
|
|
|
{
|
|
|
|
if (!mCacheDirectory) return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> file;
|
|
|
|
nsresult rv = mCacheDirectory->Clone(getter_AddRefs(file));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
2001-03-25 19:49:31 +04:00
|
|
|
|
2001-05-19 03:24:09 +04:00
|
|
|
char name[32];
|
2016-08-15 09:44:00 +03:00
|
|
|
::SprintfLiteral(name, "_CACHE_%03d_", index + 1);
|
2002-04-27 09:33:09 +04:00
|
|
|
rv = file->AppendNative(nsDependentCString(name));
|
2001-05-19 03:24:09 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2012-06-06 06:08:30 +04:00
|
|
|
NS_IF_ADDREF(*result = file);
|
2001-05-19 03:24:09 +04:00
|
|
|
|
2001-03-25 19:49:31 +04:00
|
|
|
return rv;
|
|
|
|
}
|
2001-05-19 03:24:09 +04:00
|
|
|
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t
|
|
|
|
nsDiskCacheMap::CalculateFileIndex(uint32_t size)
|
2002-08-07 05:13:29 +04:00
|
|
|
{
|
2011-01-18 17:12:10 +03:00
|
|
|
// We prefer to use block file with larger block if the wasted space would
|
|
|
|
// be the same. E.g. store entry with size of 3073 bytes in 1 4K-block
|
|
|
|
// instead of in 4 1K-blocks.
|
|
|
|
|
|
|
|
if (size <= 3 * BLOCK_SIZE_FOR_INDEX(1)) return 1;
|
|
|
|
if (size <= 3 * BLOCK_SIZE_FOR_INDEX(2)) return 2;
|
|
|
|
if (size <= 4 * BLOCK_SIZE_FOR_INDEX(3)) return 3;
|
|
|
|
return 0;
|
2001-05-19 03:24:09 +04:00
|
|
|
}
|
2006-03-01 01:18:21 +03:00
|
|
|
|
2007-12-29 07:22:54 +03:00
|
|
|
nsresult
|
2012-08-22 19:56:38 +04:00
|
|
|
nsDiskCacheMap::EnsureBuffer(uint32_t bufSize)
|
2007-12-29 07:22:54 +03:00
|
|
|
{
|
|
|
|
if (mBufferSize < bufSize) {
|
2009-03-25 16:19:08 +03:00
|
|
|
char * buf = (char *)PR_REALLOC(mBuffer, bufSize);
|
2007-12-29 07:22:54 +03:00
|
|
|
if (!buf) {
|
|
|
|
mBufferSize = 0;
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
mBuffer = buf;
|
|
|
|
mBufferSize = bufSize;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2010-06-11 06:46:51 +04:00
|
|
|
|
|
|
|
void
|
2012-08-22 19:56:38 +04:00
|
|
|
nsDiskCacheMap::NotifyCapacityChange(uint32_t capacity)
|
2010-06-11 06:46:51 +04:00
|
|
|
{
|
|
|
|
// Heuristic 1. average cache entry size is probably around 1KB
|
|
|
|
// Heuristic 2. we don't want more than 32MB reserved to store the record
|
|
|
|
// map in memory.
|
2012-08-22 19:56:38 +04:00
|
|
|
const int32_t RECORD_COUNT_LIMIT = 32 * 1024 * 1024 / sizeof(nsDiskCacheRecord);
|
2013-01-15 16:22:03 +04:00
|
|
|
int32_t maxRecordCount = std::min(int32_t(capacity), RECORD_COUNT_LIMIT);
|
2010-06-11 06:46:51 +04:00
|
|
|
if (mMaxRecordCount < maxRecordCount) {
|
|
|
|
// We can only grow
|
|
|
|
mMaxRecordCount = maxRecordCount;
|
|
|
|
}
|
|
|
|
}
|
2012-08-20 16:08:46 +04:00
|
|
|
|
2012-11-05 22:22:33 +04:00
|
|
|
size_t
|
2013-06-23 16:03:39 +04:00
|
|
|
nsDiskCacheMap::SizeOfExcludingThis(MallocSizeOf aMallocSizeOf)
|
2012-11-05 22:22:33 +04:00
|
|
|
{
|
|
|
|
size_t usage = aMallocSizeOf(mRecordArray);
|
|
|
|
|
|
|
|
usage += aMallocSizeOf(mBuffer);
|
|
|
|
usage += aMallocSizeOf(mMapFD);
|
|
|
|
usage += aMallocSizeOf(mCleanFD);
|
|
|
|
usage += aMallocSizeOf(mCacheDirectory);
|
|
|
|
usage += aMallocSizeOf(mCleanCacheTimer);
|
|
|
|
|
|
|
|
for (int i = 0; i < kNumBlockFiles; i++) {
|
|
|
|
usage += mBlockFile[i].SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
|
|
|
|
return usage;
|
|
|
|
}
|
|
|
|
|
2012-08-20 16:08:46 +04:00
|
|
|
nsresult
|
|
|
|
nsDiskCacheMap::InitCacheClean(nsIFile * cacheDirectory,
|
2016-06-28 20:29:28 +03:00
|
|
|
nsDiskCache::CorruptCacheInfo * corruptInfo)
|
2012-08-20 16:08:46 +04:00
|
|
|
{
|
|
|
|
// The _CACHE_CLEAN_ file will be used in the future to determine
|
|
|
|
// if the cache is clean or not.
|
|
|
|
bool cacheCleanFileExists = false;
|
|
|
|
nsCOMPtr<nsIFile> cacheCleanFile;
|
2012-08-24 06:23:10 +04:00
|
|
|
nsresult rv = cacheDirectory->GetParent(getter_AddRefs(cacheCleanFile));
|
2012-08-20 16:08:46 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = cacheCleanFile->AppendNative(
|
|
|
|
NS_LITERAL_CSTRING("_CACHE_CLEAN_"));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
// Check if the file already exists, if it does, we will later read the
|
|
|
|
// value and report it to telemetry.
|
|
|
|
cacheCleanFile->Exists(&cacheCleanFileExists);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Could not build cache clean file path");
|
|
|
|
*corruptInfo = nsDiskCache::kCacheCleanFilePathError;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the _CACHE_CLEAN_ file exists
|
|
|
|
rv = cacheCleanFile->OpenNSPRFileDesc(PR_RDWR | PR_CREATE_FILE,
|
|
|
|
00600, &mCleanFD);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Could not open cache clean file");
|
|
|
|
*corruptInfo = nsDiskCache::kCacheCleanOpenFileError;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cacheCleanFileExists) {
|
|
|
|
char clean = '0';
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t bytesRead = PR_Read(mCleanFD, &clean, 1);
|
2012-08-20 16:08:46 +04:00
|
|
|
if (bytesRead != 1) {
|
|
|
|
NS_WARNING("Could not read _CACHE_CLEAN_ file contents");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a timer that will be used to validate the cache
|
|
|
|
// as long as an activity threshold was met
|
|
|
|
mCleanCacheTimer = do_CreateInstance("@mozilla.org/timer;1", &rv);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
mCleanCacheTimer->SetTarget(nsCacheService::GlobalInstance()->mCacheIOThread);
|
|
|
|
rv = ResetCacheTimer();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Could not create cache clean timer");
|
|
|
|
mCleanCacheTimer = nullptr;
|
|
|
|
*corruptInfo = nsDiskCache::kCacheCleanTimerError;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsDiskCacheMap::WriteCacheClean(bool clean)
|
|
|
|
{
|
|
|
|
nsCacheService::AssertOwnsLock();
|
2012-08-24 06:22:14 +04:00
|
|
|
if (!mCleanFD) {
|
|
|
|
NS_WARNING("Cache clean file is not open!");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2012-08-20 16:08:46 +04:00
|
|
|
CACHE_LOG_DEBUG(("CACHE: WriteCacheClean: %d\n", clean? 1 : 0));
|
|
|
|
// I'm using a simple '1' or '0' to denote cache clean
|
|
|
|
// since it can be edited easily by any text editor for testing.
|
|
|
|
char data = clean? '1' : '0';
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t filePos = PR_Seek(mCleanFD, 0, PR_SEEK_SET);
|
2012-08-20 16:08:46 +04:00
|
|
|
if (filePos != 0) {
|
2012-08-24 06:22:14 +04:00
|
|
|
NS_WARNING("Could not seek in cache clean file!");
|
2012-08-20 16:08:46 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t bytesWritten = PR_Write(mCleanFD, &data, 1);
|
2012-08-20 16:08:46 +04:00
|
|
|
if (bytesWritten != 1) {
|
2012-08-24 06:22:14 +04:00
|
|
|
NS_WARNING("Could not write cache clean file!");
|
2012-08-20 16:08:46 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
PRStatus err = PR_Sync(mCleanFD);
|
|
|
|
if (err != PR_SUCCESS) {
|
2012-08-24 06:22:14 +04:00
|
|
|
NS_WARNING("Could not flush cache clean file!");
|
2012-08-20 16:08:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsDiskCacheMap::InvalidateCache()
|
|
|
|
{
|
|
|
|
nsCacheService::AssertOwnsLock();
|
|
|
|
CACHE_LOG_DEBUG(("CACHE: InvalidateCache\n"));
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
if (!mIsDirtyCacheFlushed) {
|
|
|
|
rv = WriteCacheClean(false);
|
2012-09-04 23:05:19 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2012-08-20 16:08:46 +04:00
|
|
|
mIsDirtyCacheFlushed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = ResetCacheTimer();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-08-22 19:56:38 +04:00
|
|
|
nsDiskCacheMap::ResetCacheTimer(int32_t timeout)
|
2012-08-20 16:08:46 +04:00
|
|
|
{
|
|
|
|
mCleanCacheTimer->Cancel();
|
|
|
|
nsresult rv =
|
|
|
|
mCleanCacheTimer->InitWithFuncCallback(RevalidateTimerCallback,
|
2012-08-24 06:23:10 +04:00
|
|
|
nullptr, timeout,
|
2012-08-20 16:08:46 +04:00
|
|
|
nsITimer::TYPE_ONE_SHOT);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
mLastInvalidateTime = PR_IntervalNow();
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsDiskCacheMap::RevalidateTimerCallback(nsITimer *aTimer, void *arg)
|
|
|
|
{
|
2016-06-29 16:19:13 +03:00
|
|
|
nsCacheServiceAutoLock lock;
|
2012-08-24 06:23:10 +04:00
|
|
|
if (!nsCacheService::gService->mDiskDevice ||
|
|
|
|
!nsCacheService::gService->mDiskDevice->Initialized()) {
|
|
|
|
return;
|
|
|
|
}
|
2012-08-20 16:08:46 +04:00
|
|
|
|
2012-08-24 06:23:10 +04:00
|
|
|
nsDiskCacheMap *diskCacheMap =
|
|
|
|
&nsCacheService::gService->mDiskDevice->mCacheMap;
|
|
|
|
|
|
|
|
// If we have less than kRevalidateCacheTimeout since the last timer was
|
|
|
|
// issued then another thread called InvalidateCache. This won't catch
|
|
|
|
// all cases where we wanted to cancel the timer, but under the lock it
|
|
|
|
// is always OK to revalidate as long as IsCacheInSafeState() returns
|
|
|
|
// true. We just want to avoid revalidating when we can to reduce IO
|
|
|
|
// and this check will do that.
|
|
|
|
uint32_t delta =
|
|
|
|
PR_IntervalToMilliseconds(PR_IntervalNow() -
|
|
|
|
diskCacheMap->mLastInvalidateTime) +
|
|
|
|
kRevalidateCacheTimeoutTolerance;
|
|
|
|
if (delta < kRevalidateCacheTimeout) {
|
|
|
|
diskCacheMap->ResetCacheTimer();
|
|
|
|
return;
|
2012-08-20 16:08:46 +04:00
|
|
|
}
|
|
|
|
|
2012-08-24 06:23:10 +04:00
|
|
|
nsresult rv = diskCacheMap->RevalidateCache();
|
2012-08-20 16:08:46 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
diskCacheMap->ResetCacheTimer(kRevalidateCacheErrorTimeout);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
nsDiskCacheMap::IsCacheInSafeState()
|
|
|
|
{
|
|
|
|
return nsCacheService::GlobalInstance()->IsDoomListEmpty();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsDiskCacheMap::RevalidateCache()
|
|
|
|
{
|
|
|
|
CACHE_LOG_DEBUG(("CACHE: RevalidateCache\n"));
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
if (!IsCacheInSafeState()) {
|
2012-09-10 18:59:41 +04:00
|
|
|
CACHE_LOG_DEBUG(("CACHE: Revalidation should not performed because "
|
2012-08-20 16:08:46 +04:00
|
|
|
"cache not in a safe state\n"));
|
2012-09-10 18:59:41 +04:00
|
|
|
// Normally we would return an error here, but there is a bug where
|
|
|
|
// the doom list sometimes gets an entry 'stuck' and doens't clear it
|
|
|
|
// until browser shutdown. So we allow revalidation for the time being
|
|
|
|
// to get proper telemetry data of how much the cache corruption plan
|
|
|
|
// would help.
|
2012-08-20 16:08:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// If telemetry data shows it is worth it, we'll be flushing headers and
|
|
|
|
// records before flushing the clean cache file.
|
|
|
|
|
|
|
|
// Write out the _CACHE_CLEAN_ file with '1'
|
|
|
|
rv = WriteCacheClean(true);
|
2012-09-04 23:05:19 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2012-08-20 16:08:46 +04:00
|
|
|
mIsDirtyCacheFlushed = false;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|