Store zip file location in manifest_x
[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/backup_paths.h"
13 #include "common/request.h"
14 #include "common/tzip_interface.h"
15 #include "common/utils/file_util.h"
16
17 namespace bf = boost::filesystem;
18 namespace bs = boost::system;
19
20 namespace common_installer {
21 namespace mount {
22
23 Step::Status StepMountUpdate::process() {
24   context_->pkg_path.set(
25      context_->root_application_path.get() / context_->pkgid.get());
26
27   TzipInterface tzip_unpack(context_->unpacked_dir_path.get());
28   if (!tzip_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_->pkg_path.get(), 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   context_->manifest_data.get()->zip_mount_file =
46       strdup(zip_destination_path.c_str());
47
48   bf::path mount_point = GetMountLocation(context_->pkg_path.get());
49   TzipInterface tzip_final(mount_point);
50   if (!tzip_final.MountZip(zip_destination_path)) {
51     LOG(ERROR) << "Failed to mount zip package in installation path";
52     return Status::APP_DIR_ERROR;
53   }
54
55   LOG(INFO) << "Successfully mount zip package in: " << mount_point;
56   return Status::OK;
57 }
58
59 Step::Status StepMountUpdate::clean() {
60   bf::path backup_zip_location =
61       GetBackupPathForZipFile(GetZipPackageLocation(
62           context_->pkg_path.get(), context_->pkgid.get()));
63   bs::error_code error;
64   bf::remove(backup_zip_location, error);
65
66   bf::path mount_point = GetMountLocation(context_->pkg_path.get());
67   TzipInterface tzip_final(mount_point);
68   if (!tzip_final.UnmountZip()) {
69     LOG(ERROR) << "Failed to unmount zip package after installation";
70     return Status::APP_DIR_ERROR;
71   }
72   return Status::OK;
73 }
74
75 Step::Status StepMountUpdate::undo() {
76   bf::path zip_location = GetZipPackageLocation(
77         context_->pkg_path.get(), context_->pkgid.get());
78   bf::path backup_zip_location = GetBackupPathForZipFile(zip_location);
79
80   if (bf::exists(backup_zip_location)) {
81     bs::error_code error;
82     bf::remove(zip_location, error);
83     if (error) {
84       LOG(ERROR) << "Failed to remove: " << zip_location;
85       return Status::APP_DIR_ERROR;
86     }
87     if (!MoveFile(backup_zip_location, zip_location)) {
88       LOG(ERROR) << "Failed to restore backup of zip file: "
89                  << backup_zip_location;
90       return Status::APP_DIR_ERROR;
91     }
92   }
93   bs::error_code error;
94   bf::remove(context_->pkg_path.get(), error);
95   if (error) {
96     LOG(ERROR) << "Failed to remove package content";
97     return Status::APP_DIR_ERROR;
98   }
99   return Status::OK;
100 }
101
102 Step::Status StepMountUpdate::precheck() {
103   if (context_->root_application_path.get().empty()) {
104     LOG(ERROR) << "root_application_path attribute is empty";
105     return Step::Status::INVALID_VALUE;
106   }
107   if (!boost::filesystem::exists(context_->root_application_path.get())) {
108     LOG(ERROR) << "root_application_path ("
109                << context_->root_application_path.get()
110                << ") path does not exist";
111     return Step::Status::INVALID_VALUE;
112   }
113
114   if (context_->unpacked_dir_path.get().empty()) {
115     LOG(ERROR) << "unpacked_dir_path attribute is empty";
116     return Step::Status::INVALID_VALUE;
117   }
118   if (!boost::filesystem::exists(context_->unpacked_dir_path.get())) {
119     LOG(ERROR) << "unpacked_dir_path ("
120                << context_->unpacked_dir_path.get()
121                << ") path does not exist";
122     return Step::Status::INVALID_VALUE;
123   }
124
125   if (context_->pkgid.get().empty()) {
126     LOG(ERROR) << "pkgid attribute is empty";
127     return Step::Status::PACKAGE_NOT_FOUND;
128   }
129
130   return Step::Status::OK;
131 }
132
133 }  // namespace mount
134 }  // namespace common_installer