Image_manager:Remove temporary untar image dir

When trying to upload same version image, object path for version is not
created. So it should delete the "/tmp/images/imageXXXXXX" directory.
If trying to upload different version image, it should be renaming the
temp directory to image version ID and create the version object path.

Tested:

case1: Uploaded the same version functional state image.
case2: Uploaded the same version ready state image.
case3: Activate the image without reboot bmc, then uploaded
       the same version active state image.
All the above cases failed to upload and image files
directory are not available in /tmp/images

Signed-off-by: Selvaganapathi M <selvaganapathim@ami.com>
Change-Id: I9809fa857c2345207581413024d0d80a813aa8f6
diff --git a/image_manager.cpp b/image_manager.cpp
index 915a4b3..a9a5a69 100644
--- a/image_manager.cpp
+++ b/image_manager.cpp
@@ -192,17 +192,6 @@
     fs::path imageDirPath = std::string{IMG_UPLOAD_DIR};
     imageDirPath /= id;
 
-    if (fs::exists(imageDirPath))
-    {
-        fs::remove_all(imageDirPath);
-    }
-
-    // Rename the temp dir to image dir
-    fs::rename(tmpDirPath, imageDirPath);
-
-    // Clear the path, so it does not attemp to remove a non-existing path
-    tmpDirToRemove.path.clear();
-
     auto objPath = std::string{SOFTWARE_OBJPATH} + '/' + id;
 
     // This service only manages the uploaded versions, and there could be
@@ -213,6 +202,11 @@
         std::find(allSoftwareObjs.begin(), allSoftwareObjs.end(), objPath);
     if (versions.find(id) == versions.end() && it == allSoftwareObjs.end())
     {
+        // Rename the temp dir to image dir
+        fs::rename(tmpDirPath, imageDirPath);
+        // Clear the path, so it does not attemp to remove a non-existing path
+        tmpDirToRemove.path.clear();
+
         // Create Version object
         auto versionPtr = std::make_unique<Version>(
             bus, objPath, version, purpose, extendedVersion,
@@ -227,7 +221,6 @@
     {
         info("Software Object with the same version ({VERSION}) already exists",
              "VERSION", id);
-        fs::remove_all(imageDirPath);
     }
     return 0;
 }