Add core file name validation in core manager

Move the core file name validation from dreport to core manager.
systemd-coredump creates temporary file in core file path prior
to actual core file creation.
This check will help to limit dump creation only for the new core files.

Change-Id: I196d3f372d85aae0a7a36ba7171e6bfd2ff4991b
Signed-off-by: Jayanth Othayoth <ojayanth@in.ibm.com>
diff --git a/core_manager.cpp b/core_manager.cpp
index f48e8dc..eb6cef5 100644
--- a/core_manager.cpp
+++ b/core_manager.cpp
@@ -1,3 +1,6 @@
+#include <regex>
+#include <experimental/filesystem>
+
 #include <phosphor-logging/log.hpp>
 
 #include "core_manager.hpp"
@@ -22,12 +25,31 @@
     for (const auto& i : fileInfo)
     {
         // Get list of debug files.
-        if (IN_CLOSE_WRITE == i.second)
+        if (IN_CLOSE_WRITE != i.second)
         {
-            files.push_back(i.first.string());
+            continue;
+        }
+
+        namespace fs = std::experimental::filesystem;
+        fs::path file(i.first);
+        std::string name = file.filename();
+
+        /*
+          As per coredump source code systemd-coredump uses below format
+          https://github.com/systemd/systemd/blob/master/src/coredump/coredump.c
+          /var/lib/systemd/coredump/core.%s.%s." SD_ID128_FORMAT_STR “
+          systemd-coredump also creates temporary file in core file path prior
+          to actual core file creation. Checking the file name format will help
+          to limit dump creation only for the new core files.
+        */
+        if("core" == name.substr(0, name.find('.')))
+        {
+            //Consider only file name start with "core."
+            files.push_back(file);
         }
     }
-    if(!files.empty())
+
+    if (!files.empty())
     {
         createHelper(files);
     }
@@ -40,7 +62,7 @@
     constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper";
     constexpr auto IFACE_INTERNAL("xyz.openbmc_project.Dump.Internal.Create");
     constexpr auto APPLICATION_CORED =
-              "xyz.openbmc_project.Dump.Internal.Create.Type.ApplicationCored";
+        "xyz.openbmc_project.Dump.Internal.Create.Type.ApplicationCored";
 
     auto b = sdbusplus::bus::new_default();
     auto mapper = b.new_method_call(
diff --git a/tools/dreport b/tools/dreport
index 0b5f77b..41b53d7 100755
--- a/tools/dreport
+++ b/tools/dreport
@@ -336,21 +336,13 @@
 #        https://github.com/systemd/systemd/blob/master/src/coredump/coredump.c
 #        /var/lib/systemd/coredump/core.%s.%s." SD_ID128_FORMAT_STR “.
 #        <process ID>.%s000000"
-#        Added additional check to make sure that user provided in
-#        systemd-coredump format.
 function set_pid()
 {
-    #from coredump.c file.
-    CORE_BASE_STRING="/var/lib/systemd/coredump/core"
-
     #Escape bash characters in file name
     file=$(printf %q "$optional_file")
 
-    string=$(echo $file | awk -F . '{ print $1}')
-    if [ "$CORE_BASE_STRING" = "$string" ]; then
-        #matching systemd-coredump core file format.
-        core_pid=$(echo $file | awk -F . '{ print $5}')
-    fi
+    #matching systemd-coredump core file format.
+    core_pid=$(echo $file | awk -F . '{ print $5}')
 }
 
 # @brief Capture debug data based on the input command array.