ItemUpdater: Validate PNOR squashfs image

- Validate the image dir by checking its content.
- Set Activation to "Ready" if validation passed or
  else set it to "Invalid".

Resolves openbmc/openbmc#1356

Change-Id: Ica73ae5604e74db11cd58c223d82265309ea76a6
Signed-off-by: Saqib Khan <khansa@us.ibm.com>
diff --git a/item_updater.cpp b/item_updater.cpp
index 42e7729..7a5629d 100755
--- a/item_updater.cpp
+++ b/item_updater.cpp
@@ -3,6 +3,7 @@
 #include <phosphor-logging/log.hpp>
 #include "config.h"
 #include "item_updater.hpp"
+#include "activation.hpp"
 
 namespace openpower
 {
@@ -11,8 +12,13 @@
 namespace updater
 {
 
+// When you see server:: you know we're referencing our base class
+namespace server = sdbusplus::xyz::openbmc_project::Software::server;
+
 using namespace phosphor::logging;
 
+constexpr auto squashFSImage = "pnor.xz.squashfs";
+
 int ItemUpdater::createActivation(sd_bus_message* msg,
                                   void* userData,
                                   sd_bus_error* retErr)
@@ -59,6 +65,14 @@
         }
 
         auto versionId = resp.substr(pos + 1);
+
+        // Determine the Activation state by processing the given image dir.
+        auto activationState = server::Activation::Activations::Invalid;
+        if (ItemUpdater::validateSquashFSImage(versionId) == 0)
+        {
+            activationState = server::Activation::Activations::Ready;
+        }
+
         if (updater->activations.find(versionId) == updater->activations.end())
         {
             updater->activations.insert(std::make_pair(
@@ -67,7 +81,8 @@
                             updater->busItem,
                             resp,
                             versionId,
-                            extendedVersion)));
+                            extendedVersion,
+                            activationState)));
         }
     }
     return 0;
@@ -109,10 +124,26 @@
     {
         log<level::ERR>("Error in reading Host MANIFEST file");
     }
-
     return extendedVersion;
 }
 
+int ItemUpdater::validateSquashFSImage(const std::string& versionId)
+{
+    auto file = IMAGE_DIR + versionId + "/" +
+                std::string(squashFSImage);
+    std::ifstream efile(file.c_str());
+
+    if (efile.good() == 1)
+    {
+        return 0;
+    }
+    else
+    {
+        log<level::ERR>("Failed to find the SquashFS image.");
+        return -1;
+    }
+}
+
 } // namespace updater
 } // namespace software
 } // namespace openpower