Bug 828281 - UpdateRecord in CloseOutputStream can be removed. r=michal.novotny
authorAlfred Kayser <alfredkayser@gmail.com>
Sun, 03 Feb 2013 17:09:48 +0100
changeset 130571 4fe38a0ee8c5a3af493e78627228d0c067ac934f
parent 130570 94ff3af8d554b45c94adb8aea496683b0b162104
child 130572 847e28c7ba6738b22cdf7c7021f423509464356d
child 130573 db8ec0f3badf2168e5ce2e1c3e8ea9b311094a9f
push id2323
push userbbajaj@mozilla.com
push dateMon, 01 Apr 2013 19:47:02 +0000
treeherdermozilla-beta@7712be144d91 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmichal
bugs828281
milestone21.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 828281 - UpdateRecord in CloseOutputStream can be removed. r=michal.novotny
netwerk/cache/nsDiskCacheStreams.cpp
--- a/netwerk/cache/nsDiskCacheStreams.cpp
+++ b/netwerk/cache/nsDiskCacheStreams.cpp
@@ -458,76 +458,57 @@ nsDiskCacheStreamIO::Flush()
     // When writing to a file, just close the file
     if (mFD) {
         (void) PR_Close(mFD);
         mFD = nullptr;
         return NS_OK;
     }
 
     // write data to cache blocks, or flush mBuffer to file
+    NS_ASSERTION(mStreamEnd <= kMaxBufferSize, "stream is bigger than buffer");
+
     nsDiskCacheMap *cacheMap = mDevice->CacheMap();  // get map reference
-    nsresult rv;
-
-    bool written = false;
+    nsDiskCacheRecord * record = &mBinding->mRecord;
+    nsresult rv = NS_OK;
 
-    if (mStreamEnd <= kMaxBufferSize) {
-        // store data (if any) in cache block files
+    // delete existing storage
+    if (record->DataLocationInitialized()) {
+        rv = cacheMap->DeleteStorage(record, nsDiskCache::kData);
+        NS_ENSURE_SUCCESS(rv, rv);
 
-        // delete existing storage
-        nsDiskCacheRecord * record = &mBinding->mRecord;
-        if (record->DataLocationInitialized()) {
-            rv = cacheMap->DeleteStorage(record, nsDiskCache::kData);
+        // Only call UpdateRecord when there is no data to write,
+        // because WriteDataCacheBlocks / FlushBufferToFile calls it.
+        if ((mStreamEnd == 0) && (!mBinding->mDoomed)) {
+            rv = cacheMap->UpdateRecord(record);
             if (NS_FAILED(rv)) {
-                NS_WARNING("cacheMap->DeleteStorage() failed.");
-                return rv;
-            }
-        }
-
-        // flush buffer to block files
-        written = true;
-        if (mStreamEnd > 0) {
-            rv = cacheMap->WriteDataCacheBlocks(mBinding, mBuffer, mStreamEnd);
-            if (NS_FAILED(rv)) {
-                NS_WARNING("WriteDataCacheBlocks() failed.");
-                written = false;
+                NS_WARNING("cacheMap->UpdateRecord() failed.");
+                return rv;   // XXX doom cache entry
             }
         }
     }
+  
+    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.");
 
-    if (!written && mStreamEnd > 0) {
         // failed to store in cacheblocks, save as separate file
         rv = FlushBufferToFile(); // initializes DataFileLocation() if necessary
-
         if (mFD) {
-          // Update the file size of the disk file in the cache
-          UpdateFileSize();
-
-          // close file descriptor
-          (void) PR_Close(mFD);
-          mFD = nullptr;
+            UpdateFileSize();
+            (void) PR_Close(mFD);
+            mFD = nullptr;
         }
         else
-          NS_WARNING("no file descriptor");
-
-        // close mFD first if possible before returning if FlushBufferToFile
-        // failed
-        NS_ENSURE_SUCCESS(rv, rv);
+            NS_WARNING("no file descriptor");
     }
-    
-    // XXX do we need this here?  WriteDataCacheBlocks() calls UpdateRecord()
-    // update cache map if entry isn't doomed
-    if (!mBinding->mDoomed) {
-        rv = cacheMap->UpdateRecord(&mBinding->mRecord);
-        if (NS_FAILED(rv)) {
-            NS_WARNING("cacheMap->UpdateRecord() failed.");
-            return rv;   // XXX doom cache entry
-        }
-    }
-    
-    return NS_OK;
+   
+    return rv;
 }
 
 
 // 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()