blob: 65918fd5567869ae5bf39b9034dfc4a676665730 [file] [log] [blame]
Tom Joseph0b14c472018-09-30 01:42:59 +05301#include <cereal/archives/binary.hpp>
2#include <cereal/types/string.hpp>
3#include <fstream>
Gunnar Mills703131f2020-10-28 14:26:33 -05004#include <filesystem>
Tom Joseph0b14c472018-09-30 01:42:59 +05305#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"
12CEREAL_CLASS_VERSION(phosphor::user::LDAPMapperEntry, CLASS_VERSION);
13
14namespace phosphor
15{
16namespace user
17{
18
19using 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 */
29template <class Archive>
30void 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 */
44template <class Archive>
45void 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 Mills703131f2020-10-28 14:26:33 -050058std::filesystem::path serialize(const LDAPMapperEntry& entry, Id id,
59 const std::filesystem::path& dir)
Tom Joseph0b14c472018-09-30 01:42:59 +053060{
Tom Joseph0b14c472018-09-30 01:42:59 +053061 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 Mills703131f2020-10-28 14:26:33 -050068bool deserialize(const std::filesystem::path& path, LDAPMapperEntry& entry)
Tom Joseph0b14c472018-09-30 01:42:59 +053069{
70 try
71 {
Gunnar Mills703131f2020-10-28 14:26:33 -050072 if (std::filesystem::exists(path))
Tom Joseph0b14c472018-09-30 01:42:59 +053073 {
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 Mills703131f2020-10-28 14:26:33 -050084 std::filesystem::remove(path);
Tom Joseph0b14c472018-09-30 01:42:59 +053085 return false;
86 }
87 catch (const std::length_error& e)
88 {
89 log<level::ERR>(e.what());
Gunnar Mills703131f2020-10-28 14:26:33 -050090 std::filesystem::remove(path);
Tom Joseph0b14c472018-09-30 01:42:59 +053091 return false;
92 }
93}
94
95} // namespace user
96} // namespace phosphor