blob: e8e56d4d624c90e4f488300bccb17283d2fb2399 [file] [log] [blame]
Patrick Williams9638afb2021-02-22 17:16:24 -06001#include "config.h"
2
3#include "ldap_mapper_serialize.hpp"
4
Ratan Guptafd761da2019-04-12 21:48:57 +05305#include <cereal/archives/binary.hpp>
6#include <cereal/types/string.hpp>
Ratan Guptafd761da2019-04-12 21:48:57 +05307#include <phosphor-logging/log.hpp>
Patrick Williams9638afb2021-02-22 17:16:24 -06008
9#include <fstream>
Ratan Guptafd761da2019-04-12 21:48:57 +053010
11// Register class version
12// From cereal documentation;
13// "This macro should be placed at global scope"
Ratan Gupta7b04c352019-04-12 21:46:29 +053014CEREAL_CLASS_VERSION(phosphor::ldap::LDAPMapperEntry, CLASS_VERSION);
Ratan Guptafd761da2019-04-12 21:48:57 +053015
16namespace phosphor
17{
Ratan Gupta7b04c352019-04-12 21:46:29 +053018namespace ldap
Ratan Guptafd761da2019-04-12 21:48:57 +053019{
20
21using 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 */
31template <class Archive>
32void 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 */
46template <class Archive>
47void 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
Ratan Gupta7b04c352019-04-12 21:46:29 +053060fs::path serialize(const LDAPMapperEntry& entry, const fs::path& path)
Ratan Guptafd761da2019-04-12 21:48:57 +053061{
Ratan Gupta7b04c352019-04-12 21:46:29 +053062 fs::create_directories(path.parent_path());
63 std::ofstream os(path.c_str(), std::ios::binary | std::ios::out);
Ratan Guptafd761da2019-04-12 21:48:57 +053064 cereal::BinaryOutputArchive oarchive(os);
65 oarchive(entry);
66 return path;
67}
68
69bool 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 (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
Ratan Gupta7b04c352019-04-12 21:46:29 +053096} // namespace ldap
Ratan Guptafd761da2019-04-12 21:48:57 +053097} // namespace phosphor