Saqib Khan | 167601b | 2017-06-18 23:33:46 -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 | #include <phosphor-logging/elog-errors.hpp> |
| 9 | #include "xyz/openbmc_project/Common/error.hpp" |
Leonel Gonzalez | 9c8adfa | 2017-07-12 11:08:40 -0500 | [diff] [blame] | 10 | #include "item_updater.hpp" |
Saqib Khan | 167601b | 2017-06-18 23:33:46 -0500 | [diff] [blame] | 11 | |
| 12 | namespace openpower |
| 13 | { |
| 14 | namespace software |
| 15 | { |
| 16 | namespace updater |
| 17 | { |
| 18 | |
| 19 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
| 20 | using namespace phosphor::logging; |
Gunnar Mills | a93a07b | 2017-09-21 15:40:09 -0500 | [diff] [blame] | 21 | using Argument = xyz::openbmc_project::Common::InvalidArgument; |
Saqib Khan | 167601b | 2017-06-18 23:33:46 -0500 | [diff] [blame] | 22 | |
| 23 | std::string Version::getId(const std::string& version) |
| 24 | { |
| 25 | std::stringstream hexId; |
| 26 | |
| 27 | if (version.empty()) |
| 28 | { |
| 29 | log<level::ERR>("Error version is empty"); |
Gunnar Mills | a93a07b | 2017-09-21 15:40:09 -0500 | [diff] [blame] | 30 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("Version"), |
| 31 | Argument::ARGUMENT_VALUE(version.c_str())); |
Saqib Khan | 167601b | 2017-06-18 23:33:46 -0500 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | // Only want 8 hex digits. |
| 35 | hexId << std::hex << ((std::hash<std::string> {}( |
| 36 | version)) & 0xFFFFFFFF); |
| 37 | return hexId.str(); |
| 38 | } |
| 39 | |
| 40 | std::map<std::string, std::string> Version::getValue( |
| 41 | const std::string& filePath, std::map<std::string, std::string> keys) |
| 42 | { |
Saqib Khan | 167601b | 2017-06-18 23:33:46 -0500 | [diff] [blame] | 43 | if (filePath.empty()) |
| 44 | { |
| 45 | log<level::ERR>("Error filePath is empty"); |
Gunnar Mills | a93a07b | 2017-09-21 15:40:09 -0500 | [diff] [blame] | 46 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("FilePath"), |
| 47 | Argument::ARGUMENT_VALUE(filePath.c_str())); |
Saqib Khan | 167601b | 2017-06-18 23:33:46 -0500 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | std::ifstream efile; |
| 51 | std::string line; |
Gunnar Mills | a93a07b | 2017-09-21 15:40:09 -0500 | [diff] [blame] | 52 | efile.exceptions(std::ifstream::failbit | |
| 53 | std::ifstream::badbit | |
| 54 | std::ifstream::eofbit); |
Saqib Khan | 167601b | 2017-06-18 23:33:46 -0500 | [diff] [blame] | 55 | |
| 56 | try |
| 57 | { |
| 58 | efile.open(filePath); |
| 59 | while (getline(efile, line)) |
| 60 | { |
| 61 | for(auto& key : keys) |
| 62 | { |
| 63 | auto value = key.first + "="; |
| 64 | auto keySize = value.length(); |
| 65 | if (line.compare(0, keySize, value) == 0) |
| 66 | { |
| 67 | key.second = line.substr(keySize); |
| 68 | break; |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | efile.close(); |
| 73 | } |
| 74 | catch (const std::exception& e) |
| 75 | { |
Saqib Khan | e53222d | 2017-08-19 16:57:24 -0500 | [diff] [blame] | 76 | if (!efile.eof()) |
| 77 | { |
| 78 | log<level::ERR>("Error in reading file"); |
| 79 | } |
| 80 | efile.close(); |
Saqib Khan | 167601b | 2017-06-18 23:33:46 -0500 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | return keys; |
| 84 | } |
| 85 | |
Leonel Gonzalez | 9c8adfa | 2017-07-12 11:08:40 -0500 | [diff] [blame] | 86 | void Version::delete_() |
| 87 | { |
| 88 | parent.erase(getId(version())); |
| 89 | } |
| 90 | |
Saqib Khan | 167601b | 2017-06-18 23:33:46 -0500 | [diff] [blame] | 91 | } // namespace updater |
| 92 | } // namespace software |
| 93 | } // namespace openpower |