Use std::filesystem in favor of custom module

Reuse some code.  Fix a bug in the process.

Resolves openbmc/openbmc#1254

Signed-off-by: Brad Bishop <bradleyb@fuzziesquirrel.com>
Change-Id: I3fdbb70d6372f4a3193204bd2c9b6535315a3c70
diff --git a/Makefile.am b/Makefile.am
index 7bfb12a..b7057ad 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -7,6 +7,7 @@
 noinst_LTLIBRARIES = libhwmon.la
 libhwmon_la_LDFLAGS = -static
 libhwmon_la_LIBADD = \
+	-lstdc++fs \
 	$(SDBUSPLUS_LIBS) \
 	$(PHOSPHOR_DBUS_INTERFACES_LIBS) \
 	$(PHOSPHOR_LOGGING_LIBS)
@@ -17,7 +18,6 @@
 
 libhwmon_la_SOURCES = \
 	argument.cpp \
-	directory.cpp \
 	sensorset.cpp \
 	mainloop.cpp \
 	sysfs.cpp \
diff --git a/configure.ac b/configure.ac
index b717976..f08030e 100644
--- a/configure.ac
+++ b/configure.ac
@@ -23,6 +23,7 @@
 
 # Checks for header files.
 AC_CHECK_HEADER(sdbusplus/server.hpp, ,[AC_MSG_ERROR([Could not find sdbusplus/server.hpp...sdbusplus developement package required])])
+AC_CHECK_HEADER(experimental/filesystem, ,[AC_MSG_ERROR([Could not find experimental/filesystem...libstdc++fs developement package required])])
 
 # Checks for library functions.
 LT_INIT
diff --git a/sensorset.cpp b/sensorset.cpp
index b76cef7..2952775 100644
--- a/sensorset.cpp
+++ b/sensorset.cpp
@@ -13,10 +13,10 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+#include <experimental/filesystem>
 #include <regex>
 #include <iostream>
 #include "sensorset.hpp"
-#include "directory.hpp"
 #include "hwmon.hpp"
 
 // TODO: Issue#2 - STL regex generates really bloated code.  Use POSIX regex
@@ -28,13 +28,13 @@
 
 SensorSet::SensorSet(const std::string& path)
 {
-    Directory d(path);
-    std::string file;
+    namespace fs = std::experimental::filesystem;
 
-    while (d.next(file))
+    for (const auto& file : fs::directory_iterator(path))
     {
         std::smatch match;
-        std::regex_search(file, match, sensors_regex);
+        auto fileName = file.path().filename();
+        std::regex_search(fileName.native(), match, sensors_regex);
 
         if (match.size() != sensor_regex_match_count)
         {
diff --git a/sysfs.cpp b/sysfs.cpp
index 0cc11ef..b985149 100644
--- a/sysfs.cpp
+++ b/sysfs.cpp
@@ -14,39 +14,32 @@
  * limitations under the License.
  */
 #include <cstdlib>
+#include <experimental/filesystem>
 #include <memory>
 #include <phosphor-logging/log.hpp>
 #include "sysfs.hpp"
 #include "util.hpp"
-#include "directory.hpp"
 
 std::string findHwmon(const std::string& ofNode)
 {
+    namespace fs = std::experimental::filesystem;
     static constexpr auto hwmonRoot = "/sys/class/hwmon";
+    static constexpr auto ofRoot = "/sys/firmware/devicetree/base";
 
-    std::string fullOfPath{"/sys/firmware/devicetree/base"};
-    fullOfPath.append(ofNode);
+    fs::path fullOfPath{ofRoot};
+    fullOfPath /= ofNode;
 
-    std::string hwmonInst;
-    Directory d(hwmonRoot);
-
-    while (d.next(hwmonInst))
+    for (const auto& hwmonInst : fs::directory_iterator(hwmonRoot))
     {
-        std::string hwmonPath{hwmonRoot};
-        hwmonPath.append(1, '/');
-        hwmonPath.append(hwmonInst);
-        std::string path{hwmonPath};
-        path.append(1, '/');
-        path.append("of_node");
+        auto path = hwmonInst.path();
+        path /= "of_node";
 
-        auto real = std::unique_ptr<char, phosphor::utility::Free<char>>(realpath(
-                        path.c_str(), nullptr));
-        if (!real || real.get() != fullOfPath)
+        if (fs::canonical(path) != fullOfPath)
         {
             continue;
         }
 
-        return hwmonPath;
+        return hwmonInst.path();
     }
 
     return std::string();