b21d4dd0c768f34dfb07fd0b9b7059cb4e5f4805
[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 <boost/filesystem/operations.hpp>
8 #include <boost/filesystem/path.hpp>
9
10 #include <string>
11
12 #include "common/shared_dirs.h"
13 #include "common/utils/paths.h"
14 #include "common/tzip_interface.h"
15 #include "common/zip_interface.h"
16 #include "common/utils/file_util.h"
17 #include "common/utils/request.h"
18
19 namespace bf = boost::filesystem;
20 namespace bs = boost::system;
21 namespace ci = common_installer;
22
23 namespace common_installer {
24 namespace mount {
25
26 Step::Status StepMountUpdate::process() {
27   auto zip_unpack = CreateZipInterface(context_->unpacked_dir_path.get());
28   if (!zip_unpack->UnmountZip()) {
29     LOG(ERROR) << "Failed to unmount zip package from temporary path";
30     return Status::APP_DIR_ERROR;
31   }
32
33   bf::path zip_destination_path =
34       GetZipPackageLocation(context_->GetPkgPath(), context_->pkgid.get());
35   bf::path backup_zip_location = GetBackupPathForZipFile(zip_destination_path);
36
37   if (!MoveFile(zip_destination_path, backup_zip_location)) {
38     LOG(ERROR) << "Files to create backup of zip package file";
39     return Status::APP_DIR_ERROR;
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   bf::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 StepMountUpdate::UmountPackagePath() {
61   bf::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 StepMountUpdate::clean() {
72   bf::path backup_zip_location =
73       GetBackupPathForZipFile(GetZipPackageLocation(
74           context_->GetPkgPath(), context_->pkgid.get()));
75   Remove(backup_zip_location);
76
77   return Status::OK;
78 }
79
80 Step::Status StepMountUpdate::undo() {
81   UmountPackagePath();
82
83   bf::path zip_location = GetZipPackageLocation(
84         context_->GetPkgPath(), context_->pkgid.get());
85   bf::path backup_zip_location = GetBackupPathForZipFile(zip_location);
86
87   if (bf::exists(backup_zip_location)) {
88     if (!Remove(zip_location))
89       return Status::APP_DIR_ERROR;
90     if (!MoveFile(backup_zip_location, zip_location)) {
91       LOG(ERROR) << "Failed to restore backup of zip file: "
92                  << backup_zip_location;
93       return Status::APP_DIR_ERROR;
94     }
95
96     // mount previous file for re-registration of trust anchor
97     bf::path mount_point = GetMountLocation(context_->GetPkgPath());
98     auto zip_final = CreateZipInterface(mount_point);
99     bf::path zip_destination_path =
100       GetZipPackageLocation(context_->GetPkgPath(), context_->pkgid.get());
101     if (!zip_final->MountZip(zip_destination_path)) {
102       LOG(ERROR) << "Failed to mount zip package in installation path";
103       return Status::APP_DIR_ERROR;
104     }
105   }
106   return Status::OK;
107 }
108
109 Step::Status StepMountUpdate::precheck() {
110   if (context_->root_application_path.get().empty()) {
111     LOG(ERROR) << "root_application_path attribute is empty";
112     return Step::Status::INVALID_VALUE;
113   }
114   if (!boost::filesystem::exists(context_->root_application_path.get())) {
115     LOG(ERROR) << "root_application_path ("
116                << context_->root_application_path.get()
117                << ") path does not exist";
118     return Step::Status::INVALID_VALUE;
119   }
120
121   if (context_->unpacked_dir_path.get().empty()) {
122     LOG(ERROR) << "unpacked_dir_path attribute is empty";
123     return Step::Status::INVALID_VALUE;
124   }
125   if (!boost::filesystem::exists(context_->unpacked_dir_path.get())) {
126     LOG(ERROR) << "unpacked_dir_path ("
127                << context_->unpacked_dir_path.get()
128                << ") path does not exist";
129     return Step::Status::INVALID_VALUE;
130   }
131
132   if (context_->pkgid.get().empty()) {
133     LOG(ERROR) << "pkgid attribute is empty";
134     return Step::Status::PACKAGE_NOT_FOUND;
135   }
136
137   return Step::Status::OK;
138 }
139
140 std::unique_ptr<IZipInterface> StepMountUpdate::CreateZipInterface(
141     const boost::filesystem::path& mount_path) {
142   std::unique_ptr<IZipInterface> zip_interface(
143       new TzipInterface(mount_path));
144   return zip_interface;
145 }
146
147 }  // namespace mount
148 }  // namespace common_installer