blob: 02cf6afd929ef81b7a0476bdba7cf550f94eec24 [file] [log] [blame]
Lei YU01539e72019-07-31 10:57:38 +08001#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
14namespace phosphor
15{
16namespace software
17{
18namespace updater
19{
20
21using namespace sdbusplus::xyz::openbmc_project::Common::Error;
22using namespace phosphor::logging;
23using Argument = xyz::openbmc_project::Common::InvalidArgument;
24
25std::map<std::string, std::string>
Lei YUfda15a32019-09-19 14:43:02 +080026 Version::getValues(const std::string& filePath,
27 const std::vector<std::string>& keys)
Lei YU01539e72019-07-31 10:57:38 +080028{
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
Lei YUfda15a32019-09-19 14:43:02 +080036 std::ifstream efile(filePath);
Lei YU01539e72019-07-31 10:57:38 +080037 std::string line;
Lei YUfda15a32019-09-19 14:43:02 +080038 std::map<std::string, std::string> ret;
Lei YU01539e72019-07-31 10:57:38 +080039
Lei YUfda15a32019-09-19 14:43:02 +080040 while (getline(efile, line))
Lei YU01539e72019-07-31 10:57:38 +080041 {
Lei YUfda15a32019-09-19 14:43:02 +080042 for (const auto& key : keys)
Lei YU01539e72019-07-31 10:57:38 +080043 {
Lei YUfda15a32019-09-19 14:43:02 +080044 auto value = key + "=";
45 auto keySize = value.length();
46 if (line.compare(0, keySize, value) == 0)
Lei YU01539e72019-07-31 10:57:38 +080047 {
Lei YUfda15a32019-09-19 14:43:02 +080048 ret.emplace(key, line.substr(keySize));
49 break;
Lei YU01539e72019-07-31 10:57:38 +080050 }
51 }
Lei YU01539e72019-07-31 10:57:38 +080052 }
Lei YUfda15a32019-09-19 14:43:02 +080053 return ret;
Lei YU01539e72019-07-31 10:57:38 +080054}
55
Lei YU9edb7332019-09-19 14:46:19 +080056std::map<std::string, std::string>
57 Version::getExtVersionInfo(const std::string& extVersion)
58{
59 // The extVersion shall be key/value pairs separated by comma,
60 // e.g. key1=value1,key2=value2
61 std::map<std::string, std::string> result;
62 std::stringstream ss(extVersion);
63
64 while (ss.good())
65 {
66 std::string substr;
67 getline(ss, substr, ',');
68 auto pos = substr.find('=');
69 if (pos != std::string::npos)
70 {
71 std::string key = substr.substr(0, pos);
72 std::string value = substr.substr(pos + 1);
73 result.emplace(key, value);
74 }
75 }
76 return result;
77}
78
Lei YU01539e72019-07-31 10:57:38 +080079void Delete::delete_()
80{
81 if (version.eraseCallback)
82 {
83 version.eraseCallback(version.getVersionId());
84 }
85}
86
87} // namespace updater
88} // namespace software
89} // namespace phosphor