allow recovery packages to wipe cache

updater now has a function "wipe_cache();" which causes recovery to
wipe the cache partition after the successful installation of the
package.  Move log copying around a bit so logs and the last_install
flag file are copied to cache after it's wiped.

Bug: 5314244
Change-Id: Id35a9eb6dcd626c8f3a3a0076074f462ed3d44bd
diff --git a/install.c b/install.c
index 707cda4..9d7595e 100644
--- a/install.c
+++ b/install.c
@@ -36,11 +36,9 @@
 #define ASSUMED_UPDATE_BINARY_NAME  "META-INF/com/google/android/update-binary"
 #define PUBLIC_KEYS_FILE "/res/keys"
 
-static const char *LAST_INSTALL_FILE = "/cache/recovery/last_install";
-
 // If the package contains an update binary, extract it and run it.
 static int
-try_update_binary(const char *path, ZipArchive *zip) {
+try_update_binary(const char *path, ZipArchive *zip, int* wipe_cache) {
     const ZipEntry* binary_entry =
             mzFindZipEntry(zip, ASSUMED_UPDATE_BINARY_NAME);
     if (binary_entry == NULL) {
@@ -54,7 +52,7 @@
     if (fd < 0) {
         mzCloseZipArchive(zip);
         LOGE("Can't make %s\n", binary);
-        return 1;
+        return INSTALL_ERROR;
     }
     bool ok = mzExtractZipEntryToFile(zip, binary_entry, fd);
     close(fd);
@@ -62,7 +60,7 @@
 
     if (!ok) {
         LOGE("Can't copy %s\n", ASSUMED_UPDATE_BINARY_NAME);
-        return 1;
+        return INSTALL_ERROR;
     }
 
     int pipefd[2];
@@ -119,6 +117,8 @@
     }
     close(pipefd[1]);
 
+    *wipe_cache = 0;
+
     char buffer[1024];
     FILE* from_child = fdopen(pipefd[0], "r");
     while (fgets(buffer, sizeof(buffer), from_child) != NULL) {
@@ -145,6 +145,8 @@
             } else {
                 ui_print("\n");
             }
+        } else if (strcmp(command, "wipe_cache") == 0) {
+            *wipe_cache = 1;
         } else {
             LOGE("unknown command [%s]\n", command);
         }
@@ -236,7 +238,7 @@
 }
 
 static int
-really_install_package(const char *path)
+really_install_package(const char *path, int* wipe_cache)
 {
     ui_set_background(BACKGROUND_ICON_INSTALLING);
     ui_print("Finding update package...\n");
@@ -285,25 +287,24 @@
     /* Verify and install the contents of the package.
      */
     ui_print("Installing update...\n");
-    return try_update_binary(path, &zip);
+    return try_update_binary(path, &zip, wipe_cache);
 }
 
 int
-install_package(const char* path)
+install_package(const char* path, int* wipe_cache, const char* install_file)
 {
-    FILE* install_log = fopen_path(LAST_INSTALL_FILE, "w");
+    FILE* install_log = fopen_path(install_file, "w");
     if (install_log) {
         fputs(path, install_log);
         fputc('\n', install_log);
     } else {
         LOGE("failed to open last_install: %s\n", strerror(errno));
     }
-    int result = really_install_package(path);
+    int result = really_install_package(path, wipe_cache);
     if (install_log) {
         fputc(result == INSTALL_SUCCESS ? '1' : '0', install_log);
         fputc('\n', install_log);
         fclose(install_log);
-        chmod(LAST_INSTALL_FILE, 0644);
     }
     return result;
 }