blob: e6dd9de4a0bdb9e74026ced344739879da144c41 [file] [log] [blame]
Gunnar Mills392f2942017-04-12 11:04:37 -05001#include <iostream>
2#include <string>
3#include <sstream>
4#include <fstream>
5#include <stdexcept>
6#include <phosphor-logging/log.hpp>
Eddie James6d873712017-09-01 11:29:07 -05007#include "config.h"
Gunnar Mills392f2942017-04-12 11:04:37 -05008#include "version.hpp"
9
10namespace phosphor
11{
12namespace software
13{
14namespace manager
15{
16
17using namespace phosphor::logging;
18
Gunnar Millscebd1022017-04-17 16:10:15 -050019std::string Version::getValue(const std::string& manifestFilePath,
20 std::string key)
Gunnar Mills392f2942017-04-12 11:04:37 -050021{
Gunnar Millscebd1022017-04-17 16:10:15 -050022 key = key + "=";
23 auto keySize = key.length();
Gunnar Mills392f2942017-04-12 11:04:37 -050024
25 if (manifestFilePath.empty())
26 {
27 log<level::ERR>("Error MANIFESTFilePath is empty");
28 throw std::runtime_error("MANIFESTFilePath is empty");
29 }
30
Gunnar Millscebd1022017-04-17 16:10:15 -050031 std::string value{};
Gunnar Mills392f2942017-04-12 11:04:37 -050032 std::ifstream efile;
33 std::string line;
34 efile.exceptions(std::ifstream::failbit
35 | std::ifstream::badbit
36 | std::ifstream::eofbit);
37
38 // Too many GCC bugs (53984, 66145) to do this the right way...
39 try
40 {
41 efile.open(manifestFilePath);
42 while (getline(efile, line))
43 {
Gunnar Millscebd1022017-04-17 16:10:15 -050044 if (line.compare(0, keySize, key) == 0)
Gunnar Mills392f2942017-04-12 11:04:37 -050045 {
Gunnar Millscebd1022017-04-17 16:10:15 -050046 value = line.substr(keySize);
Gunnar Mills392f2942017-04-12 11:04:37 -050047 break;
48 }
49 }
50 efile.close();
51 }
52 catch (const std::exception& e)
53 {
Gunnar Millscebd1022017-04-17 16:10:15 -050054 log<level::ERR>("Error in reading MANIFEST file");
Gunnar Mills392f2942017-04-12 11:04:37 -050055 }
56
Gunnar Millscebd1022017-04-17 16:10:15 -050057 return value;
Gunnar Mills392f2942017-04-12 11:04:37 -050058}
59
60std::string Version::getId(const std::string& version)
61{
62 std::stringstream hexId;
63
64 if (version.empty())
65 {
Gunnar Millscebd1022017-04-17 16:10:15 -050066 log<level::ERR>("Error version is empty");
67 throw std::runtime_error("Version is empty");
Gunnar Mills392f2942017-04-12 11:04:37 -050068 }
69
70 // Only want 8 hex digits.
71 hexId << std::hex << ((std::hash<std::string> {}(
72 version)) & 0xFFFFFFFF);
73 return hexId.str();
74}
75
Saqib Khan1eef62d2017-08-10 15:29:34 -050076std::string Version::getBMCVersion(const std::string& releaseFilePath)
Saqib Khanba239882017-05-26 08:41:54 -050077{
78 std::string versionKey = "VERSION_ID=";
79 std::string version{};
80 std::ifstream efile;
81 std::string line;
Saqib Khan1eef62d2017-08-10 15:29:34 -050082 efile.open(releaseFilePath);
Saqib Khanba239882017-05-26 08:41:54 -050083
84 while (getline(efile, line))
85 {
86 if (line.substr(0, versionKey.size()).find(versionKey) !=
87 std::string::npos)
88 {
89 std::size_t pos = line.find_first_of('"') + 1;
90 version = line.substr(pos, line.find_last_of('"') - pos);
91 break;
92 }
93 }
94 efile.close();
95
96 if (version.empty())
97 {
98 log<level::ERR>("Error BMC current version is empty");
99 throw std::runtime_error("BMC current version is empty");
100 }
101
102 return version;
103}
104
Eddie James6d873712017-09-01 11:29:07 -0500105bool Version::isFunctional()
106{
107 return versionStr == getBMCVersion(OS_RELEASE_FILE);
108}
109
Gunnar Mills392f2942017-04-12 11:04:37 -0500110} // namespace manager
111} // namespace software
112} // namepsace phosphor