pldm: inband code update: clear the image LIDs when pldm daemon comes up

Clear the LID_STAGING_DIR path when pldm daemon comes up. This function
will also be used during code update process.

Signed-off-by: Varsha Kaverappa <vkaverap@in.ibm.com>
Change-Id: I2a757caaa3e204e4089235d9e3baf6d425f5bec5
diff --git a/oem/ibm/libpldmresponder/inband_code_update.cpp b/oem/ibm/libpldmresponder/inband_code_update.cpp
index 01d3725..7055c46 100644
--- a/oem/ibm/libpldmresponder/inband_code_update.cpp
+++ b/oem/ibm/libpldmresponder/inband_code_update.cpp
@@ -192,6 +192,15 @@
     oemPlatformHandler = handler;
 }
 
+void CodeUpdate::clearDirPath(const std::string& dirPath)
+{
+    for (auto& path : fs::directory_iterator(dirPath.c_str()))
+    {
+        fs::remove_all(path);
+    }
+    return;
+}
+
 uint8_t fetchBootSide(uint16_t entityInstance, CodeUpdate* codeUpdate)
 {
     uint8_t sensorOpState = tSideNum;
diff --git a/oem/ibm/libpldmresponder/inband_code_update.hpp b/oem/ibm/libpldmresponder/inband_code_update.hpp
index 048f4d1..28c7c9c 100644
--- a/oem/ibm/libpldmresponder/inband_code_update.hpp
+++ b/oem/ibm/libpldmresponder/inband_code_update.hpp
@@ -94,6 +94,13 @@
         codeUpdateInProgress = progress;
     }
 
+    /** @brief Method to clear contents the LID staging directory that contains
+     *  images such as host firmware and BMC.
+     *  @param[in] dirPath - directory system path that has to be cleared
+     *  @return none
+     */
+    void clearDirPath(const std::string& dirPath);
+
     virtual ~CodeUpdate()
     {}
 
diff --git a/oem/ibm/test/libpldmresponder_oem_platform_test.cpp b/oem/ibm/test/libpldmresponder_oem_platform_test.cpp
index 5f80f3b..64de610 100644
--- a/oem/ibm/test/libpldmresponder_oem_platform_test.cpp
+++ b/oem/ibm/test/libpldmresponder_oem_platform_test.cpp
@@ -141,3 +141,23 @@
 
     EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
 }
+
+TEST(clearDirPath, testClearDirPath)
+{
+    char dirPath[] = "/tmp/testClearDir/";
+    fs::path dir(dirPath);
+    fs::create_directories(dir);
+    struct stat buffer;
+    ASSERT_EQ(stat(dirPath, &buffer), 0);
+    char filePath[] = "/tmp/testClearDir/file.txt";
+    std::ofstream file(filePath);
+    ASSERT_EQ(stat(filePath, &buffer), 0);
+
+    auto mockDbusHandler = std::make_unique<MockdBusHandler>();
+    std::unique_ptr<CodeUpdate> mockCodeUpdate =
+        std::make_unique<MockCodeUpdate>(mockDbusHandler.get());
+
+    mockCodeUpdate->clearDirPath(dirPath);
+    ASSERT_EQ(stat(filePath, &buffer), -1);
+    ASSERT_EQ(stat(dirPath, &buffer), 0);
+}
diff --git a/pldmd/pldmd.cpp b/pldmd/pldmd.cpp
index 59018b6..c657d49 100644
--- a/pldmd/pldmd.cpp
+++ b/pldmd/pldmd.cpp
@@ -200,6 +200,7 @@
 #ifdef OEM_IBM
     std::unique_ptr<pldm::responder::CodeUpdate> codeUpdate =
         std::make_unique<pldm::responder::CodeUpdate>(dbusHandler.get());
+    codeUpdate->clearDirPath(LID_STAGING_DIR);
     oemPlatformHandler = std::make_unique<oem_ibm_platform::Handler>(
         dbusHandler.get(), codeUpdate.get(), sockfd, hostEID, dbusImplReq);
     codeUpdate->setOemPlatformHandler(oemPlatformHandler.get());