Remove boost dependency
[platform/core/appfw/app-installers.git] / src / common / step / mount / step_mount_install.cc
1 // Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
2 // Use of this source code is governed by an apache-2.0 license that can be
3 // found in the LICENSE file.
4
5 #include "step/mount/step_mount_install.h"
6
7 #include <filesystem>
8 #include <string>
9
10 #include "common/shared_dirs.h"
11 #include "common/utils/paths.h"
12 #include "common/tzip_interface.h"
13 #include "common/zip_interface.h"
14 #include "common/utils/file_util.h"
15 #include "common/utils/request.h"
16
17 namespace ci = common_installer;
18 namespace fs = std::filesystem;
19
20 namespace common_installer {
21 namespace mount {
22
23 Step::Status StepMountInstall::process() {
24   auto zip_unpack = CreateZipInterface(context_->unpacked_dir_path.get());
25   if (!zip_unpack->UnmountZip()) {
26     LOG(ERROR) << "Failed to unmount zip package from temporary path";
27     return Status::APP_DIR_ERROR;
28   }
29
30   fs::path zip_destination_path =
31       GetZipPackageLocation(context_->GetPkgPath(), context_->pkgid.get());
32   if (!fs::exists(zip_destination_path.parent_path())) {
33     std::error_code error;
34     fs::create_directories(zip_destination_path.parent_path(), error);
35     if (error) {
36       LOG(ERROR) << "Failed to create diretory: "
37                  << zip_destination_path.parent_path();
38       return Status::APP_DIR_ERROR;
39     }
40   }
41
42   if (!CopyFile(context_->file_path.get(), zip_destination_path)) {
43     return Status::APP_DIR_ERROR;
44   }
45   ci::RemoveRWDirectories(zip_destination_path);
46   context_->manifest_data.get()->zip_mount_file =
47       strdup(zip_destination_path.c_str());
48
49   fs::path mount_point = GetMountLocation(context_->GetPkgPath());
50   auto zip_final = CreateZipInterface(mount_point);
51   if (!zip_final->MountZip(zip_destination_path)) {
52     LOG(ERROR) << "Failed to mount zip package in installation path";
53     return Status::APP_DIR_ERROR;
54   }
55
56   LOG(INFO) << "Successfully mount zip package in: " << mount_point;
57   return Status::OK;
58 }
59
60 Step::Status StepMountInstall::UmountPackagePath() {
61   fs::path mount_point = GetMountLocation(context_->GetPkgPath());
62   auto zip_final = CreateZipInterface(mount_point);
63   if (!zip_final->UnmountZip()) {
64     LOG(ERROR) << "Failed to unmount zip package after installation";
65     return Status::APP_DIR_ERROR;
66   }
67
68   return Status::OK;
69 }
70
71 Step::Status StepMountInstall::undo() {
72   std::error_code error;
73   UmountPackagePath();
74   fs::remove(GetZipPackageLocation(context_->GetPkgPath(),
75                                    context_->pkgid.get()), error);
76   if (error) {
77     LOG(ERROR) << "Failed to remove zip package";
78     return Status::APP_DIR_ERROR;
79   }
80   fs::remove_all(context_->GetPkgPath(), error);
81   if (error) {
82     LOG(ERROR) << "Failed to remove package content";
83     return Status::APP_DIR_ERROR;
84   }
85   return Status::OK;
86 }
87
88 Step::Status StepMountInstall::precheck() {
89   if (context_->root_application_path.get().empty()) {
90     LOG(ERROR) << "root_application_path attribute is empty";
91     return Step::Status::INVALID_VALUE;
92   }
93   if (!fs::exists(context_->root_application_path.get())) {
94     LOG(ERROR) << "root_application_path ("
95                << context_->root_application_path.get()
96                << ") path does not exist";
97     return Step::Status::INVALID_VALUE;
98   }
99
100   if (context_->unpacked_dir_path.get().empty()) {
101     LOG(ERROR) << "unpacked_dir_path attribute is empty";
102     return Step::Status::INVALID_VALUE;
103   }
104   if (!fs::exists(context_->unpacked_dir_path.get())) {
105     LOG(ERROR) << "unpacked_dir_path ("
106                << context_->unpacked_dir_path.get()
107                << ") path does not exist";
108     return Step::Status::INVALID_VALUE;
109   }
110
111   if (context_->pkgid.get().empty()) {
112     LOG(ERROR) << "pkgid attribute is empty";
113     return Step::Status::PACKAGE_NOT_FOUND;
114   }
115
116   return Step::Status::OK;
117 }
118
119 std::unique_ptr<IZipInterface> StepMountInstall::CreateZipInterface(
120     const fs::path& mount_path) {
121   std::unique_ptr<IZipInterface> zip_interface(
122       new TzipInterface(mount_path));
123   return zip_interface;
124 }
125
126 }  // namespace mount
127 }  // namespace common_installer