blob: 47b88a92c6e53ff410a2248bc1fbbf92600d01f1 [file] [edit]
#pragma once
#include "common/utils.hpp"
#include "pdr_utils.hpp"
#include <libpldm/platform.h>
#include <sys/stat.h>
#include <time.h>
#include <nlohmann/json.hpp>
#include <chrono>
namespace pldm
{
namespace responder
{
namespace pdr_file
{
/**
* Data structue to hold the complete file path, C style file pointer, 16 bit
* file indetifier and record handle
*/
struct FileInfo
{
std::string filePath = "";
uint32_t fileSize;
FILE* fp = nullptr;
uint16_t fileIdentifier = 0;
uint32_t recordHandle = 0;
struct timespec last_modified{};
pldm::utils::Json fileJsonEntry;
std::string directoryPath = "";
uint16_t entityType = 0;
uint16_t containerId = 0;
std::string configFilePath = "";
struct timespec config_last_modified{};
std::chrono::steady_clock::time_point snapshotCreationTime{};
};
/**
* @brief Parse PDR JSON file and generate file descriptor PDR structure
*
* @param[in] json - the JSON Object with the file descriptor PDR
* @param[in] configFilePath - path to the JSON configuration file
* @param[in] config_last_modified - last modified time of the JSON
* configuration file
* @param[out] fileIdentifierToFileInfo - store FileInfo details based on the
* fileIdentifier
* @param[out] recordHandleToFileInfo - store FileInfo details based on the
* recordHandle
* @param[out] repo - pdr::RepoInterface
*/
void generateFileDescriptorPDR(
const nlohmann::json& json, const std::string& configFilePath,
const struct timespec& config_last_modified,
std::map<uint16_t, FileInfo>& fileIdentifierToFileInfo,
std::map<uint32_t, FileInfo>& recordHandleToFileInfo,
pdr_utils::RepoInterface& repo);
/** @brief Fetch the updated file entry from the configuration JSON
*
* This API is needed to isolate the logic of parsing the PDR configuration
* JSON to find a specific file entry when a configuration change occurs.
* Since properties like FileIdentifier or FileSize can change, this function
* uses the FileName as a stable key to locate the correct file definition
* within the newly updated JSON structure.
*
* @param[in] configFilePath - path to the JSON configuration file
* @param[in] fileName - the name of the file to locate
* @param[out] fileEntry - the extracted JSON object for the file
*
* @return true if the file entry was successfully found, false otherwise
*/
bool getFileEntryFromConfig(const std::string& configFilePath,
const std::string& fileName,
nlohmann::json& fileEntry);
/**
* @brief Generate a single file PDR
*
* @param[in] fileEntry - the JSON object representing the file entry
* @param[in] directoryPath - the path to the directory containing the file
* @param[in] entityType - the type of the entity
* @param[in] containerId - the ID of the container
* @param[in] configFilePath - path to the JSON configuration file
* @param[in] config_last_modified - last modified time of the JSON
* configuration file
* @param[out] fileIdentifierToFileInfo - store FileInfo details based on the
* fileIdentifier
* @param[out] recordHandleToFileInfo - store FileInfo details based on the
* recordHandle
* @param[out] repo - pdr::RepoInterface
* @param[out] recordHandleToUpdate - if provided, this record handle will be
* used to store the PDR instead of generating a new one. This is useful when we
* want to update an existing PDR with new file information.
*/
bool generateSingleFilePDR(
const nlohmann::json& fileEntry, const std::string& directoryPath,
uint16_t entityType, uint16_t containerId,
const std::string& configFilePath,
const struct timespec& config_last_modified,
std::map<uint16_t, FileInfo>& fileIdentifierToFileInfo,
std::map<uint32_t, FileInfo>& recordHandleToFileInfo,
pdr_utils::RepoInterface& repo, uint32_t* recordHandleToUpdate = nullptr);
} // namespace pdr_file
} // namespace responder
} // namespace pldm