sysfs: refactor findHwmon

 - Invert branch logic to reduce indent levels.
 - Handle disappearing hwmon instances while searching.

Change-Id: I20f93003bd3fa4f849341c4cf08a0f6a29bf785a
Signed-off-by: Brad Bishop <bradleyb@fuzziesquirrel.com>
diff --git a/sysfs.cpp b/sysfs.cpp
index a155b8e..4117b2f 100644
--- a/sysfs.cpp
+++ b/sysfs.cpp
@@ -121,26 +121,34 @@
     {
         auto path = hwmonInst.path();
         path /= "of_node";
-        if (fs::canonical(path) != fullOfPath)
+
+        try
         {
-            // Try to find HWMON instance via phandle values.
-            // Used for IIO device drivers.
-            auto ofpath = fullOfPath.string();
-            auto matchpath = findPhandleMatch(path, ofpath);
-            if (!std::string(matchpath).empty())
-            {
-                return hwmonInst.path();
-            }
-            else
-            {
-                continue;
-            }
+            path = fs::canonical(path);
+        }
+        catch (const std::system_error& e)
+        {
+            // realpath may encounter ENOENT (Hwmon
+            // instances have a nasty habit of
+            // going away without warning).
+            continue;
         }
 
-        return hwmonInst.path();
+        if (path == fullOfPath)
+        {
+            return hwmonInst.path();
+        }
+
+        // Try to find HWMON instance via phandle values.
+        // Used for IIO device drivers.
+        auto matchpath = findPhandleMatch(path, fullOfPath);
+        if (!matchpath.empty())
+        {
+            return hwmonInst.path();
+        }
     }
 
-    return std::string();
+    return emptyString;
 }
 
 int readSysfsWithCallout(const std::string& root,