2001-02-23 13:18:01 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
*
|
2012-05-21 11:12:37 +00: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-02-23 13:18:01 +00:00
|
|
|
|
2001-03-23 02:52:13 +00:00
|
|
|
#include <limits.h>
|
|
|
|
|
2012-12-14 23:58:45 +00:00
|
|
|
#include "mozilla/DebugOnly.h"
|
|
|
|
|
|
|
|
#include "nsCache.h"
|
2013-01-15 00:26:47 +00:00
|
|
|
#include "nsIMemoryReporter.h"
|
2012-12-14 23:58:45 +00:00
|
|
|
|
2002-08-07 01:13:29 +00:00
|
|
|
// include files for ftruncate (or equivalent)
|
Bug 627277 - Remove (broken) BeOS support. r=biesi,dwitte,gavin,joe,jorendorff,josh,khuey,mfinkle,neil,Pike,roc,shaver,smontagu,taras
2011-02-19 19:10:24 +00:00
|
|
|
#if defined(XP_UNIX)
|
2002-08-07 01:13:29 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#elif defined(XP_WIN)
|
|
|
|
#include <windows.h>
|
|
|
|
#elif defined(XP_OS2)
|
|
|
|
#define INCL_DOSERRORS
|
|
|
|
#include <os2.h>
|
|
|
|
#else
|
|
|
|
// XXX add necessary include file for ftruncate (or equivalent)
|
|
|
|
#endif
|
|
|
|
|
2003-03-10 23:19:05 +00:00
|
|
|
#include "prthread.h"
|
|
|
|
|
2002-08-07 01:13:29 +00:00
|
|
|
#include "private/pprio.h"
|
|
|
|
|
2001-02-23 13:18:01 +00:00
|
|
|
#include "nsDiskCacheDevice.h"
|
2001-03-20 22:42:03 +00:00
|
|
|
#include "nsDiskCacheEntry.h"
|
2001-03-29 05:54:58 +00:00
|
|
|
#include "nsDiskCacheMap.h"
|
2002-08-07 01:13:29 +00:00
|
|
|
#include "nsDiskCacheStreams.h"
|
|
|
|
|
2001-05-18 23:24:09 +00:00
|
|
|
#include "nsDiskCache.h"
|
|
|
|
|
2001-03-09 00:04:03 +00:00
|
|
|
#include "nsCacheService.h"
|
|
|
|
|
2005-04-26 00:37:23 +00:00
|
|
|
#include "nsDeleteDir.h"
|
|
|
|
|
2001-03-09 00:04:03 +00:00
|
|
|
#include "nsICacheVisitor.h"
|
2001-09-29 08:28:41 +00:00
|
|
|
#include "nsReadableUtils.h"
|
2001-03-01 17:31:06 +00:00
|
|
|
#include "nsIInputStream.h"
|
|
|
|
#include "nsIOutputStream.h"
|
2002-05-15 18:55:21 +00:00
|
|
|
#include "nsCRT.h"
|
2003-03-10 23:19:05 +00:00
|
|
|
#include "nsCOMArray.h"
|
|
|
|
#include "nsISimpleEnumerator.h"
|
2001-03-31 07:37:19 +00:00
|
|
|
|
2011-04-17 17:31:15 +00:00
|
|
|
#include "nsThreadUtils.h"
|
2013-06-23 12:03:39 +00:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
2011-07-21 00:02:58 +00:00
|
|
|
#include "mozilla/Telemetry.h"
|
2010-05-19 23:22:19 +00:00
|
|
|
|
2001-03-09 00:04:03 +00:00
|
|
|
static const char DISK_CACHE_DEVICE_ID[] = { "disk" };
|
2011-07-21 00:02:58 +00:00
|
|
|
using namespace mozilla;
|
2001-05-18 23:24:09 +00:00
|
|
|
|
2011-09-06 16:23:26 +00:00
|
|
|
class nsDiskCacheDeviceDeactivateEntryEvent : public nsRunnable {
|
|
|
|
public:
|
|
|
|
nsDiskCacheDeviceDeactivateEntryEvent(nsDiskCacheDevice *device,
|
|
|
|
nsCacheEntry * entry,
|
|
|
|
nsDiskCacheBinding * binding)
|
2011-10-17 14:59:28 +00:00
|
|
|
: mCanceled(false),
|
2011-09-06 16:23:26 +00:00
|
|
|
mEntry(entry),
|
|
|
|
mDevice(device),
|
|
|
|
mBinding(binding)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
2012-06-29 23:20:34 +00:00
|
|
|
nsCacheServiceAutoLock lock(LOCK_TELEM(NSDISKCACHEDEVICEDEACTIVATEENTRYEVENT_RUN));
|
2011-09-06 16:23:26 +00:00
|
|
|
#ifdef PR_LOGGING
|
|
|
|
CACHE_LOG_DEBUG(("nsDiskCacheDeviceDeactivateEntryEvent[%p]\n", this));
|
|
|
|
#endif
|
|
|
|
if (!mCanceled) {
|
|
|
|
(void) mDevice->DeactivateEntry_Private(mEntry, mBinding);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
void CancelEvent() { mCanceled = true; }
|
2011-09-06 16:23:26 +00:00
|
|
|
private:
|
2011-09-29 06:19:26 +00:00
|
|
|
bool mCanceled;
|
2011-09-06 16:23:26 +00:00
|
|
|
nsCacheEntry *mEntry;
|
|
|
|
nsDiskCacheDevice *mDevice;
|
|
|
|
nsDiskCacheBinding *mBinding;
|
|
|
|
};
|
|
|
|
|
2012-01-06 15:19:10 +00:00
|
|
|
class nsEvictDiskCacheEntriesEvent : public nsRunnable {
|
|
|
|
public:
|
|
|
|
nsEvictDiskCacheEntriesEvent(nsDiskCacheDevice *device)
|
|
|
|
: mDevice(device) {}
|
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
2012-06-29 23:20:34 +00:00
|
|
|
nsCacheServiceAutoLock lock(LOCK_TELEM(NSEVICTDISKCACHEENTRIESEVENT_RUN));
|
2012-01-06 15:19:10 +00:00
|
|
|
mDevice->EvictDiskCacheEntries(mDevice->mCacheCapacity);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsDiskCacheDevice *mDevice;
|
|
|
|
};
|
|
|
|
|
2001-05-18 23:24:09 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* nsDiskCacheEvictor
|
|
|
|
*
|
|
|
|
* Helper class for nsDiskCacheDevice.
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
class nsDiskCacheEvictor : public nsDiskCacheRecordVisitor
|
|
|
|
{
|
|
|
|
public:
|
2006-02-25 02:58:51 +00:00
|
|
|
nsDiskCacheEvictor( nsDiskCacheMap * cacheMap,
|
2001-05-18 23:24:09 +00:00
|
|
|
nsDiskCacheBindery * cacheBindery,
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t targetSize,
|
2001-05-18 23:24:09 +00:00
|
|
|
const char * clientID)
|
2006-02-25 02:58:51 +00:00
|
|
|
: mCacheMap(cacheMap)
|
2001-05-18 23:24:09 +00:00
|
|
|
, mBindery(cacheBindery)
|
|
|
|
, mTargetSize(targetSize)
|
|
|
|
, mClientID(clientID)
|
2006-02-25 02:58:51 +00:00
|
|
|
{
|
|
|
|
mClientIDSize = clientID ? strlen(clientID) : 0;
|
|
|
|
}
|
2001-05-18 23:24:09 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
virtual int32_t VisitRecord(nsDiskCacheRecord * mapRecord);
|
2001-05-18 23:24:09 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
nsDiskCacheMap * mCacheMap;
|
|
|
|
nsDiskCacheBindery * mBindery;
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t mTargetSize;
|
2001-05-18 23:24:09 +00:00
|
|
|
const char * mClientID;
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t mClientIDSize;
|
2001-05-18 23:24:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t
|
2001-05-18 23:24:09 +00:00
|
|
|
nsDiskCacheEvictor::VisitRecord(nsDiskCacheRecord * mapRecord)
|
2001-03-01 17:31:06 +00:00
|
|
|
{
|
2006-02-25 02:58:51 +00:00
|
|
|
if (mCacheMap->TotalSize() < mTargetSize)
|
|
|
|
return kStopVisitingRecords;
|
|
|
|
|
2001-05-18 23:24:09 +00:00
|
|
|
if (mClientID) {
|
2006-02-25 02:58:51 +00:00
|
|
|
// we're just evicting records for a specific client
|
2007-12-29 04:22:54 +00:00
|
|
|
nsDiskCacheEntry * diskEntry = mCacheMap->ReadDiskCacheEntry(mapRecord);
|
|
|
|
if (!diskEntry)
|
2006-02-25 02:58:51 +00:00
|
|
|
return kVisitNextRecord; // XXX or delete record?
|
|
|
|
|
|
|
|
// Compare clientID's without malloc
|
|
|
|
if ((diskEntry->mKeySize <= mClientIDSize) ||
|
2007-01-25 23:09:01 +00:00
|
|
|
(diskEntry->Key()[mClientIDSize] != ':') ||
|
|
|
|
(memcmp(diskEntry->Key(), mClientID, mClientIDSize) != 0)) {
|
2006-02-25 02:58:51 +00:00
|
|
|
return kVisitNextRecord; // clientID doesn't match, skip it
|
|
|
|
}
|
2001-05-18 23:24:09 +00:00
|
|
|
}
|
|
|
|
|
2006-02-25 02:58:51 +00:00
|
|
|
nsDiskCacheBinding * binding = mBindery->FindActiveBinding(mapRecord->HashNumber());
|
2001-05-18 23:24:09 +00:00
|
|
|
if (binding) {
|
2011-09-06 16:23:26 +00:00
|
|
|
// If the entry is pending deactivation, cancel deactivation and doom
|
|
|
|
// the entry
|
|
|
|
if (binding->mDeactivateEvent) {
|
|
|
|
binding->mDeactivateEvent->CancelEvent();
|
2012-07-30 14:20:58 +00:00
|
|
|
binding->mDeactivateEvent = nullptr;
|
2011-09-06 16:23:26 +00:00
|
|
|
}
|
2003-03-10 23:19:05 +00:00
|
|
|
// We are currently using this entry, so all we can do is doom it.
|
|
|
|
// Since we're enumerating the records, we don't want to call
|
|
|
|
// DeleteRecord when nsCacheService::DoomEntry() calls us back.
|
2011-10-17 14:59:28 +00:00
|
|
|
binding->mDoomed = true; // mark binding record as 'deleted'
|
2002-08-07 01:13:29 +00:00
|
|
|
nsCacheService::DoomEntry(binding->mCacheEntry);
|
2001-05-18 23:24:09 +00:00
|
|
|
} else {
|
|
|
|
// entry not in use, just delete storage because we're enumerating the records
|
|
|
|
(void) mCacheMap->DeleteStorage(mapRecord);
|
|
|
|
}
|
|
|
|
|
2006-02-25 02:58:51 +00:00
|
|
|
return kDeleteRecordAndContinue; // this will REALLY delete the record
|
2001-03-01 17:31:06 +00:00
|
|
|
}
|
|
|
|
|
2001-05-18 23:24:09 +00:00
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* nsDiskCacheDeviceInfo
|
|
|
|
*****************************************************************************/
|
2001-03-01 17:31:06 +00:00
|
|
|
|
2001-03-09 00:04:03 +00:00
|
|
|
class nsDiskCacheDeviceInfo : public nsICacheDeviceInfo {
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSICACHEDEVICEINFO
|
|
|
|
|
|
|
|
nsDiskCacheDeviceInfo(nsDiskCacheDevice* device)
|
|
|
|
: mDevice(device)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~nsDiskCacheDeviceInfo() {}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsDiskCacheDevice* mDevice;
|
|
|
|
};
|
2001-05-18 23:24:09 +00:00
|
|
|
|
2003-09-07 23:12:15 +00:00
|
|
|
NS_IMPL_ISUPPORTS1(nsDiskCacheDeviceInfo, nsICacheDeviceInfo)
|
2001-03-09 00:04:03 +00:00
|
|
|
|
|
|
|
/* readonly attribute string description; */
|
2001-03-09 19:01:15 +00:00
|
|
|
NS_IMETHODIMP nsDiskCacheDeviceInfo::GetDescription(char ** aDescription)
|
2001-03-09 00:04:03 +00:00
|
|
|
{
|
2001-03-29 05:54:58 +00:00
|
|
|
NS_ENSURE_ARG_POINTER(aDescription);
|
2007-05-14 20:09:20 +00:00
|
|
|
*aDescription = NS_strdup("Disk cache device");
|
2001-05-18 23:24:09 +00:00
|
|
|
return *aDescription ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
|
2001-03-09 00:04:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* readonly attribute string usageReport; */
|
2001-05-18 23:24:09 +00:00
|
|
|
NS_IMETHODIMP nsDiskCacheDeviceInfo::GetUsageReport(char ** usageReport)
|
2001-03-09 00:04:03 +00:00
|
|
|
{
|
2001-05-18 23:24:09 +00:00
|
|
|
NS_ENSURE_ARG_POINTER(usageReport);
|
|
|
|
nsCString buffer;
|
|
|
|
|
2010-07-02 20:56:09 +00:00
|
|
|
buffer.AssignLiteral(" <tr>\n"
|
|
|
|
" <th>Cache Directory:</th>\n"
|
|
|
|
" <td>");
|
2012-06-06 02:08:30 +00:00
|
|
|
nsCOMPtr<nsIFile> cacheDir;
|
|
|
|
nsAutoString path;
|
2001-05-18 23:24:09 +00:00
|
|
|
mDevice->getCacheDirectory(getter_AddRefs(cacheDir));
|
2002-04-27 05:33:09 +00:00
|
|
|
nsresult rv = cacheDir->GetPath(path);
|
2001-05-18 23:24:09 +00:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2003-12-23 16:48:40 +00:00
|
|
|
AppendUTF16toUTF8(path, buffer);
|
2001-05-18 23:24:09 +00:00
|
|
|
} else {
|
2004-12-04 10:19:29 +00:00
|
|
|
buffer.AppendLiteral("directory unavailable");
|
2001-05-18 23:24:09 +00:00
|
|
|
}
|
2010-07-02 20:56:09 +00:00
|
|
|
buffer.AppendLiteral("</td>\n"
|
|
|
|
" </tr>\n");
|
|
|
|
|
2001-09-29 08:28:41 +00:00
|
|
|
*usageReport = ToNewCString(buffer);
|
2001-05-18 23:24:09 +00:00
|
|
|
if (!*usageReport) return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
2001-03-09 19:01:15 +00:00
|
|
|
return NS_OK;
|
2001-03-09 00:04:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* readonly attribute unsigned long entryCount; */
|
2012-08-22 15:56:38 +00:00
|
|
|
NS_IMETHODIMP nsDiskCacheDeviceInfo::GetEntryCount(uint32_t *aEntryCount)
|
2001-03-09 00:04:03 +00:00
|
|
|
{
|
2001-03-29 05:54:58 +00:00
|
|
|
NS_ENSURE_ARG_POINTER(aEntryCount);
|
|
|
|
*aEntryCount = mDevice->getEntryCount();
|
|
|
|
return NS_OK;
|
2001-03-09 00:04:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* readonly attribute unsigned long totalSize; */
|
2012-08-22 15:56:38 +00:00
|
|
|
NS_IMETHODIMP nsDiskCacheDeviceInfo::GetTotalSize(uint32_t *aTotalSize)
|
2001-03-09 00:04:03 +00:00
|
|
|
{
|
2001-03-29 05:54:58 +00:00
|
|
|
NS_ENSURE_ARG_POINTER(aTotalSize);
|
2006-02-28 22:18:21 +00:00
|
|
|
// Returned unit's are in bytes
|
|
|
|
*aTotalSize = mDevice->getCacheSize() * 1024;
|
2001-03-09 19:01:15 +00:00
|
|
|
return NS_OK;
|
2001-03-09 00:04:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* readonly attribute unsigned long maximumSize; */
|
2012-08-22 15:56:38 +00:00
|
|
|
NS_IMETHODIMP nsDiskCacheDeviceInfo::GetMaximumSize(uint32_t *aMaximumSize)
|
2001-03-09 00:04:03 +00:00
|
|
|
{
|
2001-03-29 05:54:58 +00:00
|
|
|
NS_ENSURE_ARG_POINTER(aMaximumSize);
|
2006-02-28 22:18:21 +00:00
|
|
|
// Returned unit's are in bytes
|
|
|
|
*aMaximumSize = mDevice->getCacheCapacity() * 1024;
|
2001-03-09 19:01:15 +00:00
|
|
|
return NS_OK;
|
2001-03-09 00:04:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-05-18 23:24:09 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* nsDiskCache
|
|
|
|
*****************************************************************************/
|
2001-03-09 00:04:03 +00:00
|
|
|
|
2001-03-29 05:54:58 +00:00
|
|
|
/**
|
2009-03-27 17:58:06 +00:00
|
|
|
* nsDiskCache::Hash(const char * key, PLDHashNumber initval)
|
|
|
|
*
|
|
|
|
* See http://burtleburtle.net/bob/hash/evahash.html for more information
|
|
|
|
* about this hash function.
|
2001-05-18 23:24:09 +00:00
|
|
|
*
|
|
|
|
* This algorithm of this method implies nsDiskCacheRecords will be stored
|
|
|
|
* in a certain order on disk. If the algorithm changes, existing cache
|
|
|
|
* map files may become invalid, and therefore the kCurrentVersion needs
|
|
|
|
* to be revised.
|
2001-03-29 05:54:58 +00:00
|
|
|
*/
|
2009-03-27 17:58:06 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
static inline void hashmix(uint32_t& a, uint32_t& b, uint32_t& c)
|
2001-05-18 23:24:09 +00:00
|
|
|
{
|
2009-03-27 17:58:06 +00:00
|
|
|
a -= b; a -= c; a ^= (c>>13);
|
|
|
|
b -= c; b -= a; b ^= (a<<8);
|
|
|
|
c -= a; c -= b; c ^= (b>>13);
|
|
|
|
a -= b; a -= c; a ^= (c>>12);
|
|
|
|
b -= c; b -= a; b ^= (a<<16);
|
|
|
|
c -= a; c -= b; c ^= (b>>5);
|
|
|
|
a -= b; a -= c; a ^= (c>>3);
|
|
|
|
b -= c; b -= a; b ^= (a<<10);
|
|
|
|
c -= a; c -= b; c ^= (b>>15);
|
2001-05-18 23:24:09 +00:00
|
|
|
}
|
2001-03-29 05:54:58 +00:00
|
|
|
|
2009-03-27 17:58:06 +00:00
|
|
|
PLDHashNumber
|
|
|
|
nsDiskCache::Hash(const char * key, PLDHashNumber initval)
|
|
|
|
{
|
2012-08-22 15:56:38 +00:00
|
|
|
const uint8_t *k = reinterpret_cast<const uint8_t*>(key);
|
|
|
|
uint32_t a, b, c, len, length;
|
2009-03-27 17:58:06 +00:00
|
|
|
|
2013-05-08 03:40:12 +00:00
|
|
|
length = strlen(key);
|
2009-03-27 17:58:06 +00:00
|
|
|
/* Set up the internal state */
|
|
|
|
len = length;
|
|
|
|
a = b = 0x9e3779b9; /* the golden ratio; an arbitrary value */
|
|
|
|
c = initval; /* variable initialization of internal state */
|
|
|
|
|
|
|
|
/*---------------------------------------- handle most of the key */
|
|
|
|
while (len >= 12)
|
|
|
|
{
|
2012-08-22 15:56:38 +00:00
|
|
|
a += k[0] + (uint32_t(k[1])<<8) + (uint32_t(k[2])<<16) + (uint32_t(k[3])<<24);
|
|
|
|
b += k[4] + (uint32_t(k[5])<<8) + (uint32_t(k[6])<<16) + (uint32_t(k[7])<<24);
|
|
|
|
c += k[8] + (uint32_t(k[9])<<8) + (uint32_t(k[10])<<16) + (uint32_t(k[11])<<24);
|
2009-03-27 17:58:06 +00:00
|
|
|
hashmix(a, b, c);
|
|
|
|
k += 12; len -= 12;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*------------------------------------- handle the last 11 bytes */
|
|
|
|
c += length;
|
|
|
|
switch(len) { /* all the case statements fall through */
|
2012-08-22 15:56:38 +00:00
|
|
|
case 11: c += (uint32_t(k[10])<<24);
|
|
|
|
case 10: c += (uint32_t(k[9])<<16);
|
|
|
|
case 9 : c += (uint32_t(k[8])<<8);
|
2009-03-27 17:58:06 +00:00
|
|
|
/* the low-order byte of c is reserved for the length */
|
2012-08-22 15:56:38 +00:00
|
|
|
case 8 : b += (uint32_t(k[7])<<24);
|
|
|
|
case 7 : b += (uint32_t(k[6])<<16);
|
|
|
|
case 6 : b += (uint32_t(k[5])<<8);
|
2009-03-27 17:58:06 +00:00
|
|
|
case 5 : b += k[4];
|
2012-08-22 15:56:38 +00:00
|
|
|
case 4 : a += (uint32_t(k[3])<<24);
|
|
|
|
case 3 : a += (uint32_t(k[2])<<16);
|
|
|
|
case 2 : a += (uint32_t(k[1])<<8);
|
2009-03-27 17:58:06 +00:00
|
|
|
case 1 : a += k[0];
|
|
|
|
/* case 0: nothing left to add */
|
|
|
|
}
|
|
|
|
hashmix(a, b, c);
|
|
|
|
|
|
|
|
return c;
|
|
|
|
}
|
2001-03-29 05:54:58 +00:00
|
|
|
|
2002-08-07 01:13:29 +00:00
|
|
|
nsresult
|
2012-08-22 15:56:38 +00:00
|
|
|
nsDiskCache::Truncate(PRFileDesc * fd, uint32_t newEOF)
|
2002-08-07 01:13:29 +00:00
|
|
|
{
|
|
|
|
// use modified SetEOF from nsFileStreams::SetEOF()
|
|
|
|
|
Bug 627277 - Remove (broken) BeOS support. r=biesi,dwitte,gavin,joe,jorendorff,josh,khuey,mfinkle,neil,Pike,roc,shaver,smontagu,taras
2011-02-19 19:10:24 +00:00
|
|
|
#if defined(XP_UNIX)
|
2002-08-07 01:13:29 +00:00
|
|
|
if (ftruncate(PR_FileDesc2NativeHandle(fd), newEOF) != 0) {
|
|
|
|
NS_ERROR("ftruncate failed");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
#elif defined(XP_WIN)
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t cnt = PR_Seek(fd, newEOF, PR_SEEK_SET);
|
2002-08-07 01:13:29 +00:00
|
|
|
if (cnt == -1) return NS_ERROR_FAILURE;
|
|
|
|
if (!SetEndOfFile((HANDLE) PR_FileDesc2NativeHandle(fd))) {
|
|
|
|
NS_ERROR("SetEndOfFile failed");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
#elif defined(XP_OS2)
|
|
|
|
if (DosSetFileSize((HFILE) PR_FileDesc2NativeHandle(fd), newEOF) != NO_ERROR) {
|
|
|
|
NS_ERROR("DosSetFileSize failed");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
// add implementations for other platforms here
|
|
|
|
#endif
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-05-18 23:24:09 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* nsDiskCacheDevice
|
|
|
|
*****************************************************************************/
|
2001-03-13 04:54:14 +00:00
|
|
|
|
2001-02-28 04:00:08 +00:00
|
|
|
nsDiskCacheDevice::nsDiskCacheDevice()
|
2002-08-29 04:30:54 +00:00
|
|
|
: mCacheCapacity(0)
|
2011-06-27 12:21:15 +00:00
|
|
|
, mMaxEntrySize(-1) // -1 means "no limit"
|
2011-10-17 14:59:28 +00:00
|
|
|
, mInitialized(false)
|
2012-04-12 16:27:38 +00:00
|
|
|
, mClearingDiskCache(false)
|
2001-02-28 04:00:08 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsDiskCacheDevice::~nsDiskCacheDevice()
|
|
|
|
{
|
2001-03-29 05:54:58 +00:00
|
|
|
Shutdown();
|
2001-02-28 04:00:08 +00:00
|
|
|
}
|
2001-02-23 13:18:01 +00:00
|
|
|
|
2001-05-18 23:24:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* methods of nsCacheDevice
|
|
|
|
*/
|
2001-02-23 13:18:01 +00:00
|
|
|
nsresult
|
|
|
|
nsDiskCacheDevice::Init()
|
|
|
|
{
|
2001-04-05 22:28:17 +00:00
|
|
|
nsresult rv;
|
2001-08-17 00:01:01 +00:00
|
|
|
|
2010-09-17 03:21:16 +00:00
|
|
|
if (Initialized()) {
|
|
|
|
NS_ERROR("Disk cache already initialized!");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
2002-08-07 01:13:29 +00:00
|
|
|
|
2006-02-25 02:58:51 +00:00
|
|
|
if (!mCacheDirectory)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2001-05-18 23:24:09 +00:00
|
|
|
rv = mBindery.Init();
|
2006-02-25 02:58:51 +00:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
2011-12-02 17:10:57 +00:00
|
|
|
|
|
|
|
nsDeleteDir::RemoveOldTrashes(mCacheDirectory);
|
|
|
|
|
2003-03-10 23:19:05 +00:00
|
|
|
// Open Disk Cache
|
|
|
|
rv = OpenDiskCache();
|
|
|
|
if (NS_FAILED(rv)) {
|
2011-10-17 14:59:28 +00:00
|
|
|
(void) mCacheMap.Close(false);
|
2006-02-25 02:58:51 +00:00
|
|
|
return rv;
|
2001-05-18 23:24:09 +00:00
|
|
|
}
|
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
mInitialized = true;
|
2001-03-12 19:31:21 +00:00
|
|
|
return NS_OK;
|
2001-02-23 13:18:01 +00:00
|
|
|
}
|
|
|
|
|
2001-05-18 23:24:09 +00:00
|
|
|
|
2002-08-29 04:30:54 +00:00
|
|
|
/**
|
|
|
|
* NOTE: called while holding the cache service lock
|
|
|
|
*/
|
2001-03-29 05:54:58 +00:00
|
|
|
nsresult
|
|
|
|
nsDiskCacheDevice::Shutdown()
|
|
|
|
{
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-04-01 04:29:02 +00:00
|
|
|
nsCacheService::AssertOwnsLock();
|
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
nsresult rv = Shutdown_Private(true);
|
2006-01-24 14:47:06 +00:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
return NS_OK;
|
2003-03-10 23:19:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
2011-09-29 06:19:26 +00:00
|
|
|
nsDiskCacheDevice::Shutdown_Private(bool flush)
|
2003-03-10 23:19:05 +00:00
|
|
|
{
|
2006-03-09 20:02:54 +00:00
|
|
|
CACHE_LOG_DEBUG(("CACHE: disk Shutdown_Private [%u]\n", flush));
|
|
|
|
|
2006-01-24 14:47:06 +00:00
|
|
|
if (Initialized()) {
|
2002-08-29 04:30:54 +00:00
|
|
|
// check cache limits in case we need to evict.
|
2006-02-28 22:18:21 +00:00
|
|
|
EvictDiskCacheEntries(mCacheCapacity);
|
2001-03-29 05:54:58 +00:00
|
|
|
|
2011-01-20 21:41:10 +00:00
|
|
|
// At this point there may be a number of pending cache-requests on the
|
|
|
|
// cache-io thread. Wait for all these to run before we wipe out our
|
|
|
|
// datastructures (see bug #620660)
|
|
|
|
(void) nsCacheService::SyncWithCacheIOThread();
|
|
|
|
|
2002-08-29 04:30:54 +00:00
|
|
|
// write out persistent information about the cache.
|
2006-02-25 02:58:51 +00:00
|
|
|
(void) mCacheMap.Close(flush);
|
2001-08-17 00:01:01 +00:00
|
|
|
|
2002-08-29 04:30:54 +00:00
|
|
|
mBindery.Reset();
|
2001-03-29 05:54:58 +00:00
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
mInitialized = false;
|
2001-03-29 05:54:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-02-24 01:02:37 +00:00
|
|
|
const char *
|
|
|
|
nsDiskCacheDevice::GetDeviceID()
|
|
|
|
{
|
2001-03-09 00:04:03 +00:00
|
|
|
return DISK_CACHE_DEVICE_ID;
|
2001-02-24 01:02:37 +00:00
|
|
|
}
|
|
|
|
|
2001-05-18 23:24:09 +00:00
|
|
|
/**
|
|
|
|
* FindEntry -
|
|
|
|
*
|
|
|
|
* cases: key not in disk cache, hash number free
|
|
|
|
* key not in disk cache, hash number used
|
|
|
|
* key in disk cache
|
2002-08-29 04:30:54 +00:00
|
|
|
*
|
|
|
|
* NOTE: called while holding the cache service lock
|
2001-05-18 23:24:09 +00:00
|
|
|
*/
|
2001-02-27 05:35:53 +00:00
|
|
|
nsCacheEntry *
|
2011-09-29 06:19:26 +00:00
|
|
|
nsDiskCacheDevice::FindEntry(nsCString * key, bool *collision)
|
2001-02-23 13:18:01 +00:00
|
|
|
{
|
2012-08-23 17:23:03 +00:00
|
|
|
Telemetry::AutoTimer<Telemetry::CACHE_DISK_SEARCH_2> timer;
|
2012-07-30 14:20:58 +00:00
|
|
|
if (!Initialized()) return nullptr; // NS_ERROR_NOT_INITIALIZED
|
|
|
|
if (mClearingDiskCache) return nullptr;
|
2001-05-18 23:24:09 +00:00
|
|
|
nsDiskCacheRecord record;
|
2012-07-30 14:20:58 +00:00
|
|
|
nsDiskCacheBinding * binding = nullptr;
|
2001-05-18 23:24:09 +00:00
|
|
|
PLDHashNumber hashNumber = nsDiskCache::Hash(key->get());
|
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
*collision = false;
|
2006-12-01 04:41:30 +00:00
|
|
|
|
2001-05-18 23:24:09 +00:00
|
|
|
binding = mBindery.FindActiveBinding(hashNumber);
|
2009-02-19 17:23:21 +00:00
|
|
|
if (binding && !binding->mCacheEntry->Key()->Equals(*key)) {
|
2011-10-17 14:59:28 +00:00
|
|
|
*collision = true;
|
2012-07-30 14:20:58 +00:00
|
|
|
return nullptr;
|
2011-04-17 17:31:15 +00:00
|
|
|
} else if (binding && binding->mDeactivateEvent) {
|
|
|
|
binding->mDeactivateEvent->CancelEvent();
|
2012-07-30 14:20:58 +00:00
|
|
|
binding->mDeactivateEvent = nullptr;
|
2011-04-17 17:31:15 +00:00
|
|
|
CACHE_LOG_DEBUG(("CACHE: reusing deactivated entry %p " \
|
|
|
|
"req-key=%s entry-key=%s\n",
|
|
|
|
binding->mCacheEntry, key, binding->mCacheEntry->Key()));
|
|
|
|
|
|
|
|
return binding->mCacheEntry; // just return this one, observing that
|
|
|
|
// FindActiveBinding() does not return
|
|
|
|
// bindings to doomed entries
|
2008-09-26 13:33:36 +00:00
|
|
|
}
|
2012-07-30 14:20:58 +00:00
|
|
|
binding = nullptr;
|
2008-09-26 13:33:36 +00:00
|
|
|
|
2001-05-18 23:24:09 +00:00
|
|
|
// lookup hash number in cache map
|
2007-12-29 04:22:54 +00:00
|
|
|
nsresult rv = mCacheMap.FindRecord(hashNumber, &record);
|
2012-07-30 14:20:58 +00:00
|
|
|
if (NS_FAILED(rv)) return nullptr; // XXX log error?
|
2001-05-18 23:24:09 +00:00
|
|
|
|
2007-12-29 04:22:54 +00:00
|
|
|
nsDiskCacheEntry * diskEntry = mCacheMap.ReadDiskCacheEntry(&record);
|
2012-07-30 14:20:58 +00:00
|
|
|
if (!diskEntry) return nullptr;
|
2001-05-18 23:24:09 +00:00
|
|
|
|
|
|
|
// compare key to be sure
|
2009-02-19 17:23:21 +00:00
|
|
|
if (!key->Equals(diskEntry->Key())) {
|
2011-10-17 14:59:28 +00:00
|
|
|
*collision = true;
|
2012-07-30 14:20:58 +00:00
|
|
|
return nullptr;
|
2001-05-18 23:24:09 +00:00
|
|
|
}
|
|
|
|
|
2007-12-29 04:22:54 +00:00
|
|
|
nsCacheEntry * entry = diskEntry->CreateCacheEntry(this);
|
2012-03-30 09:21:01 +00:00
|
|
|
if (entry) {
|
|
|
|
binding = mBindery.CreateBinding(entry, &record);
|
|
|
|
if (!binding) {
|
|
|
|
delete entry;
|
2012-07-30 14:20:58 +00:00
|
|
|
entry = nullptr;
|
2012-03-30 09:21:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!entry) {
|
|
|
|
(void) mCacheMap.DeleteStorage(&record);
|
|
|
|
(void) mCacheMap.DeleteRecord(&record);
|
2001-03-02 04:57:09 +00:00
|
|
|
}
|
2001-05-18 23:24:09 +00:00
|
|
|
|
2001-03-02 04:57:09 +00:00
|
|
|
return entry;
|
2001-02-23 13:18:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-08-29 04:30:54 +00:00
|
|
|
/**
|
|
|
|
* NOTE: called while holding the cache service lock
|
|
|
|
*/
|
2001-02-23 13:18:01 +00:00
|
|
|
nsresult
|
|
|
|
nsDiskCacheDevice::DeactivateEntry(nsCacheEntry * entry)
|
|
|
|
{
|
2001-05-18 23:24:09 +00:00
|
|
|
nsDiskCacheBinding * binding = GetCacheEntryBinding(entry);
|
2011-04-17 17:31:15 +00:00
|
|
|
if (!IsValidBinding(binding))
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
2001-04-05 22:28:17 +00:00
|
|
|
|
2006-03-09 20:02:54 +00:00
|
|
|
CACHE_LOG_DEBUG(("CACHE: disk DeactivateEntry [%p %x]\n",
|
|
|
|
entry, binding->mRecord.HashNumber()));
|
|
|
|
|
2011-04-17 17:31:15 +00:00
|
|
|
nsDiskCacheDeviceDeactivateEntryEvent *event =
|
|
|
|
new nsDiskCacheDeviceDeactivateEntryEvent(this, entry, binding);
|
|
|
|
|
|
|
|
// ensure we can cancel the event via the binding later if necessary
|
|
|
|
binding->mDeactivateEvent = event;
|
|
|
|
|
2012-10-25 19:47:55 +00:00
|
|
|
DebugOnly<nsresult> rv = nsCacheService::DispatchToCacheIOThread(event);
|
2011-04-17 17:31:15 +00:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "DeactivateEntry: Failed dispatching "
|
|
|
|
"deactivation event");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* NOTE: called while holding the cache service lock
|
|
|
|
*/
|
|
|
|
nsresult
|
|
|
|
nsDiskCacheDevice::DeactivateEntry_Private(nsCacheEntry * entry,
|
|
|
|
nsDiskCacheBinding * binding)
|
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
2001-05-18 23:24:09 +00:00
|
|
|
if (entry->IsDoomed()) {
|
|
|
|
// delete data, entry, record from disk for entry
|
2006-02-25 02:58:51 +00:00
|
|
|
rv = mCacheMap.DeleteStorage(&binding->mRecord);
|
2001-04-04 03:30:45 +00:00
|
|
|
|
2001-05-18 23:24:09 +00:00
|
|
|
} else {
|
|
|
|
// save stuff to disk for entry
|
2006-02-25 02:58:51 +00:00
|
|
|
rv = mCacheMap.WriteDiskCacheEntry(binding);
|
2001-05-18 23:24:09 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// clean up as best we can
|
2006-02-25 02:58:51 +00:00
|
|
|
(void) mCacheMap.DeleteStorage(&binding->mRecord);
|
|
|
|
(void) mCacheMap.DeleteRecord(&binding->mRecord);
|
2011-10-17 14:59:28 +00:00
|
|
|
binding->mDoomed = true; // record is no longer in cache map
|
2001-03-11 00:19:09 +00:00
|
|
|
}
|
2001-03-03 03:41:38 +00:00
|
|
|
}
|
2001-03-02 04:57:09 +00:00
|
|
|
|
2001-05-18 23:24:09 +00:00
|
|
|
mBindery.RemoveBinding(binding); // extract binding from collision detection stuff
|
|
|
|
delete entry; // which will release binding
|
|
|
|
return rv;
|
2001-02-23 13:18:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-05-18 23:24:09 +00:00
|
|
|
/**
|
|
|
|
* BindEntry()
|
|
|
|
* no hash number collision -> no problem
|
|
|
|
* collision
|
|
|
|
* record not active -> evict, no problem
|
|
|
|
* record is active
|
|
|
|
* record is already doomed -> record shouldn't have been in map, no problem
|
|
|
|
* record is not doomed -> doom, and replace record in map
|
|
|
|
*
|
|
|
|
* walk matching hashnumber list to find lowest generation number
|
|
|
|
* take generation number from other (data/meta) location,
|
|
|
|
* or walk active list
|
2002-08-29 04:30:54 +00:00
|
|
|
*
|
|
|
|
* NOTE: called while holding the cache service lock
|
2001-05-18 23:24:09 +00:00
|
|
|
*/
|
2001-02-23 13:18:01 +00:00
|
|
|
nsresult
|
2001-05-18 23:24:09 +00:00
|
|
|
nsDiskCacheDevice::BindEntry(nsCacheEntry * entry)
|
2001-02-23 13:18:01 +00:00
|
|
|
{
|
2003-05-23 05:44:15 +00:00
|
|
|
if (!Initialized()) return NS_ERROR_NOT_INITIALIZED;
|
2012-04-12 16:27:38 +00:00
|
|
|
if (mClearingDiskCache) return NS_ERROR_NOT_AVAILABLE;
|
2001-05-18 23:24:09 +00:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
nsDiskCacheRecord record, oldRecord;
|
2009-02-19 17:23:21 +00:00
|
|
|
nsDiskCacheBinding *binding;
|
|
|
|
PLDHashNumber hashNumber = nsDiskCache::Hash(entry->Key()->get());
|
|
|
|
|
|
|
|
// Find out if there is already an active binding for this hash. If yes it
|
|
|
|
// should have another key since BindEntry() shouldn't be called twice for
|
|
|
|
// the same entry. Doom the old entry, the new one will get another
|
|
|
|
// generation number so files won't collide.
|
|
|
|
binding = mBindery.FindActiveBinding(hashNumber);
|
|
|
|
if (binding) {
|
|
|
|
NS_ASSERTION(!binding->mCacheEntry->Key()->Equals(*entry->Key()),
|
|
|
|
"BindEntry called for already bound entry!");
|
2011-09-06 16:23:26 +00:00
|
|
|
// If the entry is pending deactivation, cancel deactivation
|
|
|
|
if (binding->mDeactivateEvent) {
|
|
|
|
binding->mDeactivateEvent->CancelEvent();
|
2012-07-30 14:20:58 +00:00
|
|
|
binding->mDeactivateEvent = nullptr;
|
2011-09-06 16:23:26 +00:00
|
|
|
}
|
2009-02-19 17:23:21 +00:00
|
|
|
nsCacheService::DoomEntry(binding->mCacheEntry);
|
2012-07-30 14:20:58 +00:00
|
|
|
binding = nullptr;
|
2009-02-19 17:23:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Lookup hash number in cache map. There can be a colliding inactive entry.
|
|
|
|
// See bug #321361 comment 21 for the scenario. If there is such entry,
|
|
|
|
// delete it.
|
|
|
|
rv = mCacheMap.FindRecord(hashNumber, &record);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
nsDiskCacheEntry * diskEntry = mCacheMap.ReadDiskCacheEntry(&record);
|
|
|
|
if (diskEntry) {
|
|
|
|
// compare key to be sure
|
|
|
|
if (!entry->Key()->Equals(diskEntry->Key())) {
|
|
|
|
mCacheMap.DeleteStorage(&record);
|
|
|
|
rv = mCacheMap.DeleteRecord(&record);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
record = nsDiskCacheRecord();
|
|
|
|
}
|
|
|
|
|
2001-05-18 23:24:09 +00:00
|
|
|
// create a new record for this entry
|
|
|
|
record.SetHashNumber(nsDiskCache::Hash(entry->Key()->get()));
|
2001-05-21 22:05:10 +00:00
|
|
|
record.SetEvictionRank(ULONG_MAX - SecondsFromPRTime(PR_Now()));
|
2001-05-18 23:24:09 +00:00
|
|
|
|
2006-03-09 20:02:54 +00:00
|
|
|
CACHE_LOG_DEBUG(("CACHE: disk BindEntry [%p %x]\n",
|
|
|
|
entry, record.HashNumber()));
|
|
|
|
|
2001-05-18 23:24:09 +00:00
|
|
|
if (!entry->IsDoomed()) {
|
|
|
|
// if entry isn't doomed, add it to the cache map
|
2006-02-25 02:58:51 +00:00
|
|
|
rv = mCacheMap.AddRecord(&record, &oldRecord); // deletes old record, if any
|
2001-05-18 23:24:09 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t oldHashNumber = oldRecord.HashNumber();
|
2001-05-18 23:24:09 +00:00
|
|
|
if (oldHashNumber) {
|
|
|
|
// gotta evict this one first
|
|
|
|
nsDiskCacheBinding * oldBinding = mBindery.FindActiveBinding(oldHashNumber);
|
|
|
|
if (oldBinding) {
|
|
|
|
// XXX if debug : compare keys for hashNumber collision
|
|
|
|
|
|
|
|
if (!oldBinding->mCacheEntry->IsDoomed()) {
|
2011-09-06 16:23:26 +00:00
|
|
|
// If the old entry is pending deactivation, cancel deactivation
|
|
|
|
if (oldBinding->mDeactivateEvent) {
|
|
|
|
oldBinding->mDeactivateEvent->CancelEvent();
|
2012-07-30 14:20:58 +00:00
|
|
|
oldBinding->mDeactivateEvent = nullptr;
|
2011-09-06 16:23:26 +00:00
|
|
|
}
|
2001-05-18 23:24:09 +00:00
|
|
|
// we've got a live one!
|
2002-08-07 01:13:29 +00:00
|
|
|
nsCacheService::DoomEntry(oldBinding->mCacheEntry);
|
2001-05-18 23:24:09 +00:00
|
|
|
// storage will be delete when oldBinding->mCacheEntry is Deactivated
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// delete storage
|
|
|
|
// XXX if debug : compare keys for hashNumber collision
|
2006-02-25 02:58:51 +00:00
|
|
|
rv = mCacheMap.DeleteStorage(&oldRecord);
|
2001-05-18 23:24:09 +00:00
|
|
|
if (NS_FAILED(rv)) return rv; // XXX delete record we just added?
|
|
|
|
}
|
2001-03-11 00:19:09 +00:00
|
|
|
}
|
|
|
|
}
|
2001-05-18 23:24:09 +00:00
|
|
|
|
|
|
|
// Make sure this entry has its associated nsDiskCacheBinding attached.
|
2009-02-19 17:23:21 +00:00
|
|
|
binding = mBindery.CreateBinding(entry, &record);
|
2001-05-18 23:24:09 +00:00
|
|
|
NS_ASSERTION(binding, "nsDiskCacheDevice::BindEntry");
|
|
|
|
if (!binding) return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
NS_ASSERTION(binding->mRecord.ValidRecord(), "bad cache map record");
|
2001-03-11 00:19:09 +00:00
|
|
|
|
2001-05-18 23:24:09 +00:00
|
|
|
return NS_OK;
|
2001-02-28 04:00:08 +00:00
|
|
|
}
|
2001-02-28 03:42:33 +00:00
|
|
|
|
2001-03-08 05:37:00 +00:00
|
|
|
|
2002-08-29 04:30:54 +00:00
|
|
|
/**
|
|
|
|
* NOTE: called while holding the cache service lock
|
|
|
|
*/
|
2001-03-08 05:37:00 +00:00
|
|
|
void
|
2001-02-28 03:42:33 +00:00
|
|
|
nsDiskCacheDevice::DoomEntry(nsCacheEntry * entry)
|
|
|
|
{
|
2006-03-09 20:02:54 +00:00
|
|
|
CACHE_LOG_DEBUG(("CACHE: disk DoomEntry [%p]\n", entry));
|
|
|
|
|
2001-05-18 23:24:09 +00:00
|
|
|
nsDiskCacheBinding * binding = GetCacheEntryBinding(entry);
|
2012-07-30 14:20:58 +00:00
|
|
|
NS_ASSERTION(binding, "DoomEntry: binding == nullptr");
|
2011-04-17 17:31:15 +00:00
|
|
|
if (!binding)
|
|
|
|
return;
|
2001-05-18 23:24:09 +00:00
|
|
|
|
|
|
|
if (!binding->mDoomed) {
|
|
|
|
// so it can't be seen by FindEntry() ever again.
|
2008-09-15 13:34:46 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
nsresult rv =
|
|
|
|
#endif
|
|
|
|
mCacheMap.DeleteRecord(&binding->mRecord);
|
2006-02-25 02:58:51 +00:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv),"DeleteRecord failed.");
|
2011-10-17 14:59:28 +00:00
|
|
|
binding->mDoomed = true; // record in no longer in cache map
|
2001-05-18 23:24:09 +00:00
|
|
|
}
|
2001-02-28 03:42:33 +00:00
|
|
|
}
|
|
|
|
|
2003-03-10 23:19:05 +00:00
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
/**
|
|
|
|
* NOTE: called while holding the cache service lock
|
|
|
|
*/
|
|
|
|
nsresult
|
|
|
|
nsDiskCacheDevice::OpenInputStreamForEntry(nsCacheEntry * entry,
|
|
|
|
nsCacheAccessMode mode,
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t offset,
|
2003-01-18 02:15:14 +00:00
|
|
|
nsIInputStream ** result)
|
|
|
|
{
|
2006-03-09 20:02:54 +00:00
|
|
|
CACHE_LOG_DEBUG(("CACHE: disk OpenInputStreamForEntry [%p %x %u]\n",
|
|
|
|
entry, mode, offset));
|
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
NS_ENSURE_ARG_POINTER(entry);
|
|
|
|
NS_ENSURE_ARG_POINTER(result);
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
nsDiskCacheBinding * binding = GetCacheEntryBinding(entry);
|
2011-04-17 17:31:15 +00:00
|
|
|
if (!IsValidBinding(binding))
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
NS_ASSERTION(binding->mCacheEntry == entry, "binding & entry don't point to each other");
|
|
|
|
|
|
|
|
rv = binding->EnsureStreamIO();
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
return binding->mStreamIO->GetInputStream(offset, result);
|
|
|
|
}
|
2001-03-08 05:37:00 +00:00
|
|
|
|
2003-03-10 23:19:05 +00:00
|
|
|
|
2002-08-29 04:30:54 +00:00
|
|
|
/**
|
|
|
|
* NOTE: called while holding the cache service lock
|
|
|
|
*/
|
2001-02-24 01:02:37 +00:00
|
|
|
nsresult
|
2003-01-18 02:15:14 +00:00
|
|
|
nsDiskCacheDevice::OpenOutputStreamForEntry(nsCacheEntry * entry,
|
|
|
|
nsCacheAccessMode mode,
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t offset,
|
2003-01-18 02:15:14 +00:00
|
|
|
nsIOutputStream ** result)
|
2001-02-24 01:02:37 +00:00
|
|
|
{
|
2006-03-09 20:02:54 +00:00
|
|
|
CACHE_LOG_DEBUG(("CACHE: disk OpenOutputStreamForEntry [%p %x %u]\n",
|
|
|
|
entry, mode, offset));
|
|
|
|
|
2001-02-28 04:00:08 +00:00
|
|
|
NS_ENSURE_ARG_POINTER(entry);
|
|
|
|
NS_ENSURE_ARG_POINTER(result);
|
|
|
|
|
2001-05-18 23:24:09 +00:00
|
|
|
nsresult rv;
|
|
|
|
nsDiskCacheBinding * binding = GetCacheEntryBinding(entry);
|
2011-04-17 17:31:15 +00:00
|
|
|
if (!IsValidBinding(binding))
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
2001-05-18 23:24:09 +00:00
|
|
|
|
|
|
|
NS_ASSERTION(binding->mCacheEntry == entry, "binding & entry don't point to each other");
|
2001-02-28 11:30:39 +00:00
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
rv = binding->EnsureStreamIO();
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
return binding->mStreamIO->GetOutputStream(offset, result);
|
2001-02-24 01:02:37 +00:00
|
|
|
}
|
|
|
|
|
2001-05-18 23:24:09 +00:00
|
|
|
|
2002-08-29 04:30:54 +00:00
|
|
|
/**
|
|
|
|
* NOTE: called while holding the cache service lock
|
|
|
|
*/
|
2001-03-08 21:22:18 +00:00
|
|
|
nsresult
|
|
|
|
nsDiskCacheDevice::GetFileForEntry(nsCacheEntry * entry,
|
|
|
|
nsIFile ** result)
|
|
|
|
{
|
2002-08-07 01:13:29 +00:00
|
|
|
NS_ENSURE_ARG_POINTER(result);
|
2012-07-30 14:20:58 +00:00
|
|
|
*result = nullptr;
|
2002-08-07 01:13:29 +00:00
|
|
|
|
2001-05-18 23:24:09 +00:00
|
|
|
nsresult rv;
|
2002-08-07 01:13:29 +00:00
|
|
|
|
2001-05-18 23:24:09 +00:00
|
|
|
nsDiskCacheBinding * binding = GetCacheEntryBinding(entry);
|
2011-04-17 17:31:15 +00:00
|
|
|
if (!IsValidBinding(binding))
|
2002-08-07 01:13:29 +00:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
2011-04-17 17:31:15 +00:00
|
|
|
|
2001-05-18 23:24:09 +00:00
|
|
|
// check/set binding->mRecord for separate file, sync w/mCacheMap
|
|
|
|
if (binding->mRecord.DataLocationInitialized()) {
|
2002-08-07 01:13:29 +00:00
|
|
|
if (binding->mRecord.DataFile() != 0)
|
|
|
|
return NS_ERROR_NOT_AVAILABLE; // data not stored as separate file
|
|
|
|
|
2001-05-18 23:24:09 +00:00
|
|
|
NS_ASSERTION(binding->mRecord.DataFileGeneration() == binding->mGeneration, "error generations out of sync");
|
|
|
|
} else {
|
|
|
|
binding->mRecord.SetDataFileGeneration(binding->mGeneration);
|
|
|
|
binding->mRecord.SetDataFileSize(0); // 1k minimum
|
2001-05-21 22:05:10 +00:00
|
|
|
if (!binding->mDoomed) {
|
|
|
|
// record stored in cache map, so update it
|
2006-02-25 02:58:51 +00:00
|
|
|
rv = mCacheMap.UpdateRecord(&binding->mRecord);
|
2001-05-21 22:05:10 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
}
|
2001-05-18 23:24:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> file;
|
2006-02-25 02:58:51 +00:00
|
|
|
rv = mCacheMap.GetFileForDiskCacheRecord(&binding->mRecord,
|
|
|
|
nsDiskCache::kData,
|
2011-10-17 14:59:28 +00:00
|
|
|
false,
|
2006-02-25 02:58:51 +00:00
|
|
|
getter_AddRefs(file));
|
2001-05-18 23:24:09 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
NS_IF_ADDREF(*result = file);
|
|
|
|
return NS_OK;
|
2001-03-08 21:22:18 +00:00
|
|
|
}
|
|
|
|
|
2001-05-18 23:24:09 +00:00
|
|
|
|
2001-03-01 08:29:43 +00:00
|
|
|
/**
|
2002-08-29 04:30:54 +00:00
|
|
|
* This routine will get called every time an open descriptor is written to.
|
|
|
|
*
|
|
|
|
* NOTE: called while holding the cache service lock
|
2001-03-01 08:29:43 +00:00
|
|
|
*/
|
2001-02-24 01:02:37 +00:00
|
|
|
nsresult
|
2012-08-22 15:56:38 +00:00
|
|
|
nsDiskCacheDevice::OnDataSizeChange(nsCacheEntry * entry, int32_t deltaSize)
|
2001-02-24 01:02:37 +00:00
|
|
|
{
|
2006-03-09 20:02:54 +00:00
|
|
|
CACHE_LOG_DEBUG(("CACHE: disk OnDataSizeChange [%p %d]\n",
|
|
|
|
entry, deltaSize));
|
|
|
|
|
|
|
|
// If passed a negative value, then there's nothing to do.
|
|
|
|
if (deltaSize < 0)
|
|
|
|
return NS_OK;
|
|
|
|
|
2001-05-18 23:24:09 +00:00
|
|
|
nsDiskCacheBinding * binding = GetCacheEntryBinding(entry);
|
2011-04-17 17:31:15 +00:00
|
|
|
if (!IsValidBinding(binding))
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
2001-03-20 22:42:03 +00:00
|
|
|
|
2001-05-18 23:24:09 +00:00
|
|
|
NS_ASSERTION(binding->mRecord.ValidRecord(), "bad record");
|
2001-03-08 21:00:02 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t newSize = entry->DataSize() + deltaSize;
|
|
|
|
uint32_t newSizeK = ((newSize + 0x3FF) >> 10);
|
2006-02-28 22:18:21 +00:00
|
|
|
|
|
|
|
// If the new size is larger than max. file size or larger than
|
2013-01-03 23:30:48 +00:00
|
|
|
// 1/8 the cache capacity (which is in KiB's), doom the entry and abort.
|
|
|
|
if (EntryIsTooBig(newSize)) {
|
2008-09-15 13:34:46 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
nsresult rv =
|
|
|
|
#endif
|
|
|
|
nsCacheService::DoomEntry(entry);
|
2002-08-07 01:13:29 +00:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv),"DoomEntry() failed.");
|
2004-02-25 23:01:08 +00:00
|
|
|
return NS_ERROR_ABORT;
|
2002-05-10 23:44:53 +00:00
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t sizeK = ((entry->DataSize() + 0x03FF) >> 10); // round up to next 1k
|
2001-02-23 13:18:01 +00:00
|
|
|
|
2011-05-20 17:27:11 +00:00
|
|
|
// In total count we ignore anything over kMaxDataSizeK (bug #651100), so
|
|
|
|
// the target capacity should be calculated the same way.
|
|
|
|
if (sizeK > kMaxDataSizeK) sizeK = kMaxDataSizeK;
|
|
|
|
if (newSizeK > kMaxDataSizeK) newSizeK = kMaxDataSizeK;
|
2001-04-04 03:30:45 +00:00
|
|
|
|
2002-08-07 01:13:29 +00:00
|
|
|
// pre-evict entries to make space for new data
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t targetCapacity = mCacheCapacity > (newSizeK - sizeK)
|
2006-02-28 22:18:21 +00:00
|
|
|
? mCacheCapacity - (newSizeK - sizeK)
|
|
|
|
: 0;
|
2002-08-07 01:13:29 +00:00
|
|
|
EvictDiskCacheEntries(targetCapacity);
|
2001-04-04 03:30:45 +00:00
|
|
|
|
2001-03-29 05:54:58 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2001-02-28 04:00:08 +00:00
|
|
|
|
2001-05-18 23:24:09 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* EntryInfoVisitor
|
|
|
|
*****************************************************************************/
|
|
|
|
class EntryInfoVisitor : public nsDiskCacheRecordVisitor
|
2001-03-01 17:31:06 +00:00
|
|
|
{
|
2001-03-20 22:42:03 +00:00
|
|
|
public:
|
2006-02-25 02:58:51 +00:00
|
|
|
EntryInfoVisitor(nsDiskCacheMap * cacheMap,
|
2001-05-18 23:24:09 +00:00
|
|
|
nsICacheVisitor * visitor)
|
2006-02-25 02:58:51 +00:00
|
|
|
: mCacheMap(cacheMap)
|
2001-05-18 23:24:09 +00:00
|
|
|
, mVisitor(visitor)
|
|
|
|
{}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
virtual int32_t VisitRecord(nsDiskCacheRecord * mapRecord)
|
2001-03-20 22:42:03 +00:00
|
|
|
{
|
2001-05-18 23:24:09 +00:00
|
|
|
// XXX optimization: do we have this record in memory?
|
2001-03-02 04:57:09 +00:00
|
|
|
|
2001-05-18 23:24:09 +00:00
|
|
|
// read in the entry (metadata)
|
2007-12-29 04:22:54 +00:00
|
|
|
nsDiskCacheEntry * diskEntry = mCacheMap->ReadDiskCacheEntry(mapRecord);
|
|
|
|
if (!diskEntry) {
|
2001-05-25 18:47:53 +00:00
|
|
|
return kVisitNextRecord;
|
2001-05-18 23:24:09 +00:00
|
|
|
}
|
2001-03-02 20:14:41 +00:00
|
|
|
|
2001-05-18 23:24:09 +00:00
|
|
|
// create nsICacheEntryInfo
|
|
|
|
nsDiskCacheEntryInfo * entryInfo = new nsDiskCacheEntryInfo(DISK_CACHE_DEVICE_ID, diskEntry);
|
|
|
|
if (!entryInfo) {
|
|
|
|
return kStopVisitingRecords;
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsICacheEntryInfo> ref(entryInfo);
|
2001-03-02 04:57:09 +00:00
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool keepGoing;
|
2007-12-29 04:22:54 +00:00
|
|
|
(void)mVisitor->VisitEntry(DISK_CACHE_DEVICE_ID, entryInfo, &keepGoing);
|
2001-05-18 23:24:09 +00:00
|
|
|
return keepGoing ? kVisitNextRecord : kStopVisitingRecords;
|
2001-04-04 03:30:45 +00:00
|
|
|
}
|
2001-05-18 23:24:09 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
nsDiskCacheMap * mCacheMap;
|
|
|
|
nsICacheVisitor * mVisitor;
|
|
|
|
};
|
2001-03-02 04:57:09 +00:00
|
|
|
|
|
|
|
|
2001-05-18 23:24:09 +00:00
|
|
|
nsresult
|
|
|
|
nsDiskCacheDevice::Visit(nsICacheVisitor * visitor)
|
2001-03-03 03:41:38 +00:00
|
|
|
{
|
2003-05-23 05:44:15 +00:00
|
|
|
if (!Initialized()) return NS_ERROR_NOT_INITIALIZED;
|
2001-05-18 23:24:09 +00:00
|
|
|
nsDiskCacheDeviceInfo* deviceInfo = new nsDiskCacheDeviceInfo(this);
|
|
|
|
nsCOMPtr<nsICacheDeviceInfo> ref(deviceInfo);
|
2001-03-03 03:41:38 +00:00
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool keepGoing;
|
2001-05-18 23:24:09 +00:00
|
|
|
nsresult rv = visitor->VisitDevice(DISK_CACHE_DEVICE_ID, deviceInfo, &keepGoing);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
2001-03-11 00:19:09 +00:00
|
|
|
|
2001-05-18 23:24:09 +00:00
|
|
|
if (keepGoing) {
|
2006-02-25 02:58:51 +00:00
|
|
|
EntryInfoVisitor infoVisitor(&mCacheMap, visitor);
|
|
|
|
return mCacheMap.VisitRecords(&infoVisitor);
|
2001-03-11 00:19:09 +00:00
|
|
|
}
|
2001-03-29 05:54:58 +00:00
|
|
|
|
2001-03-11 00:19:09 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-06-27 12:21:15 +00:00
|
|
|
// Max allowed size for an entry is currently MIN(mMaxEntrySize, 1/8 CacheCapacity)
|
2010-09-07 22:39:28 +00:00
|
|
|
bool
|
2012-08-22 15:56:38 +00:00
|
|
|
nsDiskCacheDevice::EntryIsTooBig(int64_t entrySize)
|
2010-09-07 22:39:28 +00:00
|
|
|
{
|
2011-06-27 12:21:15 +00:00
|
|
|
if (mMaxEntrySize == -1) // no limit
|
2012-08-22 15:56:38 +00:00
|
|
|
return entrySize > (static_cast<int64_t>(mCacheCapacity) * 1024 / 8);
|
2011-06-27 12:21:15 +00:00
|
|
|
else
|
|
|
|
return entrySize > mMaxEntrySize ||
|
2012-08-22 15:56:38 +00:00
|
|
|
entrySize > (static_cast<int64_t>(mCacheCapacity) * 1024 / 8);
|
2010-09-07 22:39:28 +00:00
|
|
|
}
|
2001-03-12 19:31:21 +00:00
|
|
|
|
2001-05-18 23:24:09 +00:00
|
|
|
nsresult
|
|
|
|
nsDiskCacheDevice::EvictEntries(const char * clientID)
|
2001-03-12 19:31:21 +00:00
|
|
|
{
|
2006-03-09 20:02:54 +00:00
|
|
|
CACHE_LOG_DEBUG(("CACHE: disk EvictEntries [%s]\n", clientID));
|
|
|
|
|
2003-05-23 05:44:15 +00:00
|
|
|
if (!Initialized()) return NS_ERROR_NOT_INITIALIZED;
|
2003-03-10 23:19:05 +00:00
|
|
|
nsresult rv;
|
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
if (clientID == nullptr) {
|
2003-03-10 23:19:05 +00:00
|
|
|
// we're clearing the entire disk cache
|
|
|
|
rv = ClearDiskCache();
|
2003-05-23 05:44:15 +00:00
|
|
|
if (rv != NS_ERROR_CACHE_IN_USE)
|
2003-03-10 23:19:05 +00:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2006-02-25 02:58:51 +00:00
|
|
|
nsDiskCacheEvictor evictor(&mCacheMap, &mBindery, 0, clientID);
|
|
|
|
rv = mCacheMap.VisitRecords(&evictor);
|
2002-08-07 01:13:29 +00:00
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
if (clientID == nullptr) // we tried to clear the entire cache
|
2006-02-25 02:58:51 +00:00
|
|
|
rv = mCacheMap.Trim(); // so trim cache block files (if possible)
|
2001-05-18 23:24:09 +00:00
|
|
|
return rv;
|
2001-03-12 19:31:21 +00:00
|
|
|
}
|
|
|
|
|
2001-04-05 22:28:17 +00:00
|
|
|
|
2001-05-18 23:24:09 +00:00
|
|
|
/**
|
|
|
|
* private methods
|
|
|
|
*/
|
2003-03-10 23:19:05 +00:00
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsDiskCacheDevice::OpenDiskCache()
|
|
|
|
{
|
2011-07-21 00:02:58 +00:00
|
|
|
Telemetry::AutoTimer<Telemetry::NETWORK_DISK_CACHE_OPEN> timer;
|
2003-03-10 23:19:05 +00:00
|
|
|
// if we don't have a cache directory, create one and open it
|
2011-09-29 06:19:26 +00:00
|
|
|
bool exists;
|
2006-02-25 02:58:51 +00:00
|
|
|
nsresult rv = mCacheDirectory->Exists(&exists);
|
2005-04-26 00:37:23 +00:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
2003-03-10 23:19:05 +00:00
|
|
|
|
2005-04-26 00:37:23 +00:00
|
|
|
if (exists) {
|
2006-02-25 02:58:51 +00:00
|
|
|
// Try opening cache map file.
|
2012-07-20 23:47:52 +00:00
|
|
|
nsDiskCache::CorruptCacheInfo corruptInfo;
|
2012-09-04 19:05:19 +00:00
|
|
|
rv = mCacheMap.Open(mCacheDirectory, &corruptInfo, true);
|
2012-07-20 23:47:52 +00:00
|
|
|
|
2012-04-06 20:10:32 +00:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2012-07-20 23:47:52 +00:00
|
|
|
Telemetry::Accumulate(Telemetry::DISK_CACHE_CORRUPT_DETAILS,
|
|
|
|
corruptInfo);
|
2012-08-07 22:46:33 +00:00
|
|
|
} else if (rv == NS_ERROR_ALREADY_INITIALIZED) {
|
|
|
|
NS_WARNING("nsDiskCacheDevice::OpenDiskCache: already open!");
|
|
|
|
} else {
|
|
|
|
// Consider cache corrupt: delete it
|
2012-07-20 23:47:52 +00:00
|
|
|
Telemetry::Accumulate(Telemetry::DISK_CACHE_CORRUPT_DETAILS,
|
|
|
|
corruptInfo);
|
2011-08-02 23:54:55 +00:00
|
|
|
// delay delete by 1 minute to avoid IO thrash at startup
|
2011-12-02 17:10:57 +00:00
|
|
|
rv = nsDeleteDir::DeleteDir(mCacheDirectory, true, 60000);
|
2005-04-26 00:37:23 +00:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
2011-10-17 14:59:28 +00:00
|
|
|
exists = false;
|
2012-04-06 20:10:32 +00:00
|
|
|
}
|
2003-03-10 23:19:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// if we don't have a cache directory, create one and open it
|
2005-04-26 00:37:23 +00:00
|
|
|
if (!exists) {
|
2012-08-07 22:46:33 +00:00
|
|
|
nsCacheService::MarkStartingFresh();
|
2006-02-25 02:58:51 +00:00
|
|
|
rv = mCacheDirectory->Create(nsIFile::DIRECTORY_TYPE, 0777);
|
|
|
|
CACHE_LOG_PATH(PR_LOG_ALWAYS, "\ncreate cache directory: %s\n", mCacheDirectory);
|
|
|
|
CACHE_LOG_ALWAYS(("mCacheDirectory->Create() = %x\n", rv));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
// reopen the cache map
|
2012-07-20 23:47:52 +00:00
|
|
|
nsDiskCache::CorruptCacheInfo corruptInfo;
|
2012-09-04 19:05:19 +00:00
|
|
|
rv = mCacheMap.Open(mCacheDirectory, &corruptInfo, false);
|
2005-04-26 00:37:23 +00:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
2003-03-10 23:19:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsDiskCacheDevice::ClearDiskCache()
|
|
|
|
{
|
2005-04-26 00:37:23 +00:00
|
|
|
if (mBindery.ActiveBindings())
|
|
|
|
return NS_ERROR_CACHE_IN_USE;
|
2003-03-10 23:19:05 +00:00
|
|
|
|
2012-04-12 16:27:38 +00:00
|
|
|
mClearingDiskCache = true;
|
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
nsresult rv = Shutdown_Private(false); // false: don't bother flushing
|
2005-04-26 00:37:23 +00:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
2003-03-10 23:19:05 +00:00
|
|
|
|
2012-04-12 16:27:38 +00:00
|
|
|
mClearingDiskCache = false;
|
|
|
|
|
2006-01-24 14:47:06 +00:00
|
|
|
// If the disk cache directory is already gone, then it's not an error if
|
|
|
|
// we fail to delete it ;-)
|
2011-12-02 17:10:57 +00:00
|
|
|
rv = nsDeleteDir::DeleteDir(mCacheDirectory, true);
|
2006-01-24 14:47:06 +00:00
|
|
|
if (NS_FAILED(rv) && rv != NS_ERROR_FILE_TARGET_DOES_NOT_EXIST)
|
2005-04-26 00:37:23 +00:00
|
|
|
return rv;
|
2003-03-10 23:19:05 +00:00
|
|
|
|
2005-04-26 00:37:23 +00:00
|
|
|
return Init();
|
2003-03-10 23:19:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-05-18 23:24:09 +00:00
|
|
|
nsresult
|
2012-08-22 15:56:38 +00:00
|
|
|
nsDiskCacheDevice::EvictDiskCacheEntries(uint32_t targetCapacity)
|
2001-03-12 19:31:21 +00:00
|
|
|
{
|
2006-03-09 20:02:54 +00:00
|
|
|
CACHE_LOG_DEBUG(("CACHE: disk EvictDiskCacheEntries [%u]\n",
|
|
|
|
targetCapacity));
|
|
|
|
|
|
|
|
NS_ASSERTION(targetCapacity > 0, "oops");
|
|
|
|
|
2006-02-25 02:58:51 +00:00
|
|
|
if (mCacheMap.TotalSize() < targetCapacity)
|
|
|
|
return NS_OK;
|
2001-05-18 23:24:09 +00:00
|
|
|
|
2006-02-28 22:18:21 +00:00
|
|
|
// targetCapacity is in KiB's
|
2012-07-30 14:20:58 +00:00
|
|
|
nsDiskCacheEvictor evictor(&mCacheMap, &mBindery, targetCapacity, nullptr);
|
2006-02-25 02:58:51 +00:00
|
|
|
return mCacheMap.EvictRecords(&evictor);
|
2001-05-18 23:24:09 +00:00
|
|
|
}
|
2001-03-13 04:54:14 +00:00
|
|
|
|
2001-04-04 03:30:45 +00:00
|
|
|
|
2001-05-18 23:24:09 +00:00
|
|
|
/**
|
|
|
|
* methods for prefs
|
|
|
|
*/
|
2001-04-04 03:30:45 +00:00
|
|
|
|
2001-06-28 01:30:26 +00:00
|
|
|
void
|
2012-06-06 02:08:30 +00:00
|
|
|
nsDiskCacheDevice::SetCacheParentDirectory(nsIFile * parentDir)
|
2001-03-20 22:42:03 +00:00
|
|
|
{
|
2001-06-28 01:30:26 +00:00
|
|
|
nsresult rv;
|
2011-09-29 06:19:26 +00:00
|
|
|
bool exists;
|
2001-08-17 00:01:01 +00:00
|
|
|
|
2002-08-07 01:13:29 +00:00
|
|
|
if (Initialized()) {
|
2011-10-17 14:59:28 +00:00
|
|
|
NS_ASSERTION(false, "Cannot switch cache directory when initialized");
|
2001-08-17 00:01:01 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2001-06-28 01:30:26 +00:00
|
|
|
if (!parentDir) {
|
2012-07-30 14:20:58 +00:00
|
|
|
mCacheDirectory = nullptr;
|
2001-06-28 01:30:26 +00:00
|
|
|
return;
|
|
|
|
}
|
2001-08-17 00:01:01 +00:00
|
|
|
|
2001-06-28 01:30:26 +00:00
|
|
|
// ensure parent directory exists
|
|
|
|
rv = parentDir->Exists(&exists);
|
|
|
|
if (NS_SUCCEEDED(rv) && !exists)
|
2001-11-19 22:48:30 +00:00
|
|
|
rv = parentDir->Create(nsIFile::DIRECTORY_TYPE, 0700);
|
2001-06-28 01:30:26 +00:00
|
|
|
if (NS_FAILED(rv)) return;
|
2003-03-10 23:19:05 +00:00
|
|
|
|
2001-06-28 01:30:26 +00:00
|
|
|
// ensure cache directory exists
|
|
|
|
nsCOMPtr<nsIFile> directory;
|
|
|
|
|
|
|
|
rv = parentDir->Clone(getter_AddRefs(directory));
|
|
|
|
if (NS_FAILED(rv)) return;
|
2002-05-07 23:07:19 +00:00
|
|
|
rv = directory->AppendNative(NS_LITERAL_CSTRING("Cache"));
|
2001-06-28 01:30:26 +00:00
|
|
|
if (NS_FAILED(rv)) return;
|
|
|
|
|
|
|
|
mCacheDirectory = do_QueryInterface(directory);
|
2001-05-18 23:24:09 +00:00
|
|
|
}
|
2001-03-29 05:54:58 +00:00
|
|
|
|
2001-05-18 23:24:09 +00:00
|
|
|
|
|
|
|
void
|
2012-06-06 02:08:30 +00:00
|
|
|
nsDiskCacheDevice::getCacheDirectory(nsIFile ** result)
|
2001-05-18 23:24:09 +00:00
|
|
|
{
|
|
|
|
*result = mCacheDirectory;
|
|
|
|
NS_IF_ADDREF(*result);
|
2001-04-05 22:28:17 +00:00
|
|
|
}
|
|
|
|
|
2001-05-18 23:24:09 +00:00
|
|
|
|
2002-08-29 04:30:54 +00:00
|
|
|
/**
|
|
|
|
* NOTE: called while holding the cache service lock
|
|
|
|
*/
|
2001-06-28 01:30:26 +00:00
|
|
|
void
|
2012-08-22 15:56:38 +00:00
|
|
|
nsDiskCacheDevice::SetCapacity(uint32_t capacity)
|
2001-04-05 22:28:17 +00:00
|
|
|
{
|
2006-02-28 22:18:21 +00:00
|
|
|
// Units are KiB's
|
|
|
|
mCacheCapacity = capacity;
|
2002-08-07 01:13:29 +00:00
|
|
|
if (Initialized()) {
|
2012-01-06 15:19:10 +00:00
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
// Do not evict entries on the main thread
|
|
|
|
nsCacheService::DispatchToCacheIOThread(
|
|
|
|
new nsEvictDiskCacheEntriesEvent(this));
|
|
|
|
} else {
|
|
|
|
// start evicting entries if the new size is smaller!
|
|
|
|
EvictDiskCacheEntries(mCacheCapacity);
|
|
|
|
}
|
2001-04-05 22:28:17 +00:00
|
|
|
}
|
2010-06-11 02:46:51 +00:00
|
|
|
// Let cache map know of the new capacity
|
|
|
|
mCacheMap.NotifyCapacityChange(capacity);
|
2001-03-20 22:42:03 +00:00
|
|
|
}
|
|
|
|
|
2001-06-28 01:30:26 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t nsDiskCacheDevice::getCacheCapacity()
|
2001-03-20 22:42:03 +00:00
|
|
|
{
|
2001-05-18 23:24:09 +00:00
|
|
|
return mCacheCapacity;
|
2001-03-29 05:54:58 +00:00
|
|
|
}
|
2001-03-23 03:35:10 +00:00
|
|
|
|
2003-03-10 23:19:05 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t nsDiskCacheDevice::getCacheSize()
|
2001-03-29 05:54:58 +00:00
|
|
|
{
|
2006-02-25 02:58:51 +00:00
|
|
|
return mCacheMap.TotalSize();
|
2001-03-20 22:42:03 +00:00
|
|
|
}
|
|
|
|
|
2003-03-10 23:19:05 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t nsDiskCacheDevice::getEntryCount()
|
2001-03-20 22:42:03 +00:00
|
|
|
{
|
2006-02-25 02:58:51 +00:00
|
|
|
return mCacheMap.EntryCount();
|
2001-03-20 22:42:03 +00:00
|
|
|
}
|
2011-06-27 12:21:15 +00:00
|
|
|
|
|
|
|
void
|
2012-08-22 15:56:38 +00:00
|
|
|
nsDiskCacheDevice::SetMaxEntrySize(int32_t maxSizeInKilobytes)
|
2011-06-27 12:21:15 +00:00
|
|
|
{
|
|
|
|
// Internal units are bytes. Changing this only takes effect *after* the
|
|
|
|
// change and has no consequences for existing cache-entries
|
|
|
|
if (maxSizeInKilobytes >= 0)
|
|
|
|
mMaxEntrySize = maxSizeInKilobytes * 1024;
|
|
|
|
else
|
|
|
|
mMaxEntrySize = -1;
|
|
|
|
}
|
2012-11-05 18:22:33 +00:00
|
|
|
|
|
|
|
size_t
|
2013-06-23 12:03:39 +00:00
|
|
|
nsDiskCacheDevice::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf)
|
2012-11-05 18:22:33 +00:00
|
|
|
{
|
|
|
|
size_t usage = aMallocSizeOf(this);
|
|
|
|
|
|
|
|
usage += mCacheMap.SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
usage += mBindery.SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
|
|
|
|
return usage;
|
|
|
|
}
|
|
|
|
|