blob: 66cea3764eb0c46aa86984df517359bd24af9035 [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,
Ratan Gupta0b1ad3d2022-01-09 14:09:35 +053033 const std::uint32_t /*version*/)
Ratan Guptafd761da2019-04-12 21:48:57 +053034{
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>
Ratan Gupta0b1ad3d2022-01-09 14:09:35 +053047void load(Archive& archive, LDAPMapperEntry& entry,
48 const std::uint32_t /*version*/)
Ratan Guptafd761da2019-04-12 21:48:57 +053049{
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
Ratan Gupta7b04c352019-04-12 21:46:29 +053061fs::path serialize(const LDAPMapperEntry& entry, const fs::path& path)
Ratan Guptafd761da2019-04-12 21:48:57 +053062{
Ratan Gupta7b04c352019-04-12 21:46:29 +053063 fs::create_directories(path.parent_path());
64 std::ofstream os(path.c_str(), std::ios::binary | std::ios::out);
Ratan Guptafd761da2019-04-12 21:48:57 +053065 cereal::BinaryOutputArchive oarchive(os);
66 oarchive(entry);
67 return path;
68}
69
70bool 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 }
Patrick Williamsd019e3d2021-10-06 12:46:55 -050083 catch (const cereal::Exception& e)
Ratan Guptafd761da2019-04-12 21:48:57 +053084 {
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
Ratan Gupta7b04c352019-04-12 21:46:29 +053097} // namespace ldap
Ratan Guptafd761da2019-04-12 21:48:57 +053098} // namespace phosphor