nsIMsgPluggableStore.discoverSubFolders should not raise NS_MSG_FOLDER_EXISTS.r=bienvenu, a=standard8, bug 759237
authorHiroyuki Ikezoe <hiikezoe@mozilla-japan.org>
Tue, 29 May 2012 12:27:32 -0700
changeset 11363 c2e021ccbd535a975a6a4ffeba1413ba4a237c28
parent 11362 0276e70d4f88f7facbb84f5b354d71004bb84060
child 11364 d4c4efd9550fd64db359995ffadf8d17278ed00a
child 11366 a819fa504ee8facb6ed6214e2d58d9c537bc742f
push idunknown
push userunknown
push dateunknown
reviewersbienvenu, standard8, bug
bugs759237
nsIMsgPluggableStore.discoverSubFolders should not raise NS_MSG_FOLDER_EXISTS.r=bienvenu, a=standard8, bug 759237
mailnews/local/src/nsMsgBrkMBoxStore.cpp
mailnews/local/src/nsMsgMaildirStore.cpp
mailnews/local/test/unit/test_nsIMsgPluggableStore.js
mailnews/local/test/unit/xpcshell.ini
--- a/mailnews/local/src/nsMsgBrkMBoxStore.cpp
+++ b/mailnews/local/src/nsMsgBrkMBoxStore.cpp
@@ -113,38 +113,36 @@ NS_IMETHODIMP nsMsgBrkMBoxStore::Discove
 
   if (directory)
   {
     aParentFolder->SetFlag(nsMsgFolderFlags::Mail | nsMsgFolderFlags::Elided |
                            nsMsgFolderFlags::Directory);
 
     // now, discover those folders
     rv = AddSubFolders(aParentFolder, path, aDeep);
-    if (NS_FAILED(rv))
-      return rv;
+    NS_ENSURE_SUCCESS(rv, rv);
 
-    bool createdDefaultMailboxes = false;
     nsCOMPtr<nsILocalMailIncomingServer> localMailServer;
 
     if (isServer)
     {
       nsCOMPtr<nsIMsgIncomingServer> server;
       rv = aParentFolder->GetServer(getter_AddRefs(server));
       NS_ENSURE_SUCCESS(rv, NS_MSG_INVALID_OR_MISSING_SERVER);
       localMailServer = do_QueryInterface(server, &rv);
       NS_ENSURE_SUCCESS(rv, NS_MSG_INVALID_OR_MISSING_SERVER);
 
       // first create the folders on disk (as empty files)
       rv = localMailServer->CreateDefaultMailboxes(path);
-      NS_ENSURE_SUCCESS(rv, rv);
-      createdDefaultMailboxes = true;
+      if (NS_FAILED(rv) && rv != NS_MSG_FOLDER_EXISTS)
+        return rv;
+
       // now, discover those folders
       rv = AddSubFolders(aParentFolder, path, aDeep);
-      if (NS_FAILED(rv))
-        return rv;
+      NS_ENSURE_SUCCESS(rv, rv);
       
       rv = localMailServer->SetFlagsOnDefaultMailboxes();
       if (NS_FAILED(rv))
         return rv;
     }
   }
   return rv;
 }
@@ -1128,17 +1126,17 @@ nsMsgBrkMBoxStore::AddSubFolders(nsIMsgF
       if (deep)
       {
         nsCOMPtr<nsILocalFile> path;
         rv = child->GetFilePath(getter_AddRefs(path));
         AddSubFolders(child, path, true);
       }
     }
   }
-  return rv;
+  return rv == NS_MSG_FOLDER_EXISTS ? NS_OK : rv;
 }
 
 /* Finds the directory associated with this folder.  That is if the path is
    c:\Inbox, it will return c:\Inbox.sbd if it succeeds.  If that path doesn't
    currently exist then it will create it. Path is strictly an out parameter.
   */
 nsresult nsMsgBrkMBoxStore::CreateDirectoryForFolder(nsILocalFile *path)
 {
--- a/mailnews/local/src/nsMsgMaildirStore.cpp
+++ b/mailnews/local/src/nsMsgMaildirStore.cpp
@@ -142,17 +142,17 @@ nsresult nsMsgMaildirStore::AddSubFolder
         bool directory = false;
         // Check that <folder>.sbd really is a directory.
         path->IsDirectory(&directory);
         if (directory)
           AddSubFolders(child, path, true);
       }
     }
   }
-  return rv;
+  return rv == NS_MSG_FOLDER_EXISTS ? NS_OK : rv;
 }
 
 NS_IMETHODIMP nsMsgMaildirStore::DiscoverSubFolders(nsIMsgFolder *aParentFolder,
                                                     bool aDeep)
 {
   NS_ENSURE_ARG_POINTER(aParentFolder);
 
   nsCOMPtr<nsILocalFile> path;
@@ -191,17 +191,17 @@ NS_IMETHODIMP nsMsgMaildirStore::Discove
       // now, discover those folders
       rv = AddSubFolders(aParentFolder, path, aDeep);
       NS_ENSURE_SUCCESS(rv, rv);
       // and add flags on them
       rv = localMailServer->SetFlagsOnDefaultMailboxes();
       NS_ENSURE_SUCCESS(rv, rv);
     }
   }
