Bug 765598 - Remove newly created MozUpdater folders in tmp on post update. r=ehsan
authorBrian R. Bondy <netzen@gmail.com>
Thu, 25 Oct 2012 22:30:34 -0400
changeset 111611 3a68d4377cd815efd7973ca515e8dbd3ce45a602
parent 111610 fb30db0fa0aa0782b4fc8f5e2de7cca299994ed3
child 111612 612a40a222d7cd9cca0d50b80da075fac00b5c41
push id93
push usernmatsakis@mozilla.com
push dateWed, 31 Oct 2012 21:26:57 +0000
reviewersehsan
bugs765598
milestone19.0a1
Bug 765598 - Remove newly created MozUpdater folders in tmp on post update. r=ehsan
toolkit/mozapps/update/nsUpdateService.js
toolkit/xre/nsUpdateDriver.cpp
--- a/toolkit/mozapps/update/nsUpdateService.js
+++ b/toolkit/mozapps/update/nsUpdateService.js
@@ -747,16 +747,36 @@ function shouldUseService() {
  */
 function writeVersionFile(dir, version) {
   var versionFile = dir.clone();
   versionFile.append(FILE_UPDATE_VERSION);
   writeStringToFile(versionFile, version);
 }
 
 /**
+ * Removes the MozUpdater folders that bgupdates/staged updates creates.
+ */
+function cleanUpMozUpdaterDirs() {
+  try {
+    var tmpDir = Components.classes["@mozilla.org/file/directory_service;1"].
+                            getService(Components.interfaces.nsIProperties).
+                            get("TmpD", Components.interfaces.nsIFile);
+    // All temp unique subfolders are inside MozUpdater, delete it recursively.
+    var mozUpdaterDir = tmpDir.clone();
+    mozUpdaterDir.append("MozUpdater");
+    if (mozUpdaterDir.exists()) {
+      LOG("cleanUpMozUpdaterDirs - Cleaning MozUpdater folder");
+      mozUpdaterDir.remove(true);
+    }
+  } catch (e) {
+    LOG("cleanUpMozUpdaterDirs - Exception: " + e);
+  }
+}
+
+/**
  * Removes the contents of the Updates Directory
  *
  * @param aBackgroundUpdate Whether the update has been performed in the
  *        background.  If this is true, we move the update log file to the
  *        updated directory, so that it survives replacing the directories
  *        later on.
  */
 function cleanUpUpdatesDir(aBackgroundUpdate) {
@@ -1692,16 +1712,19 @@ UpdateService.prototype = {
         }
       }
 
       // Something went wrong with the patch application process.
       handleFallbackToCompleteUpdate(update, false);
 
       prompter.showUpdateError(update);
     }
+
+    // Now trash the MozUpdater folders which staged/bgupdates uses.
+    cleanUpMozUpdaterDirs();
   },
 
   /**
    * Submit a telemetry ping with the boolean value of a pref for a histogram
    *
    * @param  pref
    *         The preference to report
    * @param  histogram
--- a/toolkit/xre/nsUpdateDriver.cpp
+++ b/toolkit/xre/nsUpdateDriver.cpp
@@ -371,17 +371,21 @@ SwitchToUpdatedApp(nsIFile *greDir, nsIF
     LOG(("failed getting a temp dir\n"));
     return;
   }
 
   // Try to create our own new temp directory in case there is already an
   // updater binary in the OS temporary location which we cannot write to.
   // Note that we don't check for errors here, as if this directory can't
   // be created, the following CopyUpdaterIntoUpdateDir call will fail.
+  // We create the unique directory inside a subfolder of MozUpdater instead
+  // of directly in the temp directory so we can efficiently delete everything
+  // after updates.
   tmpDir->Append(NS_LITERAL_STRING("MozUpdater"));
+  tmpDir->Append(NS_LITERAL_STRING("bgupdate"));
   tmpDir->CreateUnique(nsIFile::DIRECTORY_TYPE, 0755);
 
   nsCOMPtr<nsIFile> updater;
   if (!CopyUpdaterIntoUpdateDir(greDir, appDir, tmpDir, updater)) {
     LOG(("failed copying updater\n"));
     return;
   }