Create IBM ConfigFile base directory

The ConfigFile upload fails when the /var/lib/obmc directory is not
available at BMC

This commit changes the base directory to /var/lib/bmcweb
The subdirectories for the configfiles and locks
are created under this new path

Migration strategy of this directory and files:
 This is IBM only feature, compiled under the IBM_MANAGEMENT_CONSOLE flag
 There is no system out yet which is running this code
 Internal IBM stake holders are in agreement with the changes

Tested by :
  1. Tested configfile upload on a BMC where the base directory is not
available
  2. Tested the configfile upload on a factory BMC. Verified it creates
     the base directories and the upload is successful
  3. Tested the configfile usecases for delete and delete-all
  4. Tested the acquire-lock functionality
  5. Ran lock unit test successfully

Signed-off-by: Sunitha Harish <sunharis@in.ibm.com>
Change-Id: Ic3f5f5d0ba0b37950fd397ec835b4fa7babdaa9b
diff --git a/include/ibm/locks.hpp b/include/ibm/locks.hpp
index 3054791..0b47471 100644
--- a/include/ibm/locks.hpp
+++ b/include/ibm/locks.hpp
@@ -3,6 +3,7 @@
 #include <boost/algorithm/string.hpp>
 #include <boost/container/flat_map.hpp>
 #include <boost/endian/conversion.hpp>
+#include <include/ibm/utils.hpp>
 #include <logging.hpp>
 #include <nlohmann/json.hpp>
 
@@ -36,7 +37,8 @@
 using SessionFlags = std::pair<SType, SType>;
 using ListOfSessionIds = std::vector<std::string>;
 static constexpr const char* fileName =
-    "/var/lib/obmc/bmc-console-mgmt/locks/ibm_mc_persistent_lock_data.json";
+    "/var/lib/bmcweb/ibm-management-console/locks/"
+    "ibm_mc_persistent_lock_data.json";
 
 class Lock
 {
@@ -44,21 +46,19 @@
     boost::container::flat_map<uint32_t, LockRequests> lockTable;
 
     /*
-     * This API implements the logic to persist the locks that are contained in
-     * the lock table into a json file.
-     */
-    void saveLocks();
-
-    /*
      * This API implements the logic to load the locks that are present in the
      * json file into the lock table.
      */
     void loadLocks();
 
-    bool createPersistentLockFilePath();
-
   protected:
     /*
+     * This API implements the logic to persist the locks that are contained in
+     * the lock table into a json file.
+     */
+    void saveLocks();
+
+    /*
      * This function implements the logic for validating an incoming
      * lock request/requests.
      *
@@ -189,37 +189,6 @@
     virtual ~Lock() = default;
 };
 
-inline bool Lock::createPersistentLockFilePath()
-{
-    // The path /var/lib/obmc will be created by initrdscripts
-    // Create the directories for the persistent lock file
-    std::error_code ec;
-    if (!std::filesystem::is_directory("/var/lib/obmc/bmc-console-mgmt", ec))
-    {
-        std::filesystem::create_directory("/var/lib/obmc/bmc-console-mgmt", ec);
-    }
-    if (ec)
-    {
-        BMCWEB_LOG_DEBUG
-            << "Failed to prepare bmc-console-mgmt directory. ec : " << ec;
-        return false;
-    }
-
-    if (!std::filesystem::is_directory("/var/lib/obmc/bmc-console-mgmt/locks",
-                                       ec))
-    {
-        std::filesystem::create_directory(
-            "/var/lib/obmc/bmc-console-mgmt/locks", ec);
-    }
-    if (ec)
-    {
-        BMCWEB_LOG_DEBUG
-            << "Failed to prepare persistent lock file directory. ec : " << ec;
-        return false;
-    }
-    return true;
-}
-
 inline void Lock::loadLocks()
 {
     std::ifstream persistentFile(fileName);
@@ -247,20 +216,16 @@
 inline void Lock::saveLocks()
 {
     std::error_code ec;
-    if (!std::filesystem::is_directory("/var/lib/obmc/bmc-console-mgmt/locks",
-                                       ec))
+    std::string_view path = "/var/lib/bmcweb/ibm-management-console/locks";
+    if (!crow::ibm_utils::createDirectory(path))
     {
-        if (!createPersistentLockFilePath())
-        {
-            BMCWEB_LOG_DEBUG << "Failed to create lock persistent path";
-            return;
-        }
+        BMCWEB_LOG_DEBUG << "Failed to create lock persistent path";
+        return;
     }
     std::ofstream persistentFile(fileName);
-    // set the permission of the file to 640
+    // set the permission of the file to 600
     std::filesystem::perms permission = std::filesystem::perms::owner_read |
-                                        std::filesystem::perms::owner_write |
-                                        std::filesystem::perms::group_read;
+                                        std::filesystem::perms::owner_write;
     std::filesystem::permissions(fileName, permission);
     nlohmann::json data;
     for (const auto& it : lockTable)
@@ -359,6 +324,9 @@
 
     auto conflict = isConflictWithTable(multiRequest);
 
+    // save the lock in the persistent file
+    saveLocks();
+
     BMCWEB_LOG_DEBUG << "Done with checking conflict with the locktable";
     return std::make_pair(false, conflict);
 }
@@ -548,8 +516,6 @@
         lockTable.emplace(std::pair<uint32_t, LockRequests>(
             transactionId, refLockRequestStructure));
 
-        // save the lock in the persistent file
-        saveLocks();
         return std::make_pair(false, transactionId);
     }
     BMCWEB_LOG_DEBUG
@@ -582,8 +548,6 @@
     transactionId = generateTransactionId();
     lockTable.emplace(std::make_pair(transactionId, refLockRequestStructure));
 
-    // save the lock in the persistent file
-    saveLocks();
     return std::make_pair(false, transactionId);
 }