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