blob: 013cc68b0e9de794e6e129348908f558a37c59d2 [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;
Gunnar Millsa93a07b2017-09-21 15:40:09 -050021using Argument = xyz::openbmc_project::Common::InvalidArgument;
Saqib Khan167601b2017-06-18 23:33:46 -050022
23std::string Version::getId(const std::string& version)
24{
25 std::stringstream hexId;
26
27 if (version.empty())
28 {
29 log<level::ERR>("Error version is empty");
Gunnar Millsa93a07b2017-09-21 15:40:09 -050030 elog<InvalidArgument>(Argument::ARGUMENT_NAME("Version"),
31 Argument::ARGUMENT_VALUE(version.c_str()));
Saqib Khan167601b2017-06-18 23:33:46 -050032 }
33
34 // Only want 8 hex digits.
35 hexId << std::hex << ((std::hash<std::string> {}(
36 version)) & 0xFFFFFFFF);
37 return hexId.str();
38}
39
40std::map<std::string, std::string> Version::getValue(
41 const std::string& filePath, std::map<std::string, std::string> keys)
42{
Saqib Khan167601b2017-06-18 23:33:46 -050043 if (filePath.empty())
44 {
45 log<level::ERR>("Error filePath is empty");
Gunnar Millsa93a07b2017-09-21 15:40:09 -050046 elog<InvalidArgument>(Argument::ARGUMENT_NAME("FilePath"),
47 Argument::ARGUMENT_VALUE(filePath.c_str()));
Saqib Khan167601b2017-06-18 23:33:46 -050048 }
49
50 std::ifstream efile;
51 std::string line;
Gunnar Millsa93a07b2017-09-21 15:40:09 -050052 efile.exceptions(std::ifstream::failbit |
53 std::ifstream::badbit |
54 std::ifstream::eofbit);
Saqib Khan167601b2017-06-18 23:33:46 -050055
56 try
57 {
58 efile.open(filePath);
59 while (getline(efile, line))
60 {
61 for(auto& key : keys)
62 {
63 auto value = key.first + "=";
64 auto keySize = value.length();
65 if (line.compare(0, keySize, value) == 0)
66 {
67 key.second = line.substr(keySize);
68 break;
69 }
70 }
71 }
72 efile.close();
73 }
74 catch (const std::exception& e)
75 {
Saqib Khane53222d2017-08-19 16:57:24 -050076 if (!efile.eof())
77 {
78 log<level::ERR>("Error in reading file");
79 }
80 efile.close();
Saqib Khan167601b2017-06-18 23:33:46 -050081 }
82
83 return keys;
84}
85
Leonel Gonzalez9c8adfa2017-07-12 11:08:40 -050086void Version::delete_()
87{
88 parent.erase(getId(version()));
89}
90
Saqib Khan167601b2017-06-18 23:33:46 -050091} // namespace updater
92} // namespace software
93} // namespace openpower