Fix the core dump by using filesystem error_code

The currently used filesystem method will cause an exception if the
file system is damaged for some reason, resulting in a core dump of
the process.
So the overloaded method with the error_code parameter should be used
here to ensure that the process core dump will not be caused after an
exception is thrown.

Fixes: openbmc/phosphor-bmc-code-mgmt#12

Tested: built phosphor-bmc-code-mgmt successfully and CI passes.

Signed-off-by: George Liu <liuxiwei@inspur.com>
Change-Id: I329f78b481cb466e755bc1b78562583620f561c2
diff --git a/image_manager.cpp b/image_manager.cpp
index eb4fec4..f998871 100644
--- a/image_manager.cpp
+++ b/image_manager.cpp
@@ -22,6 +22,7 @@
 #include <filesystem>
 #include <random>
 #include <string>
+#include <system_error>
 
 namespace phosphor
 {
@@ -76,9 +77,11 @@
 
 int Manager::processImage(const std::string& tarFilePath)
 {
-    if (!fs::is_regular_file(tarFilePath))
+    std::error_code ec;
+    if (!fs::is_regular_file(tarFilePath, ec))
     {
-        error("Tarball {PATH} does not exist", "PATH", tarFilePath);
+        error("Tarball {PATH} does not exist: {ERROR_MSG}", "PATH", tarFilePath,
+              "ERROR_MSG", ec.message());
         report<ManifestFileFailure>(ManifestFail::PATH(tarFilePath.c_str()));
         return -1;
     }
@@ -109,9 +112,10 @@
     }
 
     // Verify the manifest file
-    if (!fs::is_regular_file(manifestPath))
+    if (!fs::is_regular_file(manifestPath, ec))
     {
-        error("No manifest file {PATH}", "PATH", tarFilePath);
+        error("No manifest file {PATH}: {ERROR_MSG}", "PATH", tarFilePath,
+              "ERROR_MSG", ec.message());
         report<ManifestFileFailure>(ManifestFail::PATH(tarFilePath.c_str()));
         return -1;
     }
@@ -210,7 +214,7 @@
     if (versions.find(id) == versions.end() && it == allSoftwareObjs.end())
     {
         // Rename the temp dir to image dir
-        fs::rename(tmpDirPath, imageDirPath);
+        fs::rename(tmpDirPath, imageDirPath, ec);
         // Clear the path, so it does not attemp to remove a non-existing path
         tmpDirToRemove.path.clear();
 
@@ -242,9 +246,10 @@
 
     // Delete image dir
     fs::path imageDirPath = (*(it->second)).path();
-    if (fs::exists(imageDirPath))
+    std::error_code ec;
+    if (fs::exists(imageDirPath, ec))
     {
-        fs::remove_all(imageDirPath);
+        fs::remove_all(imageDirPath, ec);
     }
     this->versions.erase(entryId);
 }