From cf38d1c59a790a0c683757883f104d29a0931301 Mon Sep 17 00:00:00 2001 From: Taras Glek Date: Tue, 14 Sep 2010 15:47:47 -0400 Subject: [PATCH] Bug 594172 - ZipWriter write()s too often r=Mossop a=2.0 --- .../libjar/zipwriter/src/nsZipDataStream.cpp | 15 ++------------ modules/libjar/zipwriter/src/nsZipHeader.h | 2 ++ modules/libjar/zipwriter/src/nsZipWriter.cpp | 20 ++++++++++++++++++- 3 files changed, 23 insertions(+), 14 deletions(-) diff --git a/modules/libjar/zipwriter/src/nsZipDataStream.cpp b/modules/libjar/zipwriter/src/nsZipDataStream.cpp index 46f0d131eec..f8559c61101 100644 --- a/modules/libjar/zipwriter/src/nsZipDataStream.cpp +++ b/modules/libjar/zipwriter/src/nsZipDataStream.cpp @@ -152,8 +152,7 @@ NS_IMETHODIMP nsZipDataStream::OnStopRequest(nsIRequest *aRequest, inline nsresult nsZipDataStream::CompleteEntry() { - nsresult rv = mStream->Flush(); - NS_ENSURE_SUCCESS(rv, rv); + nsresult rv; nsCOMPtr seekable = do_QueryInterface(mStream, &rv); NS_ENSURE_SUCCESS(rv, rv); PRInt64 pos; @@ -161,17 +160,7 @@ inline nsresult nsZipDataStream::CompleteEntry() NS_ENSURE_SUCCESS(rv, rv); mHeader->mCSize = pos - mHeader->mOffset - mHeader->GetFileHeaderLength(); - - // Go back and rewrite the file header - rv = seekable->Seek(nsISeekableStream::NS_SEEK_SET, mHeader->mOffset); - NS_ENSURE_SUCCESS(rv, rv); - rv = mHeader->WriteFileHeader(mStream); - NS_ENSURE_SUCCESS(rv, rv); - rv = mStream->Flush(); - NS_ENSURE_SUCCESS(rv, rv); - rv = seekable->Seek(nsISeekableStream::NS_SEEK_SET, pos); - NS_ENSURE_SUCCESS(rv, rv); - + mHeader->mWriteOnClose = PR_TRUE; return NS_OK; } diff --git a/modules/libjar/zipwriter/src/nsZipHeader.h b/modules/libjar/zipwriter/src/nsZipHeader.h index 22f86cc2cf3..9d7c269fbe4 100644 --- a/modules/libjar/zipwriter/src/nsZipHeader.h +++ b/modules/libjar/zipwriter/src/nsZipHeader.h @@ -78,6 +78,7 @@ public: mDisk(0), mIAttr(0), mInited(PR_FALSE), + mWriteOnClose(PR_FALSE), mExtraField(NULL), mLocalExtraField(NULL) { @@ -105,6 +106,7 @@ public: PRUint16 mDisk; PRUint16 mIAttr; PRPackedBool mInited; + PRPackedBool mWriteOnClose; nsCString mName; nsCString mComment; nsAutoArrayPtr mExtraField; diff --git a/modules/libjar/zipwriter/src/nsZipWriter.cpp b/modules/libjar/zipwriter/src/nsZipWriter.cpp index e5f7a434929..57fe1cab1a0 100644 --- a/modules/libjar/zipwriter/src/nsZipWriter.cpp +++ b/modules/libjar/zipwriter/src/nsZipWriter.cpp @@ -301,7 +301,7 @@ NS_IMETHODIMP nsZipWriter::Open(nsIFile *aFile, PRInt32 aIoFlags) return rv; } - rv = NS_NewBufferedOutputStream(getter_AddRefs(mStream), stream, 0x800); + rv = NS_NewBufferedOutputStream(getter_AddRefs(mStream), stream, 3 * 1024 * 1024); if (NS_FAILED(rv)) { stream->Close(); mHeaders.Clear(); @@ -711,6 +711,24 @@ NS_IMETHODIMP nsZipWriter::Close() Cleanup(); return rv; } + + // Go back and rewrite the file headers + for (PRInt32 i = 0; i < mHeaders.Count(); i++) { + nsZipHeader *header = mHeaders[i]; + if (!header->mWriteOnClose) + continue; + + rv = seekable->Seek(nsISeekableStream::NS_SEEK_SET, header->mOffset); + if (NS_FAILED(rv)) { + Cleanup(); + return rv; + } + rv = header->WriteFileHeader(mStream); + if (NS_FAILED(rv)) { + Cleanup(); + return rv; + } + } } nsresult rv = mStream->Close();