diff options
author | Harsh Sharma <harsh.sharma@multitech.com> | 2020-01-08 13:09:53 -0600 |
---|---|---|
committer | Harsh Sharma <harsh.sharma@multitech.com> | 2020-01-08 13:09:53 -0600 |
commit | afd71988ef74e331a0ac24c53f85175aee082a43 (patch) | |
tree | 6d03373a184fb0159c98768f8e6572b4383a51f8 | |
parent | c4811dd1b73b37b0916803097237acd31f1df98b (diff) | |
download | mts-io-sysfs-afd71988ef74e331a0ac24c53f85175aee082a43.tar.gz mts-io-sysfs-afd71988ef74e331a0ac24c53f85175aee082a43.tar.bz2 mts-io-sysfs-afd71988ef74e331a0ac24c53f85175aee082a43.zip |
Added error checking file read and stat
-rw-r--r-- | include/Utility/Utility.h | 9 | ||||
-rw-r--r-- | src/Device/Device.cpp | 13 | ||||
-rw-r--r-- | src/Version.cpp | 2 |
3 files changed, 12 insertions, 12 deletions
diff --git a/include/Utility/Utility.h b/include/Utility/Utility.h index 062e422..0c1f16c 100644 --- a/include/Utility/Utility.h +++ b/include/Utility/Utility.h @@ -48,9 +48,12 @@ inline void exitHandler(int code) { } inline mode_t fileType(std::string file) { - struct stat buf; - stat (file.c_str(), &buf); - return buf.st_mode & S_IFMT; + struct stat buf = { .st_dev = 0 }; + if (stat (file.c_str(), &buf) == 0) { + return buf.st_mode & S_IFMT; + } else { + return -1; + } } #endif /* UTILITIES_H_ */ diff --git a/src/Device/Device.cpp b/src/Device/Device.cpp index b806f06..21ad9e8 100644 --- a/src/Device/Device.cpp +++ b/src/Device/Device.cpp @@ -48,16 +48,13 @@ void Device::getSystemTreeJson(const char * dir_name) { exitHandler(99); } while (1) { - struct dirent * entry; - const char * d_name; - entry = readdir (d); // Gets subsequent entries from "d" - if (!entry) { // If there are no more entries, exit + struct dirent * entry = readdir (d); // Gets subsequent entries from "d" + if (!entry) { // If there are no more entries, exit break; } - d_name = entry->d_name; // Get file name - if (!(entry->d_type & DT_DIR)) { - std::string fileData; - MTS::System::readFile(fullPath + "/" + std::string(d_name), fileData); + const char * d_name = entry->d_name; // Get file name + std::string fileData; + if (!(entry->d_type & DT_DIR) && (MTS::System::readFile(fullPath + "/" + std::string(d_name), fileData) == 0)) { fileData = MTS::Text::trim(fileData); if (strlen(dir_name) > 0) { if (std::binary_search(apIdentifiers.begin(), apIdentifiers.end(), dir_name) && !regex_match(d_name, apFilters)) { diff --git a/src/Version.cpp b/src/Version.cpp index 0f6d228..5513345 100644 --- a/src/Version.cpp +++ b/src/Version.cpp @@ -1,4 +1,4 @@ //Pre-Build Auto-Generated Source #include "Version.h" -const std::string Version::version("v1.0.0-5-g68c0c5e"); +const std::string Version::version("v1.0.0-6-gc4811dd"); |