UserMgr: Fix ldap config persistance issue

With exiting implementation during restart of the
phosphor-ldap-conf creates the default object and
restore the config data from the persistent path.
Due to a bug while creating a default object it overrides
the persistent file and fails to load the configuration.
This commit fixes that issue.

Tested by:
1.Created LDAP config for openldap and AD
and verified config persisted after reboot

Signed-off-by: Ravi Teja <raviteja28031990@gmail.com>
Change-Id: I88d1d7a38aec9adc3336d14d14dbe9fbce79eac0
diff --git a/phosphor-ldap-config/ldap_config.cpp b/phosphor-ldap-config/ldap_config.cpp
index ed40d17..d01757a 100644
--- a/phosphor-ldap-config/ldap_config.cpp
+++ b/phosphor-ldap-config/ldap_config.cpp
@@ -91,12 +91,6 @@
 
     configPersistPath += "/config";
 
-    std::ofstream os(configPersistPath, std::ios::binary | std::ios::out);
-    // remove the read permission from others
-    auto permission =
-        fs::perms::owner_read | fs::perms::owner_write | fs::perms::group_read;
-    fs::permissions(configPersistPath, permission);
-
     serialize();
 
     // Emit deferred signal.
@@ -129,12 +123,6 @@
     fs::create_directories(configPersistPath);
 
     configPersistPath += "/config";
-
-    std::ofstream os(configPersistPath, std::ios::binary | std::ios::out);
-    // remove the read permission from others
-    auto permission =
-        fs::perms::owner_read | fs::perms::owner_write | fs::perms::group_read;
-    fs::permissions(configPersistPath, permission);
 }
 
 void Config::certificateInstalled(sdbusplus::message::message& msg)
@@ -638,7 +626,6 @@
 template <class Archive>
 void Config::load(Archive& archive, const std::uint32_t version)
 {
-
     bool bVal;
     archive(bVal);
     EnableIface::enabled(bVal);
@@ -669,10 +656,24 @@
 
 void Config::serialize()
 {
-    std::ofstream os(configPersistPath.string(),
-                     std::ios::binary | std::ios::out);
-    cereal::BinaryOutputArchive oarchive(os);
-    oarchive(*this);
+
+    if (!fs::exists(configPersistPath.c_str()))
+    {
+        std::ofstream os(configPersistPath.string(),
+                         std::ios::binary | std::ios::out);
+        auto permission = fs::perms::owner_read | fs::perms::owner_write |
+                          fs::perms::group_read;
+        fs::permissions(configPersistPath, permission);
+        cereal::BinaryOutputArchive oarchive(os);
+        oarchive(*this);
+    }
+    else
+    {
+        std::ofstream os(configPersistPath.string(),
+                         std::ios::binary | std::ios::out);
+        cereal::BinaryOutputArchive oarchive(os);
+        oarchive(*this);
+    }
     return;
 }