blob: 85f417c7bf7d3f7ff4730467e654aabfd1eb2cc5 [file] [log] [blame]
#include "file_table.hpp"
#include <boost/crc.hpp>
#include <fstream>
#include <phosphor-logging/log.hpp>
namespace pldm
{
namespace filetable
{
using namespace phosphor::logging;
FileTable::FileTable(const std::string& fileTableConfigPath)
{
std::ifstream jsonFile(fileTableConfigPath);
if (!jsonFile.is_open())
{
log<level::ERR>("File table config file does not exist",
entry("FILE=%s", fileTableConfigPath.c_str()));
return;
}
auto data = Json::parse(jsonFile, nullptr, false);
if (data.is_discarded())
{
log<level::ERR>("Parsing config file failed");
return;
}
uint16_t fileNameLength = 0;
uint32_t fileSize = 0;
uint32_t traits = 0;
size_t tableSize = 0;
Handle handle = 0;
auto iter = fileTable.begin();
// Iterate through each JSON object in the config file
for (const auto& record : data)
{
constexpr auto path = "path";
constexpr auto fileTraits = "file_traits";
std::string filepath = record.value(path, "");
traits = static_cast<uint32_t>(record.value(fileTraits, 0));
fs::path fsPath(filepath);
if (!fs::is_regular_file(fsPath))
{
continue;
}
fileNameLength =
static_cast<uint16_t>(fsPath.filename().string().size());
fileSize = static_cast<uint32_t>(fs::file_size(fsPath));
tableSize = fileTable.size();
fileTable.resize(tableSize + sizeof(handle) + sizeof(fileNameLength) +
fileNameLength + sizeof(fileSize) + sizeof(traits));
iter = fileTable.begin() + tableSize;
// Populate the file table with the contents of the JSON entry
std::copy_n(reinterpret_cast<uint8_t*>(&handle), sizeof(handle), iter);
std::advance(iter, sizeof(handle));
std::copy_n(reinterpret_cast<uint8_t*>(&fileNameLength),
sizeof(fileNameLength), iter);
std::advance(iter, sizeof(fileNameLength));
std::copy_n(reinterpret_cast<const uint8_t*>(fsPath.filename().c_str()),
fileNameLength, iter);
std::advance(iter, fileNameLength);
std::copy_n(reinterpret_cast<uint8_t*>(&fileSize), sizeof(fileSize),
iter);
std::advance(iter, sizeof(fileSize));
std::copy_n(reinterpret_cast<uint8_t*>(&traits), sizeof(traits), iter);
std::advance(iter, sizeof(traits));
// Create the file entry for the JSON entry
FileEntry entry{};
entry.handle = handle;
entry.fsPath = std::move(fsPath);
entry.traits.value = traits;
// Insert the file entries in the map
tableEntries.emplace(handle, std::move(entry));
handle++;
}
constexpr uint8_t padWidth = 4;
tableSize = fileTable.size();
// Add pad bytes
if ((tableSize % padWidth) != 0)
{
padCount = padWidth - (tableSize % padWidth);
fileTable.resize(tableSize + padCount, 0);
}
// Calculate the checksum
boost::crc_32_type result;
result.process_bytes(fileTable.data(), fileTable.size());
checkSum = result.checksum();
}
Table FileTable::operator()() const
{
Table table(fileTable);
table.resize(fileTable.size() + sizeof(checkSum));
auto iter = table.begin() + fileTable.size();
std::copy_n(reinterpret_cast<const uint8_t*>(&checkSum), sizeof(checkSum),
iter);
return table;
}
} // namespace filetable
} // namespace pldm