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/powercap.cpp b/powercap.cpp
index 682131c..cac7465 100644
--- a/powercap.cpp
+++ b/powercap.cpp
@@ -6,6 +6,7 @@
 #include <powercap.hpp>
 
 #include <cassert>
+#include <filesystem>
 
 namespace open_power
 {
@@ -141,23 +142,28 @@
 {
     if (pcapBasePathname.empty())
     {
-        static bool tracedError = false;
         pcapBasePathname = occStatus.getHwmonPath();
-        if (!fs::exists(pcapBasePathname) && !tracedError)
-        {
-            log<level::ERR>(fmt::format("Power Cap base filename not found: {}",
-                                        pcapBasePathname.c_str())
-                                .c_str());
-            tracedError = true;
-        }
     }
-    for (auto& file : fs::directory_iterator(pcapBasePathname))
+
+    if (fs::exists(pcapBasePathname))
     {
-        if (std::regex_search(file.path().string(), expr))
+        // Search for pcap file based on the supplied expr
+        for (auto& file : fs::directory_iterator(pcapBasePathname))
         {
-            return file;
+            if (std::regex_search(file.path().string(), expr))
+            {
+                // Found match
+                return file;
+            }
         }
     }
+    else
+    {
+        log<level::ERR>(fmt::format("Power Cap base filename not found: {}",
+                                    pcapBasePathname.c_str())
+                            .c_str());
+    }
+
     // return empty path
     return fs::path{};
 }
@@ -219,10 +225,10 @@
     }
 
     // Open the sysfs file and read the power cap
-    uint64_t cap;
     std::ifstream file(userCapName, std::ios::in);
     if (file)
     {
+        uint64_t cap;
         file >> cap;
         file.close();
         // Convert to Watts