summaryrefslogtreecommitdiffstats
path: root/phosphor-ldap-config/ldap_serialize.cpp
diff options
context:
space:
mode:
authorRatan Gupta <ratagupt@linux.vnet.ibm.com>2019-02-18 20:34:10 +0530
committerRatan Gupta <ratagupt@linux.vnet.ibm.com>2019-03-11 12:10:22 +0530
commit95a2931473dfa61a30e7a65606dab15ab24cd5b4 (patch)
tree489495eee9fde8aa5254dd515305214779df0301 /phosphor-ldap-config/ldap_serialize.cpp
parentaeaf9413a965d225d11ee1cd2c8ee9aa1f8dc862 (diff)
downloadphosphor-user-manager-95a2931473dfa61a30e7a65606dab15ab24cd5b4.tar.gz
phosphor-user-manager-95a2931473dfa61a30e7a65606dab15ab24cd5b4.zip
LDAP: Add the persistency for the "Enabled" property
This property will control that whether the LDAP service would be started or not. We are persisting this property using cereal, other properties is being persisted through nslcd.conf, nslcd doesn't give us a way to put this property under nslcd.conf. Tested By: Test the persistency of enabled property. Verified that it was getting persisted across restart/reboot. Change-Id: Id64b23b71865bac15d3be2d79abad615aa576bea Signed-off-by: Ratan Gupta <ratagupt@linux.vnet.ibm.com>
Diffstat (limited to 'phosphor-ldap-config/ldap_serialize.cpp')
-rw-r--r--phosphor-ldap-config/ldap_serialize.cpp88
1 files changed, 88 insertions, 0 deletions
diff --git a/phosphor-ldap-config/ldap_serialize.cpp b/phosphor-ldap-config/ldap_serialize.cpp
new file mode 100644
index 0000000..510686c
--- /dev/null
+++ b/phosphor-ldap-config/ldap_serialize.cpp
@@ -0,0 +1,88 @@
+#include <cereal/types/string.hpp>
+#include <cereal/types/vector.hpp>
+#include <cereal/archives/binary.hpp>
+#include <fstream>
+
+#include "ldap_serialize.hpp"
+#include "ldap_configuration.hpp"
+#include <phosphor-logging/log.hpp>
+#include "config.h"
+
+// Register class version
+// From cereal documentation;
+// "This macro should be placed at global scope"
+CEREAL_CLASS_VERSION(phosphor::ldap::Config, CLASS_VERSION);
+
+namespace phosphor
+{
+namespace ldap
+{
+
+using namespace phosphor::logging;
+
+/** @brief Function required by Cereal to perform serialization.
+ * @tparam Archive - Cereal archive type (binary in our case).
+ * @param[in] archive - reference to Cereal archive.
+ * @param[in] config - const reference to ldap config.
+ * @param[in] version - Class version that enables handling
+ * a serialized data across code levels
+ */
+template <class Archive>
+void save(Archive& archive, const Config& config, const std::uint32_t version)
+{
+ archive(config.enabled());
+}
+
+/** @brief Function required by Cereal to perform deserialization.
+ * @tparam Archive - Cereal archive type (binary in our case).
+ * @param[in] archive - reference to Cereal archive.
+ * @param[in] config - reference of ldap config object.
+ * @param[in] version - Class version that enables handling
+ * a serialized data across code levels
+ */
+template <class Archive>
+void load(Archive& archive, Config& config, const std::uint32_t version)
+{
+ bool enabled = false;
+ archive(enabled);
+ config.enabled(enabled);
+}
+
+fs::path serialize(const Config& config, const fs::path& path)
+{
+ fs::create_directories(path.parent_path());
+
+ std::ofstream os(path.string(), std::ios::binary);
+ cereal::BinaryOutputArchive oarchive(os);
+ oarchive(config);
+ return path;
+}
+
+bool deserialize(const fs::path& path, Config& config)
+{
+ try
+ {
+ if (fs::exists(path))
+ {
+ std::ifstream is(path.c_str(), std::ios::in | std::ios::binary);
+ cereal::BinaryInputArchive iarchive(is);
+ iarchive(config);
+ return true;
+ }
+ return false;
+ }
+ catch (cereal::Exception& e)
+ {
+ log<level::ERR>(e.what());
+ std::error_code ec;
+ fs::remove(path, ec);
+ return false;
+ }
+ catch (const fs::filesystem_error& e)
+ {
+ return false;
+ }
+}
+
+} // namespace ldap
+} // namespace phosphor
OpenPOWER on IntegriCloud