2013-09-20 13:11:26 +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/. */
|
|
|
|
|
2013-10-15 22:08:26 +04:00
|
|
|
#include "CacheLog.h"
|
2013-09-20 13:11:26 +04:00
|
|
|
#include "CacheFileMetadata.h"
|
|
|
|
|
|
|
|
#include "CacheFileIOManager.h"
|
2014-02-18 19:05:07 +04:00
|
|
|
#include "nsICacheEntry.h"
|
2013-09-20 13:11:26 +04:00
|
|
|
#include "CacheHashUtils.h"
|
|
|
|
#include "CacheFileChunk.h"
|
2014-02-18 21:26:48 +04:00
|
|
|
#include "CacheFileUtils.h"
|
2014-02-18 19:05:07 +04:00
|
|
|
#include "nsILoadContextInfo.h"
|
2014-05-01 15:28:12 +04:00
|
|
|
#include "nsICacheEntry.h" // for nsICacheEntryMetaDataVisitor
|
2013-09-20 13:11:26 +04:00
|
|
|
#include "../cache/nsCacheUtils.h"
|
2014-02-18 19:05:07 +04:00
|
|
|
#include "nsIFile.h"
|
2013-09-20 13:11:26 +04:00
|
|
|
#include "mozilla/Telemetry.h"
|
2014-02-18 19:05:07 +04:00
|
|
|
#include "mozilla/DebugOnly.h"
|
2016-12-16 06:16:31 +03:00
|
|
|
#include "mozilla/IntegerPrintfMacros.h"
|
2013-09-20 13:11:26 +04:00
|
|
|
#include "prnetdb.h"
|
|
|
|
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace net {
|
|
|
|
|
|
|
|
#define kMinMetadataRead 1024 // TODO find optimal value from telemetry
|
|
|
|
#define kAlignSize 4096
|
|
|
|
|
2015-03-06 17:49:04 +03:00
|
|
|
// Most of the cache entries fit into one chunk due to current chunk size. Make
|
|
|
|
// sure to tweak this value if kChunkSize is going to change.
|
|
|
|
#define kInitialHashArraySize 1
|
|
|
|
|
|
|
|
// Initial elements buffer size.
|
|
|
|
#define kInitialBufSize 64
|
|
|
|
|
2015-07-22 16:23:16 +03:00
|
|
|
// Max size of elements in bytes.
|
|
|
|
#define kMaxElementsSize 64*1024
|
|
|
|
|
2014-09-16 17:51:50 +04:00
|
|
|
#define NOW_SECONDS() (uint32_t(PR_Now() / PR_USEC_PER_SEC))
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(CacheFileMetadata, CacheFileIOListener)
|
2013-09-20 13:11:26 +04:00
|
|
|
|
2014-02-18 21:26:48 +04:00
|
|
|
CacheFileMetadata::CacheFileMetadata(CacheFileHandle *aHandle, const nsACString &aKey)
|
2014-04-06 22:45:18 +04:00
|
|
|
: CacheMemoryConsumer(NORMAL)
|
|
|
|
, mHandle(aHandle)
|
2013-09-20 13:11:26 +04:00
|
|
|
, mHashArray(nullptr)
|
|
|
|
, mHashArraySize(0)
|
|
|
|
, mHashCount(0)
|
|
|
|
, mOffset(-1)
|
|
|
|
, mBuf(nullptr)
|
|
|
|
, mBufSize(0)
|
|
|
|
, mWriteBuf(nullptr)
|
|
|
|
, mElementsSize(0)
|
|
|
|
, mIsDirty(false)
|
2014-02-18 19:05:07 +04:00
|
|
|
, mAnonymous(false)
|
2015-03-06 17:49:04 +03:00
|
|
|
, mAllocExactSize(false)
|
|
|
|
, mFirstRead(true)
|
2013-09-20 13:11:26 +04:00
|
|
|
{
|
|
|
|
LOG(("CacheFileMetadata::CacheFileMetadata() [this=%p, handle=%p, key=%s]",
|
|
|
|
this, aHandle, PromiseFlatCString(aKey).get()));
|
|
|
|
|
|
|
|
memset(&mMetaHdr, 0, sizeof(CacheFileMetadataHeader));
|
2014-04-08 00:07:43 +04:00
|
|
|
mMetaHdr.mVersion = kCacheEntryVersion;
|
2014-02-18 19:05:07 +04:00
|
|
|
mMetaHdr.mExpirationTime = nsICacheEntry::NO_EXPIRATION_TIME;
|
2013-09-20 13:11:26 +04:00
|
|
|
mKey = aKey;
|
2014-02-18 21:26:48 +04:00
|
|
|
|
|
|
|
DebugOnly<nsresult> rv;
|
|
|
|
rv = ParseKey(aKey);
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
2013-09-20 13:11:26 +04:00
|
|
|
}
|
|
|
|
|
2015-10-22 13:11:00 +03:00
|
|
|
CacheFileMetadata::CacheFileMetadata(bool aMemoryOnly, bool aPinned, const nsACString &aKey)
|
2014-04-06 22:45:18 +04:00
|
|
|
: CacheMemoryConsumer(aMemoryOnly ? MEMORY_ONLY : NORMAL)
|
|
|
|
, mHandle(nullptr)
|
2013-09-20 13:11:26 +04:00
|
|
|
, mHashArray(nullptr)
|
|
|
|
, mHashArraySize(0)
|
|
|
|
, mHashCount(0)
|
|
|
|
, mOffset(0)
|
|
|
|
, mBuf(nullptr)
|
|
|
|
, mBufSize(0)
|
|
|
|
, mWriteBuf(nullptr)
|
|
|
|
, mElementsSize(0)
|
|
|
|
, mIsDirty(true)
|
2014-02-18 19:05:07 +04:00
|
|
|
, mAnonymous(false)
|
2015-03-06 17:49:04 +03:00
|
|
|
, mAllocExactSize(false)
|
|
|
|
, mFirstRead(true)
|
2013-09-20 13:11:26 +04:00
|
|
|
{
|
|
|
|
LOG(("CacheFileMetadata::CacheFileMetadata() [this=%p, key=%s]",
|
|
|
|
this, PromiseFlatCString(aKey).get()));
|
|
|
|
|
|
|
|
memset(&mMetaHdr, 0, sizeof(CacheFileMetadataHeader));
|
2014-04-08 00:07:43 +04:00
|
|
|
mMetaHdr.mVersion = kCacheEntryVersion;
|
2015-10-22 13:11:00 +03:00
|
|
|
if (aPinned) {
|
|
|
|
AddFlags(kCacheEntryIsPinned);
|
|
|
|
}
|
2014-02-18 19:05:07 +04:00
|
|
|
mMetaHdr.mExpirationTime = nsICacheEntry::NO_EXPIRATION_TIME;
|
2013-09-20 13:11:26 +04:00
|
|
|
mKey = aKey;
|
|
|
|
mMetaHdr.mKeySize = mKey.Length();
|
2014-02-18 19:05:07 +04:00
|
|
|
|
|
|
|
DebugOnly<nsresult> rv;
|
|
|
|
rv = ParseKey(aKey);
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
|
|
|
}
|
|
|
|
|
|
|
|
CacheFileMetadata::CacheFileMetadata()
|
2014-04-06 22:45:18 +04:00
|
|
|
: CacheMemoryConsumer(DONT_REPORT /* This is a helper class */)
|
|
|
|
, mHandle(nullptr)
|
2014-02-18 19:05:07 +04:00
|
|
|
, mHashArray(nullptr)
|
|
|
|
, mHashArraySize(0)
|
|
|
|
, mHashCount(0)
|
|
|
|
, mOffset(0)
|
|
|
|
, mBuf(nullptr)
|
|
|
|
, mBufSize(0)
|
|
|
|
, mWriteBuf(nullptr)
|
|
|
|
, mElementsSize(0)
|
|
|
|
, mIsDirty(false)
|
|
|
|
, mAnonymous(false)
|
2015-03-06 17:49:04 +03:00
|
|
|
, mAllocExactSize(false)
|
|
|
|
, mFirstRead(true)
|
2014-02-18 19:05:07 +04:00
|
|
|
{
|
|
|
|
LOG(("CacheFileMetadata::CacheFileMetadata() [this=%p]", this));
|
|
|
|
|
|
|
|
memset(&mMetaHdr, 0, sizeof(CacheFileMetadataHeader));
|
2013-09-20 13:11:26 +04:00
|
|
|
}
|
|
|
|
|
2014-04-06 22:45:18 +04:00
|
|
|
CacheFileMetadata::~CacheFileMetadata()
|
|
|
|
{
|
|
|
|
LOG(("CacheFileMetadata::~CacheFileMetadata() [this=%p]", this));
|
|
|
|
|
|
|
|
MOZ_ASSERT(!mListener);
|
|
|
|
|
|
|
|
if (mHashArray) {
|
2016-05-11 12:46:54 +03:00
|
|
|
CacheFileUtils::FreeBuffer(mHashArray);
|
2014-04-06 22:45:18 +04:00
|
|
|
mHashArray = nullptr;
|
|
|
|
mHashArraySize = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mBuf) {
|
2016-05-11 12:46:54 +03:00
|
|
|
CacheFileUtils::FreeBuffer(mBuf);
|
2014-04-06 22:45:18 +04:00
|
|
|
mBuf = nullptr;
|
|
|
|
mBufSize = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-20 13:11:26 +04:00
|
|
|
void
|
|
|
|
CacheFileMetadata::SetHandle(CacheFileHandle *aHandle)
|
|
|
|
{
|
|
|
|
LOG(("CacheFileMetadata::SetHandle() [this=%p, handle=%p]", this, aHandle));
|
|
|
|
|
|
|
|
MOZ_ASSERT(!mHandle);
|
|
|
|
|
|
|
|
mHandle = aHandle;
|
|
|
|
}
|
|
|
|
|
2013-12-16 21:01:54 +04:00
|
|
|
nsresult
|
|
|
|
CacheFileMetadata::GetKey(nsACString &_retval)
|
|
|
|
{
|
|
|
|
_retval = mKey;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-09-20 13:11:26 +04:00
|
|
|
nsresult
|
|
|
|
CacheFileMetadata::ReadMetadata(CacheFileMetadataListener *aListener)
|
|
|
|
{
|
|
|
|
LOG(("CacheFileMetadata::ReadMetadata() [this=%p, listener=%p]", this, aListener));
|
|
|
|
|
|
|
|
MOZ_ASSERT(!mListener);
|
|
|
|
MOZ_ASSERT(!mHashArray);
|
|
|
|
MOZ_ASSERT(!mBuf);
|
|
|
|
MOZ_ASSERT(!mWriteBuf);
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
int64_t size = mHandle->FileSize();
|
|
|
|
MOZ_ASSERT(size != -1);
|
|
|
|
|
|
|
|
if (size == 0) {
|
|
|
|
// this is a new entry
|
|
|
|
LOG(("CacheFileMetadata::ReadMetadata() - Filesize == 0, creating empty "
|
|
|
|
"metadata. [this=%p]", this));
|
|
|
|
|
|
|
|
InitEmptyMetadata();
|
|
|
|
aListener->OnMetadataRead(NS_OK);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (size < int64_t(sizeof(CacheFileMetadataHeader) + 2*sizeof(uint32_t))) {
|
|
|
|
// there must be at least checksum, header and offset
|
|
|
|
LOG(("CacheFileMetadata::ReadMetadata() - File is corrupted, creating "
|
2016-12-16 06:16:31 +03:00
|
|
|
"empty metadata. [this=%p, filesize=%" PRId64 "]", this, size));
|
2013-09-20 13:11:26 +04:00
|
|
|
|
|
|
|
InitEmptyMetadata();
|
|
|
|
aListener->OnMetadataRead(NS_OK);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-06-08 02:00:57 +04:00
|
|
|
// Set offset so that we read at least kMinMetadataRead if the file is big
|
|
|
|
// enough.
|
|
|
|
int64_t offset;
|
|
|
|
if (size < kMinMetadataRead) {
|
|
|
|
offset = 0;
|
|
|
|
} else {
|
|
|
|
offset = size - kMinMetadataRead;
|
|
|
|
}
|
2013-09-20 13:11:26 +04:00
|
|
|
|
2014-06-08 02:00:57 +04:00
|
|
|
// round offset to kAlignSize blocks
|
|
|
|
offset = (offset / kAlignSize) * kAlignSize;
|
2013-09-20 13:11:26 +04:00
|
|
|
|
|
|
|
mBufSize = size - offset;
|
|
|
|
mBuf = static_cast<char *>(moz_xmalloc(mBufSize));
|
|
|
|
|
|
|
|
DoMemoryReport(MemoryUsage());
|
|
|
|
|
|
|
|
LOG(("CacheFileMetadata::ReadMetadata() - Reading metadata from disk, trying "
|
2016-12-16 06:16:31 +03:00
|
|
|
"offset=%" PRId64 ", filesize=%" PRId64 " [this=%p]", offset, size, this));
|
2013-09-20 13:11:26 +04:00
|
|
|
|
2015-02-23 18:13:02 +03:00
|
|
|
mReadStart = mozilla::TimeStamp::Now();
|
2013-09-20 13:11:26 +04:00
|
|
|
mListener = aListener;
|
2014-08-18 17:36:27 +04:00
|
|
|
rv = CacheFileIOManager::Read(mHandle, offset, mBuf, mBufSize, this);
|
2013-09-20 13:11:26 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
LOG(("CacheFileMetadata::ReadMetadata() - CacheFileIOManager::Read() failed"
|
2016-12-16 06:16:31 +03:00
|
|
|
" synchronously, creating empty metadata. [this=%p, rv=0x%08" PRIx32 "]",
|
|
|
|
this, static_cast<uint32_t>(rv)));
|
2013-09-20 13:11:26 +04:00
|
|
|
|
|
|
|
mListener = nullptr;
|
|
|
|
InitEmptyMetadata();
|
|
|
|
aListener->OnMetadataRead(NS_OK);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-07-22 16:23:16 +03:00
|
|
|
uint32_t
|
|
|
|
CacheFileMetadata::CalcMetadataSize(uint32_t aElementsSize, uint32_t aHashCount)
|
|
|
|
{
|
|
|
|
return sizeof(uint32_t) + // hash of the metadata
|
|
|
|
aHashCount * sizeof(CacheHash::Hash16_t) + // array of chunk hashes
|
|
|
|
sizeof(CacheFileMetadataHeader) + // metadata header
|
|
|
|
mKey.Length() + 1 + // key with trailing null
|
|
|
|
aElementsSize + // elements
|
|
|
|
sizeof(uint32_t); // offset
|
|
|
|
}
|
|
|
|
|
2013-09-20 13:11:26 +04:00
|
|
|
nsresult
|
|
|
|
CacheFileMetadata::WriteMetadata(uint32_t aOffset,
|
|
|
|
CacheFileMetadataListener *aListener)
|
|
|
|
{
|
|
|
|
LOG(("CacheFileMetadata::WriteMetadata() [this=%p, offset=%d, listener=%p]",
|
|
|
|
this, aOffset, aListener));
|
|
|
|
|
|
|
|
MOZ_ASSERT(!mListener);
|
|
|
|
MOZ_ASSERT(!mWriteBuf);
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
mIsDirty = false;
|
|
|
|
|
2015-07-22 16:23:16 +03:00
|
|
|
mWriteBuf = static_cast<char *>(malloc(CalcMetadataSize(mElementsSize,
|
|
|
|
mHashCount)));
|
|
|
|
if (!mWriteBuf) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2013-09-20 13:11:26 +04:00
|
|
|
|
|
|
|
char *p = mWriteBuf + sizeof(uint32_t);
|
2017-11-03 10:17:00 +03:00
|
|
|
if (mHashCount) {
|
|
|
|
memcpy(p, mHashArray, mHashCount * sizeof(CacheHash::Hash16_t));
|
|
|
|
p += mHashCount * sizeof(CacheHash::Hash16_t);
|
|
|
|
}
|
2014-04-08 00:07:38 +04:00
|
|
|
mMetaHdr.WriteToBuf(p);
|
2013-09-20 13:11:26 +04:00
|
|
|
p += sizeof(CacheFileMetadataHeader);
|
|
|
|
memcpy(p, mKey.get(), mKey.Length());
|
|
|
|
p += mKey.Length();
|
|
|
|
*p = 0;
|
|
|
|
p++;
|
|
|
|
memcpy(p, mBuf, mElementsSize);
|
|
|
|
p += mElementsSize;
|
|
|
|
|
2014-02-18 19:05:07 +04:00
|
|
|
CacheHash::Hash32_t hash;
|
|
|
|
hash = CacheHash::Hash(mWriteBuf + sizeof(uint32_t),
|
|
|
|
p - mWriteBuf - sizeof(uint32_t));
|
2014-04-08 00:07:38 +04:00
|
|
|
NetworkEndian::writeUint32(mWriteBuf, hash);
|
2013-09-20 13:11:26 +04:00
|
|
|
|
2014-04-08 00:07:38 +04:00
|
|
|
NetworkEndian::writeUint32(p, aOffset);
|
2013-09-20 13:11:26 +04:00
|
|
|
p += sizeof(uint32_t);
|
|
|
|
|
2014-08-18 23:11:40 +04:00
|
|
|
char * writeBuffer = mWriteBuf;
|
2014-01-07 00:27:18 +04:00
|
|
|
if (aListener) {
|
|
|
|
mListener = aListener;
|
|
|
|
} else {
|
2014-08-18 23:11:40 +04:00
|
|
|
// We are not going to pass |this| as a callback so the buffer will be
|
|
|
|
// released by CacheFileIOManager. Just null out mWriteBuf here.
|
|
|
|
mWriteBuf = nullptr;
|
2014-01-07 00:27:18 +04:00
|
|
|
}
|
|
|
|
|
2014-08-18 23:11:40 +04:00
|
|
|
rv = CacheFileIOManager::Write(mHandle, aOffset, writeBuffer, p - writeBuffer,
|
2015-03-27 01:05:34 +03:00
|
|
|
true, true, aListener ? this : nullptr);
|
2013-09-20 13:11:26 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
LOG(("CacheFileMetadata::WriteMetadata() - CacheFileIOManager::Write() "
|
2016-12-16 06:16:31 +03:00
|
|
|
"failed synchronously. [this=%p, rv=0x%08" PRIx32 "]",
|
|
|
|
this, static_cast<uint32_t>(rv)));
|
2013-09-20 13:11:26 +04:00
|
|
|
|
|
|
|
mListener = nullptr;
|
2014-08-18 23:11:40 +04:00
|
|
|
if (mWriteBuf) {
|
2016-05-11 12:46:54 +03:00
|
|
|
CacheFileUtils::FreeBuffer(mWriteBuf);
|
2014-08-18 23:11:40 +04:00
|
|
|
mWriteBuf = nullptr;
|
2014-01-07 00:27:18 +04:00
|
|
|
}
|
2013-09-20 13:11:26 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
DoMemoryReport(MemoryUsage());
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-02-18 19:05:07 +04:00
|
|
|
nsresult
|
|
|
|
CacheFileMetadata::SyncReadMetadata(nsIFile *aFile)
|
|
|
|
{
|
|
|
|
LOG(("CacheFileMetadata::SyncReadMetadata() [this=%p]", this));
|
|
|
|
|
|
|
|
MOZ_ASSERT(!mListener);
|
|
|
|
MOZ_ASSERT(!mHandle);
|
|
|
|
MOZ_ASSERT(!mHashArray);
|
|
|
|
MOZ_ASSERT(!mBuf);
|
|
|
|
MOZ_ASSERT(!mWriteBuf);
|
|
|
|
MOZ_ASSERT(mKey.IsEmpty());
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
int64_t fileSize;
|
|
|
|
rv = aFile->GetFileSize(&fileSize);
|
2014-05-01 15:28:12 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// Don't bloat the console
|
|
|
|
return rv;
|
|
|
|
}
|
2014-02-18 19:05:07 +04:00
|
|
|
|
|
|
|
PRFileDesc *fd;
|
|
|
|
rv = aFile->OpenNSPRFileDesc(PR_RDONLY, 0600, &fd);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
int64_t offset = PR_Seek64(fd, fileSize - sizeof(uint32_t), PR_SEEK_SET);
|
|
|
|
if (offset == -1) {
|
|
|
|
PR_Close(fd);
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t metaOffset;
|
|
|
|
int32_t bytesRead = PR_Read(fd, &metaOffset, sizeof(uint32_t));
|
|
|
|
if (bytesRead != sizeof(uint32_t)) {
|
|
|
|
PR_Close(fd);
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2014-04-08 00:07:38 +04:00
|
|
|
metaOffset = NetworkEndian::readUint32(&metaOffset);
|
2014-02-18 19:05:07 +04:00
|
|
|
if (metaOffset > fileSize) {
|
|
|
|
PR_Close(fd);
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2015-06-24 07:33:37 +03:00
|
|
|
mBuf = static_cast<char *>(malloc(fileSize - metaOffset));
|
|
|
|
if (!mBuf) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2014-02-18 19:05:07 +04:00
|
|
|
mBufSize = fileSize - metaOffset;
|
|
|
|
|
|
|
|
DoMemoryReport(MemoryUsage());
|
|
|
|
|
|
|
|
offset = PR_Seek64(fd, metaOffset, PR_SEEK_SET);
|
|
|
|
if (offset == -1) {
|
|
|
|
PR_Close(fd);
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
bytesRead = PR_Read(fd, mBuf, mBufSize);
|
|
|
|
PR_Close(fd);
|
|
|
|
if (bytesRead != static_cast<int32_t>(mBufSize)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2014-02-18 21:26:48 +04:00
|
|
|
rv = ParseMetadata(metaOffset, 0, false);
|
2014-02-18 19:05:07 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-09-20 13:11:26 +04:00
|
|
|
const char *
|
|
|
|
CacheFileMetadata::GetElement(const char *aKey)
|
|
|
|
{
|
|
|
|
const char *data = mBuf;
|
|
|
|
const char *limit = mBuf + mElementsSize;
|
|
|
|
|
2017-04-05 15:13:00 +03:00
|
|
|
while (data != limit) {
|
|
|
|
size_t maxLen = limit - data;
|
|
|
|
size_t keyLen = strnlen(data, maxLen);
|
|
|
|
MOZ_RELEASE_ASSERT(keyLen != maxLen, "Metadata elements corrupted. Key "
|
|
|
|
"isn't null terminated!");
|
|
|
|
MOZ_RELEASE_ASSERT(keyLen + 1 != maxLen, "Metadata elements corrupted. "
|
|
|
|
"There is no value for the key!");
|
|
|
|
|
|
|
|
const char *value = data + keyLen + 1;
|
|
|
|
maxLen = limit - value;
|
|
|
|
size_t valueLen = strnlen(value, maxLen);
|
|
|
|
MOZ_RELEASE_ASSERT(valueLen != maxLen, "Metadata elements corrupted. Value "
|
|
|
|
"isn't null terminated!");
|
|
|
|
|
2013-09-20 13:11:26 +04:00
|
|
|
if (strcmp(data, aKey) == 0) {
|
|
|
|
LOG(("CacheFileMetadata::GetElement() - Key found [this=%p, key=%s]",
|
|
|
|
this, aKey));
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2017-04-05 15:13:00 +03:00
|
|
|
// point to next pair
|
|
|
|
data += keyLen + valueLen + 2;
|
2013-09-20 13:11:26 +04:00
|
|
|
}
|
|
|
|
LOG(("CacheFileMetadata::GetElement() - Key not found [this=%p, key=%s]",
|
|
|
|
this, aKey));
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
CacheFileMetadata::SetElement(const char *aKey, const char *aValue)
|
|
|
|
{
|
|
|
|
LOG(("CacheFileMetadata::SetElement() [this=%p, key=%s, value=%p]",
|
|
|
|
this, aKey, aValue));
|
|
|
|
|
|
|
|
MarkDirty();
|
|
|
|
|
2015-07-22 16:23:16 +03:00
|
|
|
nsresult rv;
|
|
|
|
|
2013-09-20 13:11:26 +04:00
|
|
|
const uint32_t keySize = strlen(aKey) + 1;
|
|
|
|
char *pos = const_cast<char *>(GetElement(aKey));
|
|
|
|
|
|
|
|
if (!aValue) {
|
|
|
|
// No value means remove the key/value pair completely, if existing
|
|
|
|
if (pos) {
|
|
|
|
uint32_t oldValueSize = strlen(pos) + 1;
|
|
|
|
uint32_t offset = pos - mBuf;
|
|
|
|
uint32_t remainder = mElementsSize - (offset + oldValueSize);
|
|
|
|
|
|
|
|
memmove(pos - keySize, pos + oldValueSize, remainder);
|
|
|
|
mElementsSize -= keySize + oldValueSize;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint32_t valueSize = strlen(aValue) + 1;
|
|
|
|
uint32_t newSize = mElementsSize + valueSize;
|
|
|
|
if (pos) {
|
|
|
|
const uint32_t oldValueSize = strlen(pos) + 1;
|
|
|
|
const uint32_t offset = pos - mBuf;
|
|
|
|
const uint32_t remainder = mElementsSize - (offset + oldValueSize);
|
|
|
|
|
|
|
|
// Update the value in place
|
|
|
|
newSize -= oldValueSize;
|
2015-07-22 16:23:16 +03:00
|
|
|
rv = EnsureBuffer(newSize);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2013-09-20 13:11:26 +04:00
|
|
|
|
|
|
|
// Move the remainder to the right place
|
|
|
|
pos = mBuf + offset;
|
|
|
|
memmove(pos + valueSize, pos + oldValueSize, remainder);
|
|
|
|
} else {
|
|
|
|
// allocate new meta data element
|
|
|
|
newSize += keySize;
|
2015-07-22 16:23:16 +03:00
|
|
|
rv = EnsureBuffer(newSize);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2013-09-20 13:11:26 +04:00
|
|
|
|
|
|
|
// Add after last element
|
|
|
|
pos = mBuf + mElementsSize;
|
|
|
|
memcpy(pos, aKey, keySize);
|
|
|
|
pos += keySize;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update value
|
|
|
|
memcpy(pos, aValue, valueSize);
|
|
|
|
mElementsSize = newSize;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-05-01 15:28:12 +04:00
|
|
|
nsresult
|
|
|
|
CacheFileMetadata::Visit(nsICacheEntryMetaDataVisitor *aVisitor)
|
|
|
|
{
|
|
|
|
const char *data = mBuf;
|
|
|
|
const char *limit = mBuf + mElementsSize;
|
|
|
|
|
|
|
|
while (data < limit) {
|
|
|
|
// Point to the value part
|
|
|
|
const char *value = data + strlen(data) + 1;
|
|
|
|
MOZ_ASSERT(value < limit, "Metadata elements corrupted");
|
|
|
|
|
|
|
|
aVisitor->OnMetaDataElement(data, value);
|
|
|
|
|
|
|
|
// Skip value part
|
|
|
|
data = value + strlen(value) + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(data == limit, "Metadata elements corrupted");
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-02-18 19:05:07 +04:00
|
|
|
CacheHash::Hash16_t
|
2013-09-20 13:11:26 +04:00
|
|
|
CacheFileMetadata::GetHash(uint32_t aIndex)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aIndex < mHashCount);
|
2014-04-08 00:07:38 +04:00
|
|
|
return NetworkEndian::readUint16(&mHashArray[aIndex]);
|
2013-09-20 13:11:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-02-18 19:05:07 +04:00
|
|
|
CacheFileMetadata::SetHash(uint32_t aIndex, CacheHash::Hash16_t aHash)
|
2013-09-20 13:11:26 +04:00
|
|
|
{
|
|
|
|
LOG(("CacheFileMetadata::SetHash() [this=%p, idx=%d, hash=%x]",
|
|
|
|
this, aIndex, aHash));
|
|
|
|
|
|
|
|
MarkDirty();
|
|
|
|
|
|
|
|
MOZ_ASSERT(aIndex <= mHashCount);
|
|
|
|
|
|
|
|
if (aIndex > mHashCount) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
} else if (aIndex == mHashCount) {
|
2014-02-18 19:05:07 +04:00
|
|
|
if ((aIndex + 1) * sizeof(CacheHash::Hash16_t) > mHashArraySize) {
|
2013-09-20 13:11:26 +04:00
|
|
|
// reallocate hash array buffer
|
2015-03-06 17:49:04 +03:00
|
|
|
if (mHashArraySize == 0) {
|
|
|
|
mHashArraySize = kInitialHashArraySize * sizeof(CacheHash::Hash16_t);
|
|
|
|
} else {
|
2013-09-20 13:11:26 +04:00
|
|
|
mHashArraySize *= 2;
|
2015-03-06 17:49:04 +03:00
|
|
|
}
|
2014-02-18 19:05:07 +04:00
|
|
|
mHashArray = static_cast<CacheHash::Hash16_t *>(
|
2013-09-20 13:11:26 +04:00
|
|
|
moz_xrealloc(mHashArray, mHashArraySize));
|
|
|
|
}
|
|
|
|
|
|
|
|
mHashCount++;
|
|
|
|
}
|
|
|
|
|
2014-04-08 00:07:38 +04:00
|
|
|
NetworkEndian::writeUint16(&mHashArray[aIndex], aHash);
|
2013-09-20 13:11:26 +04:00
|
|
|
|
|
|
|
DoMemoryReport(MemoryUsage());
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-03-09 19:28:56 +03:00
|
|
|
nsresult
|
|
|
|
CacheFileMetadata::RemoveHash(uint32_t aIndex)
|
|
|
|
{
|
|
|
|
LOG(("CacheFileMetadata::RemoveHash() [this=%p, idx=%d]", this, aIndex));
|
|
|
|
|
|
|
|
MarkDirty();
|
|
|
|
|
|
|
|
MOZ_ASSERT((aIndex + 1) == mHashCount, "Can remove only last hash!");
|
|
|
|
|
|
|
|
if (aIndex + 1 != mHashCount) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
mHashCount--;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-10-22 13:11:00 +03:00
|
|
|
nsresult
|
|
|
|
CacheFileMetadata::AddFlags(uint32_t aFlags)
|
|
|
|
{
|
|
|
|
MarkDirty(false);
|
|
|
|
mMetaHdr.mFlags |= aFlags;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
CacheFileMetadata::RemoveFlags(uint32_t aFlags)
|
|
|
|
{
|
|
|
|
MarkDirty(false);
|
|
|
|
mMetaHdr.mFlags &= ~aFlags;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
CacheFileMetadata::GetFlags(uint32_t *_retval)
|
|
|
|
{
|
|
|
|
*_retval = mMetaHdr.mFlags;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-09-20 13:11:26 +04:00
|
|
|
nsresult
|
|
|
|
CacheFileMetadata::SetExpirationTime(uint32_t aExpirationTime)
|
|
|
|
{
|
|
|
|
LOG(("CacheFileMetadata::SetExpirationTime() [this=%p, expirationTime=%d]",
|
|
|
|
this, aExpirationTime));
|
|
|
|
|
2014-09-16 17:51:50 +04:00
|
|
|
MarkDirty(false);
|
2013-09-20 13:11:26 +04:00
|
|
|
mMetaHdr.mExpirationTime = aExpirationTime;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
CacheFileMetadata::GetExpirationTime(uint32_t *_retval)
|
|
|
|
{
|
|
|
|
*_retval = mMetaHdr.mExpirationTime;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-09-11 16:31:54 +04:00
|
|
|
CacheFileMetadata::SetFrecency(uint32_t aFrecency)
|
2013-09-20 13:11:26 +04:00
|
|
|
{
|
2014-09-11 16:31:54 +04:00
|
|
|
LOG(("CacheFileMetadata::SetFrecency() [this=%p, frecency=%f]",
|
|
|
|
this, (double)aFrecency));
|
|
|
|
|
2014-09-16 17:51:50 +04:00
|
|
|
MarkDirty(false);
|
2014-09-11 16:31:54 +04:00
|
|
|
mMetaHdr.mFrecency = aFrecency;
|
2013-09-20 13:11:26 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-09-11 16:31:54 +04:00
|
|
|
CacheFileMetadata::GetFrecency(uint32_t *_retval)
|
2013-09-20 13:11:26 +04:00
|
|
|
{
|
2014-09-11 16:31:54 +04:00
|
|
|
*_retval = mMetaHdr.mFrecency;
|
2013-09-20 13:11:26 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-09-16 17:51:50 +04:00
|
|
|
nsresult
|
|
|
|
CacheFileMetadata::GetLastModified(uint32_t *_retval)
|
|
|
|
{
|
|
|
|
*_retval = mMetaHdr.mLastModified;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-09-11 13:44:31 +04:00
|
|
|
nsresult
|
2014-09-11 16:31:54 +04:00
|
|
|
CacheFileMetadata::GetLastFetched(uint32_t *_retval)
|
2014-09-11 13:44:31 +04:00
|
|
|
{
|
2014-09-11 16:31:54 +04:00
|
|
|
*_retval = mMetaHdr.mLastFetched;
|
2014-09-11 13:44:31 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-09-11 16:31:54 +04:00
|
|
|
nsresult
|
|
|
|
CacheFileMetadata::GetFetchCount(uint32_t *_retval)
|
2014-09-11 13:44:31 +04:00
|
|
|
{
|
2014-09-11 16:31:54 +04:00
|
|
|
*_retval = mMetaHdr.mFetchCount;
|
|
|
|
return NS_OK;
|
2014-09-11 13:44:31 +04:00
|
|
|
}
|
|
|
|
|
2014-09-16 17:51:50 +04:00
|
|
|
nsresult
|
|
|
|
CacheFileMetadata::OnFetched()
|
|
|
|
{
|
|
|
|
MarkDirty(false);
|
|
|
|
|
|
|
|
mMetaHdr.mLastFetched = NOW_SECONDS();
|
|
|
|
++mMetaHdr.mFetchCount;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CacheFileMetadata::MarkDirty(bool aUpdateLastModified)
|
|
|
|
{
|
|
|
|
mIsDirty = true;
|
|
|
|
if (aUpdateLastModified) {
|
|
|
|
mMetaHdr.mLastModified = NOW_SECONDS();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-20 13:11:26 +04:00
|
|
|
nsresult
|
|
|
|
CacheFileMetadata::OnFileOpened(CacheFileHandle *aHandle, nsresult aResult)
|
|
|
|
{
|
|
|
|
MOZ_CRASH("CacheFileMetadata::OnFileOpened should not be called!");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
CacheFileMetadata::OnDataWritten(CacheFileHandle *aHandle, const char *aBuf,
|
|
|
|
nsresult aResult)
|
|
|
|
{
|
2016-12-16 06:16:31 +03:00
|
|
|
LOG(("CacheFileMetadata::OnDataWritten() [this=%p, handle=%p, result=0x%08" PRIx32 "]",
|
|
|
|
this, aHandle, static_cast<uint32_t>(aResult)));
|
2013-09-20 13:11:26 +04:00
|
|
|
|
|
|
|
MOZ_ASSERT(mListener);
|
|
|
|
MOZ_ASSERT(mWriteBuf);
|
|
|
|
|
2016-05-11 12:46:54 +03:00
|
|
|
CacheFileUtils::FreeBuffer(mWriteBuf);
|
2013-09-20 13:11:26 +04:00
|
|
|
mWriteBuf = nullptr;
|
|
|
|
|
|
|
|
nsCOMPtr<CacheFileMetadataListener> listener;
|
|
|
|
|
|
|
|
mListener.swap(listener);
|
|
|
|
listener->OnMetadataWritten(aResult);
|
|
|
|
|
|
|
|
DoMemoryReport(MemoryUsage());
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
CacheFileMetadata::OnDataRead(CacheFileHandle *aHandle, char *aBuf,
|
|
|
|
nsresult aResult)
|
|
|
|
{
|
2016-12-16 06:16:31 +03:00
|
|
|
LOG(("CacheFileMetadata::OnDataRead() [this=%p, handle=%p, result=0x%08" PRIx32 "]",
|
|
|
|
this, aHandle, static_cast<uint32_t>(aResult)));
|
2013-09-20 13:11:26 +04:00
|
|
|
|
|
|
|
MOZ_ASSERT(mListener);
|
|
|
|
|
2015-06-24 07:33:37 +03:00
|
|
|
nsresult rv;
|
2013-09-20 13:11:26 +04:00
|
|
|
nsCOMPtr<CacheFileMetadataListener> listener;
|
|
|
|
|
|
|
|
if (NS_FAILED(aResult)) {
|
2014-02-18 21:26:48 +04:00
|
|
|
LOG(("CacheFileMetadata::OnDataRead() - CacheFileIOManager::Read() failed"
|
2016-12-16 06:16:31 +03:00
|
|
|
", creating empty metadata. [this=%p, rv=0x%08" PRIx32 "]",
|
|
|
|
this, static_cast<uint32_t>(aResult)));
|
2013-12-16 21:01:54 +04:00
|
|
|
|
2014-02-18 21:26:48 +04:00
|
|
|
InitEmptyMetadata();
|
2013-09-20 13:11:26 +04:00
|
|
|
|
|
|
|
mListener.swap(listener);
|
2015-06-24 07:33:37 +03:00
|
|
|
listener->OnMetadataRead(NS_OK);
|
2013-09-20 13:11:26 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-02-23 18:13:02 +03:00
|
|
|
if (mFirstRead) {
|
|
|
|
Telemetry::AccumulateTimeDelta(
|
|
|
|
Telemetry::NETWORK_CACHE_METADATA_FIRST_READ_TIME_MS, mReadStart);
|
|
|
|
Telemetry::Accumulate(
|
|
|
|
Telemetry::NETWORK_CACHE_METADATA_FIRST_READ_SIZE, mBufSize);
|
|
|
|
} else {
|
|
|
|
Telemetry::AccumulateTimeDelta(
|
|
|
|
Telemetry::NETWORK_CACHE_METADATA_SECOND_READ_TIME_MS, mReadStart);
|
|
|
|
}
|
|
|
|
|
2013-09-20 13:11:26 +04:00
|
|
|
// check whether we have read all necessary data
|
2014-04-08 00:07:38 +04:00
|
|
|
uint32_t realOffset = NetworkEndian::readUint32(mBuf + mBufSize -
|
|
|
|
sizeof(uint32_t));
|
2013-09-20 13:11:26 +04:00
|
|
|
|
|
|
|
int64_t size = mHandle->FileSize();
|
|
|
|
MOZ_ASSERT(size != -1);
|
|
|
|
|
|
|
|
if (realOffset >= size) {
|
2014-02-18 21:26:48 +04:00
|
|
|
LOG(("CacheFileMetadata::OnDataRead() - Invalid realOffset, creating "
|
2016-12-16 06:16:31 +03:00
|
|
|
"empty metadata. [this=%p, realOffset=%u, size=%" PRId64 "]", this,
|
2014-02-18 21:26:48 +04:00
|
|
|
realOffset, size));
|
2013-12-16 21:01:54 +04:00
|
|
|
|
2014-02-18 21:26:48 +04:00
|
|
|
InitEmptyMetadata();
|
2013-09-20 13:11:26 +04:00
|
|
|
|
|
|
|
mListener.swap(listener);
|
2015-06-24 07:33:37 +03:00
|
|
|
listener->OnMetadataRead(NS_OK);
|
2013-09-20 13:11:26 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-07-22 16:23:16 +03:00
|
|
|
uint32_t maxHashCount = size / kChunkSize;
|
|
|
|
uint32_t maxMetadataSize = CalcMetadataSize(kMaxElementsSize, maxHashCount);
|
|
|
|
if (size - realOffset > maxMetadataSize) {
|
|
|
|
LOG(("CacheFileMetadata::OnDataRead() - Invalid realOffset, metadata would "
|
|
|
|
"be too big, creating empty metadata. [this=%p, realOffset=%u, "
|
2016-12-16 06:16:31 +03:00
|
|
|
"maxMetadataSize=%u, size=%" PRId64 "]", this, realOffset, maxMetadataSize,
|
2015-07-22 16:23:16 +03:00
|
|
|
size));
|
|
|
|
|
|
|
|
InitEmptyMetadata();
|
|
|
|
|
|
|
|
mListener.swap(listener);
|
|
|
|
listener->OnMetadataRead(NS_OK);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-09-20 13:11:26 +04:00
|
|
|
uint32_t usedOffset = size - mBufSize;
|
|
|
|
|
|
|
|
if (realOffset < usedOffset) {
|
|
|
|
uint32_t missing = usedOffset - realOffset;
|
|
|
|
// we need to read more data
|
2015-06-24 07:33:37 +03:00
|
|
|
char *newBuf = static_cast<char *>(realloc(mBuf, mBufSize + missing));
|
|
|
|
if (!newBuf) {
|
|
|
|
LOG(("CacheFileMetadata::OnDataRead() - Error allocating %d more bytes "
|
|
|
|
"for the missing part of the metadata, creating empty metadata. "
|
|
|
|
"[this=%p]", missing, this));
|
|
|
|
|
|
|
|
InitEmptyMetadata();
|
|
|
|
|
|
|
|
mListener.swap(listener);
|
|
|
|
listener->OnMetadataRead(NS_OK);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
mBuf = newBuf;
|
2013-09-20 13:11:26 +04:00
|
|
|
memmove(mBuf + missing, mBuf, mBufSize);
|
|
|
|
mBufSize += missing;
|
|
|
|
|
|
|
|
DoMemoryReport(MemoryUsage());
|
|
|
|
|
|
|
|
LOG(("CacheFileMetadata::OnDataRead() - We need to read %d more bytes to "
|
|
|
|
"have full metadata. [this=%p]", missing, this));
|
|
|
|
|
2015-02-23 18:13:02 +03:00
|
|
|
mFirstRead = false;
|
|
|
|
mReadStart = mozilla::TimeStamp::Now();
|
2014-08-18 17:36:27 +04:00
|
|
|
rv = CacheFileIOManager::Read(mHandle, realOffset, mBuf, missing, this);
|
2013-09-20 13:11:26 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
2014-02-18 21:26:48 +04:00
|
|
|
LOG(("CacheFileMetadata::OnDataRead() - CacheFileIOManager::Read() "
|
|
|
|
"failed synchronously, creating empty metadata. [this=%p, "
|
2016-12-16 06:16:31 +03:00
|
|
|
"rv=0x%08" PRIx32 "]", this, static_cast<uint32_t>(rv)));
|
2014-02-18 21:26:48 +04:00
|
|
|
|
|
|
|
InitEmptyMetadata();
|
2013-09-20 13:11:26 +04:00
|
|
|
|
|
|
|
mListener.swap(listener);
|
2015-06-24 07:33:37 +03:00
|
|
|
listener->OnMetadataRead(NS_OK);
|
2013-09-20 13:11:26 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-02-23 18:13:02 +03:00
|
|
|
Telemetry::Accumulate(Telemetry::NETWORK_CACHE_METADATA_SIZE,
|
|
|
|
size - realOffset);
|
|
|
|
|
2013-09-20 13:11:26 +04:00
|
|
|
// We have all data according to offset information at the end of the entry.
|
|
|
|
// Try to parse it.
|
2014-02-18 21:26:48 +04:00
|
|
|
rv = ParseMetadata(realOffset, realOffset - usedOffset, true);
|
2013-09-20 13:11:26 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
2014-02-18 21:26:48 +04:00
|
|
|
LOG(("CacheFileMetadata::OnDataRead() - Error parsing metadata, creating "
|
|
|
|
"empty metadata. [this=%p]", this));
|
|
|
|
InitEmptyMetadata();
|
2015-03-06 17:49:04 +03:00
|
|
|
} else {
|
|
|
|
// Shrink elements buffer.
|
|
|
|
mBuf = static_cast<char *>(moz_xrealloc(mBuf, mElementsSize));
|
|
|
|
mBufSize = mElementsSize;
|
|
|
|
|
|
|
|
// There is usually no or just one call to SetMetadataElement() when the
|
|
|
|
// metadata is parsed from disk. Avoid allocating power of two sized buffer
|
|
|
|
// which we do in case of newly created metadata.
|
|
|
|
mAllocExactSize = true;
|
2013-09-20 13:11:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
mListener.swap(listener);
|
2015-06-24 07:33:37 +03:00
|
|
|
listener->OnMetadataRead(NS_OK);
|
2013-09-20 13:11:26 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
CacheFileMetadata::OnFileDoomed(CacheFileHandle *aHandle, nsresult aResult)
|
|
|
|
{
|
|
|
|
MOZ_CRASH("CacheFileMetadata::OnFileDoomed should not be called!");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
CacheFileMetadata::OnEOFSet(CacheFileHandle *aHandle, nsresult aResult)
|
|
|
|
{
|
|
|
|
MOZ_CRASH("CacheFileMetadata::OnEOFSet should not be called!");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
2014-02-18 19:05:07 +04:00
|
|
|
nsresult
|
|
|
|
CacheFileMetadata::OnFileRenamed(CacheFileHandle *aHandle, nsresult aResult)
|
|
|
|
{
|
|
|
|
MOZ_CRASH("CacheFileMetadata::OnFileRenamed should not be called!");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
2013-09-20 13:11:26 +04:00
|
|
|
void
|
|
|
|
CacheFileMetadata::InitEmptyMetadata()
|
|
|
|
{
|
|
|
|
if (mBuf) {
|
2016-05-11 12:46:54 +03:00
|
|
|
CacheFileUtils::FreeBuffer(mBuf);
|
2013-09-20 13:11:26 +04:00
|
|
|
mBuf = nullptr;
|
|
|
|
mBufSize = 0;
|
|
|
|
}
|
2016-08-17 13:58:44 +03:00
|
|
|
mAllocExactSize = false;
|
2013-09-20 13:11:26 +04:00
|
|
|
mOffset = 0;
|
2014-04-08 00:07:43 +04:00
|
|
|
mMetaHdr.mVersion = kCacheEntryVersion;
|
2014-09-16 17:51:50 +04:00
|
|
|
mMetaHdr.mFetchCount = 0;
|
2014-02-18 19:05:07 +04:00
|
|
|
mMetaHdr.mExpirationTime = nsICacheEntry::NO_EXPIRATION_TIME;
|
2013-09-20 13:11:26 +04:00
|
|
|
mMetaHdr.mKeySize = mKey.Length();
|
|
|
|
|
2015-10-22 13:11:00 +03:00
|
|
|
// Deliberately not touching the "kCacheEntryIsPinned" flag.
|
|
|
|
|
2013-09-20 13:11:26 +04:00
|
|
|
DoMemoryReport(MemoryUsage());
|
2015-03-27 01:05:34 +03:00
|
|
|
|
|
|
|
// We're creating a new entry. If there is any old data truncate it.
|
2015-10-22 13:11:00 +03:00
|
|
|
if (mHandle) {
|
|
|
|
mHandle->SetPinned(Pinned());
|
2016-08-05 12:10:00 +03:00
|
|
|
// We can pronounce the handle as invalid now, because it simply
|
|
|
|
// doesn't have the correct metadata. This will cause IO operations
|
|
|
|
// be bypassed during shutdown (mainly dooming it, when a channel
|
|
|
|
// is canceled by closing the window.)
|
|
|
|
mHandle->SetInvalid();
|
2015-10-22 13:11:00 +03:00
|
|
|
if (mHandle->FileExists() && mHandle->FileSize()) {
|
|
|
|
CacheFileIOManager::TruncateSeekSetEOF(mHandle, 0, 0, nullptr);
|
|
|
|
}
|
2015-03-27 01:05:34 +03:00
|
|
|
}
|
2013-09-20 13:11:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-02-18 21:26:48 +04:00
|
|
|
CacheFileMetadata::ParseMetadata(uint32_t aMetaOffset, uint32_t aBufOffset,
|
|
|
|
bool aHaveKey)
|
2013-09-20 13:11:26 +04:00
|
|
|
{
|
|
|
|
LOG(("CacheFileMetadata::ParseMetadata() [this=%p, metaOffset=%d, "
|
2014-02-18 21:26:48 +04:00
|
|
|
"bufOffset=%d, haveKey=%u]", this, aMetaOffset, aBufOffset, aHaveKey));
|
2013-09-20 13:11:26 +04:00
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
uint32_t metaposOffset = mBufSize - sizeof(uint32_t);
|
|
|
|
uint32_t hashesOffset = aBufOffset + sizeof(uint32_t);
|
|
|
|
uint32_t hashCount = aMetaOffset / kChunkSize;
|
|
|
|
if (aMetaOffset % kChunkSize)
|
|
|
|
hashCount++;
|
2014-02-18 19:05:07 +04:00
|
|
|
uint32_t hashesLen = hashCount * sizeof(CacheHash::Hash16_t);
|
2013-09-20 13:11:26 +04:00
|
|
|
uint32_t hdrOffset = hashesOffset + hashesLen;
|
|
|
|
uint32_t keyOffset = hdrOffset + sizeof(CacheFileMetadataHeader);
|
|
|
|
|
|
|
|
LOG(("CacheFileMetadata::ParseMetadata() [this=%p]\n metaposOffset=%d\n "
|
|
|
|
"hashesOffset=%d\n hashCount=%d\n hashesLen=%d\n hdfOffset=%d\n "
|
|
|
|
"keyOffset=%d\n", this, metaposOffset, hashesOffset, hashCount,
|
|
|
|
hashesLen,hdrOffset, keyOffset));
|
|
|
|
|
|
|
|
if (keyOffset > metaposOffset) {
|
|
|
|
LOG(("CacheFileMetadata::ParseMetadata() - Wrong keyOffset! [this=%p]",
|
|
|
|
this));
|
|
|
|
return NS_ERROR_FILE_CORRUPTED;
|
|
|
|
}
|
|
|
|
|
2014-04-10 16:29:46 +04:00
|
|
|
mMetaHdr.ReadFromBuf(mBuf + hdrOffset);
|
|
|
|
|
2015-10-22 13:11:00 +03:00
|
|
|
if (mMetaHdr.mVersion == 1) {
|
|
|
|
// Backward compatibility before we've added flags to the header
|
|
|
|
keyOffset -= sizeof(uint32_t);
|
2016-08-17 13:58:44 +03:00
|
|
|
} else if (mMetaHdr.mVersion == 2) {
|
|
|
|
// Version 2 just lacks the ability to store alternative data. Nothing to do
|
|
|
|
// here.
|
2015-10-22 13:11:00 +03:00
|
|
|
} else if (mMetaHdr.mVersion != kCacheEntryVersion) {
|
2014-04-10 16:29:46 +04:00
|
|
|
LOG(("CacheFileMetadata::ParseMetadata() - Not a version we understand to. "
|
|
|
|
"[version=0x%x, this=%p]", mMetaHdr.mVersion, this));
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
2015-10-22 13:11:00 +03:00
|
|
|
// Update the version stored in the header to make writes
|
|
|
|
// store the header in the current version form.
|
|
|
|
mMetaHdr.mVersion = kCacheEntryVersion;
|
|
|
|
|
2014-04-10 16:29:46 +04:00
|
|
|
uint32_t elementsOffset = mMetaHdr.mKeySize + keyOffset + 1;
|
2013-09-20 13:11:26 +04:00
|
|
|
|
|
|
|
if (elementsOffset > metaposOffset) {
|
|
|
|
LOG(("CacheFileMetadata::ParseMetadata() - Wrong elementsOffset %d "
|
|
|
|
"[this=%p]", elementsOffset, this));
|
|
|
|
return NS_ERROR_FILE_CORRUPTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check that key ends with \0
|
|
|
|
if (mBuf[elementsOffset - 1] != 0) {
|
|
|
|
LOG(("CacheFileMetadata::ParseMetadata() - Elements not null terminated. "
|
|
|
|
"[this=%p]", this));
|
|
|
|
return NS_ERROR_FILE_CORRUPTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-18 21:26:48 +04:00
|
|
|
if (!aHaveKey) {
|
|
|
|
// get the key form metadata
|
2014-04-10 16:29:46 +04:00
|
|
|
mKey.Assign(mBuf + keyOffset, mMetaHdr.mKeySize);
|
2014-02-18 19:05:07 +04:00
|
|
|
|
2014-02-18 21:26:48 +04:00
|
|
|
rv = ParseKey(mKey);
|
2014-02-18 19:05:07 +04:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
2013-12-16 21:01:54 +04:00
|
|
|
}
|
|
|
|
else {
|
2014-04-10 16:29:46 +04:00
|
|
|
if (mMetaHdr.mKeySize != mKey.Length()) {
|
2013-12-16 21:01:54 +04:00
|
|
|
LOG(("CacheFileMetadata::ParseMetadata() - Key collision (1), key=%s "
|
2014-04-10 16:29:46 +04:00
|
|
|
"[this=%p]", nsCString(mBuf + keyOffset, mMetaHdr.mKeySize).get(),
|
|
|
|
this));
|
2013-09-20 13:11:26 +04:00
|
|
|
return NS_ERROR_FILE_CORRUPTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (memcmp(mKey.get(), mBuf + keyOffset, mKey.Length()) != 0) {
|
2013-12-16 21:01:54 +04:00
|
|
|
LOG(("CacheFileMetadata::ParseMetadata() - Key collision (2), key=%s "
|
2014-04-10 16:29:46 +04:00
|
|
|
"[this=%p]", nsCString(mBuf + keyOffset, mMetaHdr.mKeySize).get(),
|
|
|
|
this));
|
2013-09-20 13:11:26 +04:00
|
|
|
return NS_ERROR_FILE_CORRUPTED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check metadata hash (data from hashesOffset to metaposOffset)
|
2014-04-08 00:07:38 +04:00
|
|
|
CacheHash::Hash32_t hashComputed, hashExpected;
|
|
|
|
hashComputed = CacheHash::Hash(mBuf + hashesOffset,
|
|
|
|
metaposOffset - hashesOffset);
|
|
|
|
hashExpected = NetworkEndian::readUint32(mBuf + aBufOffset);
|
2013-09-20 13:11:26 +04:00
|
|
|
|
2014-04-08 00:07:38 +04:00
|
|
|
if (hashComputed != hashExpected) {
|
2013-09-20 13:11:26 +04:00
|
|
|
LOG(("CacheFileMetadata::ParseMetadata() - Metadata hash mismatch! Hash of "
|
2014-04-08 00:07:38 +04:00
|
|
|
"the metadata is %x, hash in file is %x [this=%p]", hashComputed,
|
|
|
|
hashExpected, this));
|
2013-09-20 13:11:26 +04:00
|
|
|
return NS_ERROR_FILE_CORRUPTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check elements
|
|
|
|
rv = CheckElements(mBuf + elementsOffset, metaposOffset - elementsOffset);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2015-10-22 13:11:00 +03:00
|
|
|
if (mHandle) {
|
|
|
|
if (!mHandle->SetPinned(Pinned())) {
|
|
|
|
LOG(("CacheFileMetadata::ParseMetadata() - handle was doomed for this "
|
|
|
|
"pinning state, truncate the file [this=%p, pinned=%d]", this, Pinned()));
|
|
|
|
return NS_ERROR_FILE_CORRUPTED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-20 13:11:26 +04:00
|
|
|
mHashArraySize = hashesLen;
|
|
|
|
mHashCount = hashCount;
|
|
|
|
if (mHashArraySize) {
|
2014-02-18 19:05:07 +04:00
|
|
|
mHashArray = static_cast<CacheHash::Hash16_t *>(
|
2013-09-20 13:11:26 +04:00
|
|
|
moz_xmalloc(mHashArraySize));
|
|
|
|
memcpy(mHashArray, mBuf + hashesOffset, mHashArraySize);
|
|
|
|
}
|
|
|
|
|
|
|
|
MarkDirty();
|
|
|
|
|
|
|
|
mElementsSize = metaposOffset - elementsOffset;
|
|
|
|
memmove(mBuf, mBuf + elementsOffset, mElementsSize);
|
|
|
|
mOffset = aMetaOffset;
|
|
|
|
|
|
|
|
DoMemoryReport(MemoryUsage());
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
CacheFileMetadata::CheckElements(const char *aBuf, uint32_t aSize)
|
|
|
|
{
|
|
|
|
if (aSize) {
|
|
|
|
// Check if the metadata ends with a zero byte.
|
|
|
|
if (aBuf[aSize - 1] != 0) {
|
|
|
|
NS_ERROR("Metadata elements are not null terminated");
|
|
|
|
LOG(("CacheFileMetadata::CheckElements() - Elements are not null "
|
|
|
|
"terminated. [this=%p]", this));
|
|
|
|
return NS_ERROR_FILE_CORRUPTED;
|
|
|
|
}
|
|
|
|
// Check that there are an even number of zero bytes
|
|
|
|
// to match the pattern { key \0 value \0 }
|
|
|
|
bool odd = false;
|
|
|
|
for (uint32_t i = 0; i < aSize; i++) {
|
|
|
|
if (aBuf[i] == 0)
|
|
|
|
odd = !odd;
|
|
|
|
}
|
|
|
|
if (odd) {
|
|
|
|
NS_ERROR("Metadata elements are malformed");
|
|
|
|
LOG(("CacheFileMetadata::CheckElements() - Elements are malformed. "
|
|
|
|
"[this=%p]", this));
|
|
|
|
return NS_ERROR_FILE_CORRUPTED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-07-22 16:23:16 +03:00
|
|
|
nsresult
|
2013-09-20 13:11:26 +04:00
|
|
|
CacheFileMetadata::EnsureBuffer(uint32_t aSize)
|
|
|
|
{
|
2015-07-22 16:23:16 +03:00
|
|
|
if (aSize > kMaxElementsSize) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2013-09-20 13:11:26 +04:00
|
|
|
if (mBufSize < aSize) {
|
2015-03-06 17:49:04 +03:00
|
|
|
if (mAllocExactSize) {
|
|
|
|
// If this is not the only allocation, use power of two for following
|
|
|
|
// allocations.
|
|
|
|
mAllocExactSize = false;
|
|
|
|
} else {
|
|
|
|
// find smallest power of 2 greater than or equal to aSize
|
|
|
|
--aSize;
|
|
|
|
aSize |= aSize >> 1;
|
|
|
|
aSize |= aSize >> 2;
|
|
|
|
aSize |= aSize >> 4;
|
|
|
|
aSize |= aSize >> 8;
|
|
|
|
aSize |= aSize >> 16;
|
|
|
|
++aSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aSize < kInitialBufSize) {
|
|
|
|
aSize = kInitialBufSize;
|
|
|
|
}
|
|
|
|
|
2015-07-22 16:23:16 +03:00
|
|
|
char *newBuf = static_cast<char *>(realloc(mBuf, aSize));
|
|
|
|
if (!newBuf) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2013-09-20 13:11:26 +04:00
|
|
|
mBufSize = aSize;
|
2015-07-22 16:23:16 +03:00
|
|
|
mBuf = newBuf;
|
2013-09-20 13:11:26 +04:00
|
|
|
|
2015-03-06 17:49:04 +03:00
|
|
|
DoMemoryReport(MemoryUsage());
|
|
|
|
}
|
2015-07-22 16:23:16 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
2013-09-20 13:11:26 +04:00
|
|
|
}
|
|
|
|
|
2014-02-18 19:05:07 +04:00
|
|
|
nsresult
|
|
|
|
CacheFileMetadata::ParseKey(const nsACString &aKey)
|
|
|
|
{
|
2014-04-06 22:44:40 +04:00
|
|
|
nsCOMPtr<nsILoadContextInfo> info = CacheFileUtils::ParseKey(aKey);
|
|
|
|
NS_ENSURE_TRUE(info, NS_ERROR_FAILURE);
|
2014-02-18 19:05:07 +04:00
|
|
|
|
2014-02-18 21:26:48 +04:00
|
|
|
mAnonymous = info->IsAnonymous();
|
2015-10-11 19:13:09 +03:00
|
|
|
mOriginAttributes = *info->OriginAttributesPtr();
|
2014-02-18 19:05:07 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-02-27 03:11:40 +04:00
|
|
|
// Memory reporting
|
|
|
|
|
|
|
|
size_t
|
|
|
|
CacheFileMetadata::SizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) const
|
|
|
|
{
|
|
|
|
size_t n = 0;
|
|
|
|
// mHandle reported via CacheFileIOManager.
|
|
|
|
n += mKey.SizeOfExcludingThisIfUnshared(mallocSizeOf);
|
|
|
|
n += mallocSizeOf(mHashArray);
|
|
|
|
n += mallocSizeOf(mBuf);
|
2017-10-03 17:49:39 +03:00
|
|
|
// Ignore mWriteBuf, it's not safe to access it when metadata is being
|
|
|
|
// written and it's null otherwise.
|
2014-02-27 03:11:40 +04:00
|
|
|
// mListener is usually the owning CacheFile.
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
CacheFileMetadata::SizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf) const
|
|
|
|
{
|
|
|
|
return mallocSizeOf(this) + SizeOfExcludingThis(mallocSizeOf);
|
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace net
|
|
|
|
} // namespace mozilla
|