blob: 4260a89fde51d4ee2bb4e40b599ec21ef7bdce4c [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
75} // namespace manager
76} // namespace software
77} // namepsace phosphor
78