blob: c7fab30f59c59d8b7d91048cd7a6acaba8a9a1a8 [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>
7#include "version.hpp"
8
9namespace phosphor
10{
11namespace software
12{
13namespace manager
14{
15
16using namespace phosphor::logging;
17
Gunnar Millscebd1022017-04-17 16:10:15 -050018std::string Version::getValue(const std::string& manifestFilePath,
19 std::string key)
Gunnar Mills392f2942017-04-12 11:04:37 -050020{
Gunnar Millscebd1022017-04-17 16:10:15 -050021 key = key + "=";
22 auto keySize = key.length();
Gunnar Mills392f2942017-04-12 11:04:37 -050023
24 if (manifestFilePath.empty())
25 {
26 log<level::ERR>("Error MANIFESTFilePath is empty");
27 throw std::runtime_error("MANIFESTFilePath is empty");
28 }
29
Gunnar Millscebd1022017-04-17 16:10:15 -050030 std::string value{};
Gunnar Mills392f2942017-04-12 11:04:37 -050031 std::ifstream efile;
32 std::string line;
33 efile.exceptions(std::ifstream::failbit
34 | std::ifstream::badbit
35 | std::ifstream::eofbit);
36
37 // Too many GCC bugs (53984, 66145) to do this the right way...
38 try
39 {
40 efile.open(manifestFilePath);
41 while (getline(efile, line))
42 {
Gunnar Millscebd1022017-04-17 16:10:15 -050043 if (line.compare(0, keySize, key) == 0)
Gunnar Mills392f2942017-04-12 11:04:37 -050044 {
Gunnar Millscebd1022017-04-17 16:10:15 -050045 value = line.substr(keySize);
Gunnar Mills392f2942017-04-12 11:04:37 -050046 break;
47 }
48 }
49 efile.close();
50 }
51 catch (const std::exception& e)
52 {
Gunnar Millscebd1022017-04-17 16:10:15 -050053 log<level::ERR>("Error in reading MANIFEST file");
Gunnar Mills392f2942017-04-12 11:04:37 -050054 }
55
Gunnar Millscebd1022017-04-17 16:10:15 -050056 return value;
Gunnar Mills392f2942017-04-12 11:04:37 -050057}
58
59std::string Version::getId(const std::string& version)
60{
61 std::stringstream hexId;
62
63 if (version.empty())
64 {
Gunnar Millscebd1022017-04-17 16:10:15 -050065 log<level::ERR>("Error version is empty");
66 throw std::runtime_error("Version is empty");
Gunnar Mills392f2942017-04-12 11:04:37 -050067 }
68
69 // Only want 8 hex digits.
70 hexId << std::hex << ((std::hash<std::string> {}(
71 version)) & 0xFFFFFFFF);
72 return hexId.str();
73}
74
Saqib Khanba239882017-05-26 08:41:54 -050075std::string Version::getBMCVersion()
76{
77 std::string versionKey = "VERSION_ID=";
78 std::string version{};
79 std::ifstream efile;
80 std::string line;
81 efile.open("/etc/os-release");
82
83 while (getline(efile, line))
84 {
85 if (line.substr(0, versionKey.size()).find(versionKey) !=
86 std::string::npos)
87 {
88 std::size_t pos = line.find_first_of('"') + 1;
89 version = line.substr(pos, line.find_last_of('"') - pos);
90 break;
91 }
92 }
93 efile.close();
94
95 if (version.empty())
96 {
97 log<level::ERR>("Error BMC current version is empty");
98 throw std::runtime_error("BMC current version is empty");
99 }
100
101 return version;
102}
103
Leonel Gonzaleze4233682017-07-07 14:38:25 -0500104void Version::delete_()
105{
106 if (eraseCallback)
107 {
108 eraseCallback(getId(version()));
109 }
110}
111
Gunnar Mills392f2942017-04-12 11:04:37 -0500112} // namespace manager
113} // namespace software
114} // namepsace phosphor
115