adb: rationalize fatal/error logging.

Let's use LOG(FATAL)/PLOG(FATAL) for actual fatal stuff.

Add a Windows error(3) and move folks who didn't really mean "abort"
fatal over to it. Also get rid of syntax_error which wasn't adding a
lot of value, and most of the places it was adding "usage: " didn't seem
entirely appropriate anyway.

In particular, we seemed to have confused fastdeploy.cpp into aborting
in most user error cases, and none of the reviewers noticed. Clearly
we'd all lost track of far too many options.

(I've also cleaned up a few random instances of fprintf(3) + exit(2).)

Bug: N/A
Test: manual
Change-Id: I3e8440848a24e30d928de9eded505916bc324786
diff --git a/adb/client/fastdeploy.cpp b/adb/client/fastdeploy.cpp
index 183a1fa..933b913 100644
--- a/adb/client/fastdeploy.cpp
+++ b/adb/client/fastdeploy.cpp
@@ -27,6 +27,7 @@
 #include "client/file_sync_client.h"
 #include "commandline.h"
 #include "fastdeploycallbacks.h"
+#include "sysdeps.h"
 #include "utils/String16.h"
 
 static constexpr long kRequiredAgentVersion = 0x00000001;
@@ -72,13 +73,14 @@
 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()) {
-        fatal("Could not determine location of adb!");
+        error(1, 0, "Could not determine location of adb!");
     }
 
     if (g_use_localagent) {
         const char* product_out = getenv("ANDROID_PRODUCT_OUT");
         if (product_out == nullptr) {
-            fatal("Could not locate %s because $ANDROID_PRODUCT_OUT is not defined", local_path);
+            error(1, 0, "Could not locate %s because $ANDROID_PRODUCT_OUT is not defined",
+                  local_path);
         }
         return android::base::StringPrintf("%s%s", product_out, local_path);
     } else {
@@ -103,10 +105,10 @@
                 android::base::StringPrintf(kChmodCommandPattern, kDeviceAgentPath);
         int ret = send_shell_command(chmodCommand);
         if (ret != 0) {
-            fatal("Error executing %s returncode: %d", chmodCommand.c_str(), ret);
+            error(1, 0, "Error executing %s returncode: %d", chmodCommand.c_str(), ret);
         }
     } else {
-        fatal("Error pushing agent files to device");
+        error(1, 0, "Error pushing agent files to device");
     }
 
     return true;
@@ -136,7 +138,7 @@
 
     agent_version = get_agent_version();
     if (agent_version != kRequiredAgentVersion) {
-        fatal("After update agent version remains incorrect! Expected %ld but version is %ld",
+        error(1, 0, "After update agent version remains incorrect! Expected %ld but version is %ld",
               kRequiredAgentVersion, agent_version);
     }
 }
@@ -153,26 +155,28 @@
 }
 
 static std::string get_packagename_from_apk(const char* apkPath) {
+#undef open
     std::unique_ptr<android::ZipFileRO> zipFile(android::ZipFileRO::open(apkPath));
+#define open ___xxx_unix_open
     if (zipFile == nullptr) {
-        fatal("Could not open %s", apkPath);
+        error(1, errno, "Could not open %s", apkPath);
     }
     android::ZipEntryRO entry = zipFile->findEntryByName("AndroidManifest.xml");
     if (entry == nullptr) {
-        fatal("Could not find AndroidManifest.xml inside %s", apkPath);
+        error(1, 0, "Could not find AndroidManifest.xml inside %s", apkPath);
     }
     uint32_t manifest_len = 0;
     if (!zipFile->getEntryInfo(entry, NULL, &manifest_len, NULL, NULL, NULL, NULL)) {
-        fatal("Could not read AndroidManifest.xml inside %s", apkPath);
+        error(1, 0, "Could not read AndroidManifest.xml inside %s", apkPath);
     }
     std::vector<char> manifest_data(manifest_len);
     if (!zipFile->uncompressEntry(entry, manifest_data.data(), manifest_len)) {
-        fatal("Could not uncompress AndroidManifest.xml inside %s", apkPath);
+        error(1, 0, "Could not uncompress AndroidManifest.xml inside %s", apkPath);
     }
     android::ResXMLTree tree;
     android::status_t setto_status = tree.setTo(manifest_data.data(), manifest_len, true);
     if (setto_status != android::OK) {
-        fatal("Could not parse AndroidManifest.xml inside %s", apkPath);
+        error(1, 0, "Could not parse AndroidManifest.xml inside %s", apkPath);
     }
     android::ResXMLParser::event_code_t code;
     while ((code = tree.next()) != android::ResXMLParser::BAD_DOCUMENT &&
@@ -213,7 +217,8 @@
                 break;
         }
     }
