Ratan Gupta | 95a2931 | 2019-02-18 20:34:10 +0530 | [diff] [blame] | 1 | #include <cereal/types/string.hpp> |
| 2 | #include <cereal/types/vector.hpp> |
| 3 | #include <cereal/archives/binary.hpp> |
| 4 | #include <fstream> |
| 5 | |
| 6 | #include "ldap_serialize.hpp" |
| 7 | #include "ldap_configuration.hpp" |
| 8 | #include <phosphor-logging/log.hpp> |
| 9 | #include "config.h" |
| 10 | |
| 11 | // Register class version |
| 12 | // From cereal documentation; |
| 13 | // "This macro should be placed at global scope" |
| 14 | CEREAL_CLASS_VERSION(phosphor::ldap::Config, 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 | * @tparam Archive - Cereal archive type (binary in our case). |
| 25 | * @param[in] archive - reference to Cereal archive. |
| 26 | * @param[in] config - const reference to ldap config. |
| 27 | * @param[in] version - Class version that enables handling |
| 28 | * a serialized data across code levels |
| 29 | */ |
| 30 | template <class Archive> |
| 31 | void save(Archive& archive, const Config& config, const std::uint32_t version) |
| 32 | { |
| 33 | archive(config.enabled()); |
| 34 | } |
| 35 | |
| 36 | /** @brief Function required by Cereal to perform deserialization. |
| 37 | * @tparam Archive - Cereal archive type (binary in our case). |
| 38 | * @param[in] archive - reference to Cereal archive. |
| 39 | * @param[in] config - reference of ldap config object. |
| 40 | * @param[in] version - Class version that enables handling |
| 41 | * a serialized data across code levels |
| 42 | */ |
| 43 | template <class Archive> |
| 44 | void load(Archive& archive, Config& config, const std::uint32_t version) |
| 45 | { |
| 46 | bool enabled = false; |
| 47 | archive(enabled); |
| 48 | config.enabled(enabled); |
| 49 | } |
| 50 | |
| 51 | fs::path serialize(const Config& config, const fs::path& path) |
| 52 | { |
| 53 | fs::create_directories(path.parent_path()); |
| 54 | |
| 55 | std::ofstream os(path.string(), std::ios::binary); |
| 56 | cereal::BinaryOutputArchive oarchive(os); |
| 57 | oarchive(config); |
| 58 | return path; |
| 59 | } |
| 60 | |
| 61 | bool deserialize(const fs::path& path, Config& config) |
| 62 | { |
| 63 | try |
| 64 | { |
| 65 | if (fs::exists(path)) |
| 66 | { |
| 67 | std::ifstream is(path.c_str(), std::ios::in | std::ios::binary); |
| 68 | cereal::BinaryInputArchive iarchive(is); |
| 69 | iarchive(config); |
| 70 | return true; |
| 71 | } |
| 72 | return false; |
| 73 | } |
| 74 | catch (cereal::Exception& e) |
| 75 | { |
| 76 | log<level::ERR>(e.what()); |
| 77 | std::error_code ec; |
| 78 | fs::remove(path, ec); |
| 79 | return false; |
| 80 | } |
| 81 | catch (const fs::filesystem_error& e) |
| 82 | { |
| 83 | return false; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | } // namespace ldap |
| 88 | } // namespace phosphor |