Refactor singleton and filesystem path handling
- Remove singleton PostCodeDataHolder. It only contained a single int
which could easily be held inside the main PostCode class instead.
- Simplify D-Bus match construction by using predefined
PropertiesChanged rule.
- Remove unnecessary PostCode members which were just copies of const
strings.
- Refactor some filesystem path construction/handling to simplify code.
Tested:
Rebooted host a few times and observed that correct POST code history is
still populated in Redfish.
Signed-off-by: Jonathan Doman <jonathan.doman@intel.com>
Change-Id: Ifd61751807da704eaf2a64dac34ca708fd28c872
diff --git a/src/main.cpp b/src/main.cpp
index 98e09fb..6d53629 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -19,12 +19,10 @@
int main(int argc, char* argv[])
{
- PostCodeDataHolder& postcodeDataHolderObj =
- PostCodeDataHolder::getInstance();
-
int arg;
int optIndex = 0;
int ret = 0;
+ int node = 0;
std::string intfName;
@@ -36,7 +34,7 @@
switch (arg)
{
case 'h':
- postcodeDataHolderObj.node = std::stoi(optarg);
+ node = std::stoi(optarg);
break;
default:
break;
@@ -59,15 +57,14 @@
sdbusplus::bus_t bus = sdbusplus::bus::new_default();
- std::string dbusObjName =
- DBUS_OBJECT_NAME + std::to_string(postcodeDataHolderObj.node);
+ std::string dbusObjName = DBUS_OBJECT_NAME + std::to_string(node);
sdbusplus::server::manager_t m{bus, dbusObjName.c_str()};
- intfName = DBUS_INTF_NAME + std::to_string(postcodeDataHolderObj.node);
+ intfName = DBUS_INTF_NAME + std::to_string(node);
bus.request_name(intfName.c_str());
- PostCode postCode{bus, dbusObjName.c_str(), eventP};
+ PostCode postCode{bus, dbusObjName.c_str(), node};
try
{
diff --git a/src/post_code.cpp b/src/post_code.cpp
index 82dba46..6186f97 100644
--- a/src/post_code.cpp
+++ b/src/post_code.cpp
@@ -15,18 +15,14 @@
*/
#include "post_code.hpp"
-#include "iomanip"
+#include <iomanip>
void PostCode::deleteAll()
{
- auto dir = fs::path(postcodeDataHolderObj.PostCodeListPathPrefix +
- std::to_string(postcodeDataHolderObj.node));
- std::uintmax_t n = fs::remove_all(dir);
+ std::uintmax_t n = fs::remove_all(postCodeListPath);
std::cerr << "clearPostCodes deleted " << n << " files in "
- << postcodeDataHolderObj.PostCodeListPathPrefix +
- std::to_string(postcodeDataHolderObj.node)
- << std::endl;
- fs::create_directories(dir);
+ << postCodeListPath << std::endl;
+ fs::create_directories(postCodeListPath);
postCodes.clear();
currentBootCycleIndex = 0;
currentBootCycleCount(0);
@@ -46,8 +42,7 @@
uint16_t bootNum = getBootNum(index);
decltype(postCodes) codes;
- deserializePostCodes(
- fs::path(strPostCodeListPath + std::to_string(bootNum)), codes);
+ deserializePostCodes(postCodeListPath / std::to_string(bootNum), codes);
std::transform(codes.begin(), codes.end(), std::back_inserter(codesVec),
[](const auto& kv) { return kv.second; });
}
@@ -64,8 +59,7 @@
uint16_t bootNum = getBootNum(index);
decltype(postCodes) codes;
- deserializePostCodes(
- fs::path(strPostCodeListPath + std::to_string(bootNum)), codes);
+ deserializePostCodes(postCodeListPath / std::to_string(bootNum), codes);
return codes;
}
@@ -97,7 +91,7 @@
{
postCodes.erase(postCodes.begin());
}
- serialize(fs::path(strPostCodeListPath));
+ serialize(postCodeListPath);
#ifdef ENABLE_BIOS_POST_CODE_LOG
uint64_t usTimeOffset = tsUS - firstPostCodeUsSinceEpoch;
@@ -125,23 +119,20 @@
return;
}
-fs::path PostCode::serialize(const std::string& path)
+fs::path PostCode::serialize(const fs::path& path)
{
try
{
- fs::path idxPath(path + strCurrentBootCycleIndexName);
- std::ofstream osIdx(idxPath.c_str(), std::ios::binary);
+ std::ofstream osIdx(path / CurrentBootCycleIndexName, std::ios::binary);
cereal::JSONOutputArchive idxArchive(osIdx);
idxArchive(currentBootCycleIndex);
uint16_t count = currentBootCycleCount();
- fs::path cntPath(path + strCurrentBootCycleCountName);
- std::ofstream osCnt(cntPath.c_str(), std::ios::binary);
+ std::ofstream osCnt(path / CurrentBootCycleCountName, std::ios::binary);
cereal::JSONOutputArchive cntArchive(osCnt);
cntArchive(count);
- std::ofstream osPostCodes(
- (path + std::to_string(currentBootCycleIndex)));
+ std::ofstream osPostCodes(path / std::to_string(currentBootCycleIndex));
cereal::JSONOutputArchive oarchivePostCodes(osPostCodes);
oarchivePostCodes(postCodes);
}
@@ -164,7 +155,7 @@
{
if (fs::exists(path))
{
- std::ifstream is(path.c_str(), std::ios::in | std::ios::binary);
+ std::ifstream is(path, std::ios::in | std::ios::binary);
cereal::JSONInputArchive iarchive(is);
iarchive(index);
return true;
@@ -191,7 +182,7 @@
{
if (fs::exists(path))
{
- std::ifstream is(path.c_str(), std::ios::in | std::ios::binary);
+ std::ifstream is(path, std::ios::in | std::ios::binary);
cereal::JSONInputArchive iarchive(is);
iarchive(codes);
return true;