e922b0cb0921ea05308007f85cf70a0785dc7c7f
[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/paths.h"
13 #include "common/tzip_interface.h"
14 #include "common/zip_interface.h"
15
16 namespace bf = boost::filesystem;
17
18 namespace common_installer {
19 namespace mount {
20
21 Step::Status StepMountRecover::RecoveryMountUpdate() {
22   bf::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   bf::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::clean() {
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   UmountPackagePath();
50   return Status::OK;
51 }
52
53 Step::Status StepMountRecover::undo() {
54   recovery::RecoveryFile* recovery_file =
55       context_->recovery_info.get().recovery_file.get();
56   if (!recovery_file) {
57     LOG(ERROR) << "Failed to get recovery info";
58     return Status::RECOVERY_ERROR;
59   }
60
61   if (recovery_file->type() != RequestType::MountUpdate)
62     return Status::OK;
63
64   UmountPackagePath();
65   return Status::OK;
66 }
67
68 Step::Status StepMountRecover::UmountPackagePath() {
69   bf::path mount_point = GetMountLocation(context_->GetPkgPath());
70   auto zip_final = CreateZipInterface(mount_point);
71   if (!zip_final->UnmountZip()) {
72     LOG(ERROR) << "Failed to unmount zip package after installation";
73     return Status::APP_DIR_ERROR;
74   }
75
76   return Status::OK;
77 }
78
79 std::unique_ptr<IZipInterface> StepMountRecover::CreateZipInterface(
80     const boost::filesystem::path& mount_path) {
81   std::unique_ptr<IZipInterface> zip_interface(
82       new TzipInterface(mount_path));
83   return zip_interface;
84 }
85
86 }  // namespace mount
87 }  // namespace common_installer