blob: 2000ab1474e4c1afed30928b2b29442fb8ceb1ce [file] [log] [blame]
Saqib Khan167601b2017-06-18 23:33:46 -05001#include <iostream>
2#include <string>
3#include <sstream>
4#include <fstream>
5#include <stdexcept>
6#include <phosphor-logging/log.hpp>
7#include "version.hpp"
8#include <phosphor-logging/elog-errors.hpp>
9#include "xyz/openbmc_project/Common/error.hpp"
Leonel Gonzalez9c8adfa2017-07-12 11:08:40 -050010#include "item_updater.hpp"
Saqib Khan167601b2017-06-18 23:33:46 -050011
12namespace openpower
13{
14namespace software
15{
16namespace updater
17{
18
19using namespace sdbusplus::xyz::openbmc_project::Common::Error;
20using namespace phosphor::logging;
21
22std::string Version::getId(const std::string& version)
23{
24 std::stringstream hexId;
25
26 if (version.empty())
27 {
28 log<level::ERR>("Error version is empty");
29 elog<InvalidArgument>(xyz::openbmc_project::Common::InvalidArgument::
30 ARGUMENT_NAME("Version"),
31 xyz::openbmc_project::Common::InvalidArgument::
32 ARGUMENT_VALUE(version.c_str()));
33 }
34
35 // Only want 8 hex digits.
36 hexId << std::hex << ((std::hash<std::string> {}(
37 version)) & 0xFFFFFFFF);
38 return hexId.str();
39}
40
41std::map<std::string, std::string> Version::getValue(
42 const std::string& filePath, std::map<std::string, std::string> keys)
43{
44
45 if (filePath.empty())
46 {
47 log<level::ERR>("Error filePath is empty");
48 elog<InvalidArgument>(xyz::openbmc_project::Common::InvalidArgument::
49 ARGUMENT_NAME("FilePath"),
50 xyz::openbmc_project::Common::InvalidArgument::
51 ARGUMENT_VALUE(filePath.c_str()));
52 }
53
54 std::ifstream efile;
55 std::string line;
56 efile.exceptions(std::ifstream::failbit
57 | std::ifstream::badbit
58 | std::ifstream::eofbit);
59
60 try
61 {
62 efile.open(filePath);
63 while (getline(efile, line))
64 {
65 for(auto& key : keys)
66 {
67 auto value = key.first + "=";
68 auto keySize = value.length();
69 if (line.compare(0, keySize, value) == 0)
70 {
71 key.second = line.substr(keySize);
72 break;
73 }
74 }
75 }
76 efile.close();
77 }
78 catch (const std::exception& e)
79 {
Saqib Khane53222d2017-08-19 16:57:24 -050080 if (!efile.eof())
81 {
82 log<level::ERR>("Error in reading file");
83 }
84 efile.close();
Saqib Khan167601b2017-06-18 23:33:46 -050085 }
86
87 return keys;
88}
89
Leonel Gonzalez9c8adfa2017-07-12 11:08:40 -050090void Version::delete_()
91{
92 parent.erase(getId(version()));
93}
94
Saqib Khan167601b2017-06-18 23:33:46 -050095} // namespace updater
96} // namespace software
97} // namespace openpower