bug 505120 - updater does not copy permissions correctly r=robstrong
authorBrad Lassey <blassey@mozilla.com>
Mon, 20 Jul 2009 18:53:46 -0400
changeset 30474 a568cdcd510d572e9035b554c7d028de5503d7a8
parent 30473 02ff5f05a1a66d2b2bc015bf7fefd04790ec0a3e
child 30475 a28347623d0f7332186df1c5a84b016864fb66ba
push idunknown
push userunknown
push dateunknown
reviewersrobstrong
bugs505120
milestone1.9.2a1pre
bug 505120 - updater does not copy permissions correctly r=robstrong
toolkit/mozapps/update/src/updater/updater.cpp
--- a/toolkit/mozapps/update/src/updater/updater.cpp
+++ b/toolkit/mozapps/update/src/updater/updater.cpp
@@ -404,20 +404,30 @@ static int ensure_remove(const char *pat
 {
   ensure_write_permissions(path);
   int rv = remove(path);
   if (rv)
     LOG(("remove failed: %d,%d (%s)\n", rv, errno, path));
   return rv;
 }
 
-static FILE* ensure_open(const char *path, char* flags)
+static FILE* ensure_open(const char *path, char* flags, int options)
 {
   ensure_write_permissions(path);
-  return fopen(path, flags);
+  FILE* f = fopen(path, flags);
+  if (chmod(path, options) != 0) {
+    fclose(f);
+    return NULL;
+  }
+  struct stat ss;
+  if (stat(path, &ss) != 0 || ss.st_mode != options) {
+    fclose(f);
+    return NULL;
+  }
+  return f;
 }
 
 // Ensure that the directory containing this file exists.
 static int ensure_parent_dir(const char *path)
 {
   int rv = OK;
 
   char *slash = (char *) strrchr(path, '/');
@@ -448,17 +458,17 @@ static int copy_file(const char *spath, 
   struct stat ss;
 
   AutoFile sfile = fopen(spath, "rb");
   if (sfile == NULL || fstat(fileno(sfile), &ss)) {
     LOG(("copy_file: failed to open or stat: %p,%s,%d\n", sfile, spath, errno));
     return READ_ERROR;
   }
 
-  AutoFile dfile = ensure_open(dpath, "wb+"); 
+  AutoFile dfile = ensure_open(dpath, "wb+", ss.st_mode); 
   if (dfile == NULL) {
     LOG(("copy_file: failed to open: %s,%d\n", dpath, errno));
     return WRITE_ERROR;
   }
 
   char buf[BUFSIZ];
   int sc;
   while ((sc = fread(buf, 1, sizeof(buf), sfile)) > 0) {
@@ -903,17 +913,17 @@ PatchFile::Execute()
   int rv = backup_create(mFile);
   if (rv)
     return rv;
 
   rv = ensure_remove(mFile);
   if (rv)
     return WRITE_ERROR;
 
-  AutoFile ofile = ensure_open(mFile, "wb+");
+  AutoFile ofile = ensure_open(mFile, "wb+", ss.st_mode);
   if (ofile == NULL)
     return WRITE_ERROR;
 
   return MBS_ApplyPatch(&header, pfile, buf, ofile);
 }
 
 void
 PatchFile::Finish(int status)