close update package before installing; allow remount

Close the update package before invoking the binary, to allow the
installer to unmount /cache if it wants to.  Add a function to allow
remounting of a mount as read-only.

Change-Id: Idfcc96c3da66083295177f729263560be58034e4
diff --git a/install.c b/install.c
index 35ba6ca..40f182d 100644
--- a/install.c
+++ b/install.c
@@ -42,6 +42,7 @@
     const ZipEntry* binary_entry =
             mzFindZipEntry(zip, ASSUMED_UPDATE_BINARY_NAME);
     if (binary_entry == NULL) {
+        mzCloseZipArchive(zip);
         return INSTALL_CORRUPT;
     }
 
@@ -49,11 +50,13 @@
     unlink(binary);
     int fd = creat(binary, 0755);
     if (fd < 0) {
+        mzCloseZipArchive(zip);
         LOGE("Can't make %s\n", binary);
         return 1;
     }
     bool ok = mzExtractZipEntryToFile(zip, binary_entry, fd);
     close(fd);
+    mzCloseZipArchive(zip);
 
     if (!ok) {
         LOGE("Can't copy %s\n", ASSUMED_UPDATE_BINARY_NAME);
@@ -298,6 +301,5 @@
     /* Verify and install the contents of the package.
      */
     int status = handle_update_package(path, &zip);
-    mzCloseZipArchive(&zip);
     return status;
 }
diff --git a/mtdutils/mounts.c b/mtdutils/mounts.c
index 2ab3ff6..c90fc8a 100644
--- a/mtdutils/mounts.c
+++ b/mtdutils/mounts.c
@@ -212,3 +212,11 @@
     }
     return ret;
 }
+
+int
+remount_read_only(const MountedVolume* volume)
+{
+    return mount(volume->device, volume->mount_point, volume->filesystem,
+                 MS_NOATIME | MS_NODEV | MS_NODIRATIME |
+                 MS_RDONLY | MS_REMOUNT, 0);
+}
diff --git a/mtdutils/mounts.h b/mtdutils/mounts.h
index 2e2765a..30b2927 100644
--- a/mtdutils/mounts.h
+++ b/mtdutils/mounts.h
@@ -28,4 +28,6 @@
 
 int unmount_mounted_volume(const MountedVolume *volume);
 
+int remount_read_only(const MountedVolume* volume);
+
 #endif  // MTDUTILS_MOUNTS_H_
diff --git a/mtdutils/mtdutils.c b/mtdutils/mtdutils.c
index d16f44e..48d5ea9 100644
--- a/mtdutils/mtdutils.c
+++ b/mtdutils/mtdutils.c
@@ -455,7 +455,7 @@
             if (retry > 0) {
                 fprintf(stderr, "mtd: wrote block after %d retries\n", retry);
             }
-            fprintf(stderr, "mtd: successfully wrote block at %x\n", pos);
+            fprintf(stderr, "mtd: successfully wrote block at %llx\n", pos);
             return 0;  // Success!
         }
 
diff --git a/updater/updater.c b/updater/updater.c
index 6537a94..aa626d2 100644
--- a/updater/updater.c
+++ b/updater/updater.c
@@ -136,7 +136,9 @@
         free(result);
     }
 
-    mzCloseZipArchive(&za);
+    if (updater_info.package_zip) {
+        mzCloseZipArchive(updater_info.package_zip);
+    }
     free(script);
 
     return 0;