Kuiying Wang | ce75d9e | 2020-12-15 16:17:05 +0800 | [diff] [blame] | 1 | #include "manager_serialize.hpp" |
| 2 | |
| 3 | #include <cereal/archives/binary.hpp> |
| 4 | #include <cereal/cereal.hpp> |
| 5 | #include <cereal/types/map.hpp> |
| 6 | #include <cereal/types/string.hpp> |
| 7 | #include <cereal/types/tuple.hpp> |
| 8 | #include <cereal/types/variant.hpp> |
| 9 | #include <cereal/types/vector.hpp> |
| 10 | #include <phosphor-logging/log.hpp> |
| 11 | |
| 12 | #include <fstream> |
| 13 | |
| 14 | namespace bios_config |
| 15 | { |
| 16 | |
| 17 | using namespace phosphor::logging; |
| 18 | |
| 19 | /** @brief Function required by Cereal to perform serialization. |
| 20 | * |
| 21 | * @tparam Archive - Cereal archive type (binary in this case). |
| 22 | * @param[in] archive - reference to cereal archive. |
| 23 | * @param[in] entry- const reference to bios manager object |
| 24 | * @param[in] version - Class version that enables handling a serialized data |
| 25 | * across code levels |
| 26 | */ |
| 27 | template <class Archive> |
| 28 | void save(Archive& archive, const Manager& entry, |
| 29 | const std::uint32_t /*version*/) |
| 30 | { |
| 31 | archive(entry.sdbusplus::xyz::openbmc_project::BIOSConfig::server::Manager:: |
| 32 | baseBIOSTable(), |
| 33 | entry.sdbusplus::xyz::openbmc_project::BIOSConfig::server::Manager:: |
| 34 | pendingAttributes()); |
| 35 | } |
| 36 | |
| 37 | /** @brief Function required by Cereal to perform deserialization. |
| 38 | * |
| 39 | * @tparam Archive - Cereal archive type (binary in our case). |
| 40 | * @param[in] archive - reference to cereal archive. |
| 41 | * @param[out] entry - reference to bios manager object |
| 42 | * @param[in] version - Class version that enables handling a serialized data |
| 43 | * across code levels |
| 44 | */ |
| 45 | template <class Archive> |
| 46 | void load(Archive& archive, Manager& entry, const std::uint32_t /*version*/) |
| 47 | { |
| 48 | Manager::BaseTable baseTable; |
| 49 | Manager::PendingAttributes pendingAttrs; |
| 50 | |
| 51 | archive(baseTable, pendingAttrs); |
| 52 | entry.sdbusplus::xyz::openbmc_project::BIOSConfig::server::Manager:: |
| 53 | baseBIOSTable(baseTable, true); |
| 54 | entry.sdbusplus::xyz::openbmc_project::BIOSConfig::server::Manager:: |
| 55 | pendingAttributes(pendingAttrs, true); |
| 56 | } |
| 57 | |
| 58 | void serialize(const Manager& obj, const fs::path& path) |
| 59 | { |
| 60 | std::ofstream os(path.c_str(), std::ios::out | std::ios::binary); |
| 61 | cereal::BinaryOutputArchive oarchive(os); |
| 62 | oarchive(obj); |
| 63 | } |
| 64 | |
| 65 | bool deserialize(const fs::path& path, Manager& entry) |
| 66 | { |
| 67 | try |
| 68 | { |
| 69 | if (fs::exists(path)) |
| 70 | { |
| 71 | std::ifstream is(path.c_str(), std::ios::in | std::ios::binary); |
| 72 | cereal::BinaryInputArchive iarchive(is); |
| 73 | iarchive(entry); |
| 74 | return true; |
| 75 | } |
| 76 | return false; |
| 77 | } |
| 78 | catch (cereal::Exception& e) |
| 79 | { |
| 80 | log<level::ERR>(e.what()); |
| 81 | fs::remove(path); |
| 82 | return false; |
| 83 | } |
| 84 | catch (const std::length_error& e) |
| 85 | { |
| 86 | log<level::ERR>(e.what()); |
| 87 | fs::remove(path); |
| 88 | return false; |
| 89 | } |
| 90 | } |
| 91 | |
Tom Joseph | f1101df | 2020-11-06 08:23:34 +0530 | [diff] [blame] | 92 | } // namespace bios_config |