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