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