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 | |
Gunnar Mills | cebd102 | 2017-04-17 16:10:15 -0500 | [diff] [blame] | 18 | std::string Version::getValue(const std::string& manifestFilePath, |
| 19 | std::string key) |
Gunnar Mills | 392f294 | 2017-04-12 11:04:37 -0500 | [diff] [blame] | 20 | { |
Gunnar Mills | cebd102 | 2017-04-17 16:10:15 -0500 | [diff] [blame] | 21 | key = key + "="; |
| 22 | auto keySize = key.length(); |
Gunnar Mills | 392f294 | 2017-04-12 11:04:37 -0500 | [diff] [blame] | 23 | |
| 24 | if (manifestFilePath.empty()) |
| 25 | { |
| 26 | log<level::ERR>("Error MANIFESTFilePath is empty"); |
| 27 | throw std::runtime_error("MANIFESTFilePath is empty"); |
| 28 | } |
| 29 | |
Gunnar Mills | cebd102 | 2017-04-17 16:10:15 -0500 | [diff] [blame] | 30 | std::string value{}; |
Gunnar Mills | 392f294 | 2017-04-12 11:04:37 -0500 | [diff] [blame] | 31 | std::ifstream efile; |
| 32 | std::string line; |
| 33 | efile.exceptions(std::ifstream::failbit |
| 34 | | std::ifstream::badbit |
| 35 | | std::ifstream::eofbit); |
| 36 | |
| 37 | // Too many GCC bugs (53984, 66145) to do this the right way... |
| 38 | try |
| 39 | { |
| 40 | efile.open(manifestFilePath); |
| 41 | while (getline(efile, line)) |
| 42 | { |
Gunnar Mills | cebd102 | 2017-04-17 16:10:15 -0500 | [diff] [blame] | 43 | if (line.compare(0, keySize, key) == 0) |
Gunnar Mills | 392f294 | 2017-04-12 11:04:37 -0500 | [diff] [blame] | 44 | { |
Gunnar Mills | cebd102 | 2017-04-17 16:10:15 -0500 | [diff] [blame] | 45 | value = line.substr(keySize); |
Gunnar Mills | 392f294 | 2017-04-12 11:04:37 -0500 | [diff] [blame] | 46 | break; |
| 47 | } |
| 48 | } |
| 49 | efile.close(); |
| 50 | } |
| 51 | catch (const std::exception& e) |
| 52 | { |
Gunnar Mills | cebd102 | 2017-04-17 16:10:15 -0500 | [diff] [blame] | 53 | log<level::ERR>("Error in reading MANIFEST file"); |
Gunnar Mills | 392f294 | 2017-04-12 11:04:37 -0500 | [diff] [blame] | 54 | } |
| 55 | |
Gunnar Mills | cebd102 | 2017-04-17 16:10:15 -0500 | [diff] [blame] | 56 | return value; |
Gunnar Mills | 392f294 | 2017-04-12 11:04:37 -0500 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | std::string Version::getId(const std::string& version) |
| 60 | { |
| 61 | std::stringstream hexId; |
| 62 | |
| 63 | if (version.empty()) |
| 64 | { |
Gunnar Mills | cebd102 | 2017-04-17 16:10:15 -0500 | [diff] [blame] | 65 | log<level::ERR>("Error version is empty"); |
| 66 | throw std::runtime_error("Version is empty"); |
Gunnar Mills | 392f294 | 2017-04-12 11:04:37 -0500 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | // Only want 8 hex digits. |
| 70 | hexId << std::hex << ((std::hash<std::string> {}( |
| 71 | version)) & 0xFFFFFFFF); |
| 72 | return hexId.str(); |
| 73 | } |
| 74 | |
Saqib Khan | ba23988 | 2017-05-26 08:41:54 -0500 | [diff] [blame] | 75 | std::string Version::getBMCVersion() |
| 76 | { |
| 77 | std::string versionKey = "VERSION_ID="; |
| 78 | std::string version{}; |
| 79 | std::ifstream efile; |
| 80 | std::string line; |
| 81 | efile.open("/etc/os-release"); |
| 82 | |
| 83 | while (getline(efile, line)) |
| 84 | { |
| 85 | if (line.substr(0, versionKey.size()).find(versionKey) != |
| 86 | std::string::npos) |
| 87 | { |
| 88 | std::size_t pos = line.find_first_of('"') + 1; |
| 89 | version = line.substr(pos, line.find_last_of('"') - pos); |
| 90 | break; |
| 91 | } |
| 92 | } |
| 93 | efile.close(); |
| 94 | |
| 95 | if (version.empty()) |
| 96 | { |
| 97 | log<level::ERR>("Error BMC current version is empty"); |
| 98 | throw std::runtime_error("BMC current version is empty"); |
| 99 | } |
| 100 | |
| 101 | return version; |
| 102 | } |
| 103 | |
Leonel Gonzalez | e423368 | 2017-07-07 14:38:25 -0500 | [diff] [blame] | 104 | void Version::delete_() |
| 105 | { |
| 106 | if (eraseCallback) |
| 107 | { |
| 108 | eraseCallback(getId(version())); |
| 109 | } |
| 110 | } |
| 111 | |
Gunnar Mills | 392f294 | 2017-04-12 11:04:37 -0500 | [diff] [blame] | 112 | } // namespace manager |
| 113 | } // namespace software |
| 114 | } // namepsace phosphor |
| 115 | |