Remove boost dependency
[platform/core/appfw/app-installers.git] / src / common / step / mount / step_mount_update.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 "common/step/mount/step_mount_update.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 StepMountUpdate::process() {
24   std::error_code error;
25
26   if (!fs::exists(backup_path_)) {
27     fs::create_directories(backup_path_, error);
28     if (error) {
29       LOG(ERROR) << "Failed to create backup directory: " << backup_path_;
30       return Status::APP_DIR_ERROR;
31     }
32   }
33
34   auto zip_unpack = CreateZipInterface(context_->unpacked_dir_path.get());
35   if (!zip_unpack->UnmountZip()) {
36     LOG(ERROR) << "Failed to unmount zip package from temporary path";
37     return Status::APP_DIR_ERROR;
38   }
39
40   fs::path zip_destination_path =
41       GetZipPackageLocation(context_->GetPkgPath(), context_->pkgid.get());
42   fs::path backup_zip_location = GetBackupPathForZipFile(zip_destination_path);
43
44   if (!MoveFile(zip_destination_path, backup_zip_location)) {
45     LOG(ERROR) << "Files to create backup of zip package file";
46     return Status::APP_DIR_ERROR;
47   }
48
49   if (!CopyFile(context_->file_path.get(), zip_destination_path)) {
50     return Status::APP_DIR_ERROR;
51   }
52   ci::RemoveRWDirectories(zip_destination_path);
53   context_->manifest_data.get()->zip_mount_file =
54       strdup(zip_destination_path.c_str());
55
56   fs::path mount_point = GetMountLocation(context_->GetPkgPath());
57   auto zip_final = CreateZipInterface(mount_point);
58   if (!zip_final->MountZip(zip_destination_path)) {
59     LOG(ERROR) << "Failed to mount zip package in installation path";
60     return Status::APP_DIR_ERROR;
61   }
62
63   LOG(INFO) << "Successfully mount zip package in: " << mount_point;
64   return Status::OK;
65 }
66
67 Step::Status StepMountUpdate::UmountPackagePath() {
68   fs::path mount_point = GetMountLocation(context_->GetPkgPath());
69   auto zip_final = CreateZipInterface(mount_point);
70   if (!zip_final->UnmountZip()) {
71     LOG(ERROR) << "Failed to unmount zip package after installation";
72     return Status::APP_DIR_ERROR;
73   }
74
75   return Status::OK;
76 }
77
78 Step::Status StepMountUpdate::clean() {
79   fs::path backup_zip_location =
80       GetBackupPathForZipFile(GetZipPackageLocation(
81           context_->GetPkgPath(), context_->pkgid.get()));
82   Remove(backup_zip_location);
83
84   if (fs::exists(backup_path_))
85     RemoveAll(backup_path_);
86
87   return Status::OK;
88 }
89
90 Step::Status StepMountUpdate::undo() {
91   UmountPackagePath();
92
93   fs::path zip_location = GetZipPackageLocation(
94         context_->GetPkgPath(), context_->pkgid.get());
95   fs::path backup_zip_location = GetBackupPathForZipFile(zip_location);
96
97   if (fs::exists(backup_zip_location)) {
98     if (!Remove(zip_location))
99       return Status::APP_DIR_ERROR;
100     if (!MoveFile(backup_zip_location, zip_location)) {
101       LOG(ERROR) << "Failed to restore backup of zip file: "
102                  << backup_zip_location;
103       return Status::APP_DIR_ERROR;
104     }
105
106     // mount previous file for re-registration of trust anchor
107     fs::path mount_point = GetMountLocation(context_->GetPkgPath());
108     auto zip_final = CreateZipInterface(mount_point);
109     fs::path zip_destination_path =
110       GetZipPackageLocation(context_->GetPkgPath(), context_->pkgid.get());
111     if (!zip_final->MountZip(zip_destination_path)) {
112       LOG(ERROR) << "Failed to mount zip package in installation path";
113       return Status::APP_DIR_ERROR;
114     }
115   }
116
117   if (fs::exists(backup_path_))
118     RemoveAll(backup_path_);
119
120   return Status::OK;
121 }
122
123 Step::Status StepMountUpdate::precheck() {
124   if (context_->root_application_path.get().empty()) {
125     LOG(ERROR) << "root_application_path attribute is empty";
126     return Step::Status::INVALID_VALUE;
127   }
128   if (!std::filesystem::exists(context_->root_application_path.get())) {
129     LOG(ERROR) << "root_application_path ("
130                << context_->root_application_path.get()
131                << ") path does not exist";
132     return Step::Status::INVALID_VALUE;
133   }
134
135   if (context_->unpacked_dir_path.get().empty()) {
136     LOG(ERROR) << "unpacked_dir_path attribute is empty";
137     return Step::Status::INVALID_VALUE;
138   }
139   if (!std::filesystem::exists(context_->unpacked_dir_path.get())) {
140     LOG(ERROR) << "unpacked_dir_path ("
141                << context_->unpacked_dir_path.get()
142                << ") path does not exist";
143     return Step::Status::INVALID_VALUE;
144   }
145
146   if (context_->pkgid.get().empty()) {
147     LOG(ERROR) << "pkgid attribute is empty";
148     return Step::Status::PACKAGE_NOT_FOUND;
149   }
150
151   backup_path_ = GetBackupPathForPackagePath(context_->GetPkgPath());
152
153   return Step::Status::OK;
154 }
155
156 std::unique_ptr<IZipInterface> StepMountUpdate::CreateZipInterface(
157     const fs::path& mount_path) {
158   std::unique_ptr<IZipInterface> zip_interface(
159       new TzipInterface(mount_path));
160   return zip_interface;
161 }
162
163 }  // namespace mount
164 }  // namespace common_installer