Add rotate support if the space is full

delete the first existing file if the space is not enough

Tested:
1. create dump and make the space is not enough
busctl call xyz.openbmc_project.Dump.Manager /xyz/openbmc_project/dump/bmc xyz.openbmc_project.Dump.Create CreateDump a{sv} 0
busctl tree xyz.openbmc_project.Dump.Manager                                                                        `-/xyz
  `-/xyz/openbmc_project
    `-/xyz/openbmc_project/dump
      |-/xyz/openbmc_project/dump/bmc
      | `-/xyz/openbmc_project/dump/bmc/entry
      |   |-/xyz/openbmc_project/dump/bmc/entry/10
      |   |-/xyz/openbmc_project/dump/bmc/entry/6
      |   |-/xyz/openbmc_project/dump/bmc/entry/7
      |   |-/xyz/openbmc_project/dump/bmc/entry/8
      |   `-/xyz/openbmc_project/dump/bmc/entry/9
      `-/xyz/openbmc_project/dump/internal
        `-/xyz/openbmc_project/dump/internal/manager

2. create dump again and the the first existing file will be deleted

Signed-off-by: Xie Ning <xiening.xll@bytedance.com>
Change-Id: Ic3b376d7e6512a264f849de760363f4e167421c7
diff --git a/dump_manager_bmc.cpp b/dump_manager_bmc.cpp
index 26adf67..ef128df 100644
--- a/dump_manager_bmc.cpp
+++ b/dump_manager_bmc.cpp
@@ -248,32 +248,54 @@
     }
 }
 
-size_t Manager::getAllowedSize()
+size_t getDirectorySize(const std::string dir)
 {
-    using namespace sdbusplus::xyz::openbmc_project::Dump::Create::Error;
-    using Reason = xyz::openbmc_project::Dump::Create::QuotaExceeded::REASON;
-
     auto size = 0;
-
-    // Get current size of the dump directory.
-    for (const auto& p : std::filesystem::recursive_directory_iterator(dumpDir))
+    for (const auto& p : std::filesystem::recursive_directory_iterator(dir))
     {
         if (!std::filesystem::is_directory(p))
         {
             size += std::ceil(std::filesystem::file_size(p) / 1024.0);
         }
     }
+    return size;
+}
+
+size_t Manager::getAllowedSize()
+{
+    // Get current size of the dump directory.
+    auto size = getDirectorySize(dumpDir);
 
     // Set the Dump size to Maximum  if the free space is greater than
     // Dump max size otherwise return the available size.
 
     size = (size > BMC_DUMP_TOTAL_SIZE ? 0 : BMC_DUMP_TOTAL_SIZE - size);
 
+#ifdef BMC_DUMP_ROTATE_CONFIG
+    // Delete the first existing file until the space is enough
+    while (size < BMC_DUMP_MIN_SPACE_REQD)
+    {
+        auto delEntry = min_element(
+            entries.begin(), entries.end(),
+            [](const auto& l, const auto& r) { return l.first < r.first; });
+        auto delPath =
+            std::filesystem::path(dumpDir) / std::to_string(delEntry->first);
+
+        size += getDirectorySize(delPath);
+
+        delEntry->second->delete_();
+    }
+#else
+    using namespace sdbusplus::xyz::openbmc_project::Dump::Create::Error;
+    using Reason = xyz::openbmc_project::Dump::Create::QuotaExceeded::REASON;
+
     if (size < BMC_DUMP_MIN_SPACE_REQD)
     {
         // Reached to maximum limit
         elog<QuotaExceeded>(Reason("Not enough space: Delete old dumps"));
     }
+#endif
+
     if (size > BMC_DUMP_MAX_SIZE)
     {
         size = BMC_DUMP_MAX_SIZE;