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