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