Remove boost dependency
[platform/core/appfw/app-installers.git] / src / common / step / mount / step_mount_update.cc
index 1915f3f..486c3df 100644 (file)
@@ -4,35 +4,42 @@
 
 #include "common/step/mount/step_mount_update.h"
 
-#include <boost/filesystem/operations.hpp>
-#include <boost/filesystem/path.hpp>
-
+#include <filesystem>
 #include <string>
 
-#include "common/paths.h"
-#include "common/request.h"
+#include "common/shared_dirs.h"
+#include "common/utils/paths.h"
 #include "common/tzip_interface.h"
+#include "common/zip_interface.h"
 #include "common/utils/file_util.h"
+#include "common/utils/request.h"
 
-namespace bf = boost::filesystem;
-namespace bs = boost::system;
+namespace ci = common_installer;
+namespace fs = std::filesystem;
 
 namespace common_installer {
 namespace mount {
 
 Step::Status StepMountUpdate::process() {
-  context_->pkg_path.set(
-     context_->root_application_path.get() / context_->pkgid.get());
+  std::error_code error;
 
-  TzipInterface tzip_unpack(context_->unpacked_dir_path.get());
-  if (!tzip_unpack.UnmountZip()) {
+  if (!fs::exists(backup_path_)) {
+    fs::create_directories(backup_path_, error);
+    if (error) {
+      LOG(ERROR) << "Failed to create backup directory: " << backup_path_;
+      return Status::APP_DIR_ERROR;
+    }
+  }
+
+  auto zip_unpack = CreateZipInterface(context_->unpacked_dir_path.get());
+  if (!zip_unpack->UnmountZip()) {
     LOG(ERROR) << "Failed to unmount zip package from temporary path";
     return Status::APP_DIR_ERROR;
   }
 
-  bf::path zip_destination_path =
-      GetZipPackageLocation(context_->pkg_path.get(), context_->pkgid.get());
-  bf::path backup_zip_location = GetBackupPathForZipFile(zip_destination_path);
+  fs::path zip_destination_path =
+      GetZipPackageLocation(context_->GetPkgPath(), context_->pkgid.get());
+  fs::path backup_zip_location = GetBackupPathForZipFile(zip_destination_path);
 
   if (!MoveFile(zip_destination_path, backup_zip_location)) {
     LOG(ERROR) << "Files to create backup of zip package file";
@@ -42,12 +49,13 @@ Step::Status StepMountUpdate::process() {
   if (!CopyFile(context_->file_path.get(), zip_destination_path)) {
     return Status::APP_DIR_ERROR;
   }
+  ci::RemoveRWDirectories(zip_destination_path);
   context_->manifest_data.get()->zip_mount_file =
       strdup(zip_destination_path.c_str());
 
-  bf::path mount_point = GetMountLocation(context_->pkg_path.get());
-  TzipInterface tzip_final(mount_point);
-  if (!tzip_final.MountZip(zip_destination_path)) {
+  fs::path mount_point = GetMountLocation(context_->GetPkgPath());
+  auto zip_final = CreateZipInterface(mount_point);
+  if (!zip_final->MountZip(zip_destination_path)) {
     LOG(ERROR) << "Failed to mount zip package in installation path";
     return Status::APP_DIR_ERROR;
   }
@@ -56,35 +64,37 @@ Step::Status StepMountUpdate::process() {
   return Status::OK;
 }
 
-Step::Status StepMountUpdate::clean() {
-  bf::path backup_zip_location =
-      GetBackupPathForZipFile(GetZipPackageLocation(
-          context_->pkg_path.get(), context_->pkgid.get()));
-  bs::error_code error;
-  bf::remove(backup_zip_location, error);
-
-  bf::path mount_point = GetMountLocation(context_->pkg_path.get());
-  TzipInterface tzip_final(mount_point);
-  if (!tzip_final.UnmountZip()) {
+Step::Status StepMountUpdate::UmountPackagePath() {
+  fs::path mount_point = GetMountLocation(context_->GetPkgPath());
+  auto zip_final = CreateZipInterface(mount_point);
+  if (!zip_final->UnmountZip()) {
     LOG(ERROR) << "Failed to unmount zip package after installation";
     return Status::APP_DIR_ERROR;
   }
+
+  return Status::OK;
+}
+
+Step::Status StepMountUpdate::clean() {
+  fs::path backup_zip_location =
+      GetBackupPathForZipFile(GetZipPackageLocation(
+          context_->GetPkgPath(), context_->pkgid.get()));
+  Remove(backup_zip_location);
+
+  if (fs::exists(backup_path_))
+    RemoveAll(backup_path_);
+
   return Status::OK;
 }
 
 Step::Status StepMountUpdate::undo() {
-  bf::path mount_point = GetMountLocation(context_->pkg_path.get());
-  TzipInterface tzip_final(mount_point);
-  if (!tzip_final.UnmountZip()) {
-    LOG(ERROR) << "Failed to unmount zip package after installation";
-    return Status::APP_DIR_ERROR;
-  }
+  UmountPackagePath();
 
-  bf::path zip_location = GetZipPackageLocation(
-        context_->pkg_path.get(), context_->pkgid.get());
-  bf::path backup_zip_location = GetBackupPathForZipFile(zip_location);
+  fs::path zip_location = GetZipPackageLocation(
+        context_->GetPkgPath(), context_->pkgid.get());
+  fs::path backup_zip_location = GetBackupPathForZipFile(zip_location);
 
-  if (bf::exists(backup_zip_location)) {
+  if (fs::exists(backup_zip_location)) {
     if (!Remove(zip_location))
       return Status::APP_DIR_ERROR;
     if (!MoveFile(backup_zip_location, zip_location)) {
@@ -92,7 +102,21 @@ Step::Status StepMountUpdate::undo() {
                  << backup_zip_location;
       return Status::APP_DIR_ERROR;
     }
+
+    // mount previous file for re-registration of trust anchor
+    fs::path mount_point = GetMountLocation(context_->GetPkgPath());
+    auto zip_final = CreateZipInterface(mount_point);
+    fs::path zip_destination_path =
+      GetZipPackageLocation(context_->GetPkgPath(), context_->pkgid.get());
+    if (!zip_final->MountZip(zip_destination_path)) {
+      LOG(ERROR) << "Failed to mount zip package in installation path";
+      return Status::APP_DIR_ERROR;
+    }
   }
+
+  if (fs::exists(backup_path_))
+    RemoveAll(backup_path_);
+
   return Status::OK;
 }
 
@@ -101,7 +125,7 @@ Step::Status StepMountUpdate::precheck() {
     LOG(ERROR) << "root_application_path attribute is empty";
     return Step::Status::INVALID_VALUE;
   }
-  if (!boost::filesystem::exists(context_->root_application_path.get())) {
+  if (!std::filesystem::exists(context_->root_application_path.get())) {
     LOG(ERROR) << "root_application_path ("
                << context_->root_application_path.get()
                << ") path does not exist";
@@ -112,7 +136,7 @@ Step::Status StepMountUpdate::precheck() {
     LOG(ERROR) << "unpacked_dir_path attribute is empty";
     return Step::Status::INVALID_VALUE;
   }
-  if (!boost::filesystem::exists(context_->unpacked_dir_path.get())) {
+  if (!std::filesystem::exists(context_->unpacked_dir_path.get())) {
     LOG(ERROR) << "unpacked_dir_path ("
                << context_->unpacked_dir_path.get()
                << ") path does not exist";
@@ -124,8 +148,17 @@ Step::Status StepMountUpdate::precheck() {
     return Step::Status::PACKAGE_NOT_FOUND;
   }
 
+  backup_path_ = GetBackupPathForPackagePath(context_->GetPkgPath());
+
   return Step::Status::OK;
 }
 
+std::unique_ptr<IZipInterface> StepMountUpdate::CreateZipInterface(
+    const fs::path& mount_path) {
+  std::unique_ptr<IZipInterface> zip_interface(
+      new TzipInterface(mount_path));
+  return zip_interface;
+}
+
 }  // namespace mount
 }  // namespace common_installer