Bug 616775 - Use OpenNSPRFileDesc instead of OpenANSIFileDesc in nsUpdateDriver.cpp. r=dtownsend, a=approval2.0
authorRobert Strong <robert.bugzilla@gmail.com>
Tue, 07 Dec 2010 12:06:22 -0800
changeset 58791 dbb99009d49e0550ae6c04eae3cb041f7a1ad667
parent 58790 12bc6188524ca7aaeb176fd4f403ee1242cc705c
child 58792 df921a9d5124fc20638f87761a80f8a9d7e3dc93
push id1
push usershaver@mozilla.com
push dateTue, 04 Jan 2011 17:58:04 +0000
reviewersdtownsend, approval2
bugs616775
milestone2.0b8pre
Bug 616775 - Use OpenNSPRFileDesc instead of OpenANSIFileDesc in nsUpdateDriver.cpp. r=dtownsend, a=approval2.0
toolkit/xre/nsUpdateDriver.cpp
--- a/toolkit/xre/nsUpdateDriver.cpp
+++ b/toolkit/xre/nsUpdateDriver.cpp
@@ -188,82 +188,81 @@ static PRBool
 GetStatusFile(nsIFile *dir, nsCOMPtr<nsILocalFile> &result)
 {
   return GetFile(dir, NS_LITERAL_CSTRING("update.status"), result);
 }
 
 static PRBool
 IsPending(nsILocalFile *statusFile)
 {
-  nsresult rv;
-
-  FILE *fp;
-  rv = statusFile->OpenANSIFileDesc("r", &fp);
+  PRFileDesc *fd = nsnull;
+  nsresult rv = statusFile->OpenNSPRFileDesc(PR_RDONLY, 0660, &fd);
   if (NS_FAILED(rv))
     return PR_FALSE;
 
   char buf[32];
-  char *result = fgets(buf, sizeof(buf), fp);
-  fclose(fp);
-  if (!result)
+  const PRInt32 n = PR_Read(fd, buf, sizeof(buf));
+  PR_Close(fd);
+
+  if (n < 0)
     return PR_FALSE;
   
   const char kPending[] = "pending";
   return (strncmp(buf, kPending, sizeof(kPending) - 1) == 0);
 }
 
 static PRBool
-SetStatus(nsILocalFile *statusFile, const char *status)
+SetStatusApplying(nsILocalFile *statusFile)
 {
-  FILE *fp;
-  nsresult rv = statusFile->OpenANSIFileDesc("w", &fp);
+  PRFileDesc *fd = nsnull;
+  nsresult rv = statusFile->OpenNSPRFileDesc(PR_WRONLY, 0660, &fd);
   if (NS_FAILED(rv))
     return PR_FALSE;
 
-  fprintf(fp, "%s\n", status);
-  fclose(fp);
+  static const char kApplying[] = "Applying\n";
+  PR_Write(fd, kApplying, sizeof(kApplying) - 1);
+  PR_Close(fd);
+
   return PR_TRUE;
 }
 
 static PRBool
 GetVersionFile(nsIFile *dir, nsCOMPtr<nsILocalFile> &result)
 {
   return GetFile(dir, NS_LITERAL_CSTRING("update.version"), result);
 }
 
 // Compares the current application version with the update's application
 // version.
 static PRBool
 IsOlderVersion(nsILocalFile *versionFile, const char *&appVersion)
 {
-  nsresult rv;
-
-  FILE *fp;
-  rv = versionFile->OpenANSIFileDesc("r", &fp);
+  PRFileDesc *fd = nsnull;
+  nsresult rv = versionFile->OpenNSPRFileDesc(PR_RDONLY, 0660, &fd);
   if (NS_FAILED(rv))
     return PR_TRUE;
 
   char buf[32];
-  char *result = fgets(buf, sizeof(buf), fp);
-  fclose(fp);
-  if (!result)
-    return PR_TRUE;
+  const PRInt32 n = PR_Read(fd, buf, sizeof(buf));
+  PR_Close(fd);
 
-  // Trim off any trailing newline
-  int len = strlen(result);
-  if (len > 0 && result[len - 1] == '\n')
-    result[len - 1] = '\0';
+  if (n < 0)
+    return PR_FALSE;
+
+  // Trim off the trailing newline
+  if (buf[n - 1] == '\n')
+    buf[n - 1] = '\0';
 
   // If the update xml doesn't provide the application version the file will
   // contain the string "null" and it is assumed that the update is not older.
   const char kNull[] = "null";
   if (strncmp(buf, kNull, sizeof(kNull) - 1) == 0)
     return PR_FALSE;
 
-  if (NS_CompareVersions(appVersion, result) > 0)
+  if (NS_CompareVersions(appVersion, buf) > 0)
     return PR_TRUE;
 
   return PR_FALSE;
 }
 
 static PRBool
 CopyFileIntoUpdateDir(nsIFile *parentDir, const char *leafName, nsIFile *updateDir)
 {
@@ -428,17 +427,17 @@ ApplyUpdate(nsIFile *greDir, nsIFile *up
     return;
 
   // Get the current working directory.
   char workingDirPath[MAXPATHLEN];
   rv = GetCurrentWorkingDir(workingDirPath, sizeof(workingDirPath));
   if (NS_FAILED(rv))
     return;
 
-  if (!SetStatus(statusFile, "applying")) {
+  if (!SetStatusApplying(statusFile)) {
     LOG(("failed setting status to 'applying'\n"));
     return;
   }
 
   // Construct the PID argument for this process.  If we are using execv, then
   // we pass "0" which is then ignored by the updater.
 #if defined(USE_EXECV)
   NS_NAMED_LITERAL_CSTRING(pid, "0");