bmc/fs: Ignore missing directories

We shouldn't error out for a missing directory, just ignore it as they
don't have to be present.

Change-Id: I00bd66ca079059753480c73587e0ee41941cd7e6
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/bmc/firmware-handler/test/firmware_json_unittest.cpp b/bmc/firmware-handler/test/firmware_json_unittest.cpp
index 89d4741..9e562c6 100644
--- a/bmc/firmware-handler/test/firmware_json_unittest.cpp
+++ b/bmc/firmware-handler/test/firmware_json_unittest.cpp
@@ -671,5 +671,12 @@
     }
 }
 
+TEST(FirmwareJsonTest, BuildFromMissingDirectory)
+{
+    EXPECT_THAT(
+        FirmwareHandlersBuilder().buildHandlerConfigs("./no-such-directory"),
+        IsEmpty());
+}
+
 } // namespace
 } // namespace ipmi_flash
diff --git a/bmc/fs.cpp b/bmc/fs.cpp
index c43e310..4bad725 100644
--- a/bmc/fs.cpp
+++ b/bmc/fs.cpp
@@ -19,6 +19,7 @@
 #include <filesystem>
 #include <regex>
 #include <string>
+#include <system_error>
 #include <vector>
 
 namespace ipmi_flash
@@ -29,18 +30,30 @@
 {
     std::vector<std::string> output;
 
-    for (const auto& p : fs::recursive_directory_iterator(directory))
+    try
     {
-        auto ps = p.path().string();
-
-        /** TODO: openbmc/phosphor-ipmi-blobs/blob/de8a16e2e8/fs.cpp#L27 is
-         * nicer, may be worth finding a way to make this into a util.
-         */
-        if (std::regex_match(ps, std::regex(".+.json$")))
+        for (const auto& p : fs::recursive_directory_iterator(directory))
         {
-            output.push_back(ps);
+            auto ps = p.path().string();
+
+            /** TODO: openbmc/phosphor-ipmi-blobs/blob/de8a16e2e8/fs.cpp#L27 is
+             * nicer, may be worth finding a way to make this into a util.
+             */
+            if (std::regex_match(ps, std::regex(".+.json$")))
+            {
+                output.push_back(ps);
+            }
         }
     }
+    catch (const fs::filesystem_error& e)
+    {
+        // Ignore missing directories and just return an empty list
+        if (e.code() == std::error_code(ENOENT, std::generic_category()))
+        {
+            return output;
+        }
+        throw;
+    }
 
     return output;
 }