Remove boost dependency
[platform/core/appfw/app-installers.git] / src / common / step / mount / step_mount_recover.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_recover.h"
6
7 #include <pkgmgrinfo_basic.h>
8
9 #include <filesystem>
10
11 #include "common/utils/file_util.h"
12 #include "common/utils/paths.h"
13 #include "common/tzip_interface.h"
14 #include "common/zip_interface.h"
15
16 namespace fs = std::filesystem;
17
18 namespace common_installer {
19 namespace mount {
20
21 Step::Status StepMountRecover::RecoveryMountUpdate() {
22   fs::path zip_destination_path =
23       GetZipPackageLocation(context_->GetPkgPath(), context_->pkgid.get());
24   manifest_x* manifest = context_->manifest_data.get();
25   if (manifest)
26     manifest->zip_mount_file = strdup(zip_destination_path.c_str());
27
28   fs::path mount_point = GetMountLocation(context_->GetPkgPath());
29   auto zip_final = CreateZipInterface(mount_point);
30   if (!zip_final->MountZip(zip_destination_path)) {
31     LOG(ERROR) << "Failed to mount zip package in installation path";
32     return Status::APP_DIR_ERROR;
33   }
34
35   return Status::OK;
36 }
37
38 Step::Status StepMountRecover::Cleanup() {
39   recovery::RecoveryFile* recovery_file =
40       context_->recovery_info.get().recovery_file.get();
41   if (!recovery_file) {
42     LOG(ERROR) << "Failed to get recovery info";
43     return Status::RECOVERY_ERROR;
44   }
45
46   if (recovery_file->type() != RequestType::MountUpdate)
47     return Status::OK;
48
49   fs::path zip_destination_path =
50       GetZipPackageLocation(context_->GetPkgPath(), context_->pkgid.get());
51   fs::path backup_zip_location = GetBackupPathForZipFile(zip_destination_path);
52
53   if (fs::exists(backup_zip_location)) {
54     if (!Remove(backup_zip_location)) {
55       LOG(ERROR) << "Fail to remove backup zip location : "
56           << backup_zip_location;
57       return Status::RECOVERY_ERROR;
58     }
59   }
60
61   return Status::OK;
62 }
63
64 Step::Status StepMountRecover::clean() {
65   recovery::RecoveryFile* recovery_file =
66       context_->recovery_info.get().recovery_file.get();
67   if (!recovery_file) {
68     LOG(ERROR) << "Failed to get recovery info";
69     return Status::RECOVERY_ERROR;
70   }
71
72   if (recovery_file->type() != RequestType::MountUpdate)
73     return Status::OK;
74
75   UmountPackagePath();
76   return Status::OK;
77 }
78
79 Step::Status StepMountRecover::undo() {
80   recovery::RecoveryFile* recovery_file =
81       context_->recovery_info.get().recovery_file.get();
82   if (!recovery_file) {
83     LOG(ERROR) << "Failed to get recovery info";
84     return Status::RECOVERY_ERROR;
85   }
86
87   if (recovery_file->type() != RequestType::MountUpdate)
88     return Status::OK;
89
90   UmountPackagePath();
91   return Status::OK;
92 }
93
94 Step::Status StepMountRecover::UmountPackagePath() {
95   fs::path mount_point = GetMountLocation(context_->GetPkgPath());
96   auto zip_final = CreateZipInterface(mount_point);
97   if (!zip_final->UnmountZip()) {
98     LOG(ERROR) << "Failed to unmount zip package after installation";
99     return Status::APP_DIR_ERROR;
100   }
101
102   return Status::OK;
103 }
104
105 std::unique_ptr<IZipInterface> StepMountRecover::CreateZipInterface(
106     const std::filesystem::path& mount_path) {
107   std::unique_ptr<IZipInterface> zip_interface(
108       new TzipInterface(mount_path));
109   return zip_interface;
110 }
111
112 }  // namespace mount
113 }  // namespace common_installer