Store zip file location in manifest_x
[platform/core/appfw/app-installers.git] / src / common / step / mount / step_mount_install.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_install.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 StepMountInstall::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   if (!bf::exists(zip_destination_path.parent_path())) {
36     bs::error_code error;
37     bf::create_directories(zip_destination_path.parent_path(), error);
38     if (error) {
39       LOG(ERROR) << "Failed to create diretory: "
40                  << zip_destination_path.parent_path();
41       return Status::APP_DIR_ERROR;
42     }
43   }
44
45   if (!CopyFile(context_->file_path.get(), zip_destination_path)) {
46     return Status::APP_DIR_ERROR;
47   }
48   context_->manifest_data.get()->zip_mount_file =
49       strdup(zip_destination_path.c_str());
50
51   bf::path mount_point = GetMountLocation(context_->pkg_path.get());
52   if (!bf::exists(mount_point)) {
53     bs::error_code error;
54     bf::create_directories(mount_point, error);
55     if (error) {
56       LOG(ERROR) << "Failed to create mount point directory: " << mount_point;
57       return Status::APP_DIR_ERROR;
58     }
59   }
60   TzipInterface tzip_final(mount_point);
61   if (!tzip_final.MountZip(zip_destination_path)) {
62     LOG(ERROR) << "Failed to mount zip package in installation path";
63     return Status::APP_DIR_ERROR;
64   }
65
66   LOG(INFO) << "Successfully mount zip package in: " << mount_point;
67   return Status::OK;
68 }
69
70 Step::Status StepMountInstall::clean() {
71   bf::path mount_point = GetMountLocation(context_->pkg_path.get());
72   TzipInterface tzip_final(mount_point);
73   if (!tzip_final.UnmountZip()) {
74     LOG(ERROR) << "Failed to unmount zip package after installation";
75     return Status::APP_DIR_ERROR;
76   }
77   bs::error_code error;
78   bf::remove(mount_point, error);
79   return Status::OK;
80 }
81
82 Step::Status StepMountInstall::undo() {
83   bs::error_code error;
84   bf::path mount_point = GetMountLocation(context_->pkg_path.get());
85   TzipInterface tzip_final(mount_point);
86   tzip_final.UnmountZip();
87   bf::remove(GetZipPackageLocation(context_->pkg_path.get(),
88                                    context_->pkgid.get()), error);
89   bf::remove(context_->pkg_path.get(), error);
90   if (error) {
91     LOG(ERROR) << "Failed to remove package content";
92     return Status::APP_DIR_ERROR;
93   }
94   return Status::OK;
95 }
96
97 Step::Status StepMountInstall::precheck() {
98   if (context_->root_application_path.get().empty()) {
99     LOG(ERROR) << "root_application_path attribute is empty";
100     return Step::Status::INVALID_VALUE;
101   }
102   if (!bf::exists(context_->root_application_path.get())) {
103     LOG(ERROR) << "root_application_path ("
104                << context_->root_application_path.get()
105                << ") path does not exist";
106     return Step::Status::INVALID_VALUE;
107   }
108
109   if (context_->unpacked_dir_path.get().empty()) {
110     LOG(ERROR) << "unpacked_dir_path attribute is empty";
111     return Step::Status::INVALID_VALUE;
112   }
113   if (!bf::exists(context_->unpacked_dir_path.get())) {
114     LOG(ERROR) << "unpacked_dir_path ("
115                << context_->unpacked_dir_path.get()
116                << ") path does not exist";
117     return Step::Status::INVALID_VALUE;
118   }
119
120   if (context_->pkgid.get().empty()) {
121     LOG(ERROR) << "pkgid attribute is empty";
122     return Step::Status::PACKAGE_NOT_FOUND;
123   }
124
125   return Step::Status::OK;
126 }
127
128 }  // namespace mount
129 }  // namespace common_installer