Bug 344157 - properly check success of GetMDBFactory(). r=jorgk
authoraceman <acelists@atlas.sk>
Fri, 30 Nov 2018 13:02:00 +0100
changeset 33831 80212fc6395f1c205553669fdcee4c671d64ef98
parent 33830 6e49a3ec00cd10eb0bddad465f5f6a7254e8bc6c
child 33832 120405ddc5daffb395b2e691f13df5b3e8c41c7e
push id388
push userclokep@gmail.com
push dateMon, 28 Jan 2019 20:54:56 +0000
reviewersjorgk
bugs344157
Bug 344157 - properly check success of GetMDBFactory(). r=jorgk
mailnews/addrbook/src/nsAddrDatabase.cpp
mailnews/addrbook/src/nsAddrDatabase.h
mailnews/base/src/nsMsgFolderCache.cpp
mailnews/base/src/nsMsgFolderCache.h
mailnews/db/msgdb/public/nsMsgDatabase.h
mailnews/db/msgdb/src/nsMsgDatabase.cpp
--- a/mailnews/addrbook/src/nsAddrDatabase.cpp
+++ b/mailnews/addrbook/src/nsAddrDatabase.cpp
@@ -266,26 +266,31 @@ bool nsAddrDatabase::MatchDbName(nsIFile
 // RemoveFromCache
 //----------------------------------------------------------------------
 void nsAddrDatabase::RemoveFromCache(nsAddrDatabase* pAddrDB)
 {
   if (m_dbCache)
     m_dbCache->RemoveElement(pAddrDB);
 }
 
-void nsAddrDatabase::GetMDBFactory(nsIMdbFactory ** aMdbFactory)
+nsresult nsAddrDatabase::GetMDBFactory(nsIMdbFactory ** aMdbFactory)
 {
   if (!mMdbFactory)
   {
     nsresult rv;
     nsCOMPtr <nsIMdbFactoryService> mdbFactoryService = do_GetService(NS_MORK_CONTRACTID, &rv);
-    if (NS_SUCCEEDED(rv) && mdbFactoryService)
+    if (NS_SUCCEEDED(rv) && mdbFactoryService) {
       rv = mdbFactoryService->GetMdbFactory(getter_AddRefs(mMdbFactory));
+      NS_ENSURE_SUCCESS(rv, rv);
+      if (!mMdbFactory)
+        return NS_ERROR_FAILURE;
+    }
   }
-  NS_IF_ADDREF(*aMdbFactory = mMdbFactory);
+  NS_ADDREF(*aMdbFactory = mMdbFactory);
+  return NS_OK;
 }
 
 /* caller need to delete *aDbPath */
 NS_IMETHODIMP nsAddrDatabase::GetDbPath(nsIFile* *aDbPath)
 {
   if (!aDbPath)
     return NS_ERROR_NULL_POINTER;
 
@@ -461,20 +466,19 @@ nsAddrDatabase::OpenInternal(nsIFile *aM
   return rv;
 }
 
 // Open the MDB database synchronously. If successful, this routine
 // will set up the m_mdbStore and m_mdbEnv of the database object
 // so other database calls can work.
 NS_IMETHODIMP nsAddrDatabase::OpenMDB(nsIFile *dbName, bool create)
 {
-  nsresult ret;
   nsCOMPtr<nsIMdbFactory> mdbFactory;
-  GetMDBFactory(getter_AddRefs(mdbFactory));
-  NS_ENSURE_TRUE(mdbFactory, NS_ERROR_FAILURE);
+  nsresult ret = GetMDBFactory(getter_AddRefs(mdbFactory));
+  NS_ENSURE_SUCCESS(ret, ret);
 
   ret = mdbFactory->MakeEnv(NULL, &m_mdbEnv);
   if (NS_SUCCEEDED(ret))
   {
     nsIMdbThumb *thumb = nullptr;
 
     PathString filePath = dbName->NativePath();
 
--- a/mailnews/addrbook/src/nsAddrDatabase.h
+++ b/mailnews/addrbook/src/nsAddrDatabase.h
@@ -243,17 +243,17 @@ public:
   NS_IMETHOD GetCardCount(uint32_t *count) override;
 
   NS_IMETHOD SetCardValue(nsIAbCard *card, const char *name, const char16_t *value, bool notify) override;
   NS_IMETHOD GetCardValue(nsIAbCard *card, const char *name, char16_t **value) override;
   // nsAddrDatabase methods:
 
   nsAddrDatabase();
 
-  void GetMDBFactory(nsIMdbFactory ** aMdbFactory);
+  nsresult GetMDBFactory(nsIMdbFactory ** aMdbFactory);
   nsIMdbEnv    *GetEnv() {return m_mdbEnv;}
   uint32_t    GetCurVersion();
   nsIMdbTableRowCursor *GetTableRowCursor();
   nsIMdbTable    *GetPabTable() {return m_mdbPabTable;}
 
   static already_AddRefed<nsAddrDatabase> FindInCache(nsIFile *dbName);
 
   static void    CleanupCache();
--- a/mailnews/base/src/nsMsgFolderCache.cpp
+++ b/mailnews/base/src/nsMsgFolderCache.cpp
@@ -35,26 +35,31 @@ nsMsgFolderCache::~nsMsgFolderCache()
   NS_IF_RELEASE(gMDBFactory);
   if (m_mdbEnv)
     m_mdbEnv->Release();
 }
 
 
 NS_IMPL_ISUPPORTS(nsMsgFolderCache, nsIMsgFolderCache)
 
-void nsMsgFolderCache::GetMDBFactory(nsIMdbFactory ** aMdbFactory)
+nsresult nsMsgFolderCache::GetMDBFactory(nsIMdbFactory ** aMdbFactory)
 {
   if (!mMdbFactory)
   {
     nsresult rv;
     nsCOMPtr <nsIMdbFactoryService> mdbFactoryService = do_GetService(NS_MORK_CONTRACTID, &rv);
-    if (NS_SUCCEEDED(rv) && mdbFactoryService)
+    if (NS_SUCCEEDED(rv) && mdbFactoryService) {
       rv = mdbFactoryService->GetMdbFactory(getter_AddRefs(mMdbFactory));
+      NS_ENSURE_SUCCESS(rv, rv);
+      if (!mMdbFactory)
+        return NS_ERROR_FAILURE;
+    }
   }
-  NS_IF_ADDREF(*aMdbFactory = mMdbFactory);
+  NS_ADDREF(*aMdbFactory = mMdbFactory);
+  return NS_OK;
 }
 
 // initialize the various tokens and tables in our db's env
 nsresult nsMsgFolderCache::InitMDBInfo()
 {
   nsresult err = NS_OK;
   if (GetStore())
   {
@@ -123,113 +128,112 @@ nsresult nsMsgFolderCache::InitExistingD
   else
     err = NS_ERROR_FAILURE;
 
   return err;
 }
 
 nsresult nsMsgFolderCache::OpenMDB(const PathString& dbName, bool exists)
 {
-  nsresult ret=NS_OK;
   nsCOMPtr<nsIMdbFactory> mdbFactory;
-  GetMDBFactory(getter_AddRefs(mdbFactory));
-  if (mdbFactory)
-  {
-    ret = mdbFactory->MakeEnv(nullptr, &m_mdbEnv);
-    if (NS_SUCCEEDED(ret))
-    {
-      nsIMdbThumb *thumb = nullptr;
-      nsIMdbHeap* dbHeap = nullptr;
+  nsresult ret = GetMDBFactory(getter_AddRefs(mdbFactory));
+  NS_ENSURE_SUCCESS(ret, ret);
 
-      if (m_mdbEnv)
-        m_mdbEnv->SetAutoClear(true);
-      if (exists)
-      {
-        mdbOpenPolicy inOpenPolicy;
-        mdb_bool canOpen;
-        mdbYarn outFormatVersion;
-
-        nsIMdbFile* oldFile = nullptr;
-        ret = mdbFactory->OpenOldFile(m_mdbEnv, dbHeap, dbName.get(),
-                                      mdbBool_kFalse, // not readonly, we want modifiable
-                                      &oldFile);
-        if ( oldFile )
-        {
-          if (NS_SUCCEEDED(ret))
-          {
-            ret = mdbFactory->CanOpenFilePort(m_mdbEnv, oldFile, // file to investigate
-              &canOpen, &outFormatVersion);
-            if (NS_SUCCEEDED(ret) && canOpen)
-            {
-              inOpenPolicy.mOpenPolicy_ScopePlan.mScopeStringSet_Count = 0;
-              inOpenPolicy.mOpenPolicy_MinMemory = 0;
-              inOpenPolicy.mOpenPolicy_MaxLazy = 0;
+  ret = mdbFactory->MakeEnv(nullptr, &m_mdbEnv);
+  if (NS_SUCCEEDED(ret))
+  {
+    nsIMdbThumb *thumb = nullptr;
+    nsIMdbHeap* dbHeap = nullptr;
 
-              ret = mdbFactory->OpenFileStore(m_mdbEnv, NULL, oldFile, &inOpenPolicy,
-                &thumb);
-            }
-            else
-              ret = NS_MSG_ERROR_FOLDER_SUMMARY_OUT_OF_DATE;
-          }
-          NS_RELEASE(oldFile); // always release our file ref, store has own
-        }
-      }
-      if (NS_SUCCEEDED(ret) && thumb)
+    if (m_mdbEnv)
+      m_mdbEnv->SetAutoClear(true);
+    if (exists)
+    {
+      mdbOpenPolicy inOpenPolicy;
+      mdb_bool canOpen;
+      mdbYarn outFormatVersion;
+
+      nsIMdbFile* oldFile = nullptr;
+      ret = mdbFactory->OpenOldFile(m_mdbEnv, dbHeap, dbName.get(),
+                                    mdbBool_kFalse, // not readonly, we want modifiable
+                                    &oldFile);
+      if ( oldFile )
       {
-        mdb_count outTotal;    // total somethings to do in operation
-        mdb_count outCurrent;  // subportion of total completed so far
-        mdb_bool outDone = false;      // is operation finished?
-        mdb_bool outBroken;     // is operation irreparably dead and broken?
-        do
+        if (NS_SUCCEEDED(ret))
         {
-          ret = thumb->DoMore(m_mdbEnv, &outTotal, &outCurrent, &outDone, &outBroken);
-          if (NS_FAILED(ret))
+          ret = mdbFactory->CanOpenFilePort(m_mdbEnv, oldFile, // file to investigate
+            &canOpen, &outFormatVersion);
+          if (NS_SUCCEEDED(ret) && canOpen)
           {
-            outDone = true;
-            break;
-          }
-        }
-        while (NS_SUCCEEDED(ret) && !outBroken && !outDone);
-        // m_mdbEnv->ClearErrors(); // ### temporary...
-        if (NS_SUCCEEDED(ret) && outDone)
-        {
-          ret = mdbFactory->ThumbToOpenStore(m_mdbEnv, thumb, &m_mdbStore);
-          if (NS_SUCCEEDED(ret) && m_mdbStore)
-            ret = InitExistingDB();
-        }
-#ifdef DEBUG_bienvenu1
-        DumpContents();
-#endif
-      }
-      else // ### need error code saying why open file store failed
-      {
-        nsIMdbFile* newFile = 0;
-        ret = mdbFactory->CreateNewFile(m_mdbEnv, dbHeap, dbName.get(), &newFile);
-        if ( newFile )
-        {
-          if (NS_SUCCEEDED(ret))
-          {
-            mdbOpenPolicy inOpenPolicy;
-
             inOpenPolicy.mOpenPolicy_ScopePlan.mScopeStringSet_Count = 0;
             inOpenPolicy.mOpenPolicy_MinMemory = 0;
             inOpenPolicy.mOpenPolicy_MaxLazy = 0;
 
-            ret = mdbFactory->CreateNewFileStore(m_mdbEnv, dbHeap,
-              newFile, &inOpenPolicy, &m_mdbStore);
-            if (NS_SUCCEEDED(ret))
-              ret = InitNewDB();
+            ret = mdbFactory->OpenFileStore(m_mdbEnv, NULL, oldFile, &inOpenPolicy,
+              &thumb);
           }
-          NS_RELEASE(newFile); // always release our file ref, store has own
+          else
+            ret = NS_MSG_ERROR_FOLDER_SUMMARY_OUT_OF_DATE;
         }
-
+        NS_RELEASE(oldFile); // always release our file ref, store has own
+      }
+    }
+    if (NS_SUCCEEDED(ret) && thumb)
+    {
+      mdb_count outTotal;    // total somethings to do in operation
+      mdb_count outCurrent;  // subportion of total completed so far
+      mdb_bool outDone = false;      // is operation finished?
+      mdb_bool outBroken;     // is operation irreparably dead and broken?
+      do
+      {
+        ret = thumb->DoMore(m_mdbEnv, &outTotal, &outCurrent, &outDone, &outBroken);
+        if (NS_FAILED(ret))
+        {
+          outDone = true;
+          break;
+        }
+      }
+      while (NS_SUCCEEDED(ret) && !outBroken && !outDone);
+      // m_mdbEnv->ClearErrors(); // ### temporary...
+      if (NS_SUCCEEDED(ret) && outDone)
+      {
+        ret = mdbFactory->ThumbToOpenStore(m_mdbEnv, thumb, &m_mdbStore);
+        if (NS_SUCCEEDED(ret) && m_mdbStore)
+          ret = InitExistingDB();
       }
-      NS_IF_RELEASE(thumb);
+#ifdef DEBUG_bienvenu1
+      DumpContents();
+#endif
     }
+    else // ### need error code saying why open file store failed
+    {
+      nsIMdbFile* newFile = 0;
+      ret = mdbFactory->CreateNewFile(m_mdbEnv, dbHeap, dbName.get(), &newFile);
+      if ( newFile )
+      {
+        if (NS_SUCCEEDED(ret))
+        {
+          mdbOpenPolicy inOpenPolicy;
+
+          inOpenPolicy.mOpenPolicy_ScopePlan.mScopeStringSet_Count = 0;
+          inOpenPolicy.mOpenPolicy_MinMemory = 0;
+          inOpenPolicy.mOpenPolicy_MaxLazy = 0;
+
+          ret = mdbFactory->CreateNewFileStore(m_mdbEnv, dbHeap,
+            newFile, &inOpenPolicy, &m_mdbStore);
+          if (NS_SUCCEEDED(ret))
+            ret = InitNewDB();
+        }
+        NS_RELEASE(newFile); // always release our file ref, store has own
+      }
+
+    }
+    NS_IF_RELEASE(thumb);
   }
+
   return ret;
 }
 
 NS_IMETHODIMP nsMsgFolderCache::Init(nsIFile *aFile)
 {
   NS_ENSURE_ARG_POINTER(aFile);
 
   bool exists;
--- a/mailnews/base/src/nsMsgFolderCache.h
+++ b/mailnews/base/src/nsMsgFolderCache.h
@@ -23,17 +23,17 @@ public:
   nsMsgFolderCache();
 
   NS_DECL_ISUPPORTS
   NS_DECL_NSIMSGFOLDERCACHE
 
 protected:
   virtual ~nsMsgFolderCache();
 
-  void GetMDBFactory(nsIMdbFactory ** aMdbFactory);
+  nsresult GetMDBFactory(nsIMdbFactory ** aMdbFactory);
   nsresult AddCacheElement(const nsACString& key, nsIMdbRow *row, nsIMsgFolderCacheElement **result);
   nsresult RowCellColumnToCharPtr(nsIMdbRow *hdrRow, mdb_token columnToken, nsACString& resultPtr);
   nsresult InitMDBInfo();
   nsresult InitNewDB();
   nsresult InitExistingDB();
   nsresult OpenMDB(const PathString& dbName, bool create);
   nsIMdbEnv *GetEnv() {return m_mdbEnv;}
   nsIMdbStore *GetStore() {return m_mdbStore;}
--- a/mailnews/db/msgdb/public/nsMsgDatabase.h
+++ b/mailnews/db/msgdb/public/nsMsgDatabase.h
@@ -173,17 +173,17 @@ public:
   nsresult         GetSearchResultsTable(const char *searchFolderUri, bool createIfMissing, nsIMdbTable **table);
 
   // this might just be for debugging - we'll see.
   nsresult ListAllThreads(nsTArray<nsMsgKey> *threadIds);
   //////////////////////////////////////////////////////////////////////////////
   // nsMsgDatabase methods:
   nsMsgDatabase();
 
-  void GetMDBFactory(nsIMdbFactory ** aMdbFactory);
+  nsresult GetMDBFactory(nsIMdbFactory ** aMdbFactory);
   nsIMdbEnv             *GetEnv() {return m_mdbEnv;}
   nsIMdbStore           *GetStore() {return m_mdbStore;}
   virtual uint32_t      GetCurVersion();
   nsresult              GetCollationKeyGenerator();
   nsIMimeConverter *    GetMimeConverter();
 
   nsresult GetTableCreateIfMissing(const char *scope, const char *kind, nsIMdbTable **table,
                                    mdb_token &scopeToken, mdb_token &kindToken);
--- a/mailnews/db/msgdb/src/nsMsgDatabase.cpp
+++ b/mailnews/db/msgdb/src/nsMsgDatabase.cpp
@@ -245,18 +245,18 @@ NS_IMETHODIMP nsMsgDBService::OpenMore(n
     rv = msgDatabase->m_thumb->DoMore(msgDatabase->m_mdbEnv,
                                        &outTotal, &outCurrent, &outDone,
                                        &outBroken);
     if (NS_FAILED(rv))
       break;
     if (outDone)
     {
       nsCOMPtr<nsIMdbFactory> mdbFactory;
-      msgDatabase->GetMDBFactory(getter_AddRefs(mdbFactory));
-      NS_ENSURE_TRUE(mdbFactory, NS_ERROR_FAILURE);
+      rv = msgDatabase->GetMDBFactory(getter_AddRefs(mdbFactory));
+      NS_ENSURE_SUCCESS(rv, rv);
       rv = mdbFactory->ThumbToOpenStore(msgDatabase->m_mdbEnv, msgDatabase->m_thumb, &msgDatabase->m_mdbStore);
       msgDatabase->m_thumb = nullptr;
       nsCOMPtr<nsIFile> folderPath;
       (void) msgDatabase->m_folder->GetFilePath(getter_AddRefs(folderPath));
       nsCOMPtr <nsIFile> summaryFile;
       (void) GetSummaryFileLocation(folderPath, getter_AddRefs(summaryFile));
 
       if (NS_SUCCEEDED(rv))
@@ -1160,26 +1160,31 @@ nsMsgDatabase::~nsMsgDatabase()
     m_mdbEnv->Release(); //??? is this right?
     m_mdbEnv = nullptr;
   }
   m_ChangeListeners.Clear();
 }
 
 NS_IMPL_ISUPPORTS(nsMsgDatabase, nsIMsgDatabase, nsIDBChangeAnnouncer)
 
-void nsMsgDatabase::GetMDBFactory(nsIMdbFactory ** aMdbFactory)
+nsresult nsMsgDatabase::GetMDBFactory(nsIMdbFactory ** aMdbFactory)
 {
   if (!mMdbFactory)
   {
     nsresult rv;
     nsCOMPtr <nsIMdbFactoryService> mdbFactoryService = do_GetService(NS_MORK_CONTRACTID, &rv);
-    if (NS_SUCCEEDED(rv) && mdbFactoryService)
-      mdbFactoryService->GetMdbFactory(getter_AddRefs(mMdbFactory));
+    if (NS_SUCCEEDED(rv) && mdbFactoryService) {
+      rv = mdbFactoryService->GetMdbFactory(getter_AddRefs(mMdbFactory));
+      NS_ENSURE_SUCCESS(rv, rv);
+      if (!mMdbFactory)
+        return NS_ERROR_FAILURE;
+    }
   }
-  NS_IF_ADDREF(*aMdbFactory = mMdbFactory);
+  NS_ADDREF(*aMdbFactory = mMdbFactory);
+  return NS_OK;
 }
 
 // aLeaveInvalidDB: true if caller wants back a db even out of date.
 // If so, they'll extract out the interesting info from the db, close it,
 // delete it, and then try to open the db again, prior to reparsing.
 nsresult nsMsgDatabase::Open(nsMsgDBService *aDBService, nsIFile *aFolderName,
                              bool aCreate, bool aLeaveInvalidDB)
 {
@@ -1305,127 +1310,123 @@ nsresult nsMsgDatabase::CheckForErrors(n
 
 /**
  * Open the MDB database synchronously or async based on sync argument.
  * If successful, this routine will set up the m_mdbStore and m_mdbEnv of
  * the database object so other database calls can work.
  */
 nsresult nsMsgDatabase::OpenMDB(nsIFile *dbFile, bool create, bool sync)
 {
-  nsresult ret = NS_OK;
   nsCOMPtr<nsIMdbFactory> mdbFactory;
-  GetMDBFactory(getter_AddRefs(mdbFactory));
-  if (mdbFactory)
+  nsresult ret = GetMDBFactory(getter_AddRefs(mdbFactory));
+  NS_ENSURE_SUCCESS(ret, ret);
+
+  ret = mdbFactory->MakeEnv(NULL, &m_mdbEnv);
+  if (NS_SUCCEEDED(ret))
   {
-    ret = mdbFactory->MakeEnv(NULL, &m_mdbEnv);
-    if (NS_SUCCEEDED(ret))
+    nsIMdbHeap* dbHeap = nullptr;
+
+    if (m_mdbEnv)
+      m_mdbEnv->SetAutoClear(true);
+    PathString dbName = dbFile->NativePath();
+    ret = dbFile->Clone(getter_AddRefs(m_dbFile));
+    NS_ENSURE_SUCCESS(ret, ret);
+    bool exists = false;
+    ret = dbFile->Exists(&exists);
+    if (!exists)
     {
-      nsIMdbHeap* dbHeap = nullptr;
-
-      if (m_mdbEnv)
-        m_mdbEnv->SetAutoClear(true);
-      PathString dbName = dbFile->NativePath();
-      ret = dbFile->Clone(getter_AddRefs(m_dbFile));
-      NS_ENSURE_SUCCESS(ret, ret);
-      bool exists = false;
-      ret = dbFile->Exists(&exists);
-      if (!exists)
+      ret = NS_MSG_ERROR_FOLDER_SUMMARY_MISSING;
+    }
+    // If m_thumb is set, we're asynchronously opening the db already.
+    else if (!m_thumb)
+    {
+      mdbOpenPolicy inOpenPolicy;
+      mdb_bool  canOpen;
+      mdbYarn    outFormatVersion;
+
+      nsIMdbFile* oldFile = nullptr;
+      ret = mdbFactory->OpenOldFile(m_mdbEnv, dbHeap, dbName.get(),
+                                    mdbBool_kFalse, // not readonly, we want modifiable
+                                    &oldFile);
+      if (oldFile)
       {
-        ret = NS_MSG_ERROR_FOLDER_SUMMARY_MISSING;
-      }
-      // If m_thumb is set, we're asynchronously opening the db already.
-      else if (!m_thumb)
-      {
-        mdbOpenPolicy inOpenPolicy;
-        mdb_bool  canOpen;
-        mdbYarn    outFormatVersion;
-
-        nsIMdbFile* oldFile = nullptr;
-        ret = mdbFactory->OpenOldFile(m_mdbEnv, dbHeap, dbName.get(),
-                                      mdbBool_kFalse, // not readonly, we want modifiable
-                                      &oldFile);
-        if (oldFile)
+        if (NS_SUCCEEDED(ret))
         {
-          if (NS_SUCCEEDED(ret))
+          ret = mdbFactory->CanOpenFilePort(m_mdbEnv, oldFile, // the file to investigate
+            &canOpen, &outFormatVersion);
+          if (NS_SUCCEEDED(ret) && canOpen)
           {
-            ret = mdbFactory->CanOpenFilePort(m_mdbEnv, oldFile, // the file to investigate
-              &canOpen, &outFormatVersion);
-            if (NS_SUCCEEDED(ret) && canOpen)
-            {
-              inOpenPolicy.mOpenPolicy_ScopePlan.mScopeStringSet_Count = 0;
-              inOpenPolicy.mOpenPolicy_MinMemory = 0;
-              inOpenPolicy.mOpenPolicy_MaxLazy = 0;
-
-              ret = mdbFactory->OpenFileStore(m_mdbEnv, dbHeap,
-                oldFile, &inOpenPolicy, getter_AddRefs(m_thumb));
-            }
-            else
-              ret = NS_MSG_ERROR_FOLDER_SUMMARY_OUT_OF_DATE;
-          }
-          NS_RELEASE(oldFile); // always release our file ref, store has own
-        }
-      }
-      if (NS_SUCCEEDED(ret) && m_thumb && sync)
-      {
-        mdb_count outTotal;    // total somethings to do in operation
-        mdb_count outCurrent;  // subportion of total completed so far
-        mdb_bool outDone = false;      // is operation finished?
-        mdb_bool outBroken;     // is operation irreparably dead and broken?
-        do
-        {
-          ret = m_thumb->DoMore(m_mdbEnv, &outTotal, &outCurrent, &outDone, &outBroken);
-          if (NS_FAILED(ret))
-          {// mork isn't really doing NS errors yet.
-            outDone = true;
-            break;
-          }
-        }
-        while (NS_SUCCEEDED(ret) && !outBroken && !outDone);
-        //        m_mdbEnv->ClearErrors(); // ### temporary...
-        // only 0 is a non-error return.
-        if (NS_SUCCEEDED(ret) && outDone)
-        {
-          ret = mdbFactory->ThumbToOpenStore(m_mdbEnv, m_thumb, &m_mdbStore);
-          if (NS_SUCCEEDED(ret))
-            ret = (m_mdbStore) ? InitExistingDB() : NS_ERROR_FAILURE;
-        }
-#ifdef DEBUG_bienvenu1
-        DumpContents();
-#endif
-        m_thumb = nullptr;
-      }
-      else if (create)  // ### need error code saying why open file store failed
-      {
-        nsIMdbFile* newFile = 0;
-        ret = mdbFactory->CreateNewFile(m_mdbEnv, dbHeap, dbName.get(), &newFile);
-        if (NS_FAILED(ret))
-          ret = NS_ERROR_FILE_TARGET_DOES_NOT_EXIST;
-        if ( newFile )
-        {
-          if (NS_SUCCEEDED(ret))
-          {
-            mdbOpenPolicy inOpenPolicy;
-
             inOpenPolicy.mOpenPolicy_ScopePlan.mScopeStringSet_Count = 0;
             inOpenPolicy.mOpenPolicy_MinMemory = 0;
             inOpenPolicy.mOpenPolicy_MaxLazy = 0;
 
-            ret = mdbFactory->CreateNewFileStore(m_mdbEnv, dbHeap,
-              newFile, &inOpenPolicy, &m_mdbStore);
-            if (NS_SUCCEEDED(ret))
-              ret = (m_mdbStore) ? InitNewDB() : NS_ERROR_FAILURE;
+            ret = mdbFactory->OpenFileStore(m_mdbEnv, dbHeap,
+              oldFile, &inOpenPolicy, getter_AddRefs(m_thumb));
           }
-          NS_RELEASE(newFile); // always release our file ref, store has own
+          else
+            ret = NS_MSG_ERROR_FOLDER_SUMMARY_OUT_OF_DATE;
+        }
+        NS_RELEASE(oldFile); // always release our file ref, store has own
+      }
+    }
+    if (NS_SUCCEEDED(ret) && m_thumb && sync)
+    {
+      mdb_count outTotal;    // total somethings to do in operation
+      mdb_count outCurrent;  // subportion of total completed so far
+      mdb_bool outDone = false;      // is operation finished?
+      mdb_bool outBroken;     // is operation irreparably dead and broken?
+      do
+      {
+        ret = m_thumb->DoMore(m_mdbEnv, &outTotal, &outCurrent, &outDone, &outBroken);
+        if (NS_FAILED(ret))
+        {// mork isn't really doing NS errors yet.
+          outDone = true;
+          break;
         }
       }
+      while (NS_SUCCEEDED(ret) && !outBroken && !outDone);
+      //        m_mdbEnv->ClearErrors(); // ### temporary...
+      // only 0 is a non-error return.
+      if (NS_SUCCEEDED(ret) && outDone)
+      {
+        ret = mdbFactory->ThumbToOpenStore(m_mdbEnv, m_thumb, &m_mdbStore);
+        if (NS_SUCCEEDED(ret))
+          ret = (m_mdbStore) ? InitExistingDB() : NS_ERROR_FAILURE;
+      }
+#ifdef DEBUG_bienvenu1
+      DumpContents();
+#endif
+      m_thumb = nullptr;
+    }
+    else if (create)  // ### need error code saying why open file store failed
+    {
+      nsIMdbFile* newFile = 0;
+      ret = mdbFactory->CreateNewFile(m_mdbEnv, dbHeap, dbName.get(), &newFile);
+      if (NS_FAILED(ret))
+        ret = NS_ERROR_FILE_TARGET_DOES_NOT_EXIST;
+      if ( newFile )
+      {
+        if (NS_SUCCEEDED(ret))
+        {
+          mdbOpenPolicy inOpenPolicy;
+
+          inOpenPolicy.mOpenPolicy_ScopePlan.mScopeStringSet_Count = 0;
+          inOpenPolicy.mOpenPolicy_MinMemory = 0;
+          inOpenPolicy.mOpenPolicy_MaxLazy = 0;
+
+          ret = mdbFactory->CreateNewFileStore(m_mdbEnv, dbHeap,
+            newFile, &inOpenPolicy, &m_mdbStore);
+          if (NS_SUCCEEDED(ret))
+            ret = (m_mdbStore) ? InitNewDB() : NS_ERROR_FAILURE;
+        }
+        NS_RELEASE(newFile); // always release our file ref, store has own
+      }
     }
   }
-#ifdef DEBUG_David_Bienvenu
-//  NS_ASSERTION(NS_SUCCEEDED(ret), "failed opening mdb");
-#endif
+
   return ret;
 }
 
 nsresult nsMsgDatabase::CloseMDB(bool commit)
 {
   if (commit)
     Commit(nsMsgDBCommitType::kSessionCommit);
   return(NS_OK);