Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
| 3 | #include "ldap_mapper_serialize.hpp" |
| 4 | |
Tom Joseph | 0b14c47 | 2018-09-30 01:42:59 +0530 | [diff] [blame] | 5 | #include <cereal/archives/binary.hpp> |
| 6 | #include <cereal/types/string.hpp> |
Tom Joseph | 0b14c47 | 2018-09-30 01:42:59 +0530 | [diff] [blame] | 7 | #include <phosphor-logging/log.hpp> |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 8 | |
| 9 | #include <filesystem> |
| 10 | #include <fstream> |
Tom Joseph | 0b14c47 | 2018-09-30 01:42:59 +0530 | [diff] [blame] | 11 | |
| 12 | // Register class version |
| 13 | // From cereal documentation; |
| 14 | // "This macro should be placed at global scope" |
| 15 | CEREAL_CLASS_VERSION(phosphor::user::LDAPMapperEntry, CLASS_VERSION); |
| 16 | |
| 17 | namespace phosphor |
| 18 | { |
| 19 | namespace user |
| 20 | { |
| 21 | |
| 22 | using namespace phosphor::logging; |
| 23 | |
| 24 | /** @brief Function required by Cereal to perform serialization. |
| 25 | * |
| 26 | * @tparam Archive - Cereal archive type (binary in this case). |
| 27 | * @param[in] archive - reference to cereal archive. |
| 28 | * @param[in] entry- const reference to LDAP mapper entry |
| 29 | * @param[in] version - Class version that enables handling a serialized data |
| 30 | * across code levels |
| 31 | */ |
| 32 | template <class Archive> |
| 33 | void save(Archive& archive, const LDAPMapperEntry& entry, |
Ratan Gupta | 0b1ad3d | 2022-01-09 14:09:35 +0530 | [diff] [blame] | 34 | const std::uint32_t /*version*/) |
Tom Joseph | 0b14c47 | 2018-09-30 01:42:59 +0530 | [diff] [blame] | 35 | { |
| 36 | archive(entry.groupName(), entry.privilege()); |
| 37 | } |
| 38 | |
| 39 | /** @brief Function required by Cereal to perform deserialization. |
| 40 | * |
| 41 | * @tparam Archive - Cereal archive type (binary in our case). |
| 42 | * @param[in] archive - reference to cereal archive. |
| 43 | * @param[out] entry - LDAP mapper entry to be read |
| 44 | * @param[in] version - Class version that enables handling a serialized data |
| 45 | * across code levels |
| 46 | */ |
| 47 | template <class Archive> |
Ratan Gupta | 0b1ad3d | 2022-01-09 14:09:35 +0530 | [diff] [blame] | 48 | void load(Archive& archive, LDAPMapperEntry& entry, |
| 49 | const std::uint32_t /*version*/) |
Tom Joseph | 0b14c47 | 2018-09-30 01:42:59 +0530 | [diff] [blame] | 50 | { |
| 51 | std::string groupName{}; |
| 52 | std::string privilege{}; |
| 53 | |
| 54 | archive(groupName, privilege); |
| 55 | |
| 56 | entry.sdbusplus::xyz::openbmc_project::User::server::PrivilegeMapperEntry:: |
| 57 | groupName(groupName, true); |
| 58 | entry.sdbusplus::xyz::openbmc_project::User::server::PrivilegeMapperEntry:: |
| 59 | privilege(privilege, true); |
| 60 | } |
| 61 | |
Gunnar Mills | 703131f | 2020-10-28 14:26:33 -0500 | [diff] [blame] | 62 | std::filesystem::path serialize(const LDAPMapperEntry& entry, Id id, |
| 63 | const std::filesystem::path& dir) |
Tom Joseph | 0b14c47 | 2018-09-30 01:42:59 +0530 | [diff] [blame] | 64 | { |
Tom Joseph | 0b14c47 | 2018-09-30 01:42:59 +0530 | [diff] [blame] | 65 | auto path = dir / std::to_string(id); |
| 66 | std::ofstream os(path.c_str(), std::ios::binary); |
| 67 | cereal::BinaryOutputArchive oarchive(os); |
| 68 | oarchive(entry); |
| 69 | return path; |
| 70 | } |
| 71 | |
Gunnar Mills | 703131f | 2020-10-28 14:26:33 -0500 | [diff] [blame] | 72 | bool deserialize(const std::filesystem::path& path, LDAPMapperEntry& entry) |
Tom Joseph | 0b14c47 | 2018-09-30 01:42:59 +0530 | [diff] [blame] | 73 | { |
| 74 | try |
| 75 | { |
Gunnar Mills | 703131f | 2020-10-28 14:26:33 -0500 | [diff] [blame] | 76 | if (std::filesystem::exists(path)) |
Tom Joseph | 0b14c47 | 2018-09-30 01:42:59 +0530 | [diff] [blame] | 77 | { |
| 78 | std::ifstream is(path.c_str(), std::ios::in | std::ios::binary); |
| 79 | cereal::BinaryInputArchive iarchive(is); |
| 80 | iarchive(entry); |
| 81 | return true; |
| 82 | } |
| 83 | return false; |
| 84 | } |
Patrick Williams | d019e3d | 2021-10-06 12:46:55 -0500 | [diff] [blame] | 85 | catch (const cereal::Exception& e) |
Tom Joseph | 0b14c47 | 2018-09-30 01:42:59 +0530 | [diff] [blame] | 86 | { |
| 87 | log<level::ERR>(e.what()); |
Gunnar Mills | 703131f | 2020-10-28 14:26:33 -0500 | [diff] [blame] | 88 | std::filesystem::remove(path); |
Tom Joseph | 0b14c47 | 2018-09-30 01:42:59 +0530 | [diff] [blame] | 89 | return false; |
| 90 | } |
| 91 | catch (const std::length_error& e) |
| 92 | { |
| 93 | log<level::ERR>(e.what()); |
Gunnar Mills | 703131f | 2020-10-28 14:26:33 -0500 | [diff] [blame] | 94 | std::filesystem::remove(path); |
Tom Joseph | 0b14c47 | 2018-09-30 01:42:59 +0530 | [diff] [blame] | 95 | return false; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | } // namespace user |
| 100 | } // namespace phosphor |