Handle file errors when device path gets unbound

occ-control was asserting when the hwmon path would get removed due to
device being unbound. This change will gracefully handle the path
getting removed / added back.

Tested on Raininer by unbind/bind of devices:
  echo occ-hwmon.2 > /sys/bus/platform/drivers/occ-hwmon/unbind
  echo occ-hwmon.2 > /sys/bus/platform/drivers/occ-hwmon/bind

Change-Id: I46fd2c2c54868ffb8183d3dc49cd0c2751165d3b
Signed-off-by: Chris Cain <cjcain@us.ibm.com>
diff --git a/occ_device.cpp b/occ_device.cpp
index c95c0f3..5cadf14 100644
--- a/occ_device.cpp
+++ b/occ_device.cpp
@@ -3,6 +3,9 @@
 #include "occ_manager.hpp"
 #include "occ_status.hpp"
 
+#include <phosphor-logging/log.hpp>
+
+#include <filesystem>
 #include <iostream>
 
 namespace open_power
@@ -10,6 +13,8 @@
 namespace occ
 {
 
+using namespace phosphor::logging;
+
 fs::path Device::bindPath = fs::path(OCC_HWMON_PATH) / "bind";
 fs::path Device::unBindPath = fs::path(OCC_HWMON_PATH) / "unbind";
 
@@ -82,5 +87,31 @@
     statusObject.throttleMemTemp(error);
 }
 
+fs::path Device::getFilenameByRegex(fs::path basePath,
+                                    const std::regex& expr) const
+{
+    try
+    {
+        for (auto& file : fs::directory_iterator(basePath))
+        {
+            if (std::regex_search(file.path().string(), expr))
+            {
+                // Found match
+                return file;
+            }
+        }
+    }
+    catch (const fs::filesystem_error& e)
+    {
+        log<level::ERR>(
+            fmt::format("getFilenameByRegex: Failed to get filename: {}",
+                        e.what())
+                .c_str());
+    }
+
+    // Return empty path
+    return fs::path{};
+}
+
 } // namespace occ
 } // namespace open_power