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 | { |
Manojkiran Eda | ae2ed8d | 2024-10-15 10:42:17 +0530 | [diff] [blame^] | 58 | try |
| 59 | { |
| 60 | std::ofstream os(path, std::ios::out | std::ios::binary); |
| 61 | |
| 62 | if (!os.is_open()) |
| 63 | { |
| 64 | lg2::error("Failed to open file for serialization: {FILE}", "FILE", |
| 65 | path); |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | cereal::BinaryOutputArchive oarchive(os); |
| 70 | oarchive(obj); |
| 71 | } |
| 72 | catch (const std::exception& e) |
| 73 | { |
| 74 | lg2::error("Failed to Serialize : {ERROR} ", "ERROR", e); |
| 75 | } |
Kuiying Wang | ce75d9e | 2020-12-15 16:17:05 +0800 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | bool deserialize(const fs::path& path, Manager& entry) |
| 79 | { |
| 80 | try |
| 81 | { |
| 82 | if (fs::exists(path)) |
| 83 | { |
| 84 | std::ifstream is(path.c_str(), std::ios::in | std::ios::binary); |
Manojkiran Eda | ae2ed8d | 2024-10-15 10:42:17 +0530 | [diff] [blame^] | 85 | if (!is.is_open()) |
| 86 | { |
| 87 | lg2::error("Failed to open file for deserialization: {FILE}", |
| 88 | "FILE", path); |
| 89 | return false; |
| 90 | } |
Kuiying Wang | ce75d9e | 2020-12-15 16:17:05 +0800 | [diff] [blame] | 91 | cereal::BinaryInputArchive iarchive(is); |
| 92 | iarchive(entry); |
| 93 | return true; |
| 94 | } |
| 95 | return false; |
| 96 | } |
| 97 | catch (cereal::Exception& e) |
| 98 | { |
George Liu | 567a3cf | 2021-12-08 10:36:03 +0800 | [diff] [blame] | 99 | lg2::error("Failed to serialize: {ERROR}", "ERROR", e); |
Kuiying Wang | ce75d9e | 2020-12-15 16:17:05 +0800 | [diff] [blame] | 100 | fs::remove(path); |
| 101 | return false; |
| 102 | } |
| 103 | catch (const std::length_error& e) |
| 104 | { |
George Liu | 567a3cf | 2021-12-08 10:36:03 +0800 | [diff] [blame] | 105 | lg2::error("Failed to serialize: {ERROR}", "ERROR", e); |
Kuiying Wang | ce75d9e | 2020-12-15 16:17:05 +0800 | [diff] [blame] | 106 | fs::remove(path); |
| 107 | return false; |
| 108 | } |
| 109 | } |
| 110 | |
Patrick Williams | f19e271 | 2023-05-10 07:51:20 -0500 | [diff] [blame] | 111 | } // namespace bios_config |