1 #include "version.hpp" 2 3 #include "item_updater.hpp" 4 5 #include <phosphor-logging/elog-errors.hpp> 6 #include <phosphor-logging/log.hpp> 7 #include <xyz/openbmc_project/Common/error.hpp> 8 9 #include <fstream> 10 #include <iostream> 11 #include <sstream> 12 #include <stdexcept> 13 #include <string> 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> 27 Version::getValues(const std::string& filePath, 28 const std::vector<std::string>& keys) 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 37 std::ifstream efile(filePath); 38 std::string line; 39 std::map<std::string, std::string> ret; 40 41 while (getline(efile, line)) 42 { 43 if (!line.empty() && line.back() == '\r') 44 { 45 // Remove \r from the end of line 46 line.pop_back(); 47 } 48 for (const auto& key : keys) 49 { 50 auto value = key + "="; 51 auto keySize = value.length(); 52 if (line.compare(0, keySize, value) == 0) 53 { 54 ret.emplace(key, line.substr(keySize)); 55 break; 56 } 57 } 58 } 59 return ret; 60 } 61 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 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 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 109