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.
5 #include "step/mount/step_mount_install.h"
7 #include <boost/filesystem/operations.hpp>
8 #include <boost/filesystem/path.hpp>
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"
19 namespace bf = boost::filesystem;
20 namespace bs = boost::system;
21 namespace ci = common_installer;
23 namespace common_installer {
26 Step::Status StepMountInstall::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;
33 bf::path zip_destination_path =
34 GetZipPackageLocation(context_->GetPkgPath(), context_->pkgid.get());
35 if (!bf::exists(zip_destination_path.parent_path())) {
37 bf::create_directories(zip_destination_path.parent_path(), error);
39 LOG(ERROR) << "Failed to create diretory: "
40 << zip_destination_path.parent_path();
41 return Status::APP_DIR_ERROR;
45 if (!CopyFile(context_->file_path.get(), zip_destination_path)) {
46 return Status::APP_DIR_ERROR;
48 ci::RemoveRWDirectories(zip_destination_path);
49 context_->manifest_data.get()->zip_mount_file =
50 strdup(zip_destination_path.c_str());
52 bf::path mount_point = GetMountLocation(context_->GetPkgPath());
53 auto zip_final = CreateZipInterface(mount_point);
54 if (!zip_final->MountZip(zip_destination_path)) {
55 LOG(ERROR) << "Failed to mount zip package in installation path";
56 return Status::APP_DIR_ERROR;
59 LOG(INFO) << "Successfully mount zip package in: " << mount_point;
63 Step::Status StepMountInstall::UmountPackagePath() {
64 bf::path mount_point = GetMountLocation(context_->GetPkgPath());
65 auto zip_final = CreateZipInterface(mount_point);
66 if (!zip_final->UnmountZip()) {
67 LOG(ERROR) << "Failed to unmount zip package after installation";
68 return Status::APP_DIR_ERROR;
74 Step::Status StepMountInstall::undo() {
77 bf::remove(GetZipPackageLocation(context_->GetPkgPath(),
78 context_->pkgid.get()), error);
80 LOG(ERROR) << "Failed to remove zip package";
81 return Status::APP_DIR_ERROR;
83 bf::remove_all(context_->GetPkgPath(), error);
85 LOG(ERROR) << "Failed to remove package content";
86 return Status::APP_DIR_ERROR;
91 Step::Status StepMountInstall::precheck() {
92 if (context_->root_application_path.get().empty()) {
93 LOG(ERROR) << "root_application_path attribute is empty";
94 return Step::Status::INVALID_VALUE;
96 if (!bf::exists(context_->root_application_path.get())) {
97 LOG(ERROR) << "root_application_path ("
98 << context_->root_application_path.get()
99 << ") path does not exist";
100 return Step::Status::INVALID_VALUE;
103 if (context_->unpacked_dir_path.get().empty()) {
104 LOG(ERROR) << "unpacked_dir_path attribute is empty";
105 return Step::Status::INVALID_VALUE;
107 if (!bf::exists(context_->unpacked_dir_path.get())) {
108 LOG(ERROR) << "unpacked_dir_path ("
109 << context_->unpacked_dir_path.get()
110 << ") path does not exist";
111 return Step::Status::INVALID_VALUE;
114 if (context_->pkgid.get().empty()) {
115 LOG(ERROR) << "pkgid attribute is empty";
116 return Step::Status::PACKAGE_NOT_FOUND;
119 return Step::Status::OK;
122 std::unique_ptr<IZipInterface> StepMountInstall::CreateZipInterface(
123 const boost::filesystem::path& mount_path) {
124 std::unique_ptr<IZipInterface> zip_interface(
125 new TzipInterface(mount_path));
126 return zip_interface;
130 } // namespace common_installer