Bug 1353956 - P6. Load the old prefixset(.pset) when there is no .vlpset. r=gcp
☠☠ backed out by 4f6e4cbf401f ☠ ☠
authordlee <dlee@mozilla.com>
Wed, 06 Mar 2019 09:41:34 +0000
changeset 520664 71dafccc22ae493b656d9130cec82ba37d4babf7
parent 520663 f1f29fe519cf169d43eeacfe710d0b84e9da55db
child 520665 37fc9c97e0d2493db260631ad2c4a548fac71ec1
push id10862
push userffxbld-merge
push dateMon, 11 Mar 2019 13:01:11 +0000
treeherdermozilla-beta@a2e7f5c935da [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgcp
bugs1353956
milestone67.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 1353956 - P6. Load the old prefixset(.pset) when there is no .vlpset. r=gcp To avoid forcing a redownload of SafeBrowsing v4 list. Differential Revision: https://phabricator.services.mozilla.com/D21876
toolkit/components/url-classifier/LookupCache.cpp
toolkit/components/url-classifier/LookupCache.h
toolkit/components/url-classifier/LookupCacheV4.cpp
toolkit/components/url-classifier/LookupCacheV4.h
--- a/toolkit/components/url-classifier/LookupCache.cpp
+++ b/toolkit/components/url-classifier/LookupCache.cpp
@@ -446,17 +446,24 @@ nsresult LookupCache::LoadPrefixSet() {
   if (exists) {
     LOG(("stored PrefixSet exists, loading from disk"));
     rv = LoadFromFile(psFile);
     if (NS_FAILED(rv)) {
       return rv;
     }
     mPrimed = true;
   } else {
-    LOG(("no (usable) stored PrefixSet found"));
+    // The only scenario we load the old .pset file is when we haven't received
+    // a SafeBrowsng update before. After receiving an update, new .vlpset will
+    // be stored while old .pset will be removed.
+    if (NS_SUCCEEDED(LoadLegacyFile())) {
+      mPrimed = true;
+    } else {
+      LOG(("no (usable) stored PrefixSet found"));
+    }
   }
 
 #ifdef DEBUG
   if (mPrimed) {
     uint32_t size = SizeOfPrefixSet();
     LOG(("SB tree done, size = %d bytes\n", size));
   }
 #endif
@@ -676,16 +683,18 @@ nsresult LookupCacheV2::StoreToFile(nsCO
 
   rv = mPrefixSet->WritePrefixes(out);
   NS_ENSURE_SUCCESS(rv, rv);
 
   LOG(("[%s] Storing PrefixSet successful", mTableName.get()));
   return NS_OK;
 }
 
+nsresult LookupCacheV2::LoadLegacyFile() { return NS_ERROR_NOT_IMPLEMENTED; }
+
 nsresult LookupCacheV2::LoadFromFile(nsCOMPtr<nsIFile>& aFile) {
   Telemetry::AutoTimer<Telemetry::URLCLASSIFIER_PS_FILELOAD_TIME> timer;
 
   nsCOMPtr<nsIInputStream> localInFile;
   nsresult rv = NS_NewLocalFileInputStream(getter_AddRefs(localInFile), aFile,
                                            PR_RDONLY | nsIFile::OS_READAHEAD);
   NS_ENSURE_SUCCESS(rv, rv);
 
--- a/toolkit/components/url-classifier/LookupCache.h
+++ b/toolkit/components/url-classifier/LookupCache.h
@@ -253,16 +253,18 @@ class LookupCache {
  private:
   nsresult LoadPrefixSet();
 
   virtual size_t SizeOfPrefixSet() const = 0;
   virtual nsCString GetPrefixSetSuffix() const = 0;
 
   virtual int Ver() const = 0;
 
+  virtual nsresult LoadLegacyFile() = 0;
+
  protected:
   virtual ~LookupCache() {}
 
   static const uint32_t MAX_BUFFER_SIZE = 64 * 1024;
 
   // Check completions in positive cache and prefix in negative cache.
   // 'aHas' and 'aConfirmed' are output parameters.
   nsresult CheckCache(const Completion& aCompletion, bool* aHas,
@@ -324,16 +326,18 @@ class LookupCacheV2 final : public Looku
   virtual size_t SizeOfPrefixSet() const override;
   virtual nsCString GetPrefixSetSuffix() const override;
 
  private:
   ~LookupCacheV2() {}
 
   virtual int Ver() const override { return VER; }
 
+  virtual nsresult LoadLegacyFile() override;
+
   // Construct a Prefix Set with known prefixes.
   // This will Clear() aAddPrefixes when done.
   nsresult ConstructPrefixSet(AddPrefixArray& aAddPrefixes);
 
   // Full length hashes obtained in update request
   CompletionArray mUpdateCompletions;
 
   // Set of prefixes known to be in the database
--- a/toolkit/components/url-classifier/LookupCacheV4.cpp
+++ b/toolkit/components/url-classifier/LookupCacheV4.cpp
@@ -339,16 +339,78 @@ nsresult LookupCacheV4::CleanOldPrefixSe
     }
 
     LOG(("[%s] Old PrefixSet is succuessfully removed!", mTableName.get()));
   }
 
   return NS_OK;
 }
 
+nsresult LookupCacheV4::LoadLegacyFile() {
+  nsCOMPtr<nsIFile> file;
+  nsresult rv = mStoreDirectory->Clone(getter_AddRefs(file));
+  if (NS_WARN_IF(NS_FAILED(rv))) {
+    return rv;
+  }
+
+  rv = file->AppendNative(mTableName + NS_LITERAL_CSTRING(".pset"));
+  if (NS_WARN_IF(NS_FAILED(rv))) {
+    return rv;
+  }
+
+  bool exists;
+  rv = file->Exists(&exists);
+  NS_ENSURE_SUCCESS(rv, rv);
+
+  if (!exists) {
+    return NS_ERROR_FAILURE;
+  }
+
+  nsCOMPtr<nsIInputStream> localInFile;
+  rv = NS_NewLocalFileInputStream(getter_AddRefs(localInFile), file,
+                                  PR_RDONLY | nsIFile::OS_READAHEAD);
+  if (NS_WARN_IF(NS_FAILED(rv))) {
+    return rv;
+  }
+
+  // Calculate how big the file is, make sure our read buffer isn't bigger
+  // than the file itself which is just wasting memory.
+  int64_t fileSize;
+  rv = file->GetFileSize(&fileSize);
+  if (NS_WARN_IF(NS_FAILED(rv))) {
+    return rv;
+  }
+
+  if (fileSize < 0 || fileSize > UINT32_MAX) {
+    return NS_ERROR_FAILURE;
+  }
+
+  uint32_t bufferSize =
+      std::min<uint32_t>(static_cast<uint32_t>(fileSize), MAX_BUFFER_SIZE);
+
+  // Convert to buffered stream
+  nsCOMPtr<nsIInputStream> in;
+  rv = NS_NewBufferedInputStream(getter_AddRefs(in), localInFile.forget(),
+                                 bufferSize);
+  if (NS_WARN_IF(NS_FAILED(rv))) {
+    return rv;
+  }
+
+  // Load data
+  rv = mVLPrefixSet->LoadPrefixes(in);
+  if (NS_WARN_IF(NS_FAILED(rv))) {
+    return rv;
+  }
+
+  mPrimed = true;
+
+  LOG(("[%s] Loading Legacy PrefixSet successful", mTableName.get()));
+  return NS_OK;
+}
+
 nsresult LookupCacheV4::LoadFromFile(nsCOMPtr<nsIFile>& aFile) {
   NS_ENSURE_ARG_POINTER(aFile);
 
   Telemetry::AutoTimer<Telemetry::URLCLASSIFIER_VLPS_FILELOAD_TIME> timer;
 
   nsCOMPtr<nsIInputStream> localInFile;
   nsresult rv = NS_NewLocalFileInputStream(getter_AddRefs(localInFile), aFile,
                                            PR_RDONLY | nsIFile::OS_READAHEAD);
--- a/toolkit/components/url-classifier/LookupCacheV4.h
+++ b/toolkit/components/url-classifier/LookupCacheV4.h
@@ -54,16 +54,18 @@ class LookupCacheV4 final : public Looku
   virtual nsCString GetPrefixSetSuffix() const override;
   nsCString GetMetadataSuffix() const;
 
  private:
   ~LookupCacheV4() {}
 
   virtual int Ver() const override { return VER; }
 
+  virtual nsresult LoadLegacyFile() override;
+
   struct Header {
     uint32_t magic;
     uint32_t version;
   };
 
   nsresult SanityCheck(const Header& aHeader);
   nsresult VerifyCRC32(nsCOMPtr<nsIInputStream>& aIn);
   nsresult CleanOldPrefixSet();