blob: 6b392d4b0457fc4c742bc49b81ded8079b38a16f [file] [log] [blame]
Gunnar Mills44c77292017-03-20 15:02:27 -05001#include <iostream>
2#include <string>
3#include <sstream>
4#include <fstream>
5#include <stdexcept>
6#include <phosphor-logging/log.hpp>
Gunnar Mills9df13162017-03-08 15:00:08 -06007#include "version_host_software_manager.hpp"
8
9namespace openpower
10{
11namespace software
12{
13namespace manager
14{
15
Gunnar Mills44c77292017-03-20 15:02:27 -050016using namespace phosphor::logging;
17
Saqib Khan77420d22017-04-04 11:07:07 -050018std::string Version::getVersion(const std::string& manifestFilePath)
Gunnar Mills44c77292017-03-20 15:02:27 -050019{
20 constexpr auto versionKey = "version=";
21 constexpr auto versionKeySize = strlen(versionKey);
22
Saqib Khan77420d22017-04-04 11:07:07 -050023 if (manifestFilePath.empty())
Gunnar Mills44c77292017-03-20 15:02:27 -050024 {
Saqib Khan77420d22017-04-04 11:07:07 -050025 log<level::ERR>("Error MANIFESTFilePath is empty");
26 throw std::runtime_error("MANIFESTFilePath is empty");
Gunnar Mills44c77292017-03-20 15:02:27 -050027 }
28
29 std::string version{};
30 std::ifstream efile;
31 std::string line;
32 efile.exceptions(std::ifstream::failbit
33 | std::ifstream::badbit
34 | std::ifstream::eofbit);
35
36 // Too many GCC bugs (53984, 66145) to do this the right way...
37 try
38 {
Saqib Khan77420d22017-04-04 11:07:07 -050039 efile.open(manifestFilePath);
Gunnar Mills44c77292017-03-20 15:02:27 -050040 while (getline(efile, line))
41 {
42 if (line.compare(0, versionKeySize, versionKey) == 0)
43 {
44 version = line.substr(versionKeySize);
45 break;
46 }
47 }
48 efile.close();
49 }
50 catch (const std::exception& e)
51 {
Saqib Khan77420d22017-04-04 11:07:07 -050052 log<level::ERR>("Error in reading Host MANIFEST file");
Gunnar Mills44c77292017-03-20 15:02:27 -050053 }
54
55 return version;
56}
57
58std::string Version::getId(const std::string& version)
59{
60 std::stringstream hexId;
61
62 if (version.empty())
63 {
64 log<level::ERR>("Error Host version is empty");
65 throw std::runtime_error("Host version is empty");
66 }
67
68 // Only want 8 hex digits.
69 hexId << std::hex << ((std::hash<std::string> {}(
70 version)) & 0xFFFFFFFF);
71 return hexId.str();
72}
73
Gunnar Mills9df13162017-03-08 15:00:08 -060074} // namespace manager
75} // namespace software
76} // namepsace openpower
77