Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 1 | #include "version.hpp" |
| 2 | |
| 3 | #include "item_updater.hpp" |
| 4 | |
| 5 | #include <fstream> |
| 6 | #include <iostream> |
| 7 | #include <phosphor-logging/elog-errors.hpp> |
| 8 | #include <phosphor-logging/log.hpp> |
| 9 | #include <sstream> |
| 10 | #include <stdexcept> |
| 11 | #include <string> |
| 12 | #include <xyz/openbmc_project/Common/error.hpp> |
| 13 | |
| 14 | namespace phosphor |
| 15 | { |
| 16 | namespace software |
| 17 | { |
| 18 | namespace updater |
| 19 | { |
| 20 | |
| 21 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
| 22 | using namespace phosphor::logging; |
| 23 | using Argument = xyz::openbmc_project::Common::InvalidArgument; |
| 24 | |
| 25 | std::map<std::string, std::string> |
Lei YU | fda15a3 | 2019-09-19 14:43:02 +0800 | [diff] [blame] | 26 | Version::getValues(const std::string& filePath, |
| 27 | const std::vector<std::string>& keys) |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 28 | { |
| 29 | if (filePath.empty()) |
| 30 | { |
| 31 | log<level::ERR>("Error filePath is empty"); |
| 32 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("FilePath"), |
| 33 | Argument::ARGUMENT_VALUE(filePath.c_str())); |
| 34 | } |
| 35 | |
Lei YU | fda15a3 | 2019-09-19 14:43:02 +0800 | [diff] [blame] | 36 | std::ifstream efile(filePath); |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 37 | std::string line; |
Lei YU | fda15a3 | 2019-09-19 14:43:02 +0800 | [diff] [blame] | 38 | std::map<std::string, std::string> ret; |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 39 | |
Lei YU | fda15a3 | 2019-09-19 14:43:02 +0800 | [diff] [blame] | 40 | while (getline(efile, line)) |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 41 | { |
Lei YU | d0f8bbb | 2019-10-18 15:49:31 +0800 | [diff] [blame] | 42 | if (!line.empty() && line.back() == '\r') |
| 43 | { |
| 44 | // Remove \r from the end of line |
| 45 | line.pop_back(); |
| 46 | } |
Lei YU | fda15a3 | 2019-09-19 14:43:02 +0800 | [diff] [blame] | 47 | for (const auto& key : keys) |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 48 | { |
Lei YU | fda15a3 | 2019-09-19 14:43:02 +0800 | [diff] [blame] | 49 | auto value = key + "="; |
| 50 | auto keySize = value.length(); |
| 51 | if (line.compare(0, keySize, value) == 0) |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 52 | { |
Lei YU | fda15a3 | 2019-09-19 14:43:02 +0800 | [diff] [blame] | 53 | ret.emplace(key, line.substr(keySize)); |
| 54 | break; |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 55 | } |
| 56 | } |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 57 | } |
Lei YU | fda15a3 | 2019-09-19 14:43:02 +0800 | [diff] [blame] | 58 | return ret; |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 59 | } |
| 60 | |
Lei YU | 58c26e3 | 2019-09-27 17:52:06 +0800 | [diff] [blame] | 61 | std::string Version::getValue(const std::string& filePath, |
| 62 | const std::string& key) |
| 63 | { |
| 64 | std::string ret; |
| 65 | auto values = Version::getValues(filePath, {key}); |
| 66 | const auto it = values.find(key); |
| 67 | if (it != values.end()) |
| 68 | { |
| 69 | ret = it->second; |
| 70 | } |
| 71 | return ret; |
| 72 | } |
| 73 | |
Lei YU | 9edb733 | 2019-09-19 14:46:19 +0800 | [diff] [blame] | 74 | std::map<std::string, std::string> |
| 75 | Version::getExtVersionInfo(const std::string& extVersion) |
| 76 | { |
| 77 | // The extVersion shall be key/value pairs separated by comma, |
| 78 | // e.g. key1=value1,key2=value2 |
| 79 | std::map<std::string, std::string> result; |
| 80 | std::stringstream ss(extVersion); |
| 81 | |
| 82 | while (ss.good()) |
| 83 | { |
| 84 | std::string substr; |
| 85 | getline(ss, substr, ','); |
| 86 | auto pos = substr.find('='); |
| 87 | if (pos != std::string::npos) |
| 88 | { |
| 89 | std::string key = substr.substr(0, pos); |
| 90 | std::string value = substr.substr(pos + 1); |
| 91 | result.emplace(key, value); |
| 92 | } |
| 93 | } |
| 94 | return result; |
| 95 | } |
| 96 | |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 97 | void Delete::delete_() |
| 98 | { |
| 99 | if (version.eraseCallback) |
| 100 | { |
| 101 | version.eraseCallback(version.getVersionId()); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | } // namespace updater |
| 106 | } // namespace software |
| 107 | } // namespace phosphor |