2002-05-02 02:08:07 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
*
|
2012-05-21 15:12:37 +04:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2002-05-02 02:08:07 +04:00
|
|
|
|
|
|
|
|
2012-11-22 05:14:00 +04:00
|
|
|
#include "nsCache.h"
|
2002-08-07 05:13:29 +04:00
|
|
|
#include "nsDiskCache.h"
|
2002-05-02 02:08:07 +04:00
|
|
|
#include "nsDiskCacheDevice.h"
|
|
|
|
#include "nsDiskCacheStreams.h"
|
2002-08-29 08:30:54 +04:00
|
|
|
#include "nsCacheService.h"
|
2010-11-24 18:42:30 +03:00
|
|
|
#include "mozilla/FileUtils.h"
|
2012-01-07 05:45:46 +04:00
|
|
|
#include "nsThreadUtils.h"
|
2013-06-23 16:03:39 +04:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
2012-01-07 05:45:46 +04:00
|
|
|
#include "mozilla/Telemetry.h"
|
|
|
|
#include "mozilla/TimeStamp.h"
|
2013-01-15 16:22:03 +04:00
|
|
|
#include <algorithm>
|
2012-11-20 03:02:21 +04:00
|
|
|
|
2013-01-06 17:51:12 +04:00
|
|
|
// we pick 16k as the max buffer size because that is the threshold above which
|
|
|
|
// we are unable to store the data in the cache block files
|
|
|
|
// see nsDiskCacheMap.[cpp,h]
|
|
|
|
#define kMaxBufferSize (16 * 1024)
|
2012-11-20 03:02:21 +04:00
|
|
|
|
2002-05-02 02:08:07 +04:00
|
|
|
// Assumptions:
|
|
|
|
// - cache descriptors live for life of streams
|
|
|
|
// - streams will only be used by FileTransport,
|
|
|
|
// they will not be directly accessible to clients
|
|
|
|
// - overlapped I/O is NOT supported
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* nsDiskCacheInputStream
|
|
|
|
*****************************************************************************/
|
2002-08-07 05:13:29 +04:00
|
|
|
class nsDiskCacheInputStream : public nsIInputStream {
|
2002-05-02 02:08:07 +04:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
nsDiskCacheInputStream( nsDiskCacheStreamIO * parent,
|
|
|
|
PRFileDesc * fileDesc,
|
|
|
|
const char * buffer,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t endOfStream);
|
2002-05-02 02:08:07 +04:00
|
|
|
|
2013-07-19 06:24:13 +04:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2002-05-02 02:08:07 +04:00
|
|
|
NS_DECL_NSIINPUTSTREAM
|
|
|
|
|
|
|
|
private:
|
2014-06-24 20:36:44 +04:00
|
|
|
virtual ~nsDiskCacheInputStream();
|
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
nsDiskCacheStreamIO * mStreamIO; // backpointer to parent
|
2002-08-07 05:13:29 +04:00
|
|
|
PRFileDesc * mFD;
|
|
|
|
const char * mBuffer;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mStreamEnd;
|
|
|
|
uint32_t mPos; // stream position
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mClosed;
|
2002-05-02 02:08:07 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsDiskCacheInputStream, nsIInputStream)
|
2002-05-02 02:08:07 +04:00
|
|
|
|
|
|
|
|
|
|
|
nsDiskCacheInputStream::nsDiskCacheInputStream( nsDiskCacheStreamIO * parent,
|
|
|
|
PRFileDesc * fileDesc,
|
|
|
|
const char * buffer,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t endOfStream)
|
2002-05-02 02:08:07 +04:00
|
|
|
: mStreamIO(parent)
|
|
|
|
, mFD(fileDesc)
|
|
|
|
, mBuffer(buffer)
|
|
|
|
, mStreamEnd(endOfStream)
|
|
|
|
, mPos(0)
|
2011-10-17 18:59:28 +04:00
|
|
|
, mClosed(false)
|
2002-05-02 02:08:07 +04:00
|
|
|
{
|
2003-01-18 05:15:14 +03:00
|
|
|
NS_ADDREF(mStreamIO);
|
2002-08-07 05:13:29 +04:00
|
|
|
mStreamIO->IncrementInputStreamCount();
|
2002-05-02 02:08:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsDiskCacheInputStream::~nsDiskCacheInputStream()
|
|
|
|
{
|
2002-08-07 05:13:29 +04:00
|
|
|
Close();
|
|
|
|
mStreamIO->DecrementInputStreamCount();
|
2003-01-18 05:15:14 +03:00
|
|
|
NS_RELEASE(mStreamIO);
|
2002-05-02 02:08:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDiskCacheInputStream::Close()
|
|
|
|
{
|
2002-09-02 01:40:45 +04:00
|
|
|
if (!mClosed) {
|
|
|
|
if (mFD) {
|
|
|
|
(void) PR_Close(mFD);
|
2012-07-30 18:20:58 +04:00
|
|
|
mFD = nullptr;
|
2002-09-02 01:40:45 +04:00
|
|
|
}
|
2011-10-17 18:59:28 +04:00
|
|
|
mClosed = true;
|
2002-05-02 02:08:07 +04:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsDiskCacheInputStream::Available(uint64_t * bytesAvailable)
|
2002-05-02 02:08:07 +04:00
|
|
|
{
|
2006-01-02 05:30:32 +03:00
|
|
|
if (mClosed) return NS_BASE_STREAM_CLOSED;
|
2002-05-02 02:08:07 +04:00
|
|
|
if (mStreamEnd < mPos) return NS_ERROR_UNEXPECTED;
|
|
|
|
|
|
|
|
*bytesAvailable = mStreamEnd - mPos;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsDiskCacheInputStream::Read(char * buffer, uint32_t count, uint32_t * bytesRead)
|
2002-05-02 02:08:07 +04:00
|
|
|
{
|
|
|
|
*bytesRead = 0;
|
2006-01-02 05:30:32 +03:00
|
|
|
|
2012-06-01 02:20:02 +04:00
|
|
|
if (mClosed) {
|
|
|
|
CACHE_LOG_DEBUG(("CACHE: nsDiskCacheInputStream::Read "
|
|
|
|
"[stream=%p] stream was closed",
|
|
|
|
this, buffer, count));
|
2006-01-02 05:30:32 +03:00
|
|
|
return NS_OK;
|
2012-06-01 02:20:02 +04:00
|
|
|
}
|
2006-01-02 05:30:32 +03:00
|
|
|
|
2012-06-01 02:20:02 +04:00
|
|
|
if (mPos == mStreamEnd) {
|
|
|
|
CACHE_LOG_DEBUG(("CACHE: nsDiskCacheInputStream::Read "
|
|
|
|
"[stream=%p] stream at end of file",
|
|
|
|
this, buffer, count));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
if (mPos > mStreamEnd) {
|
|
|
|
CACHE_LOG_DEBUG(("CACHE: nsDiskCacheInputStream::Read "
|
|
|
|
"[stream=%p] stream past end of file (!)",
|
|
|
|
this, buffer, count));
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
2002-05-02 02:08:07 +04:00
|
|
|
|
2011-01-12 05:38:12 +03:00
|
|
|
if (count > mStreamEnd - mPos)
|
|
|
|
count = mStreamEnd - mPos;
|
|
|
|
|
2002-05-02 02:08:07 +04:00
|
|
|
if (mFD) {
|
|
|
|
// just read from file
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t result = PR_Read(mFD, buffer, count);
|
2012-06-01 02:20:02 +04:00
|
|
|
if (result < 0) {
|
|
|
|
nsresult rv = NS_ErrorAccordingToNSPR();
|
|
|
|
CACHE_LOG_DEBUG(("CACHE: nsDiskCacheInputStream::Read PR_Read failed"
|
|
|
|
"[stream=%p, rv=%d, NSPR error %s",
|
2012-08-25 22:19:00 +04:00
|
|
|
this, int(rv), PR_ErrorToName(PR_GetError())));
|
2012-06-01 02:20:02 +04:00
|
|
|
return rv;
|
|
|
|
}
|
2002-05-02 02:08:07 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
mPos += (uint32_t)result;
|
|
|
|
*bytesRead = (uint32_t)result;
|
2002-05-02 02:08:07 +04:00
|
|
|
|
|
|
|
} else if (mBuffer) {
|
|
|
|
// read data from mBuffer
|
|
|
|
memcpy(buffer, mBuffer + mPos, count);
|
|
|
|
mPos += count;
|
|
|
|
*bytesRead = count;
|
|
|
|
} else {
|
|
|
|
// no data source for input stream
|
|
|
|
}
|
|
|
|
|
2012-06-01 02:20:02 +04:00
|
|
|
CACHE_LOG_DEBUG(("CACHE: nsDiskCacheInputStream::Read "
|
|
|
|
"[stream=%p, count=%ud, byteRead=%ud] ",
|
2012-08-22 19:56:38 +04:00
|
|
|
this, unsigned(count), unsigned(*bytesRead)));
|
2002-05-02 02:08:07 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDiskCacheInputStream::ReadSegments(nsWriteSegmentFun writer,
|
|
|
|
void * closure,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t count,
|
|
|
|
uint32_t * bytesRead)
|
2002-05-02 02:08:07 +04:00
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
nsDiskCacheInputStream::IsNonBlocking(bool * nonBlocking)
|
2002-05-02 02:08:07 +04:00
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
*nonBlocking = false;
|
2002-05-02 02:08:07 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-11-20 03:02:21 +04:00
|
|
|
|
2002-05-02 02:08:07 +04:00
|
|
|
|
2012-11-20 03:02:21 +04:00
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* nsDiskCacheStreamIO
|
|
|
|
*****************************************************************************/
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsDiskCacheStreamIO, nsIOutputStream)
|
2012-11-20 03:02:21 +04:00
|
|
|
|
|
|
|
nsDiskCacheStreamIO::nsDiskCacheStreamIO(nsDiskCacheBinding * binding)
|
|
|
|
: mBinding(binding)
|
|
|
|
, mInStreamCount(0)
|
|
|
|
, mFD(nullptr)
|
|
|
|
, mStreamEnd(0)
|
|
|
|
, mBufSize(0)
|
|
|
|
, mBuffer(nullptr)
|
2013-02-16 18:50:09 +04:00
|
|
|
, mOutputStreamIsOpen(false)
|
2012-11-20 03:02:21 +04:00
|
|
|
{
|
|
|
|
mDevice = (nsDiskCacheDevice *)mBinding->mCacheEntry->CacheDevice();
|
|
|
|
|
|
|
|
// acquire "death grip" on cache service
|
|
|
|
nsCacheService *service = nsCacheService::GlobalInstance();
|
|
|
|
NS_ADDREF(service);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsDiskCacheStreamIO::~nsDiskCacheStreamIO()
|
|
|
|
{
|
2013-02-16 18:50:09 +04:00
|
|
|
nsCacheService::AssertOwnsLock();
|
|
|
|
|
|
|
|
// Close the outputstream
|
|
|
|
if (mBinding && mOutputStreamIsOpen) {
|
|
|
|
(void)CloseOutputStream();
|
|
|
|
}
|
2012-11-20 03:02:21 +04:00
|
|
|
|
|
|
|
// release "death grip" on cache service
|
|
|
|
nsCacheService *service = nsCacheService::GlobalInstance();
|
|
|
|
NS_RELEASE(service);
|
|
|
|
|
|
|
|
// assert streams closed
|
2013-02-16 18:50:09 +04:00
|
|
|
NS_ASSERTION(!mOutputStreamIsOpen, "output stream still open");
|
2012-11-20 03:02:21 +04:00
|
|
|
NS_ASSERTION(mInStreamCount == 0, "input stream still open");
|
|
|
|
NS_ASSERTION(!mFD, "file descriptor not closed");
|
|
|
|
|
|
|
|
DeleteBuffer();
|
2002-05-02 02:08:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
// NOTE: called with service lock held
|
|
|
|
nsresult
|
2012-08-22 19:56:38 +04:00
|
|
|
nsDiskCacheStreamIO::GetInputStream(uint32_t offset, nsIInputStream ** inputStream)
|
2002-05-02 02:08:07 +04:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(inputStream);
|
2003-01-18 05:15:14 +03:00
|
|
|
NS_ENSURE_TRUE(offset == 0, NS_ERROR_NOT_IMPLEMENTED);
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
*inputStream = nullptr;
|
2002-05-02 02:08:07 +04:00
|
|
|
|
2002-08-07 05:13:29 +04:00
|
|
|
if (!mBinding) return NS_ERROR_NOT_AVAILABLE;
|
2002-05-02 02:08:07 +04:00
|
|
|
|
2013-02-16 18:50:09 +04:00
|
|
|
if (mOutputStreamIsOpen) {
|
2012-11-20 03:02:21 +04:00
|
|
|
NS_WARNING("already have an output stream open");
|
2002-08-07 05:13:29 +04:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
2002-05-02 02:08:07 +04:00
|
|
|
|
|
|
|
nsresult rv;
|
2012-07-30 18:20:58 +04:00
|
|
|
PRFileDesc * fd = nullptr;
|
2002-05-02 02:08:07 +04:00
|
|
|
|
|
|
|
mStreamEnd = mBinding->mCacheEntry->DataSize();
|
|
|
|
if (mStreamEnd == 0) {
|
|
|
|
// there's no data to read
|
|
|
|
NS_ASSERTION(!mBinding->mRecord.DataLocationInitialized(), "storage allocated for zero data size");
|
|
|
|
} else if (mBinding->mRecord.DataFile() == 0) {
|
|
|
|
// open file desc for data
|
|
|
|
rv = OpenCacheFile(PR_RDONLY, &fd);
|
|
|
|
if (NS_FAILED(rv)) return rv; // unable to open file
|
|
|
|
NS_ASSERTION(fd, "cache stream lacking open file.");
|
|
|
|
|
|
|
|
} else if (!mBuffer) {
|
|
|
|
// read block file for data
|
2013-01-06 17:51:12 +04:00
|
|
|
rv = ReadCacheBlocks(mStreamEnd);
|
2002-05-02 02:08:07 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
}
|
2002-09-02 01:40:45 +04:00
|
|
|
// else, mBuffer already contains all of the data (left over from a
|
|
|
|
// previous block-file read or write).
|
2002-05-02 02:08:07 +04:00
|
|
|
|
|
|
|
NS_ASSERTION(!(fd && mBuffer), "ambiguous data sources for input stream");
|
|
|
|
|
|
|
|
// create a new input stream
|
|
|
|
nsDiskCacheInputStream * inStream = new nsDiskCacheInputStream(this, fd, mBuffer, mStreamEnd);
|
|
|
|
if (!inStream) return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
NS_ADDREF(*inputStream = inStream);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
// NOTE: called with service lock held
|
|
|
|
nsresult
|
2012-08-22 19:56:38 +04:00
|
|
|
nsDiskCacheStreamIO::GetOutputStream(uint32_t offset, nsIOutputStream ** outputStream)
|
2002-05-02 02:08:07 +04:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(outputStream);
|
2012-07-30 18:20:58 +04:00
|
|
|
*outputStream = nullptr;
|
2002-05-02 02:08:07 +04:00
|
|
|
|
2002-08-07 05:13:29 +04:00
|
|
|
if (!mBinding) return NS_ERROR_NOT_AVAILABLE;
|
2012-11-20 03:02:21 +04:00
|
|
|
|
2013-02-16 18:50:09 +04:00
|
|
|
NS_ASSERTION(!mOutputStreamIsOpen, "already have an output stream open");
|
2002-08-07 05:13:29 +04:00
|
|
|
NS_ASSERTION(mInStreamCount == 0, "we already have input streams open");
|
2013-02-16 18:50:09 +04:00
|
|
|
if (mOutputStreamIsOpen || mInStreamCount) return NS_ERROR_NOT_AVAILABLE;
|
2002-05-02 02:08:07 +04:00
|
|
|
|
|
|
|
mStreamEnd = mBinding->mCacheEntry->DataSize();
|
|
|
|
|
2013-01-06 17:51:12 +04:00
|
|
|
// Inits file or buffer and truncate at the desired offset
|
|
|
|
nsresult rv = SeekAndTruncate(offset);
|
2012-11-20 03:02:21 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2003-01-18 05:15:14 +03:00
|
|
|
|
2013-02-16 18:50:09 +04:00
|
|
|
mOutputStreamIsOpen = true;
|
|
|
|
NS_ADDREF(*outputStream = this);
|
2002-05-02 02:08:07 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-01-26 15:23:45 +04:00
|
|
|
nsresult
|
2002-09-02 04:17:00 +04:00
|
|
|
nsDiskCacheStreamIO::ClearBinding()
|
|
|
|
{
|
2012-01-26 15:23:45 +04:00
|
|
|
nsresult rv = NS_OK;
|
2013-02-16 18:50:09 +04:00
|
|
|
if (mBinding && mOutputStreamIsOpen)
|
|
|
|
rv = CloseOutputStream();
|
2012-07-30 18:20:58 +04:00
|
|
|
mBinding = nullptr;
|
2012-01-26 15:23:45 +04:00
|
|
|
return rv;
|
2002-09-02 04:17:00 +04:00
|
|
|
}
|
|
|
|
|
2013-02-16 18:50:09 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDiskCacheStreamIO::Close()
|
2012-11-20 03:02:21 +04:00
|
|
|
{
|
2013-02-16 18:50:09 +04:00
|
|
|
if (!mOutputStreamIsOpen) return NS_OK;
|
2010-12-29 04:25:14 +03:00
|
|
|
|
2013-02-16 18:50:09 +04:00
|
|
|
mozilla::TimeStamp start = mozilla::TimeStamp::Now();
|
|
|
|
|
|
|
|
// grab service lock
|
|
|
|
nsCacheServiceAutoLock lock(LOCK_TELEM(NSDISKCACHESTREAMIO_CLOSEOUTPUTSTREAM));
|
2012-12-14 21:03:01 +04:00
|
|
|
|
2012-11-20 03:02:21 +04:00
|
|
|
if (!mBinding) { // if we're severed, just clear member variables
|
2013-02-16 18:50:09 +04:00
|
|
|
mOutputStreamIsOpen = false;
|
2012-11-20 03:02:21 +04:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
2002-08-07 05:13:29 +04:00
|
|
|
}
|
2002-05-02 02:08:07 +04:00
|
|
|
|
2013-02-16 18:50:09 +04:00
|
|
|
nsresult rv = CloseOutputStream();
|
2012-11-20 03:02:21 +04:00
|
|
|
if (NS_FAILED(rv))
|
2013-02-16 18:50:09 +04:00
|
|
|
NS_WARNING("CloseOutputStream() failed");
|
|
|
|
|
|
|
|
mozilla::Telemetry::ID id;
|
|
|
|
if (NS_IsMainThread())
|
|
|
|
id = mozilla::Telemetry::NETWORK_DISK_CACHE_STREAMIO_CLOSE_MAIN_THREAD;
|
|
|
|
else
|
|
|
|
id = mozilla::Telemetry::NETWORK_DISK_CACHE_STREAMIO_CLOSE;
|
|
|
|
mozilla::Telemetry::AccumulateTimeDelta(id, start);
|
2012-09-06 18:10:08 +04:00
|
|
|
|
2012-11-20 03:02:21 +04:00
|
|
|
return rv;
|
2002-09-02 04:17:00 +04:00
|
|
|
}
|
|
|
|
|
2012-11-20 03:02:21 +04:00
|
|
|
nsresult
|
2013-02-16 18:50:09 +04:00
|
|
|
nsDiskCacheStreamIO::CloseOutputStream()
|
2002-09-02 04:17:00 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(mBinding, "oops");
|
|
|
|
|
2013-02-16 18:50:09 +04:00
|
|
|
CACHE_LOG_DEBUG(("CACHE: CloseOutputStream [%x doomed=%u]\n",
|
2006-03-31 01:06:54 +04:00
|
|
|
mBinding->mRecord.HashNumber(), mBinding->mDoomed));
|
2006-03-09 23:02:54 +03:00
|
|
|
|
2013-02-16 18:50:09 +04:00
|
|
|
// Mark outputstream as closed, even if saving the stream fails
|
|
|
|
mOutputStreamIsOpen = false;
|
|
|
|
|
2013-01-06 17:51:12 +04:00
|
|
|
// When writing to a file, just close the file
|
|
|
|
if (mFD) {
|
|
|
|
(void) PR_Close(mFD);
|
|
|
|
mFD = nullptr;
|
2002-09-02 04:17:00 +04:00
|
|
|
return NS_OK;
|
2011-06-22 12:49:35 +04:00
|
|
|
}
|
2002-09-02 04:17:00 +04:00
|
|
|
|
2002-05-02 02:08:07 +04:00
|
|
|
// write data to cache blocks, or flush mBuffer to file
|
2013-02-03 20:09:48 +04:00
|
|
|
NS_ASSERTION(mStreamEnd <= kMaxBufferSize, "stream is bigger than buffer");
|
2011-01-18 17:12:10 +03:00
|
|
|
|
2013-02-03 20:09:48 +04:00
|
|
|
nsDiskCacheMap *cacheMap = mDevice->CacheMap(); // get map reference
|
|
|
|
nsDiskCacheRecord * record = &mBinding->mRecord;
|
|
|
|
nsresult rv = NS_OK;
|
2011-01-18 17:12:10 +03:00
|
|
|
|
2013-02-03 20:09:48 +04:00
|
|
|
// delete existing storage
|
|
|
|
if (record->DataLocationInitialized()) {
|
|
|
|
rv = cacheMap->DeleteStorage(record, nsDiskCache::kData);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2011-01-18 17:12:10 +03:00
|
|
|
|
2013-02-03 20:09:48 +04:00
|
|
|
// Only call UpdateRecord when there is no data to write,
|
|
|
|
// because WriteDataCacheBlocks / FlushBufferToFile calls it.
|
|
|
|
if ((mStreamEnd == 0) && (!mBinding->mDoomed)) {
|
|
|
|
rv = cacheMap->UpdateRecord(record);
|
2011-01-18 17:12:10 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
2013-02-03 20:09:48 +04:00
|
|
|
NS_WARNING("cacheMap->UpdateRecord() failed.");
|
|
|
|
return rv; // XXX doom cache entry
|
2011-01-18 17:12:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-02-03 20:09:48 +04:00
|
|
|
|
|
|
|
if (mStreamEnd == 0) return NS_OK; // nothing to write
|
|
|
|
|
|
|
|
// try to write to the cache blocks
|
|
|
|
rv = cacheMap->WriteDataCacheBlocks(mBinding, mBuffer, mStreamEnd);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("WriteDataCacheBlocks() failed.");
|
2011-01-18 17:12:10 +03:00
|
|
|
|
2013-01-06 17:51:12 +04:00
|
|
|
// failed to store in cacheblocks, save as separate file
|
2011-01-18 17:12:10 +03:00
|
|
|
rv = FlushBufferToFile(); // initializes DataFileLocation() if necessary
|
2006-04-25 22:08:09 +04:00
|
|
|
if (mFD) {
|
2013-02-03 20:09:48 +04:00
|
|
|
UpdateFileSize();
|
|
|
|
(void) PR_Close(mFD);
|
|
|
|
mFD = nullptr;
|
2006-04-25 22:08:09 +04:00
|
|
|
}
|
|
|
|
else
|
2013-02-03 20:09:48 +04:00
|
|
|
NS_WARNING("no file descriptor");
|
2002-05-02 02:08:07 +04:00
|
|
|
}
|
2013-02-03 20:09:48 +04:00
|
|
|
|
|
|
|
return rv;
|
2002-05-02 02:08:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// assumptions:
|
|
|
|
// only one thread writing at a time
|
|
|
|
// never have both output and input streams open
|
|
|
|
// OnDataSizeChanged() will have already been called to update entry->DataSize()
|
|
|
|
|
2013-02-16 18:50:09 +04:00
|
|
|
NS_IMETHODIMP
|
2002-05-02 02:08:07 +04:00
|
|
|
nsDiskCacheStreamIO::Write( const char * buffer,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t count,
|
|
|
|
uint32_t * bytesWritten)
|
2002-05-02 02:08:07 +04:00
|
|
|
{
|
2013-02-02 13:52:41 +04:00
|
|
|
NS_ENSURE_ARG_POINTER(buffer);
|
|
|
|
NS_ENSURE_ARG_POINTER(bytesWritten);
|
2013-02-16 18:50:09 +04:00
|
|
|
if (!mOutputStreamIsOpen) return NS_BASE_STREAM_CLOSED;
|
|
|
|
|
2013-02-02 13:52:41 +04:00
|
|
|
*bytesWritten = 0; // always initialize to zero in case of errors
|
|
|
|
|
|
|
|
NS_ASSERTION(count, "Write called with count of zero");
|
|
|
|
if (count == 0) {
|
|
|
|
return NS_OK; // nothing to write
|
|
|
|
}
|
|
|
|
|
2013-01-06 17:51:12 +04:00
|
|
|
// grab service lock
|
|
|
|
nsCacheServiceAutoLock lock(LOCK_TELEM(NSDISKCACHESTREAMIO_WRITE));
|
2002-08-07 05:13:29 +04:00
|
|
|
if (!mBinding) return NS_ERROR_NOT_AVAILABLE;
|
2002-05-02 02:08:07 +04:00
|
|
|
|
2002-08-07 05:13:29 +04:00
|
|
|
if (mInStreamCount) {
|
2002-05-02 02:08:07 +04:00
|
|
|
// we have open input streams already
|
|
|
|
// this is an error until we support overlapped I/O
|
|
|
|
NS_WARNING("Attempting to write to cache entry with open input streams.\n");
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
2013-01-06 17:51:12 +04:00
|
|
|
// Not writing to file, and it will fit in the cachedatablocks?
|
|
|
|
if (!mFD && (mStreamEnd + count <= kMaxBufferSize)) {
|
|
|
|
|
|
|
|
// We have more data than the current buffer size?
|
|
|
|
if ((mStreamEnd + count > mBufSize) && (mBufSize < kMaxBufferSize)) {
|
2013-02-02 13:52:41 +04:00
|
|
|
// Increase buffer to the maximum size.
|
|
|
|
mBuffer = (char *) moz_xrealloc(mBuffer, kMaxBufferSize);
|
|
|
|
mBufSize = kMaxBufferSize;
|
2005-08-19 22:38:28 +04:00
|
|
|
}
|
2013-01-06 17:51:12 +04:00
|
|
|
|
|
|
|
// Store in the buffer but only if it fits
|
2013-02-02 13:52:41 +04:00
|
|
|
if (mStreamEnd + count <= mBufSize) {
|
2013-01-06 17:51:12 +04:00
|
|
|
memcpy(mBuffer + mStreamEnd, buffer, count);
|
|
|
|
mStreamEnd += count;
|
|
|
|
*bytesWritten = count;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// There are more bytes than fit in the buffer/cacheblocks, switch to file
|
|
|
|
if (!mFD) {
|
|
|
|
// Opens a cache file and write the buffer to it
|
|
|
|
nsresult rv = FlushBufferToFile();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2005-08-19 22:38:28 +04:00
|
|
|
}
|
2013-01-06 17:51:12 +04:00
|
|
|
// Write directly to the file
|
|
|
|
if (PR_Write(mFD, buffer, count) != (int32_t)count) {
|
|
|
|
NS_WARNING("failed to write all data");
|
|
|
|
return NS_ERROR_UNEXPECTED; // NS_ErrorAccordingToNSPR()
|
2005-08-19 22:38:28 +04:00
|
|
|
}
|
2013-01-06 17:51:12 +04:00
|
|
|
mStreamEnd += count;
|
2005-08-19 22:38:28 +04:00
|
|
|
*bytesWritten = count;
|
2002-05-02 02:08:07 +04:00
|
|
|
|
2013-01-06 17:51:12 +04:00
|
|
|
UpdateFileSize();
|
|
|
|
NS_ASSERTION(mBinding->mCacheEntry->DataSize() == mStreamEnd, "bad stream");
|
2002-05-02 02:08:07 +04:00
|
|
|
|
2013-01-06 17:51:12 +04:00
|
|
|
return NS_OK;
|
2002-05-02 02:08:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-08-19 22:38:28 +04:00
|
|
|
void
|
2002-08-07 05:13:29 +04:00
|
|
|
nsDiskCacheStreamIO::UpdateFileSize()
|
2002-05-02 02:08:07 +04:00
|
|
|
{
|
2002-08-07 05:13:29 +04:00
|
|
|
NS_ASSERTION(mFD, "nsDiskCacheStreamIO::UpdateFileSize should not have been called");
|
2002-05-02 02:08:07 +04:00
|
|
|
|
2002-08-07 05:13:29 +04:00
|
|
|
nsDiskCacheRecord * record = &mBinding->mRecord;
|
2012-08-22 19:56:38 +04:00
|
|
|
const uint32_t oldSizeK = record->DataFileSize();
|
|
|
|
uint32_t newSizeK = (mStreamEnd + 0x03FF) >> 10;
|
2011-05-20 21:27:11 +04:00
|
|
|
|
|
|
|
// make sure the size won't overflow (bug #651100)
|
|
|
|
if (newSizeK > kMaxDataSizeK)
|
|
|
|
newSizeK = kMaxDataSizeK;
|
|
|
|
|
2005-08-19 22:38:28 +04:00
|
|
|
if (newSizeK == oldSizeK) return;
|
2002-08-07 05:13:29 +04:00
|
|
|
|
|
|
|
record->SetDataFileSize(newSizeK);
|
2002-05-02 02:08:07 +04:00
|
|
|
|
2002-08-07 05:13:29 +04:00
|
|
|
// update cache size totals
|
|
|
|
nsDiskCacheMap * cacheMap = mDevice->CacheMap();
|
2006-03-01 01:18:21 +03:00
|
|
|
cacheMap->DecrementTotalSize(oldSizeK); // decrement old size
|
|
|
|
cacheMap->IncrementTotalSize(newSizeK); // increment new size
|
2002-08-07 05:13:29 +04:00
|
|
|
|
|
|
|
if (!mBinding->mDoomed) {
|
2005-08-19 22:38:28 +04:00
|
|
|
nsresult rv = cacheMap->UpdateRecord(record);
|
2002-08-07 05:13:29 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("cacheMap->UpdateRecord() failed.");
|
|
|
|
// XXX doom cache entry?
|
|
|
|
}
|
|
|
|
}
|
2002-05-02 02:08:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
2012-08-09 11:09:40 +04:00
|
|
|
nsDiskCacheStreamIO::OpenCacheFile(int flags, PRFileDesc ** fd)
|
2002-05-02 02:08:07 +04:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(fd);
|
|
|
|
|
2012-06-01 02:20:03 +04:00
|
|
|
CACHE_LOG_DEBUG(("nsDiskCacheStreamIO::OpenCacheFile"));
|
|
|
|
|
2002-08-07 05:13:29 +04:00
|
|
|
nsresult rv;
|
|
|
|
nsDiskCacheMap * cacheMap = mDevice->CacheMap();
|
2012-12-21 18:25:50 +04:00
|
|
|
nsCOMPtr<nsIFile> localFile;
|
2002-08-07 05:13:29 +04:00
|
|
|
|
|
|
|
rv = cacheMap->GetLocalFileForDiskCacheRecord(&mBinding->mRecord,
|
|
|
|
nsDiskCache::kData,
|
2010-12-06 18:49:05 +03:00
|
|
|
!!(flags & PR_CREATE_FILE),
|
2012-12-21 18:25:50 +04:00
|
|
|
getter_AddRefs(localFile));
|
2002-08-07 05:13:29 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2002-05-02 02:08:07 +04:00
|
|
|
|
2005-06-10 21:32:48 +04:00
|
|
|
// create PRFileDesc for input stream - the 00600 is just for consistency
|
2012-12-21 18:25:50 +04:00
|
|
|
return localFile->OpenNSPRFileDesc(flags, 00600, fd);
|
2002-05-02 02:08:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
2013-01-06 17:51:12 +04:00
|
|
|
nsDiskCacheStreamIO::ReadCacheBlocks(uint32_t bufferSize)
|
2002-05-02 02:08:07 +04:00
|
|
|
{
|
2002-09-02 01:40:45 +04:00
|
|
|
NS_ASSERTION(mStreamEnd == mBinding->mCacheEntry->DataSize(), "bad stream");
|
2013-01-06 17:51:12 +04:00
|
|
|
NS_ASSERTION(bufferSize <= kMaxBufferSize, "bufferSize too large for buffer");
|
|
|
|
NS_ASSERTION(mStreamEnd <= bufferSize, "data too large for buffer");
|
2002-05-02 02:08:07 +04:00
|
|
|
|
|
|
|
nsDiskCacheRecord * record = &mBinding->mRecord;
|
2002-09-02 01:40:45 +04:00
|
|
|
if (!record->DataLocationInitialized()) return NS_OK;
|
2002-05-02 02:08:07 +04:00
|
|
|
|
2002-09-02 01:40:45 +04:00
|
|
|
NS_ASSERTION(record->DataFile() != kSeparateFile, "attempt to read cache blocks on separate file");
|
|
|
|
|
2002-05-02 02:08:07 +04:00
|
|
|
if (!mBuffer) {
|
2013-02-02 13:52:41 +04:00
|
|
|
mBuffer = (char *) moz_xmalloc(bufferSize);
|
2013-01-06 17:51:12 +04:00
|
|
|
mBufSize = bufferSize;
|
2002-05-02 02:08:07 +04:00
|
|
|
}
|
|
|
|
|
2006-05-26 22:21:12 +04:00
|
|
|
// read data stored in cache block files
|
2002-09-02 01:40:45 +04:00
|
|
|
nsDiskCacheMap *map = mDevice->CacheMap(); // get map reference
|
2013-01-06 17:51:12 +04:00
|
|
|
return map->ReadDataCacheBlocks(mBinding, mBuffer, mStreamEnd);
|
2002-05-02 02:08:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
2005-08-19 22:38:28 +04:00
|
|
|
nsDiskCacheStreamIO::FlushBufferToFile()
|
2002-05-02 02:08:07 +04:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsDiskCacheRecord * record = &mBinding->mRecord;
|
|
|
|
|
|
|
|
if (!mFD) {
|
|
|
|
if (record->DataLocationInitialized() && (record->DataFile() > 0)) {
|
|
|
|
// remove cache block storage
|
|
|
|
nsDiskCacheMap * cacheMap = mDevice->CacheMap();
|
|
|
|
rv = cacheMap->DeleteStorage(record, nsDiskCache::kData);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
}
|
|
|
|
record->SetDataFileGeneration(mBinding->mGeneration);
|
|
|
|
|
|
|
|
// allocate file
|
2012-11-20 03:02:21 +04:00
|
|
|
rv = OpenCacheFile(PR_RDWR | PR_CREATE_FILE, &mFD);
|
2002-05-02 02:08:07 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2010-11-24 18:42:30 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int64_t dataSize = mBinding->mCacheEntry->PredictedDataSize();
|
2010-11-24 18:42:30 +03:00
|
|
|
if (dataSize != -1)
|
2013-01-15 16:22:03 +04:00
|
|
|
mozilla::fallocate(mFD, std::min<int64_t>(dataSize, kPreallocateLimit));
|
2002-05-02 02:08:07 +04:00
|
|
|
}
|
|
|
|
|
2013-02-02 13:52:41 +04:00
|
|
|
// write buffer to the file when there is data in it
|
|
|
|
if (mStreamEnd > 0) {
|
|
|
|
if (!mBuffer) {
|
|
|
|
NS_RUNTIMEABORT("Fix me!");
|
|
|
|
}
|
|
|
|
if (PR_Write(mFD, mBuffer, mStreamEnd) != (int32_t)mStreamEnd) {
|
|
|
|
NS_WARNING("failed to flush all data");
|
|
|
|
return NS_ERROR_UNEXPECTED; // NS_ErrorAccordingToNSPR()
|
|
|
|
}
|
2002-05-02 02:08:07 +04:00
|
|
|
}
|
2013-01-06 17:51:12 +04:00
|
|
|
|
|
|
|
// buffer is no longer valid
|
|
|
|
DeleteBuffer();
|
|
|
|
|
2002-05-02 02:08:07 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-09-02 01:40:45 +04:00
|
|
|
void
|
|
|
|
nsDiskCacheStreamIO::DeleteBuffer()
|
|
|
|
{
|
|
|
|
if (mBuffer) {
|
|
|
|
free(mBuffer);
|
2012-07-30 18:20:58 +04:00
|
|
|
mBuffer = nullptr;
|
2002-09-02 01:40:45 +04:00
|
|
|
mBufSize = 0;
|
|
|
|
}
|
|
|
|
}
|
2012-11-05 22:22:33 +04:00
|
|
|
|
|
|
|
size_t
|
2013-06-23 16:03:39 +04:00
|
|
|
nsDiskCacheStreamIO::SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf)
|
2012-11-05 22:22:33 +04:00
|
|
|
{
|
|
|
|
size_t usage = aMallocSizeOf(this);
|
|
|
|
|
|
|
|
usage += aMallocSizeOf(mFD);
|
|
|
|
usage += aMallocSizeOf(mBuffer);
|
|
|
|
|
|
|
|
return usage;
|
|
|
|
}
|
2012-11-20 03:02:21 +04:00
|
|
|
|
|
|
|
nsresult
|
2013-01-06 17:51:12 +04:00
|
|
|
nsDiskCacheStreamIO::SeekAndTruncate(uint32_t offset)
|
2012-11-20 03:02:21 +04:00
|
|
|
{
|
|
|
|
if (!mBinding) return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
|
|
|
if (uint32_t(offset) > mStreamEnd) return NS_ERROR_FAILURE;
|
|
|
|
|
2013-01-06 17:51:12 +04:00
|
|
|
// Set the current end to the desired offset
|
|
|
|
mStreamEnd = offset;
|
|
|
|
|
|
|
|
// Currently stored in file?
|
|
|
|
if (mBinding->mRecord.DataLocationInitialized() &&
|
|
|
|
(mBinding->mRecord.DataFile() == 0)) {
|
|
|
|
if (!mFD) {
|
|
|
|
// we need an mFD, we better open it now
|
|
|
|
nsresult rv = OpenCacheFile(PR_RDWR | PR_CREATE_FILE, &mFD);
|
2012-11-20 03:02:21 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
}
|
2013-01-06 17:51:12 +04:00
|
|
|
if (offset) {
|
|
|
|
if (PR_Seek(mFD, offset, PR_SEEK_SET) == -1)
|
|
|
|
return NS_ErrorAccordingToNSPR();
|
2012-11-20 03:02:21 +04:00
|
|
|
}
|
2013-01-06 17:51:12 +04:00
|
|
|
nsDiskCache::Truncate(mFD, offset);
|
2012-11-20 03:02:21 +04:00
|
|
|
UpdateFileSize();
|
2013-01-06 17:51:12 +04:00
|
|
|
|
|
|
|
// When we starting at zero again, close file and start with buffer.
|
|
|
|
// If offset is non-zero (and within buffer) an option would be
|
|
|
|
// to read the file into the buffer, but chance is high that it is
|
|
|
|
// rewritten to the file anyway.
|
|
|
|
if (offset == 0) {
|
|
|
|
// close file descriptor
|
2012-11-20 03:02:21 +04:00
|
|
|
(void) PR_Close(mFD);
|
|
|
|
mFD = nullptr;
|
|
|
|
}
|
2013-01-06 17:51:12 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// read data into mBuffer if not read yet.
|
|
|
|
if (offset && !mBuffer) {
|
|
|
|
nsresult rv = ReadCacheBlocks(kMaxBufferSize);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
2012-11-20 03:02:21 +04:00
|
|
|
}
|
|
|
|
|
2013-01-06 17:51:12 +04:00
|
|
|
// stream buffer sanity check
|
|
|
|
NS_ASSERTION(mStreamEnd <= kMaxBufferSize, "bad stream");
|
|
|
|
return NS_OK;
|
2012-11-20 03:02:21 +04:00
|
|
|
}
|
2013-02-16 18:50:09 +04:00
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDiskCacheStreamIO::Flush()
|
|
|
|
{
|
|
|
|
if (!mOutputStreamIsOpen) return NS_BASE_STREAM_CLOSED;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDiskCacheStreamIO::WriteFrom(nsIInputStream *inStream, uint32_t count, uint32_t *bytesWritten)
|
|
|
|
{
|
|
|
|
NS_NOTREACHED("WriteFrom");
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDiskCacheStreamIO::WriteSegments( nsReadSegmentFun reader,
|
|
|
|
void * closure,
|
|
|
|
uint32_t count,
|
|
|
|
uint32_t * bytesWritten)
|
|
|
|
{
|
|
|
|
NS_NOTREACHED("WriteSegments");
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDiskCacheStreamIO::IsNonBlocking(bool * nonBlocking)
|
|
|
|
{
|
|
|
|
*nonBlocking = false;
|
|
|
|
return NS_OK;
|
|
|
|
}
|