tools: blob: add exceptions

Add exceptions on failures where it's a clean failure path.

Change-Id: Iaa8b6c7a0914367866092a7e31899453183fd7b2
Signed-off-by: Patrick Venture <venture@google.com>
diff --git a/tools/updater.cpp b/tools/updater.cpp
index b9574d8..b7ea191 100644
--- a/tools/updater.cpp
+++ b/tools/updater.cpp
@@ -20,6 +20,7 @@
 #include "tool_errors.hpp"
 
 #include <algorithm>
+#include <cstring>
 #include <memory>
 
 void updaterMain(BlobInterface* blob, DataInterface* handler,
@@ -36,7 +37,15 @@
      * available and use it.
      */
     std::vector<std::string> blobs = blob->getBlobList();
-    auto blobInst = std::find(blobs.begin(), blobs.end(), goalFirmware);
+    auto blobInst = std::find_if(
+        blobs.begin(), blobs.end(), [&goalFirmware](const auto& iter) {
+            /* Running into weird scenarios where the string comparison doesn't
+             * work.  TODO: revisit.
+             */
+            return (0 == std::memcmp(goalFirmware.c_str(), iter.c_str(),
+                                     goalFirmware.length()));
+            // return (goalFirmware.compare(iter));
+        });
     if (blobInst == blobs.end())
     {
         throw ToolException(goalFirmware + " not found");
@@ -45,7 +54,17 @@
     /* Call stat on /flash/image (or /flash/tarball) and check if data interface
      * is supported.
      */
-    auto stat = blob->getStat(goalFirmware);
+    StatResponse stat;
+    try
+    {
+        stat = blob->getStat(goalFirmware);
+    }
+    catch (const BlobException& b)
+    {
+        throw ToolException("blob exception received: " +
+                            std::string(b.what()));
+    }
+
     auto supported = handler->supportedType();
     if ((stat.blob_state & supported) == 0)
     {
@@ -64,8 +83,6 @@
                             std::string(b.what()));
     }
 
-    std::fprintf(stderr, "using session: %d\n", session);
-
     /* Send over the firmware image. */
     if (!handler->sendContents(imagePath, session))
     {