Bug 727370 - Recover correctly from a Reset during an update. r=dcamp
authorGian-Carlo Pascutto <gpascutto@mozilla.com>
Fri, 28 Sep 2012 18:31:19 +0200
changeset 108615 bfcccca4ac55fc2bbf1b130b5cbee0ba2de12cf8
parent 108614 15e5706f7fdc51f98dfd3770620a5e78c7d25983
child 108616 873929a5cfdedc42f4bac2b452dddd66b180c9c2
push id82
push usershu@rfrn.org
push dateFri, 05 Oct 2012 13:20:22 +0000
reviewersdcamp
bugs727370
milestone18.0a1
Bug 727370 - Recover correctly from a Reset during an update. r=dcamp
toolkit/components/url-classifier/Classifier.cpp
toolkit/components/url-classifier/Classifier.h
toolkit/components/url-classifier/tests/unit/test_streamupdater.js
--- a/toolkit/components/url-classifier/Classifier.cpp
+++ b/toolkit/components/url-classifier/Classifier.cpp
@@ -145,16 +145,38 @@ Classifier::SetupPathNames()
 
   rv = mToDeleteDirectory->AppendNative(STORE_DIRECTORY + TO_DELETE_DIR_SUFFIX);
   NS_ENSURE_SUCCESS(rv, rv);
 
   return NS_OK;
 }
 
 nsresult
+Classifier::CreateStoreDirectory()
+{
+  // Ensure the safebrowsing directory exists.
+  bool storeExists;
+  nsresult rv = mStoreDirectory->Exists(&storeExists);
+  NS_ENSURE_SUCCESS(rv, rv);
+
+  if (!storeExists) {
+    rv = mStoreDirectory->Create(nsIFile::DIRECTORY_TYPE, 0755);
+    NS_ENSURE_SUCCESS(rv, rv);
+  } else {
+    bool storeIsDir;
+    rv = mStoreDirectory->IsDirectory(&storeIsDir);
+    NS_ENSURE_SUCCESS(rv, rv);
+    if (!storeIsDir)
+      return NS_ERROR_FILE_DESTINATION_NOT_DIR;
+  }
+
+  return NS_OK;
+}
+
+nsresult
 Classifier::Open(nsIFile& aCacheDirectory)
 {
   // Remember the Local profile directory.
   nsresult rv = aCacheDirectory.Clone(getter_AddRefs(mCacheDirectory));
   NS_ENSURE_SUCCESS(rv, rv);
 
   // Create the handles to the update and backup directories.
   rv = SetupPathNames();
@@ -164,32 +186,20 @@ Classifier::Open(nsIFile& aCacheDirector
   rv = CleanToDelete();
   NS_ENSURE_SUCCESS(rv, rv);
 
   // Check whether we have an incomplete update and recover from the
   // backup if so.
   rv = RecoverBackups();
   NS_ENSURE_SUCCESS(rv, rv);
 
-  // Ensure the safebrowsing directory exists.
-  bool storeExists;
-  rv = mStoreDirectory->Exists(&storeExists);
+  // Make sure the main store directory exists.
+  rv = CreateStoreDirectory();
   NS_ENSURE_SUCCESS(rv, rv);
 
-  if (!storeExists) {
-    rv = mStoreDirectory->Create(nsIFile::DIRECTORY_TYPE, 0755);
-    NS_ENSURE_SUCCESS(rv, rv);
-  } else {
-    bool storeIsDir;
-    rv = mStoreDirectory->IsDirectory(&storeIsDir);
-    NS_ENSURE_SUCCESS(rv, rv);
-    if (!storeIsDir)
-      return NS_ERROR_FILE_DESTINATION_NOT_DIR;
-  }
-
   mCryptoHash = do_CreateInstance(NS_CRYPTO_HASH_CONTRACTID, &rv);
   NS_ENSURE_SUCCESS(rv, rv);
 
   rv = InitKey();
   if (NS_FAILED(rv)) {
     // Without a usable key the database is useless
     Reset();
     return NS_ERROR_FAILURE;
@@ -214,16 +224,18 @@ void
 Classifier::Reset()
 {
   DropStores();
 
   mStoreDirectory->Remove(true);
   mBackupDirectory->Remove(true);
   mToDeleteDirectory->Remove(true);
 
+  CreateStoreDirectory();
+
   mTableFreshness.Clear();
   RegenActiveTables();
 }
 
 void
 Classifier::TableRequest(nsACString& aResult)
 {
   nsTArray<nsCString> tables;
--- a/toolkit/components/url-classifier/Classifier.h
+++ b/toolkit/components/url-classifier/Classifier.h
@@ -66,16 +66,17 @@ public:
    * and mask the real entry being requested
    */
   nsresult ReadNoiseEntries(const Prefix& aPrefix,
                             const nsACString& aTableName,
                             int32_t aCount,
                             PrefixArray* aNoiseEntries);
 private:
   void DropStores();
+  nsresult CreateStoreDirectory();
   nsresult SetupPathNames();
   nsresult RecoverBackups();
   nsresult CleanToDelete();
   nsresult BackupTables();
   nsresult RemoveBackupTables();
   nsresult RegenActiveTables();
   nsresult ScanStoreDir(nsTArray<nsCString>& aTables);
 
--- a/toolkit/components/url-classifier/tests/unit/test_streamupdater.js
+++ b/toolkit/components/url-classifier/tests/unit/test_streamupdater.js
@@ -449,13 +449,13 @@ function run_test()
     // XXX: we're currently "once MAC, always MAC",
     // so any test not using a MAC must go above
     testValidMAC,
     testInvalidMAC,
     testNoMAC,
     testValidForwardMAC,
     testInvalidForwardMAC,
     testNoForwardMAC,
-    testRekey,
+    testRekey
   ]);
 }
 
 do_test_pending();