Small clean ups for issues raised in reviews of fastdeploy

Removed support for -f shortcut flag as this conflicts with some package manager flags
Renamed use_localagent global to match conventions
Fixed case where tmp files were created unnecessarily
Removed dead code (delete_host_file)
Cleaned up multiple layers of error handling by using fatal() as soon as error conditions occur

Fix: 113631900

Test: mm
Test: adb install -r --fastdeploy --nostreaming --force-agent --local-agent ~/example_apks/example.apk
Test: adb install -r --fastdeploy --no-streaming --force-agent --local-agent ~/example_apks/example.apk
Test: observe that fast deploy works as usual

Test: adb install -r -f --nostreaming --force-agent --local-agent ~/example_apks/example.apk
Test: adb install -r -f --no-streaming --force-agent --local-agent ~/example_apks/example.apk
Test: observe that fast deploy is no longer invoked by -f
Change-Id: Ic719df1003ac319e48b32f7f377f6f91d17f6a6f
(cherry picked from commit 0584689beaff604ceeccaf706dc368213d07b977)
diff --git a/adb/client/fastdeploy.cpp b/adb/client/fastdeploy.cpp
index 2914836..d3f35c8 100644
--- a/adb/client/fastdeploy.cpp
+++ b/adb/client/fastdeploy.cpp
@@ -30,7 +30,7 @@
 
 static constexpr const char* kDeviceAgentPath = "/data/local/tmp/";
 
-static bool use_localagent = false;
+static bool g_use_localagent = false;
 
 long get_agent_version() {
     std::vector<char> versionOutputBuffer;
@@ -61,48 +61,36 @@
     return api_level;
 }
 
