start-update-interface: add software_utils

Refactor and move the common code into software_utils namespace. Add new
untar API which untars from a unix_fd. This change is based on -
https://gerrit.openbmc.org/c/openbmc/phosphor-dbus-interfaces/+/65738
https://gerrit.openbmc.org/c/openbmc/docs/+/65739

Change-Id: Ifcbf78a06ef482ca7c7765efd5c24a533399b979
Signed-off-by: Jagpal Singh Gill <paligill@gmail.com>
diff --git a/item_updater_main.cpp b/item_updater_main.cpp
index e0c9c18..3d725b7 100644
--- a/item_updater_main.cpp
+++ b/item_updater_main.cpp
@@ -1,18 +1,12 @@
 #include "config.h"
 
 #include "item_updater.hpp"
+#include "software_utils.hpp"
 
-#include <boost/asio/io_context.hpp>
 #include <sdbusplus/asio/connection.hpp>
 #include <sdbusplus/bus.hpp>
 #include <sdbusplus/server/manager.hpp>
 
-boost::asio::io_context& getIOContext()
-{
-    static boost::asio::io_context io;
-    return io;
-}
-
 int main()
 {
     sdbusplus::async::context ctx;
diff --git a/meson.build b/meson.build
index 92db933..82326d8 100644
--- a/meson.build
+++ b/meson.build
@@ -157,7 +157,6 @@
     'activation.cpp',
     'images.cpp',
     'item_updater.cpp',
-    'item_updater_main.cpp',
     'serialize.cpp',
     'version.cpp',
     'utils.cpp',
@@ -249,20 +248,25 @@
     install: true
 )
 
+software_common_sources = files(
+    'software_utils.cpp'
+)
+
 executable(
     'phosphor-image-updater',
     image_updater_sources,
+    software_common_sources,
+    'item_updater_main.cpp',
     dependencies: [deps, ssl, boost_dep],
     install: true
 )
 
-software_manager_common_sources = files()
-
 if get_option('software-update-dbus-interface').allowed()
     executable(
         'phosphor-software-manager',
         'software_manager.cpp',
-        software_manager_common_sources,
+        image_updater_sources,
+        software_common_sources,
         dependencies: [deps, ssl],
         install: true
     )
@@ -277,7 +281,7 @@
     'image_manager_main.cpp',
     'version.cpp',
     'watch.cpp',
-    software_manager_common_sources,
+    software_common_sources,
     dependencies: [deps, ssl],
     install: true
 )
diff --git a/software_utils.cpp b/software_utils.cpp
new file mode 100644
index 0000000..aeeaeb7
--- /dev/null
+++ b/software_utils.cpp
@@ -0,0 +1,64 @@
+#include "software_utils.hpp"
+
+#include <phosphor-logging/lg2.hpp>
+
+PHOSPHOR_LOG2_USING;
+
+namespace phosphor::software::utils
+{
+
+static bool writeToFile(int imageFd, FILE* outStream)
+{
+    const int bSize = 100;
+    ssize_t nRead = 0;
+    unsigned char buf[bSize];
+
+    while ((nRead = read(imageFd, buf, bSize)) > 0)
+    {
+        if (fwrite(buf, 1, nRead, outStream) != (size_t)nRead)
+        {
+            error("Failed to write to file");
+            return false;
+        }
+    }
+    if (nRead < 0)
+    {
+        error("Failed to read from input file");
+        return false;
+    }
+    return true;
+}
+
+bool unTar(int imageFd, const std::string& extractDirPath)
+{
+    std::string tarCmd = "tar -xf - -C " + extractDirPath + " --no-same-owner";
+    info("Executing command: {CMD}", "CMD", tarCmd);
+    FILE* outStream = popen(tarCmd.c_str(), "w");
+    if (outStream == nullptr)
+    {
+        error("Failed to open pipe to execute command: {CMD}", "CMD", tarCmd);
+        return false;
+    }
+
+    if (!writeToFile(imageFd, outStream))
+    {
+        error("Failed to write to file");
+        pclose(outStream);
+        return false;
+    }
+
+    if (pclose(outStream) != 0)
+    {
+        error("Failed to close pipe");
+        return false;
+    }
+    return true;
+}
+
+} // namespace phosphor::software::utils
+
+boost::asio::io_context& getIOContext()
+{
+    static boost::asio::io_context io;
+    return io;
+}
diff --git a/software_utils.hpp b/software_utils.hpp
new file mode 100644
index 0000000..5cfbd54
--- /dev/null
+++ b/software_utils.hpp
@@ -0,0 +1,42 @@
+#pragma once
+
+#include <boost/asio/io_context.hpp>
+
+#include <filesystem>
+
+namespace phosphor::software::utils
+{
+
+namespace fs = std::filesystem;
+
+struct RemovablePath
+{
+    fs::path path;
+
+    explicit RemovablePath(const fs::path& path) : path(path) {}
+    ~RemovablePath()
+    {
+        if (!path.empty())
+        {
+            std::error_code ec;
+            fs::remove_all(path, ec);
+        }
+    }
+
+    RemovablePath(const RemovablePath& other) = delete;
+    RemovablePath& operator=(const RemovablePath& other) = delete;
+    RemovablePath(RemovablePath&&) = delete;
+    RemovablePath& operator=(RemovablePath&&) = delete;
+};
+
+/** @brief Untar the image
+ *  @param[in] imageFd - The file descriptor of the image to untar.
+ *  @param[in] extractDirPath - The destination directory for the untarred
+ * image.
+ *  @param[out] bool - The result of the untar operation.
+ */
+bool unTar(int imageFd, const std::string& extractDirPath);
+
+} // namespace phosphor::software::utils
+
+boost::asio::io_context& getIOContext();