blob: c167a69bc409eaafb6dd20364fd73f4847496ed6 [file] [log] [blame]
Patrick Williams9638afb2021-02-22 17:16:24 -06001#include "config.h"
2
3#include "ldap_mapper_serialize.hpp"
4
Tom Joseph0b14c472018-09-30 01:42:59 +05305#include <cereal/archives/binary.hpp>
6#include <cereal/types/string.hpp>
Tom Joseph0b14c472018-09-30 01:42:59 +05307#include <phosphor-logging/log.hpp>
Patrick Williams9638afb2021-02-22 17:16:24 -06008
9#include <filesystem>
10#include <fstream>
Tom Joseph0b14c472018-09-30 01:42:59 +053011
12// Register class version
13// From cereal documentation;
14// "This macro should be placed at global scope"
15CEREAL_CLASS_VERSION(phosphor::user::LDAPMapperEntry, CLASS_VERSION);
16
17namespace phosphor
18{
19namespace user
20{
21
22using 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 */
32template <class Archive>
33void save(Archive& archive, const LDAPMapperEntry& entry,
Ratan Gupta0b1ad3d2022-01-09 14:09:35 +053034 const std::uint32_t /*version*/)
Tom Joseph0b14c472018-09-30 01:42:59 +053035{
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 */
47template <class Archive>
Ratan Gupta0b1ad3d2022-01-09 14:09:35 +053048void load(Archive& archive, LDAPMapperEntry& entry,
49 const std::uint32_t /*version*/)
Tom Joseph0b14c472018-09-30 01:42:59 +053050{
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 Mills703131f2020-10-28 14:26:33 -050062std::filesystem::path serialize(const LDAPMapperEntry& entry, Id id,
63 const std::filesystem::path& dir)
Tom Joseph0b14c472018-09-30 01:42:59 +053064{
Tom Joseph0b14c472018-09-30 01:42:59 +053065 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 Mills703131f2020-10-28 14:26:33 -050072bool deserialize(const std::filesystem::path& path, LDAPMapperEntry& entry)
Tom Joseph0b14c472018-09-30 01:42:59 +053073{
74 try
75 {
Gunnar Mills703131f2020-10-28 14:26:33 -050076 if (std::filesystem::exists(path))
Tom Joseph0b14c472018-09-30 01:42:59 +053077 {
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 Williamsd019e3d2021-10-06 12:46:55 -050085 catch (const cereal::Exception& e)
Tom Joseph0b14c472018-09-30 01:42:59 +053086 {
87 log<level::ERR>(e.what());
Gunnar Mills703131f2020-10-28 14:26:33 -050088 std::filesystem::remove(path);
Tom Joseph0b14c472018-09-30 01:42:59 +053089 return false;
90 }
91 catch (const std::length_error& e)
92 {
93 log<level::ERR>(e.what());
Gunnar Mills703131f2020-10-28 14:26:33 -050094 std::filesystem::remove(path);
Tom Joseph0b14c472018-09-30 01:42:59 +053095 return false;
96 }
97}
98
99} // namespace user
100} // namespace phosphor