Gunnar Mills | 392f294 | 2017-04-12 11:04:37 -0500 | [diff] [blame^] | 1 | #include <iostream> |
| 2 | #include <string> |
| 3 | #include <sstream> |
| 4 | #include <fstream> |
| 5 | #include <stdexcept> |
| 6 | #include <phosphor-logging/log.hpp> |
| 7 | #include "version.hpp" |
| 8 | |
| 9 | namespace phosphor |
| 10 | { |
| 11 | namespace software |
| 12 | { |
| 13 | namespace manager |
| 14 | { |
| 15 | |
| 16 | using namespace phosphor::logging; |
| 17 | |
| 18 | std::string Version::getVersion(const std::string& manifestFilePath) |
| 19 | { |
| 20 | constexpr auto versionKey = "version="; |
| 21 | constexpr auto versionKeySize = strlen(versionKey); |
| 22 | |
| 23 | if (manifestFilePath.empty()) |
| 24 | { |
| 25 | log<level::ERR>("Error MANIFESTFilePath is empty"); |
| 26 | throw std::runtime_error("MANIFESTFilePath 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(manifestFilePath); |
| 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 MANIFEST 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 | |
| 74 | } // namespace manager |
| 75 | } // namespace software |
| 76 | } // namepsace phosphor |
| 77 | |