From 4fb952ca9bcdbb53a9e4cb1d1b181b1ae97f9083 Mon Sep 17 00:00:00 2001 From: Michael Tritz Date: Sun, 13 Aug 2017 14:55:04 -0500 Subject: PNOR: Store a redundant copy of the priority persistence file In this commit, the priority persistence functionality is extended by storing a second copy of each persistence file in the RW volume for its version. Upon a reboot, the corresponding restoration from file checks the second location if necessary. Resolves openbmc/openbmc#2133 Change-Id: Ie1926ad8500d49e7ec7cf71bd703664ac23c6a7a Signed-off-by: Michael Tritz --- serialize.cpp | 46 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 36 insertions(+), 10 deletions(-) (limited to 'serialize.cpp') diff --git a/serialize.cpp b/serialize.cpp index a499798c4..8c63264f5 100644 --- a/serialize.cpp +++ b/serialize.cpp @@ -19,28 +19,54 @@ void storeToFile(std::string versionId, uint8_t priority) { fs::create_directory(PERSIST_DIR); } - std::string path = PERSIST_DIR + versionId; - std::ofstream os(path.c_str()); - cereal::JSONOutputArchive oarchive(os); - oarchive(cereal::make_nvp("priority", priority)); + // store one copy in /var/lib/obmc/openpower-pnor-code-mgmt/[versionId] + auto varPath = PERSIST_DIR + versionId; + std::ofstream varOutput(varPath.c_str()); + cereal::JSONOutputArchive varArchive(varOutput); + varArchive(cereal::make_nvp("priority", priority)); + + if(fs::is_directory(PNOR_RW_PREFIX + versionId)) + { + // store another copy in /media/pnor-rw-[versionId]/[versionId] + auto rwPath = PNOR_RW_PREFIX + versionId + "/" + versionId; + std::ofstream rwOutput(rwPath.c_str()); + cereal::JSONOutputArchive rwArchive(rwOutput); + rwArchive(cereal::make_nvp("priority", priority)); + } } bool restoreFromFile(std::string versionId, uint8_t& priority) { - std::string path = PERSIST_DIR + versionId; - if (fs::exists(path)) + auto varPath = PERSIST_DIR + versionId; + if (fs::exists(varPath)) + { + std::ifstream varInput(varPath.c_str(), std::ios::in); + try + { + cereal::JSONInputArchive varArchive(varInput); + varArchive(cereal::make_nvp("priority", priority)); + return true; + } + catch(cereal::RapidJSONException& e) + { + fs::remove(varPath); + } + } + + auto rwPath = PNOR_RW_PREFIX + versionId + "/" + versionId; + if (fs::exists(rwPath)) { - std::ifstream is(path.c_str(), std::ios::in); + std::ifstream rwInput(rwPath.c_str(), std::ios::in); try { - cereal::JSONInputArchive iarchive(is); - iarchive(cereal::make_nvp("priority", priority)); + cereal::JSONInputArchive rwArchive(rwInput); + rwArchive(cereal::make_nvp("priority", priority)); return true; } catch(cereal::RapidJSONException& e) { - fs::remove(path); + fs::remove(rwPath); } } return false; -- cgit v1.2.1