-  return rv;
+  return (rv == NS_MSG_FOLDER_EXISTS) ? NS_OK : rv;
 }
 
 /**
  *Create a Maildir-style folder with "tmp", " and "cur" subfolders
  * but no "new" subfolder, because it's not sensical in the mail client context.
  ("new" directory is for messages on the server that haven't been seen by a
 *  mail client).
  * aFolderName is already "safe" - it has been through NS_MsgHashIfNecessary
new file mode 100644
--- /dev/null
+++ b/mailnews/local/test/unit/test_nsIMsgPluggableStore.js
@@ -0,0 +1,78 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this file,
+ * You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+/**
+ * nsIMsgPluggableStore interface tests
+ */
+
+var gPluggableStores = [
+  "@mozilla.org/msgstore/berkeleystore;1",
+  "@mozilla.org/msgstore/maildirstore;1"
+];
+
+function create_temporary_directory() {
+  let directory = Cc["@mozilla.org/file/directory_service;1"]
+    .getService(Ci.nsIProperties)
+    .get("TmpD", Ci.nsIFile);
+  directory.append("mailFolder");
+  directory.createUnique(Ci.nsIFile.DIRECTORY_TYPE, 0700);
+  return directory;
+}
+
+function create_sub_folders(parent, subFolders) {
+  parent.leafName = parent.leafName + ".sbd";
+  parent.create(Ci.nsIFile.DIRECTORY_TYPE, parseInt("0700", 8));
+
+  for (let folder in subFolders) {
+    let subFolder = parent.clone();
+    subFolder.append(subFolders[folder].name);
+    subFolder.create(Ci.nsIFile.NORMAL_FILE_TYPE, parseInt("0600", 8));
+    if (subFolders[folder].subFolders)
+      create_sub_folders(subFolder, subFolders[folder].subFolders);
+  }
+}
+
+function create_mail_directory(subFolders) {
+  let root = create_temporary_directory();
+
+  for (let folder in subFolders) {
+    if (!subFolders[folder].subFolders)
+      continue;
+    let directory = root.clone();
+    directory.append(subFolders[folder].name);
+    create_sub_folders(directory, subFolders[folder].subFolders);
+  }
+
+  return root;
+}
+
+function setup_mailbox(type, mailboxPath) {
+  let user = Cc["@mozilla.org/uuid-generator;1"]
+               .getService(Ci.nsIUUIDGenerator)
+               .generateUUID().toString();
+  let incomingServer =
+    MailServices.accounts.createIncomingServer(user, "Local Folder", type);
+  incomingServer.localPath = mailboxPath;
+
+  return incomingServer.rootFolder;
+}
+
+function test_discoverSubFolders() {
+  let mailbox = setup_mailbox("none", create_temporary_directory());
+
+  mailbox.msgStore.discoverSubFolders(mailbox, true);
+
+}
+
+function run_all_tests() {
+  test_discoverSubFolders();
+}
+
+function run_test() {
+  for (let store in gPluggableStores) {
+    Services.prefs.setCharPref("mail.serverDefaultStoreContractID",
+                               gPluggableStores[store]);
+    run_all_tests();
+  }
+}
--- a/mailnews/local/test/unit/xpcshell.ini
+++ b/mailnews/local/test/unit/xpcshell.ini
@@ -1,20 +1,21 @@
 [DEFAULT]
 head = head_maillocal.js
 tail = tail_local.js
 
 [test_bug457168.js]
 [test_fileName.js]
-[test_nsIMsgLocalMailFolder.js]
 [test_mailboxContentLength.js]
 [test_mailboxProtocol.js]
 [test_movemailDownload.js]
 [test_msgCopy.js]
 [test_msgIDParsing.js]
+[test_nsIMsgLocalMailFolder.js]
+[test_nsIMsgPluggableStore.js]
 [test_over2GBMailboxes.js]
 [test_over4GBMailboxes.js]
 [test_pop3AuthMethods.js]
 [test_pop3Download.js]
 [test_pop3DownloadTempFileHandling.js]
 [test_pop3Duplicates.js]
 [test_pop3FilterActions.js]
 [test_pop3GSSAPIFail.js]
@@ -29,9 +30,8 @@ tail = tail_local.js
 [test_pop3PasswordFailure3.js]
 [test_pop3Pump.js]
 [test_pop3ServerBrokenCRAMDisconnect.js]
 [test_pop3ServerBrokenCRAMFail.js]
 [test_preview.js]
 [test_saveMessage.js]
 [test_undoDelete.js]
 [test_verifyLogon.js]
-