diff options
author | Brad Bishop <bradleyb@fuzziesquirrel.com> | 2017-03-06 21:28:46 -0500 |
---|---|---|
committer | Brad Bishop <bradleyb@fuzziesquirrel.com> | 2017-03-08 14:17:30 -0500 |
commit | 08379a31c4e75eff87a3c9575c6ffd370bda1f38 (patch) | |
tree | 0835b457061f926a9bd6d0a9f17c86c98356239d | |
parent | 0be1f8dc452470acbd1da1df463a53250d2eb463 (diff) | |
download | phosphor-hwmon-08379a31c4e75eff87a3c9575c6ffd370bda1f38.tar.gz phosphor-hwmon-08379a31c4e75eff87a3c9575c6ffd370bda1f38.zip |
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
-rw-r--r-- | Makefile.am | 2 | ||||
-rw-r--r-- | configure.ac | 1 | ||||
-rw-r--r-- | sensorset.cpp | 10 | ||||
-rw-r--r-- | sysfs.cpp | 27 |
4 files changed, 17 insertions, 23 deletions
diff --git a/Makefile.am b/Makefile.am index 7bfb12a..b7057ad 100644 --- a/Makefile.am +++ b/Makefile.am @@ -7,6 +7,7 @@ phosphor_hwmon_readd_CXXFLAGS = $(SDBUSPLUS_CFLAGS) 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_CXXFLAGS = \ 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 @@ AX_APPEND_COMPILE_FLAGS([-fpic -Wall -Werror], [CXXFLAGS]) # 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 @@ static const auto sensor_regex_match_count = 4; 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) { @@ -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(); |