ItemUpdater::erase() : Fixed double delete

In some cases, when the erase function is called, it attempts
to erase an image that is no longer present on the bmc. This
results in the following error "Error deleting image (/xyz/
openbmc_project/software/xxxxxxxx) from image manager: sd_bus_call
noreply: org.freedesktop.DBus.Error.UnknownObject: Unknown object
'/xyz/openbmc_project/software/xxxxxxxx'". This function should
only be attempting to erase images that exist.

Tested:
Case 1: When activating a new image and the process calls the
erase function on its own
Case 2: When calling delete from the BMC command line
Case 3: When calling delete all from the BMC command line

All of the above cases did not throw the aforementioned error

Signed-off-by: Zami Seck <zimzam17@gmail.com>
Change-Id: I66f8734bcaf8cf31f2b009e53c20d59ee26fd5ad
diff --git a/activation.cpp b/activation.cpp
index 91f3f33..be781d1 100644
--- a/activation.cpp
+++ b/activation.cpp
@@ -231,20 +231,45 @@
 
 void Activation::deleteImageManagerObject()
 {
-    // Call the Delete object for <versionID> inside image_manager
-    auto method = this->bus.new_method_call(VERSION_BUSNAME, path.c_str(),
-                                            "xyz.openbmc_project.Object.Delete",
-                                            "Delete");
+    // Call the Delete object for <versionID> inside image_manager if the object
+    // has not already been deleted due to a successful update or Delete call
+    const std::string interface = std::string{VERSION_IFACE};
+    auto method = this->bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
+                                            MAPPER_BUSNAME, "GetObject");
+    method.append(path.c_str());
+    method.append(std::vector<std::string>({interface}));
+
+    std::map<std::string, std::vector<std::string>> response;
+
     try
     {
-        bus.call_noreply(method);
+        auto reply = bus.call(method);
+        reply.read(response);
+        auto it = response.find(VERSION_IFACE);
+        if (it != response.end())
+        {
+            auto deleteMethod = this->bus.new_method_call(
+                VERSION_BUSNAME, path.c_str(),
+                "xyz.openbmc_project.Object.Delete", "Delete");
+            try
+            {
+                bus.call_noreply(deleteMethod);
+            }
+            catch (const sdbusplus::exception::exception& e)
+            {
+                error(
+                    "Error deleting image ({PATH}) from image manager: {ERROR}",
+                    "PATH", path, "ERROR", e);
+                return;
+            }
+        }
     }
     catch (const sdbusplus::exception::exception& e)
     {
-        error("Error deleting image ({PATH}) from image manager: {ERROR}",
-              "PATH", path, "ERROR", e);
-        return;
+        error("Error in mapper method call for ({PATH}, {INTERFACE}: {ERROR}",
+              "ERROR", e, "PATH", path, "INTERFACE", interface);
     }
+    return;
 }
 
 auto Activation::requestedActivation(RequestedActivations value)