Skip to content

Commit

Permalink
Update the file size only when we actually flush out our buffer. Bug …
Browse files Browse the repository at this point in the history
…243486,

patch by Alfred Kayser <[email protected]>, r=darin, sr=bzbarsky
  • Loading branch information
bzbarsky%mit.edu committed Aug 19, 2005
1 parent 2c08e49 commit 385a6f4
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 66 deletions.
123 changes: 60 additions & 63 deletions netwerk/cache/src/nsDiskCacheStreams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -481,11 +481,15 @@ nsDiskCacheStreamIO::Flush()
if ((mStreamEnd > kMaxBufferSize) ||
(mBinding->mCacheEntry->StoragePolicy() == nsICache::STORE_ON_DISK_AS_FILE)) {
// make sure we save as separate file
rv = FlushBufferToFile(PR_TRUE); // will initialize DataFileLocation() if necessary
rv = FlushBufferToFile(); // will initialize DataFileLocation() if necessary
NS_ASSERTION(NS_SUCCEEDED(rv), "FlushBufferToFile() failed");

// close file descriptor

NS_ASSERTION(mFD, "no file descriptor");

// Update the file size of the disk file in the cache
UpdateFileSize();

// close file descriptor
(void) PR_Close(mFD);
mFD = nsnull;

Expand Down Expand Up @@ -558,36 +562,73 @@ nsDiskCacheStreamIO::Write( const char * buffer,
return NS_ERROR_NOT_AVAILABLE;
}

*bytesWritten = WriteToBuffer(buffer, count);
if (*bytesWritten != count) return NS_ERROR_FAILURE;
NS_ASSERTION(count, "Write called with count of zero");
NS_ASSERTION(mBufPos <= mBufEnd, "streamIO buffer corrupted");

PRUint32 bytesLeft = count;
PRBool flushed = PR_FALSE;

while (bytesLeft) {
if (mBufPos == mBufSize) {
if (mBufSize < kMaxBufferSize) {
mBufSize = kMaxBufferSize;
mBuffer = (char *) realloc(mBuffer, mBufSize);
if (!mBuffer) {
mBufSize = 0;
break;
}
} else {
nsresult rv = FlushBufferToFile();
if (NS_FAILED(rv)) break;
flushed = PR_TRUE;
}
}

PRUint32 chunkSize = bytesLeft;
if (chunkSize > (mBufSize - mBufPos))
chunkSize = mBufSize - mBufPos;

memcpy(mBuffer + mBufPos, buffer, chunkSize);
mBufDirty = PR_TRUE;
mBufPos += chunkSize;
bytesLeft -= chunkSize;
buffer += chunkSize;

if (mBufEnd < mBufPos)
mBufEnd = mBufPos;
}
if (bytesLeft) {
*bytesWritten = 0;
return NS_ERROR_FAILURE;
}
*bytesWritten = count;

// update mStreamPos, mStreamEnd
mStreamPos += count;
if (mStreamEnd < mStreamPos) {
mStreamEnd = mStreamPos;
NS_ASSERTION(mBinding->mCacheEntry->DataSize() == mStreamEnd, "bad stream");

// if we have a separate file, we need to adjust the disk cache size totals here
if (mFD) {
rv = UpdateFileSize();
// If we have flushed to a file, update the file size
if (flushed && mFD) {
UpdateFileSize();
}
}

return rv;
}


nsresult
void
nsDiskCacheStreamIO::UpdateFileSize()
{
NS_ASSERTION(mFD, "nsDiskCacheStreamIO::UpdateFileSize should not have been called");
if (!mFD) return NS_ERROR_UNEXPECTED;

nsDiskCacheRecord * record = &mBinding->mRecord;
PRUint32 oldSizeK = record->DataFileSize();
PRUint32 newSizeK = (mStreamEnd + 0x03FF) >> 10;
const PRUint32 oldSizeK = record->DataFileSize();
const PRUint32 newSizeK = (mStreamEnd + 0x03FF) >> 10;

if (newSizeK == oldSizeK) return NS_OK;
if (newSizeK == oldSizeK) return;

record->SetDataFileSize(newSizeK);

Expand All @@ -597,14 +638,12 @@ nsDiskCacheStreamIO::UpdateFileSize()
cacheMap->IncrementTotalSize(newSizeK * 1024); // increment new size

if (!mBinding->mDoomed) {
nsresult rv = cacheMap->UpdateRecord(&mBinding->mRecord);
nsresult rv = cacheMap->UpdateRecord(record);
if (NS_FAILED(rv)) {
NS_WARNING("cacheMap->UpdateRecord() failed.");
// XXX doom cache entry?
return rv;
}
}
return NS_OK;
}


Expand Down Expand Up @@ -667,7 +706,7 @@ nsDiskCacheStreamIO::ReadCacheBlocks()


nsresult
nsDiskCacheStreamIO::FlushBufferToFile(PRBool clearBuffer)
nsDiskCacheStreamIO::FlushBufferToFile()
{
nsresult rv;
nsDiskCacheRecord * record = &mBinding->mRecord;
Expand All @@ -694,56 +733,14 @@ nsDiskCacheStreamIO::FlushBufferToFile(PRBool clearBuffer)
}
mBufDirty = PR_FALSE;

if (clearBuffer) {
// reset buffer
mBufPos = 0;
mBufEnd = 0;
}
// reset buffer
mBufPos = 0;
mBufEnd = 0;

return NS_OK;
}


