Bug 1529894 - Change zip logging initialization. r=aklotz
authorMike Hommey <mh+mozilla@glandium.org>
Thu, 07 Mar 2019 22:54:53 +0000
changeset 520935 e19e5857c7a8dc26f96df4e9487c6d0af86863f8
parent 520934 920cfaa83fd8ea160252f285fd61e1accd90fe1c
child 520936 346002ff619eef0712be55594102460b40f44db1
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)
reviewersaklotz
bugs1529894
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 1529894 - Change zip logging initialization. r=aklotz Instead of checking the MOZ_JAR_LOG_FILE for each log entry, only check it once, and only check whether to log once per archive rather than once per item. Differential Revision: https://phabricator.services.mozilla.com/D21655
modules/libjar/nsZipArchive.cpp
--- a/modules/libjar/nsZipArchive.cpp
+++ b/modules/libjar/nsZipArchive.cpp
@@ -73,24 +73,18 @@ static const uint16_t kSyntheticTime = 0
 static const uint16_t kSyntheticDate = (1 + (1 << 5) + (0 << 9));
 
 static uint16_t xtoint(const uint8_t *ii);
 static uint32_t xtolong(const uint8_t *ll);
 static uint32_t HashName(const char *aName, uint16_t nameLen);
 
 class ZipArchiveLogger {
  public:
-  void Write(const nsACString &zip, const char *entry) const {
-    if (!XRE_IsParentProcess()) {
-      return;
-    }
+  void Init(const char *env) {
     if (!fd) {
-      char *env = PR_GetEnv("MOZ_JAR_LOG_FILE");
-      if (!env) return;
-
       nsCOMPtr<nsIFile> logFile;
       nsresult rv = NS_NewLocalFile(NS_ConvertUTF8toUTF16(env), false,
                                     getter_AddRefs(logFile));
       if (NS_FAILED(rv)) return;
 
       // Create the log file and its parent directory (in case it doesn't exist)
       rv = logFile->Create(nsIFile::NORMAL_FILE_TYPE, 0644);
       if (NS_FAILED(rv)) return;
@@ -111,21 +105,26 @@ class ZipArchiveLogger {
       if (!file) return;
 #else
       rv = logFile->OpenNSPRFileDesc(PR_WRONLY | PR_CREATE_FILE | PR_APPEND,
                                      0644, &file);
       if (NS_FAILED(rv)) return;
 #endif
       fd = file;
     }
-    nsCString buf(zip);
-    buf.Append(' ');
-    buf.Append(entry);
-    buf.Append('\n');
-    PR_Write(fd, buf.get(), buf.Length());
+  }
+
+  void Write(const nsACString &zip, const char *entry) const {
+    if (fd) {
+      nsCString buf(zip);
+      buf.Append(' ');
+      buf.Append(entry);
+      buf.Append('\n');
+      PR_Write(fd, buf.get(), buf.Length());
+    }
   }
 
   void AddRef() {
     MOZ_ASSERT(refCnt >= 0);
     ++refCnt;
   }
 
   void Release() {
@@ -133,17 +132,17 @@ class ZipArchiveLogger {
     if ((0 == --refCnt) && fd) {
       PR_Close(fd);
       fd = nullptr;
     }
   }
 
  private:
   int refCnt;
-  mutable PRFileDesc *fd;
+  PRFileDesc *fd;
 };
 
 static ZipArchiveLogger zipLog;
 
 //***********************************************************
 // For every inflation the following allocations are done:
 // malloc(1 * 9520)
 // malloc(32768 * 1)
@@ -331,17 +330,23 @@ nsZipHandle::~nsZipHandle() {
 //  nsZipArchive::OpenArchive
 //---------------------------------------------
 nsresult nsZipArchive::OpenArchive(nsZipHandle *aZipHandle, PRFileDesc *aFd) {
   mFd = aZipHandle;
 
   //-- get table of contents for archive
   nsresult rv = BuildFileList(aFd);
   if (NS_SUCCEEDED(rv)) {
-    if (aZipHandle->mFile) aZipHandle->mFile.GetURIString(mURI);
+    if (aZipHandle->mFile && XRE_IsParentProcess()) {
+      static char *env = PR_GetEnv("MOZ_JAR_LOG_FILE");
+      if (env) {
+        zipLog.Init(env);
+        aZipHandle->mFile.GetURIString(mURI);
+      }
+    }
   }
   return rv;
 }
 
 nsresult nsZipArchive::OpenArchive(nsIFile *aFile) {
   RefPtr<nsZipHandle> handle;
 #if defined(XP_WIN)
   mozilla::AutoFDClose fd;
@@ -422,17 +427,19 @@ nsZipItem *nsZipArchive::GetItem(const c
     }
     MOZ_WIN_MEM_TRY_BEGIN
     nsZipItem *item = mFiles[HashName(aEntryName, len)];
     while (item) {
       if ((len == item->nameLength) &&
           (!memcmp(aEntryName, item->Name(), len))) {
         // Successful GetItem() is a good indicator that the file is about to be
         // read
-        zipLog.Write(mURI, aEntryName);
+        if (mURI.Length()) {
+          zipLog.Write(mURI, aEntryName);
+        }
         return item;  //-- found it
       }
       item = item->next;
     }
     MOZ_WIN_MEM_TRY_CATCH(return nullptr)
   }
   return nullptr;
 }