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