xref: /openbmc/phosphor-user-manager/phosphor-ldap-config/ldap_mapper_serialize.cpp (revision d019e3d2ae739128635d36e1b6612df9bb49a3be)
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, const std::uint32_t version)
48 {
49     std::string groupName{};
50     std::string privilege{};
51 
52     archive(groupName, privilege);
53 
54     entry.sdbusplus::xyz::openbmc_project::User::server::PrivilegeMapperEntry::
55         groupName(groupName, true);
56     entry.sdbusplus::xyz::openbmc_project::User::server::PrivilegeMapperEntry::
57         privilege(privilege, true);
58 }
59 
60 fs::path serialize(const LDAPMapperEntry& entry, const fs::path& path)
61 {
62     fs::create_directories(path.parent_path());
63     std::ofstream os(path.c_str(), std::ios::binary | std::ios::out);
64     cereal::BinaryOutputArchive oarchive(os);
65     oarchive(entry);
66     return path;
67 }
68 
69 bool deserialize(const fs::path& path, LDAPMapperEntry& entry)
70 {
71     try
72     {
73         if (fs::exists(path))
74         {
75             std::ifstream is(path.c_str(), std::ios::in | std::ios::binary);
76             cereal::BinaryInputArchive iarchive(is);
77             iarchive(entry);
78             return true;
79         }
80         return false;
81     }
82     catch (const cereal::Exception& e)
83     {
84         log<level::ERR>(e.what());
85         fs::remove(path);
86         return false;
87     }
88     catch (const std::length_error& e)
89     {
90         log<level::ERR>(e.what());
91         fs::remove(path);
92         return false;
93     }
94 }
95 
96 } // namespace ldap
97 } // namespace phosphor
98