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