-    fatal("Could not find package name tag in AndroidManifest.xml inside %s", apkPath);
+    error(1, 0, "Could not find package name tag in AndroidManifest.xml inside %s", apkPath);
+    __builtin_unreachable();
 }
 
 void extract_metadata(const char* apkPath, FILE* outputFp) {
@@ -227,7 +232,7 @@
     DeployAgentFileCallback cb(outputFp, &extractErrorBuffer, &statusCode);
     int returnCode = send_shell_command(extractCommand, false, &cb);
     if (returnCode != 0) {
-        fatal("Executing %s returned %d\n", extractCommand.c_str(), returnCode);
+        error(1, 0, "Executing %s returned %d", extractCommand.c_str(), returnCode);
     }
 }
 
@@ -236,8 +241,9 @@
         // This should never happen on a Windows machine
         const char* host_out = getenv("ANDROID_HOST_OUT");
         if (host_out == nullptr) {
-            fatal("Could not locate deploypatchgenerator.jar because $ANDROID_HOST_OUT is not "
-                  "defined");
+            error(1, 0,
+                  "Could not locate deploypatchgenerator.jar because $ANDROID_HOST_OUT "
+                  "is not defined");
         }
         return android::base::StringPrintf("java -jar %s/framework/deploypatchgenerator.jar",
                                            host_out);
@@ -245,7 +251,7 @@
 
     std::string adb_dir = android::base::GetExecutableDirectory();
     if (adb_dir.empty()) {
-        fatal("Could not locate deploypatchgenerator.jar");
+        error(1, 0, "Could not locate deploypatchgenerator.jar");
     }
     return android::base::StringPrintf(R"(java -jar "%s/deploypatchgenerator.jar")",
                                        adb_dir.c_str());
@@ -257,7 +263,7 @@
             patchPath);
     int returnCode = system(generatePatchCommand.c_str());
     if (returnCode != 0) {
-        fatal("Executing %s returned %d\n", generatePatchCommand.c_str(), returnCode);
+        error(1, 0, "Executing %s returned %d", generatePatchCommand.c_str(), returnCode);
     }
 }
 
@@ -276,7 +282,7 @@
     std::vector<const char*> srcs = {patchPath};
     bool push_ok = do_sync_push(srcs, patchDevicePath.c_str(), false);
     if (!push_ok) {
-        fatal("Error pushing %s to %s returned\n", patchPath, patchDevicePath.c_str());
+        error(1, 0, "Error pushing %s to %s returned", patchPath, patchDevicePath.c_str());
     }
 
     std::string applyPatchCommand =
@@ -285,7 +291,7 @@
 
     int returnCode = send_shell_command(applyPatchCommand);
     if (returnCode != 0) {
-        fatal("Executing %s returned %d\n", applyPatchCommand.c_str(), returnCode);
+        error(1, 0, "Executing %s returned %d", applyPatchCommand.c_str(), returnCode);
     }
 }
 
@@ -299,7 +305,7 @@
     std::vector<const char*> srcs{patchPath};
     bool push_ok = do_sync_push(srcs, patchDevicePath.c_str(), false);
     if (!push_ok) {
-        fatal("Error pushing %s to %s returned\n", patchPath, patchDevicePath.c_str());
+        error(1, 0, "Error pushing %s to %s returned", patchPath, patchDevicePath.c_str());
     }
 
     std::vector<unsigned char> applyOutputBuffer;
@@ -316,6 +322,6 @@
                                         patchDevicePath.c_str(), argsString.c_str());
     int returnCode = send_shell_command(applyPatchCommand);
     if (returnCode != 0) {
-        fatal("Executing %s returned %d\n", applyPatchCommand.c_str(), returnCode);
+        error(1, 0, "Executing %s returned %d", applyPatchCommand.c_str(), returnCode);
     }
 }