Bug 912465 - Use MsgNewSafeBufferedFileOutputStream instead of NewLocalFileOutputStream in various places. r=irving
☠☠ backed out by 933b83459851 ☠ ☠
authorJohannes Buchner <buchner.johannes@gmx.at>
Tue, 19 Nov 2013 09:36:46 -0500
changeset 13294 e75d5882e8b25df8b1be28414c6d43cb3e6f5ae7
parent 13293 0b04606f23dc9d11f1283b2f8cc88233fcb0a2b5
child 13295 933b83459851da415a3aec0994431b7f97e74e5e
push id9661
push userryanvm@gmail.com
push dateTue, 19 Nov 2013 14:37:04 +0000
treeherdercomm-central@e75d5882e8b2 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersirving
bugs912465
Bug 912465 - Use MsgNewSafeBufferedFileOutputStream instead of NewLocalFileOutputStream in various places. r=irving
mailnews/base/search/src/nsMsgFilterService.cpp
mailnews/base/src/nsMsgAccountManager.cpp
mailnews/base/util/nsMsgMailNewsUrl.cpp
mailnews/local/src/nsPop3Protocol.cpp
--- a/mailnews/base/search/src/nsMsgFilterService.cpp
+++ b/mailnews/base/search/src/nsMsgFilterService.cpp
@@ -121,33 +121,31 @@ NS_IMETHODIMP nsMsgFilterService::CloseF
 }
 
 /* save without deleting */
 NS_IMETHODIMP  nsMsgFilterService::SaveFilterList(nsIMsgFilterList *filterList, nsIFile *filterFile)
 {
   NS_ENSURE_ARG_POINTER(filterFile);
   NS_ENSURE_ARG_POINTER(filterList);
 
-  nsCOMPtr<nsIOutputStream> out;
-  nsresult rv = NS_NewSafeLocalFileOutputStream(getter_AddRefs(out),
+  nsCOMPtr<nsIOutputStream> strm;
+  nsresult rv = MsgNewSafeBufferedFileOutputStream(getter_AddRefs(strm),
                                                 filterFile, -1, 0600);
   NS_ENSURE_SUCCESS(rv, rv);
 
-  nsCOMPtr<nsIOutputStream> strm;
-  rv = NS_NewBufferedOutputStream(getter_AddRefs(strm), out, 4096);
-  NS_ENSURE_SUCCESS(rv, rv);
-
   rv = filterList->SaveToFile(strm);
 
   nsCOMPtr<nsISafeOutputStream> safeStream = do_QueryInterface(strm);
-  NS_ASSERTION(safeStream, "expected a safe output stream");
-  if (NS_SUCCEEDED(rv) && safeStream)
+  NS_ASSERTION(safeStream, "expected a safe output stream!");
+  if (safeStream) {
     rv = safeStream->Finish();
-
-  NS_ASSERTION(NS_SUCCEEDED(rv), "failed to save filter file");
+    if (NS_FAILED(rv)) {
+      NS_WARNING("failed to save filter file! possible data loss");
+    }
+  }
   return rv;
 }
 
 NS_IMETHODIMP nsMsgFilterService::CancelFilterList(nsIMsgFilterList *filterList)
 {
   return NS_ERROR_NOT_IMPLEMENTED;
 }
 
--- a/mailnews/base/src/nsMsgAccountManager.cpp
+++ b/mailnews/base/src/nsMsgAccountManager.cpp
@@ -3121,33 +3121,35 @@ NS_IMETHODIMP nsMsgAccountManager::SaveV
 {
   if (!m_virtualFoldersLoaded)
     return NS_OK;
 
   nsCOMPtr<nsIFile> file;
   GetVirtualFoldersFile(file);
 
   // Open a buffered, safe output stream
-  nsCOMPtr<nsIOutputStream> outStreamSink;
-  nsresult rv = NS_NewSafeLocalFileOutputStream(getter_AddRefs(outStreamSink),
-                                                file,
-                                                PR_CREATE_FILE | PR_WRONLY | PR_TRUNCATE,
-                                                0664);
-  NS_ENSURE_SUCCESS(rv, rv);
-
   nsCOMPtr<nsIOutputStream> outStream;
-  rv = NS_NewBufferedOutputStream(getter_AddRefs(outStream), outStreamSink, 4096);
+  nsresult rv = MsgNewSafeBufferedFileOutputStream(getter_AddRefs(outStream),
+                                                   file,
+                                                   PR_CREATE_FILE | PR_WRONLY | PR_TRUNCATE,
+                                                   0664);
   NS_ENSURE_SUCCESS(rv, rv);
 
   WriteLineToOutputStream("version=", "1", outStream);
   m_incomingServers.Enumerate(saveVirtualFolders, &outStream);
 
   nsCOMPtr<nsISafeOutputStream> safeStream = do_QueryInterface(outStream, &rv);
-  NS_ENSURE_SUCCESS(rv, rv);
-  return safeStream->Finish();
+  NS_ASSERTION(safeStream, "expected a safe output stream!");
+  if (safeStream) {
+    rv = safeStream->Finish();
+    if (NS_FAILED(rv)) {
+      NS_WARNING("failed to save personal dictionary file! possible data loss");
+    }
+  }
+  return rv;
 }
 
 PLDHashOperator
 nsMsgAccountManager::saveVirtualFolders(nsCStringHashKey::KeyType key,
                                         nsCOMPtr<nsIMsgIncomingServer>& server,
                                         void *data)
 {
   if (server)
--- a/mailnews/base/util/nsMsgMailNewsUrl.cpp
+++ b/mailnews/base/util/nsMsgMailNewsUrl.cpp
@@ -907,19 +907,17 @@ nsresult nsMsgSaveAsListener::SetupMsgWr
   // getting the outputstream.
   // Due to bug 328027, the nsSaveMsgListener created in
   // nsMessenger::SaveAs now opens the stream on the nsIFile
   // object, thus creating an empty file. Actual save operations for
   // IMAP and NNTP use this nsMsgSaveAsListener here, though, so we
   // have to close the stream before deleting the file, else data
   // would still be written happily into a now non-existing file.
   // (Windows doesn't care, btw, just unixoids do...)
-  aFile->Remove(false);
-
-  nsresult rv = MsgNewBufferedFileOutputStream(getter_AddRefs(m_outputStream),
+  nsresult rv = MsgNewSafeBufferedFileOutputStream(getter_AddRefs(m_outputStream),
                                                aFile, -1, 0666);
   NS_ENSURE_SUCCESS(rv, rv);
 
   if (m_outputStream && addDummyEnvelope)
   {
     nsAutoCString result;
     uint32_t writeCount;
 
@@ -933,17 +931,25 @@ nsresult nsMsgSaveAsListener::SetupMsgWr
     m_outputStream->Write(result.get(), result.Length(), &writeCount);
 
     result = "X-Mozilla-Status: 0001";
     result += MSG_LINEBREAK;
     result += "X-Mozilla-Status2: 00000000";
     result += MSG_LINEBREAK;
     m_outputStream->Write(result.get(), result.Length(), &writeCount);
   }
-
+  NS_ENSURE_SUCCESS(rv, rv);
+  nsCOMPtr<nsISafeOutputStream> safeStream = do_QueryInterface(m_outputStream);
+  NS_ASSERTION(safeStream, "expected a safe output stream!");
+  if (safeStream) {
+    rv = safeStream->Finish();
+    if (NS_FAILED(rv)) {
+      NS_WARNING("failed to save msg file! possible data loss");
+    }
+  }
   return rv;
 }
 
 
 NS_IMETHODIMP nsMsgMailNewsUrl::GetSaveAsListener(bool addDummyEnvelope, 
                                                   nsIFile *aFile, nsIStreamListener **aSaveListener)
 {
   NS_ENSURE_ARG_POINTER(aSaveListener);
--- a/mailnews/local/src/nsPop3Protocol.cpp
+++ b/mailnews/local/src/nsPop3Protocol.cpp
@@ -348,17 +348,23 @@ net_pop3_write_state(Pop3UidlHost* host,
       fileOutputStream->Write(host->host, strlen(host->host), &numBytesWritten);
       fileOutputStream->Write(" ", 1, &numBytesWritten);
       fileOutputStream->Write(host->user, strlen(host->user), &numBytesWritten);
       fileOutputStream->Write(MSG_LINEBREAK, MSG_LINEBREAK_LEN, &numBytesWritten);
       PL_HashTableEnumerateEntries(host->hash, net_pop3_write_mapper, (void *)fileOutputStream);
     }
   }
   nsCOMPtr<nsISafeOutputStream> safeStream = do_QueryInterface(fileOutputStream);
-  safeStream->Finish();
+  NS_ASSERTION(safeStream, "expected a safe output stream!");
+  if (safeStream) {
+    rv = safeStream->Finish();
+    if (NS_FAILED(rv)) {
+      NS_WARNING("failed to save pop state! possible data loss");
+    }
+  }
 }
 
 static void
 net_pop3_free_state(Pop3UidlHost* host)
 {
   Pop3UidlHost* h;
   while (host)
   {