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