blob: 2842ba2ea57ffee7e1dfb535b20aa28300ca890d [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>
Jiaqing Zhao11ec6662022-07-05 20:55:34 +08007#include <phosphor-logging/lg2.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
Ratan Guptafd761da2019-04-12 21:48:57 +053021/** @brief Function required by Cereal to perform serialization.
22 *
23 * @tparam Archive - Cereal archive type (binary in this case).
24 * @param[in] archive - reference to cereal archive.
25 * @param[in] entry- const reference to LDAP mapper entry
26 * @param[in] version - Class version that enables handling a serialized data
27 * across code levels
28 */
29template <class Archive>
30void save(Archive& archive, const LDAPMapperEntry& entry,
Ratan Gupta0b1ad3d2022-01-09 14:09:35 +053031 const std::uint32_t /*version*/)
Ratan Guptafd761da2019-04-12 21:48:57 +053032{
33 archive(entry.groupName(), entry.privilege());
34}
35
36/** @brief Function required by Cereal to perform deserialization.
37 *
38 * @tparam Archive - Cereal archive type (binary in our case).
39 * @param[in] archive - reference to cereal archive.
40 * @param[out] entry - LDAP mapper entry to be read
41 * @param[in] version - Class version that enables handling a serialized data
42 * across code levels
43 */
44template <class Archive>
Ratan Gupta0b1ad3d2022-01-09 14:09:35 +053045void load(Archive& archive, LDAPMapperEntry& entry,
46 const std::uint32_t /*version*/)
Ratan Guptafd761da2019-04-12 21:48:57 +053047{
48 std::string groupName{};
49 std::string privilege{};
50
51 archive(groupName, privilege);
52
53 entry.sdbusplus::xyz::openbmc_project::User::server::PrivilegeMapperEntry::
54 groupName(groupName, true);
55 entry.sdbusplus::xyz::openbmc_project::User::server::PrivilegeMapperEntry::
56 privilege(privilege, true);
57}
58
Ratan Gupta7b04c352019-04-12 21:46:29 +053059fs::path serialize(const LDAPMapperEntry& entry, const fs::path& path)
Ratan Guptafd761da2019-04-12 21:48:57 +053060{
Ratan Gupta7b04c352019-04-12 21:46:29 +053061 fs::create_directories(path.parent_path());
62 std::ofstream os(path.c_str(), std::ios::binary | std::ios::out);
Ratan Guptafd761da2019-04-12 21:48:57 +053063 cereal::BinaryOutputArchive oarchive(os);
64 oarchive(entry);
65 return path;
66}
67
68bool deserialize(const fs::path& path, LDAPMapperEntry& entry)
69{
70 try
71 {
72 if (fs::exists(path))
73 {
74 std::ifstream is(path.c_str(), std::ios::in | std::ios::binary);
75 cereal::BinaryInputArchive iarchive(is);
76 iarchive(entry);
77 return true;
78 }
79 return false;
80 }
Patrick Williamsd019e3d2021-10-06 12:46:55 -050081 catch (const cereal::Exception& e)
Ratan Guptafd761da2019-04-12 21:48:57 +053082 {
Jiaqing Zhao11ec6662022-07-05 20:55:34 +080083 lg2::error("Failed to deserialize {FILE}: {ERR}", "FILE", path, "ERR",
84 e);
Ratan Guptafd761da2019-04-12 21:48:57 +053085 fs::remove(path);
86 return false;
87 }
88 catch (const std::length_error& e)
89 {
Jiaqing Zhao11ec6662022-07-05 20:55:34 +080090 lg2::error("Failed to deserialize {FILE}: {ERR}", "FILE", path, "ERR",
91 e);
Ratan Guptafd761da2019-04-12 21:48:57 +053092 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