1 #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"
10 #include "item_updater.hpp"
11 
12 namespace openpower
13 {
14 namespace software
15 {
16 namespace updater
17 {
18 
19 using namespace sdbusplus::xyz::openbmc_project::Common::Error;
20 using namespace phosphor::logging;
21 
22 std::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 
41 std::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     {
80         if (!efile.eof())
81         {
82             log<level::ERR>("Error in reading file");
83         }
84         efile.close();
85     }
86 
87     return keys;
88 }
89 
90 void Version::delete_()
91 {
92     parent.erase(getId(version()));
93 }
94 
95 } // namespace updater
96 } // namespace software
97 } // namespace openpower
98