blob: b0124c28f6ce35128791543f6f02096e6dd533eb [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,
34 const std::uint32_t version)
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 */
47template <class Archive>
48void load(Archive& archive, LDAPMapperEntry& entry, const std::uint32_t version)
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
Gunnar Mills703131f2020-10-28 14:26:33 -050061std::filesystem::path serialize(const LDAPMapperEntry& entry, Id id,
62 const std::filesystem::path& dir)
Tom Joseph0b14c472018-09-30 01:42:59 +053063{
Tom Joseph0b14c472018-09-30 01:42:59 +053064 auto path = dir / std::to_string(id);
65 std::ofstream os(path.c_str(), std::ios::binary);
66 cereal::BinaryOutputArchive oarchive(os);
67 oarchive(entry);
68 return path;
69}
70
Gunnar Mills703131f2020-10-28 14:26:33 -050071bool deserialize(const std::filesystem::path& path, LDAPMapperEntry& entry)
Tom Joseph0b14c472018-09-30 01:42:59 +053072{
73 try
74 {
Gunnar Mills703131f2020-10-28 14:26:33 -050075 if (std::filesystem::exists(path))
Tom Joseph0b14c472018-09-30 01:42:59 +053076 {
77 std::ifstream is(path.c_str(), std::ios::in | std::ios::binary);
78 cereal::BinaryInputArchive iarchive(is);
79 iarchive(entry);
80 return true;
81 }
82 return false;
83 }
84 catch (cereal::Exception& e)
85 {
86 log<level::ERR>(e.what());
Gunnar Mills703131f2020-10-28 14:26:33 -050087 std::filesystem::remove(path);
Tom Joseph0b14c472018-09-30 01:42:59 +053088 return false;
89 }
90 catch (const std::length_error& e)
91 {
92 log<level::ERR>(e.what());
Gunnar Mills703131f2020-10-28 14:26:33 -050093 std::filesystem::remove(path);
Tom Joseph0b14c472018-09-30 01:42:59 +053094 return false;
95 }
96}
97
98} // namespace user
99} // namespace phosphor