PEL: Fix to remove files from archive folder

Archive folder contains the PEL logs (corresponding to deleted event
logs) that are deleted first, if the repo logs size exceeds the warning
size. Variable _archiveSize is used to keep track of size of archive
folder. After all logs in archive folder are deleted the _ardchiveSize
wasn't reset to zero as a result it would keep entering this function
trying to delete the logs again under archive folder.
The fix here is to reset the _archiveSize and also use robust remove
API for deletion instead of system command.

Signed-off-by: Sumit Kumar <sumit_kumar@in.ibm.com>
Change-Id: Ia98086c47d6083d8a104c1c28efc94c8b30a47ef
diff --git a/extensions/openpower-pels/repository.cpp b/extensions/openpower-pels/repository.cpp
index f552a75..c49d92f 100644
--- a/extensions/openpower-pels/repository.cpp
+++ b/extensions/openpower-pels/repository.cpp
@@ -530,21 +530,29 @@
     }
 }
 
-bool Repository::sizeWarning() const
+bool Repository::sizeWarning()
 {
+    std::error_code ec;
+
     if ((_archiveSize > 0) && ((_sizes.total + _archiveSize) >
                                ((_maxRepoSize * warningPercentage) / 100)))
     {
         log<level::INFO>(
             "Repository::sizeWarning function:Deleting the files in archive");
 
-        std::string cmd = "rm " + _archivePath.string() + "/*_*";
-        auto rc = system(cmd.c_str());
-        if (rc)
+        for (const auto& dirEntry : fs::directory_iterator(_archivePath))
         {
-            log<level::ERR>("Repository::sizeWarning function:Could not delete "
-                            "files in archive");
+            fs::remove(dirEntry.path(), ec);
+            if (ec)
+            {
+                log<level::INFO>(
+                    "Repository::sizeWarning function:Could not delete "
+                    "a file in PEL archive",
+                    entry("FILENAME=%s", dirEntry.path().c_str()));
+            }
         }
+
+        _archiveSize = 0;
     }
 
     return (_sizes.total > (_maxRepoSize * warningPercentage / 100)) ||
diff --git a/extensions/openpower-pels/repository.hpp b/extensions/openpower-pels/repository.hpp
index 0b5cab1..a57b901 100644
--- a/extensions/openpower-pels/repository.hpp
+++ b/extensions/openpower-pels/repository.hpp
@@ -370,7 +370,7 @@
      *
      * @return bool - true if repo is > 95% full or too many PELs
      */
-    bool sizeWarning() const;
+    bool sizeWarning();
 
     /**
      * @brief Deletes PELs to bring the repository size down