Merge "Skip stashing source blocks in verify mode"
diff --git a/otafault/Android.mk b/otafault/Android.mk
index 52d7067..50e385e 100644
--- a/otafault/Android.mk
+++ b/otafault/Android.mk
@@ -19,9 +19,10 @@
 include $(CLEAR_VARS)
 
 otafault_static_libs := \
+    libbase \
     libminzip \
-    libselinux \
-    libz
+    libz \
+    libselinux
 
 LOCAL_SRC_FILES := config.cpp ota_io.cpp
 LOCAL_MODULE := libotafault
diff --git a/otafault/config.cpp b/otafault/config.cpp
index c87f9a6..b456739 100644
--- a/otafault/config.cpp
+++ b/otafault/config.cpp
@@ -20,6 +20,8 @@
 #include <stdio.h>
 #include <unistd.h>
 
+#include <android-base/stringprintf.h>
+
 #include "minzip/Zip.h"
 #include "config.h"
 #include "ota_io.h"
@@ -27,12 +29,10 @@
 #define OTAIO_MAX_FNAME_SIZE 128
 
 static ZipArchive* archive;
-static std::map<const char*, bool> should_inject_cache;
+static std::map<std::string, bool> should_inject_cache;
 
-static const char* get_type_path(const char* io_type) {
-    char* path = (char*)calloc(strlen(io_type) + strlen(OTAIO_BASE_DIR) + 2, sizeof(char));
-    sprintf(path, "%s/%s", OTAIO_BASE_DIR, io_type);
-    return path;
+static std::string get_type_path(const char* io_type) {
+    return android::base::StringPrintf("%s/%s", OTAIO_BASE_DIR, io_type);
 }
 
 void ota_io_init(ZipArchive* za) {
@@ -41,13 +41,17 @@
 }
 
 bool should_fault_inject(const char* io_type) {
-    if (should_inject_cache.find(io_type) != should_inject_cache.end()) {
-        return should_inject_cache[io_type];
+    // archive will be NULL if we used an entry point other
+    // than updater/updater.cpp:main
+    if (archive == NULL) {
+        return false;
     }
-    const char* type_path = get_type_path(io_type);
-    const ZipEntry* entry = mzFindZipEntry(archive, type_path);
+    const std::string type_path = get_type_path(io_type);
+    if (should_inject_cache.find(type_path) != should_inject_cache.end()) {
+        return should_inject_cache[type_path];
+    }
+    const ZipEntry* entry = mzFindZipEntry(archive, type_path.c_str());
     should_inject_cache[type_path] = entry != nullptr;
-    free((void*)type_path);
     return entry != NULL;
 }
 
@@ -56,10 +60,10 @@
 }
 
 std::string fault_fname(const char* io_type) {
-    const char* type_path = get_type_path(io_type);
-    char* fname = (char*) calloc(OTAIO_MAX_FNAME_SIZE, sizeof(char));
-    const ZipEntry* entry = mzFindZipEntry(archive, type_path);
-    mzReadZipEntry(archive, entry, fname, OTAIO_MAX_FNAME_SIZE);
-    free((void*)type_path);
-    return std::string(fname);
+    std::string type_path = get_type_path(io_type);
+    std::string fname;
+    fname.resize(OTAIO_MAX_FNAME_SIZE);
+    const ZipEntry* entry = mzFindZipEntry(archive, type_path.c_str());
+    mzReadZipEntry(archive, entry, &fname[0], OTAIO_MAX_FNAME_SIZE);
+    return fname;
 }
diff --git a/recovery.cpp b/recovery.cpp
index 6781fa4..4ee835c 100644
--- a/recovery.cpp
+++ b/recovery.cpp
@@ -59,7 +59,6 @@
 struct selabel_handle *sehandle;
 
 static const struct option OPTIONS[] = {
-  { "send_intent", required_argument, NULL, 'i' },
   { "update_package", required_argument, NULL, 'u' },
   { "retry_count", required_argument, NULL, 'n' },
   { "wipe_data", no_argument, NULL, 'w' },
@@ -77,7 +76,6 @@
 
 static const char *CACHE_LOG_DIR = "/cache/recovery";
 static const char *COMMAND_FILE = "/cache/recovery/command";
-static const char *INTENT_FILE = "/cache/recovery/intent";
 static const char *LOG_FILE = "/cache/recovery/log";
 static const char *LAST_INSTALL_FILE = "/cache/recovery/last_install";
 static const char *LOCALE_FILE = "/cache/recovery/last_locale";
@@ -107,10 +105,8 @@
  * The recovery tool communicates with the main system through /cache files.
  *   /cache/recovery/command - INPUT - command line for tool, one arg per line
  *   /cache/recovery/log - OUTPUT - combined log file from recovery run(s)
- *   /cache/recovery/intent - OUTPUT - intent that was passed in
  *
  * The arguments which may be supplied in the recovery.command file:
- *   --send_intent=anystring - write the text out to recovery.intent
  *   --update_package=path - verify install an OTA package file
  *   --wipe_data - erase user data (and cache), then reboot
  *   --wipe_cache - wipe cache (but not user data), then reboot
@@ -467,22 +463,10 @@
 }
 
 // clear the recovery command and prepare to boot a (hopefully working) system,
-// copy our log file to cache as well (for the system to read), and
-// record any intent we were asked to communicate back to the system.
-// this function is idempotent: call it as many times as you like.
+// copy our log file to cache as well (for the system to read). This function is
+// idempotent: call it as many times as you like.
 static void
-finish_recovery(const char *send_intent) {
-    // By this point, we're ready to return to the main system...
-    if (send_intent != NULL && has_cache) {
-        FILE *fp = fopen_path(INTENT_FILE, "w");
-        if (fp == NULL) {
-            LOGE("Can't open %s\n", INTENT_FILE);
-        } else {
-            fputs(send_intent, fp);
-            check_and_fclose(fp, INTENT_FILE);
-        }
-    }
-
+finish_recovery() {
     // Save the locale to cache, so if recovery is next started up
     // without a --locale argument (eg, directly from the bootloader)
     // it will use the last-known locale.
@@ -947,7 +931,7 @@
 static Device::BuiltinAction
 prompt_and_wait(Device* device, int status) {
     for (;;) {
-        finish_recovery(NULL);
+        finish_recovery();
         switch (status) {
             case INSTALL_SUCCESS:
             case INSTALL_NONE:
@@ -1189,7 +1173,6 @@
 
     get_args(&argc, &argv);
 
-    const char *send_intent = NULL;
     const char *update_package = NULL;
     bool should_wipe_data = false;
     bool should_wipe_cache = false;
@@ -1203,7 +1186,6 @@
     int arg;
     while ((arg = getopt_long(argc, argv, "", OPTIONS, NULL)) != -1) {
         switch (arg) {
-        case 'i': send_intent = optarg; break;
         case 'n': android::base::ParseInt(optarg, &retry_count, 0); break;
         case 'u': update_package = optarg; break;
         case 'w': should_wipe_data = true; break;
@@ -1390,7 +1372,7 @@
     }
 
     // Save logs and clean up before rebooting or shutting down.
-    finish_recovery(send_intent);
+    finish_recovery();
 
     switch (after) {
         case Device::SHUTDOWN: