blob: 58abf79ecec483479618c875a54994f5bc28d766 [file] [log] [blame]
Lei YU01539e72019-07-31 10:57:38 +08001#include "version.hpp"
2
3#include "item_updater.hpp"
4
Lei YU01539e72019-07-31 10:57:38 +08005#include <phosphor-logging/elog-errors.hpp>
6#include <phosphor-logging/log.hpp>
Patrick Williams5670b182023-05-10 07:50:50 -05007#include <xyz/openbmc_project/Common/error.hpp>
8
9#include <fstream>
10#include <iostream>
Lei YU01539e72019-07-31 10:57:38 +080011#include <sstream>
12#include <stdexcept>
13#include <string>
Lei YU01539e72019-07-31 10:57:38 +080014
15namespace phosphor
16{
17namespace software
18{
19namespace updater
20{
21
22using namespace sdbusplus::xyz::openbmc_project::Common::Error;
23using namespace phosphor::logging;
24using Argument = xyz::openbmc_project::Common::InvalidArgument;
25
26std::map<std::string, std::string>
Lei YUfda15a32019-09-19 14:43:02 +080027 Version::getValues(const std::string& filePath,
28 const std::vector<std::string>& keys)
Lei YU01539e72019-07-31 10:57:38 +080029{
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 YUfda15a32019-09-19 14:43:02 +080037 std::ifstream efile(filePath);
Lei YU01539e72019-07-31 10:57:38 +080038 std::string line;
Lei YUfda15a32019-09-19 14:43:02 +080039 std::map<std::string, std::string> ret;
Lei YU01539e72019-07-31 10:57:38 +080040
Lei YUfda15a32019-09-19 14:43:02 +080041 while (getline(efile, line))
Lei YU01539e72019-07-31 10:57:38 +080042 {
Lei YUd0f8bbb2019-10-18 15:49:31 +080043 if (!line.empty() && line.back() == '\r')
44 {
45 // Remove \r from the end of line
46 line.pop_back();
47 }
Lei YUfda15a32019-09-19 14:43:02 +080048 for (const auto& key : keys)
Lei YU01539e72019-07-31 10:57:38 +080049 {
Lei YUfda15a32019-09-19 14:43:02 +080050 auto value = key + "=";
51 auto keySize = value.length();
52 if (line.compare(0, keySize, value) == 0)
Lei YU01539e72019-07-31 10:57:38 +080053 {
Lei YUfda15a32019-09-19 14:43:02 +080054 ret.emplace(key, line.substr(keySize));
55 break;
Lei YU01539e72019-07-31 10:57:38 +080056 }
57 }
Lei YU01539e72019-07-31 10:57:38 +080058 }
Lei YUfda15a32019-09-19 14:43:02 +080059 return ret;
Lei YU01539e72019-07-31 10:57:38 +080060}
61
Lei YU58c26e32019-09-27 17:52:06 +080062std::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 YU9edb7332019-09-19 14:46:19 +080075std::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 YU01539e72019-07-31 10:57:38 +080098void 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