| Gunnar Mills | 44c7729 | 2017-03-20 15:02:27 -0500 | [diff] [blame^] | 1 | #include <iostream> | 
|  | 2 | #include <string> | 
|  | 3 | #include <sstream> | 
|  | 4 | #include <fstream> | 
|  | 5 | #include <stdexcept> | 
|  | 6 | #include <phosphor-logging/log.hpp> | 
| Gunnar Mills | 9df1316 | 2017-03-08 15:00:08 -0600 | [diff] [blame] | 7 | #include "version_host_software_manager.hpp" | 
|  | 8 |  | 
|  | 9 | namespace openpower | 
|  | 10 | { | 
|  | 11 | namespace software | 
|  | 12 | { | 
|  | 13 | namespace manager | 
|  | 14 | { | 
|  | 15 |  | 
| Gunnar Mills | 44c7729 | 2017-03-20 15:02:27 -0500 | [diff] [blame^] | 16 | using namespace phosphor::logging; | 
|  | 17 |  | 
|  | 18 | std::string Version::getVersion(const std::string& tocFilePath) | 
|  | 19 | { | 
|  | 20 | constexpr auto versionKey = "version="; | 
|  | 21 | constexpr auto versionKeySize = strlen(versionKey); | 
|  | 22 |  | 
|  | 23 | if (tocFilePath.empty()) | 
|  | 24 | { | 
|  | 25 | log<level::ERR>("Error TocFilePath is empty"); | 
|  | 26 | throw std::runtime_error("TocFilePath is empty"); | 
|  | 27 | } | 
|  | 28 |  | 
|  | 29 | std::string version{}; | 
|  | 30 | std::ifstream efile; | 
|  | 31 | std::string line; | 
|  | 32 | efile.exceptions(std::ifstream::failbit | 
|  | 33 | | std::ifstream::badbit | 
|  | 34 | | std::ifstream::eofbit); | 
|  | 35 |  | 
|  | 36 | // Too many GCC bugs (53984, 66145) to do this the right way... | 
|  | 37 | try | 
|  | 38 | { | 
|  | 39 | efile.open(tocFilePath); | 
|  | 40 | while (getline(efile, line)) | 
|  | 41 | { | 
|  | 42 | if (line.compare(0, versionKeySize, versionKey) == 0) | 
|  | 43 | { | 
|  | 44 | version = line.substr(versionKeySize); | 
|  | 45 | break; | 
|  | 46 | } | 
|  | 47 | } | 
|  | 48 | efile.close(); | 
|  | 49 | } | 
|  | 50 | catch (const std::exception& e) | 
|  | 51 | { | 
|  | 52 | log<level::ERR>("Error in reading Host pnor.toc file"); | 
|  | 53 | } | 
|  | 54 |  | 
|  | 55 | return version; | 
|  | 56 | } | 
|  | 57 |  | 
|  | 58 | std::string Version::getId(const std::string& version) | 
|  | 59 | { | 
|  | 60 | std::stringstream hexId; | 
|  | 61 |  | 
|  | 62 | if (version.empty()) | 
|  | 63 | { | 
|  | 64 | log<level::ERR>("Error Host version is empty"); | 
|  | 65 | throw std::runtime_error("Host version is empty"); | 
|  | 66 | } | 
|  | 67 |  | 
|  | 68 | // Only want 8 hex digits. | 
|  | 69 | hexId << std::hex << ((std::hash<std::string> {}( | 
|  | 70 | version)) & 0xFFFFFFFF); | 
|  | 71 | return hexId.str(); | 
|  | 72 | } | 
|  | 73 |  | 
| Gunnar Mills | 9df1316 | 2017-03-08 15:00:08 -0600 | [diff] [blame] | 74 | } // namespace manager | 
|  | 75 | } // namespace software | 
|  | 76 | } // namepsace openpower | 
|  | 77 |  |