1 #include "version.hpp"
2
3 #include "item_updater.hpp"
4
5 #include <phosphor-logging/elog-errors.hpp>
6 #include <phosphor-logging/elog.hpp>
7 #include <phosphor-logging/lg2.hpp>
8 #include <xyz/openbmc_project/Common/error.hpp>
9
10 #include <fstream>
11 #include <iostream>
12 #include <sstream>
13 #include <stdexcept>
14 #include <string>
15
16 namespace phosphor
17 {
18 namespace software
19 {
20 namespace updater
21 {
22
23 using namespace sdbusplus::xyz::openbmc_project::Common::Error;
24 using namespace phosphor::logging;
25 using Argument = xyz::openbmc_project::Common::InvalidArgument;
26
getValues(const std::string & filePath,const std::vector<std::string> & keys)27 std::map<std::string, std::string> Version::getValues(
28 const std::string& filePath, const std::vector<std::string>& keys)
29 {
30 if (filePath.empty())
31 {
32 lg2::error("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
getValue(const std::string & filePath,const std::string & key)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>
getExtVersionInfo(const std::string & extVersion)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
delete_()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