-void fastdeploy_set_local_agent(bool set_use_localagent) {
-    use_localagent = set_use_localagent;
+void fastdeploy_set_local_agent(bool use_localagent) {
+    g_use_localagent = use_localagent;
 }
 
 // local_path - must start with a '/' and be relative to $ANDROID_PRODUCT_OUT
-static bool get_agent_component_host_path(const char* local_path, const char* sdk_path,
-                                          std::string* output_path) {
+static std::string get_agent_component_host_path(const char* local_path, const char* sdk_path) {
     std::string adb_dir = android::base::GetExecutableDirectory();
     if (adb_dir.empty()) {
-        return false;
+        fatal("Could not determine location of adb!");
     }
 
-    if (use_localagent) {
+    if (g_use_localagent) {
         const char* product_out = getenv("ANDROID_PRODUCT_OUT");
         if (product_out == nullptr) {
-            return false;
+            fatal("Could not locate %s because $ANDROID_PRODUCT_OUT is not defined", local_path);
         }
-        *output_path = android::base::StringPrintf("%s%s", product_out, local_path);
-        return true;
+        return android::base::StringPrintf("%s%s", product_out, local_path);
     } else {
-        *output_path = adb_dir + sdk_path;
-        return true;
+        return adb_dir + sdk_path;
     }
-    return false;
 }
 
 static bool deploy_agent(bool checkTimeStamps) {
     std::vector<const char*> srcs;
-    std::string agent_jar_path;
-    if (get_agent_component_host_path("/system/framework/deployagent.jar", "/deployagent.jar",
-                                      &agent_jar_path)) {
-        srcs.push_back(agent_jar_path.c_str());
-    } else {
-        return false;
-    }
-
-    std::string agent_sh_path;
-    if (get_agent_component_host_path("/system/bin/deployagent", "/deployagent", &agent_sh_path)) {
-        srcs.push_back(agent_sh_path.c_str());
-    } else {
-        return false;
-    }
+    std::string jar_path =
+            get_agent_component_host_path("/system/framework/deployagent.jar", "/deployagent.jar");
+    std::string script_path =
+            get_agent_component_host_path("/system/bin/deployagent", "/deployagent");
+    srcs.push_back(jar_path.c_str());
+    srcs.push_back(script_path.c_str());
 
     if (do_sync_push(srcs, kDeviceAgentPath, checkTimeStamps)) {
         // on windows the shell script might have lost execute permission
@@ -111,24 +99,24 @@
         std::string chmodCommand =
                 android::base::StringPrintf(kChmodCommandPattern, kDeviceAgentPath);
         int ret = send_shell_command(chmodCommand);
-        return (ret == 0);
+        if (ret != 0) {
+            fatal("Error executing %s returncode: %d", chmodCommand.c_str(), ret);
+        }
     } else {
-        return false;
+        fatal("Error pushing agent files to device");
     }
+
+    return true;
 }
 
-bool update_agent(FastDeploy_AgentUpdateStrategy agentUpdateStrategy) {
+void update_agent(FastDeploy_AgentUpdateStrategy agentUpdateStrategy) {
     long agent_version = get_agent_version();
     switch (agentUpdateStrategy) {
         case FastDeploy_AgentUpdateAlways:
-            if (deploy_agent(false) == false) {
-                return false;
-            }
+            deploy_agent(false);
             break;
         case FastDeploy_AgentUpdateNewerTimeStamp:
-            if (deploy_agent(true) == false) {
-                return false;
-            }
+            deploy_agent(true);
             break;
         case FastDeploy_AgentUpdateDifferentVersion:
             if (agent_version != kRequiredAgentVersion) {
@@ -138,19 +126,20 @@
                     printf("Device agent version is (%ld), (%ld) is required, re-deploying\n",
                            agent_version, kRequiredAgentVersion);
                 }
-                if (deploy_agent(false) == false) {
-                    return false;
-                }
+                deploy_agent(false);
             }
             break;
     }
 
     agent_version = get_agent_version();
-    return (agent_version == kRequiredAgentVersion);
+    if (agent_version != kRequiredAgentVersion) {
+        fatal("After update agent version remains incorrect! Expected %ld but version is %ld",
+              kRequiredAgentVersion, agent_version);
+    }
 }
 
 static std::string get_aapt2_path() {
-    if (use_localagent) {
+    if (g_use_localagent) {
         // This should never happen on a Windows machine
         const char* host_out = getenv("ANDROID_HOST_OUT");
         if (host_out == nullptr) {
@@ -186,26 +175,24 @@
 }
 
 // output is required to point to a valid output string (non-null)
-static bool get_packagename_from_apk(const char* apkPath, std::string* output) {
+static std::string get_packagename_from_apk(const char* apkPath) {
     const char* kAapt2DumpNameCommandPattern = R"(%s dump packagename "%s")";
     std::string aapt2_path_string = get_aapt2_path();
     std::string getPackagenameCommand = android::base::StringPrintf(
             kAapt2DumpNameCommandPattern, aapt2_path_string.c_str(), apkPath);
 
-    if (system_capture(getPackagenameCommand.c_str(), *output) == 0) {
-        // strip any line end characters from the output
-        *output = android::base::Trim(*output);
-        return true;
+    std::string package_name;
+    int exit_code = system_capture(getPackagenameCommand.c_str(), package_name);
+    if (exit_code != 0) {
+        fatal("Error executing '%s' exitcode: %d", getPackagenameCommand.c_str(), exit_code);
     }
-    return false;
+
+    // strip any line end characters from the output
+    return android::base::Trim(package_name);
 }
 
 int extract_metadata(const char* apkPath, FILE* outputFp) {
-    std::string packageName;
-    if (get_packagename_from_apk(apkPath, &packageName) == false) {
-        return -1;
-    }
-
+    std::string packageName = get_packagename_from_apk(apkPath);
     const char* kAgentExtractCommandPattern = "/data/local/tmp/deployagent extract %s";
     std::string extractCommand =
             android::base::StringPrintf(kAgentExtractCommandPattern, packageName.c_str());
@@ -223,7 +210,7 @@
 }
 
 static std::string get_patch_generator_command() {
-    if (use_localagent) {
+    if (g_use_localagent) {
         // This should never happen on a Windows machine
         const char* host_out = getenv("ANDROID_HOST_OUT");
         if (host_out == nullptr) {
@@ -250,11 +237,7 @@
 }
 
 std::string get_patch_path(const char* apkPath) {
-    std::string packageName;
-    if (get_packagename_from_apk(apkPath, &packageName) == false) {
-        return "";
-    }
-
+    std::string packageName = get_packagename_from_apk(apkPath);
     std::string patchDevicePath =
             android::base::StringPrintf("%s%s.patch", kDeviceAgentPath, packageName.c_str());
     return patchDevicePath;
@@ -262,12 +245,7 @@
 
 int apply_patch_on_device(const char* apkPath, const char* patchPath, const char* outputPath) {
     const std::string kAgentApplyCommandPattern = "/data/local/tmp/deployagent apply %s %s -o %s";
-
-    std::string packageName;
-    if (get_packagename_from_apk(apkPath, &packageName) == false) {
-        return -1;
-    }
-
+    std::string packageName = get_packagename_from_apk(apkPath);
     std::string patchDevicePath = get_patch_path(apkPath);
 
     std::vector<const char*> srcs = {patchPath};
@@ -286,12 +264,7 @@
 
 int install_patch(const char* apkPath, const char* patchPath, int argc, const char** argv) {
     const std::string kAgentApplyCommandPattern = "/data/local/tmp/deployagent apply %s %s -pm %s";
-
-    std::string packageName;
-    if (get_packagename_from_apk(apkPath, &packageName) == false) {
-        return -1;
-    }
-
+    std::string packageName = get_packagename_from_apk(apkPath);
     std::vector<const char*> srcs;
     std::string patchDevicePath =
             android::base::StringPrintf("%s%s.patch", kDeviceAgentPath, packageName.c_str());