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>
26     Version::getValues(const std::string& filePath,
27                        const std::vector<std::string>& keys)
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 
36     std::ifstream efile(filePath);
37     std::string line;
38     std::map<std::string, std::string> ret;
39 
40     while (getline(efile, line))
41     {
42         for (const auto& key : keys)
43         {
44             auto value = key + "=";
45             auto keySize = value.length();
46             if (line.compare(0, keySize, value) == 0)
47             {
48                 ret.emplace(key, line.substr(keySize));
49                 break;
50             }
51         }
52     }
53     return ret;
54 }
55 
56 std::string Version::getValue(const std::string& filePath,
57                               const std::string& key)
58 {
59     std::string ret;
60     auto values = Version::getValues(filePath, {key});
61     const auto it = values.find(key);
62     if (it != values.end())
63     {
64         ret = it->second;
65     }
66     return ret;
67 }
68 
69 std::map<std::string, std::string>
70     Version::getExtVersionInfo(const std::string& extVersion)
71 {
72     // The extVersion shall be key/value pairs separated by comma,
73     // e.g. key1=value1,key2=value2
74     std::map<std::string, std::string> result;
75     std::stringstream ss(extVersion);
76 
77     while (ss.good())
78     {
79         std::string substr;
80         getline(ss, substr, ',');
81         auto pos = substr.find('=');
82         if (pos != std::string::npos)
83         {
84             std::string key = substr.substr(0, pos);
85             std::string value = substr.substr(pos + 1);
86             result.emplace(key, value);
87         }
88     }
89     return result;
90 }
91 
92 void Delete::delete_()
93 {
94     if (version.eraseCallback)
95     {
96         version.eraseCallback(version.getVersionId());
97     }
98 }
99 
100 } // namespace updater
101 } // namespace software
102 } // namespace phosphor
103