blob: 7719270bc2d312839f71ba7b905b89e1996bbb41 [file] [log] [blame]
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include <stdexcept>
#include <phosphor-logging/log.hpp>
#include "version_host_software_manager.hpp"
namespace openpower
{
namespace software
{
namespace manager
{
using namespace phosphor::logging;
std::string Version::getVersion(const std::string& tocFilePath)
{
constexpr auto versionKey = "version=";
constexpr auto versionKeySize = strlen(versionKey);
if (tocFilePath.empty())
{
log<level::ERR>("Error TocFilePath is empty");
throw std::runtime_error("TocFilePath is empty");
}
std::string version{};
std::ifstream efile;
std::string line;
efile.exceptions(std::ifstream::failbit
| std::ifstream::badbit
| std::ifstream::eofbit);
// Too many GCC bugs (53984, 66145) to do this the right way...
try
{
efile.open(tocFilePath);
while (getline(efile, line))
{
if (line.compare(0, versionKeySize, versionKey) == 0)
{
version = line.substr(versionKeySize);
break;
}
}
efile.close();
}
catch (const std::exception& e)
{
log<level::ERR>("Error in reading Host pnor.toc file");
}
return version;
}
std::string Version::getId(const std::string& version)
{
std::stringstream hexId;
if (version.empty())
{
log<level::ERR>("Error Host version is empty");
throw std::runtime_error("Host version is empty");
}
// Only want 8 hex digits.
hexId << std::hex << ((std::hash<std::string> {}(
version)) & 0xFFFFFFFF);
return hexId.str();
}
} // namespace manager
} // namespace software
} // namepsace openpower