From 4776d6f1e7dd8270fd18ff3fd32f6f750364c24a Mon Sep 17 00:00:00 2001 From: "darin%netscape.com" Date: Mon, 2 Sep 2002 00:17:00 +0000 Subject: [PATCH] cache fixes required for link prefetching (b=12274) r=dougt sr=rpotts --- netwerk/cache/src/nsDiskCacheStreams.cpp | 31 +++++++++++++++++++++--- netwerk/cache/src/nsDiskCacheStreams.h | 3 ++- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/netwerk/cache/src/nsDiskCacheStreams.cpp b/netwerk/cache/src/nsDiskCacheStreams.cpp index 2b2d2c7779d..5473594d95f 100644 --- a/netwerk/cache/src/nsDiskCacheStreams.cpp +++ b/netwerk/cache/src/nsDiskCacheStreams.cpp @@ -477,6 +477,14 @@ nsDiskCacheStreamIO::GetContentLength(PRInt32 *contentLength) } +void +nsDiskCacheStreamIO::ClearBinding() +{ + if (mBinding && mOutStream) + Flush(); + mBinding = nsnull; +} + nsresult nsDiskCacheStreamIO::CloseOutputStream(nsDiskCacheOutputStream * outputStream) { @@ -490,13 +498,30 @@ nsDiskCacheStreamIO::CloseOutputStream(nsDiskCacheOutputStream * outputStream) // output stream is closing if (!mBinding) { // if we're severed, just clear member variables + NS_ASSERTION(!mBufDirty, "oops"); mOutStream = nsnull; outputStream->mStreamIO = nsnull; return NS_ERROR_NOT_AVAILABLE; } + rv = Flush(); + NS_ASSERTION(NS_SUCCEEDED(rv), "Flush() failed"); + + mOutStream = nsnull; + return rv; +} + +nsresult +nsDiskCacheStreamIO::Flush() +{ + NS_ASSERTION(mBinding, "oops"); + + if (!mBufDirty) + return NS_OK; + // write data to cache blocks, or flush mBuffer to file - nsDiskCacheMap * cacheMap = mDevice->CacheMap(); // get map reference + nsDiskCacheMap *cacheMap = mDevice->CacheMap(); // get map reference + nsresult rv; if ((mStreamEnd > kMaxBufferSize) || (mBinding->mCacheEntry->StoragePolicy() == nsICache::STORE_ON_DISK_AS_FILE)) { @@ -522,7 +547,6 @@ nsDiskCacheStreamIO::CloseOutputStream(nsDiskCacheOutputStream * outputStream) // delete existing storage nsDiskCacheRecord * record = &mBinding->mRecord; if (record->DataLocationInitialized()) { - rv = cacheMap->DeleteStorage(record, nsDiskCache::kData); if (NS_FAILED(rv)) { NS_WARNING("cacheMap->DeleteStorage() failed."); @@ -532,7 +556,7 @@ nsDiskCacheStreamIO::CloseOutputStream(nsDiskCacheOutputStream * outputStream) // flush buffer to block files if (mStreamEnd > 0) { - nsresult rv = cacheMap->WriteDataCacheBlocks(mBinding, mBuffer, mBufEnd); + rv = cacheMap->WriteDataCacheBlocks(mBinding, mBuffer, mBufEnd); if (NS_FAILED(rv)) { NS_WARNING("WriteDataCacheBlocks() failed."); return rv; // XXX doom cache entry? @@ -553,7 +577,6 @@ nsDiskCacheStreamIO::CloseOutputStream(nsDiskCacheOutputStream * outputStream) } } - mOutStream = nsnull; return NS_OK; } diff --git a/netwerk/cache/src/nsDiskCacheStreams.h b/netwerk/cache/src/nsDiskCacheStreams.h index e81736b85e1..800d228f561 100644 --- a/netwerk/cache/src/nsDiskCacheStreams.h +++ b/netwerk/cache/src/nsDiskCacheStreams.h @@ -62,7 +62,7 @@ public: nsresult Tell(PRUint32 * position); nsresult SetEOF(); - void ClearBinding() { mBinding = nsnull; } + void ClearBinding(); void IncrementInputStreamCount() { PR_AtomicIncrement(&mInStreamCount); } void DecrementInputStreamCount() @@ -83,6 +83,7 @@ private: PRUint32 WriteToBuffer(const char * buffer, PRUint32 count); nsresult UpdateFileSize(); void DeleteBuffer(); + nsresult Flush(); nsDiskCacheBinding * mBinding;