PRUint32
nsDiskCacheStreamIO::WriteToBuffer(const char * buffer, PRUint32 count)
{
NS_ASSERTION(count, "WriteToBuffer called with count of zero");
NS_ASSERTION(mBufPos <= mBufEnd, "streamIO buffer corrupted");

PRUint32 bytesLeft = count;

while (bytesLeft) {
if (mBufPos == mBufSize) {
if (mBufSize < kMaxBufferSize) {
mBufSize = kMaxBufferSize;
mBuffer = (char *) realloc(mBuffer, mBufSize);
if (!mBuffer) {
mBufSize = 0;
return 0;
}
} else {
nsresult rv = FlushBufferToFile(PR_TRUE);
if (NS_FAILED(rv)) return 0;
}
}

PRUint32 chunkSize = bytesLeft;
if (chunkSize > (mBufSize - mBufPos))
chunkSize = mBufSize - mBufPos;

memcpy(mBuffer + mBufPos, buffer, chunkSize);
mBufDirty = PR_TRUE;
mBufPos += chunkSize;
bytesLeft -= chunkSize;
buffer += chunkSize;

if (mBufEnd < mBufPos)
mBufEnd = mBufPos;
}

return count;
}

void
nsDiskCacheStreamIO::DeleteBuffer()
{
Expand Down Expand Up @@ -781,7 +778,7 @@ nsDiskCacheStreamIO::Seek(PRInt32 whence, PRInt32 offset)
// do we have data in the buffer that needs to be flushed?
if (mBufDirty) {
// XXX optimization: are we just moving within the current buffer?
nsresult rv = FlushBufferToFile(PR_TRUE);
nsresult rv = FlushBufferToFile();
if (NS_FAILED(rv)) return rv;
}

Expand Down
5 changes: 2 additions & 3 deletions netwerk/cache/src/nsDiskCacheStreams.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,8 @@ class nsDiskCacheStreamIO : public nsISupports {
void Close();
nsresult OpenCacheFile(PRIntn flags, PRFileDesc ** fd);
nsresult ReadCacheBlocks();
nsresult FlushBufferToFile(PRBool clearBuffer); // XXX clearBuffer is always PR_TRUE
PRUint32 WriteToBuffer(const char * buffer, PRUint32 count);
nsresult UpdateFileSize();
nsresult FlushBufferToFile();
void UpdateFileSize();
void DeleteBuffer();
nsresult Flush();

Expand Down

0 comments on commit 385a6f4

Please sign in to comment.