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"
10 #include "common/shared_dirs.h"
11 #include "common/utils/paths.h"
12 #include "common/tzip_interface.h"
13 #include "common/zip_interface.h"
14 #include "common/utils/file_util.h"
15 #include "common/utils/request.h"
17 namespace ci = common_installer;
18 namespace fs = std::filesystem;
20 namespace common_installer {
23 Step::Status StepMountInstall::process() {
24 auto zip_unpack = CreateZipInterface(context_->unpacked_dir_path.get());
25 if (!zip_unpack->UnmountZip()) {
26 LOG(ERROR) << "Failed to unmount zip package from temporary path";
27 return Status::APP_DIR_ERROR;
30 fs::path zip_destination_path =
31 GetZipPackageLocation(context_->GetPkgPath(), context_->pkgid.get());
32 if (!fs::exists(zip_destination_path.parent_path())) {
33 std::error_code error;
34 fs::create_directories(zip_destination_path.parent_path(), error);
36 LOG(ERROR) << "Failed to create diretory: "
37 << zip_destination_path.parent_path();
38 return Status::APP_DIR_ERROR;
42 if (!CopyFile(context_->file_path.get(), zip_destination_path)) {
43 return Status::APP_DIR_ERROR;
45 ci::RemoveRWDirectories(zip_destination_path);
46 context_->manifest_data.get()->zip_mount_file =
47 strdup(zip_destination_path.c_str());
49 fs::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;
56 LOG(INFO) << "Successfully mount zip package in: " << mount_point;
60 Step::Status StepMountInstall::UmountPackagePath() {
61 fs::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;
71 Step::Status StepMountInstall::undo() {
72 std::error_code error;
74 fs::remove(GetZipPackageLocation(context_->GetPkgPath(),
75 context_->pkgid.get()), error);
77 LOG(ERROR) << "Failed to remove zip package";
78 return Status::APP_DIR_ERROR;
80 fs::remove_all(context_->GetPkgPath(), error);
82 LOG(ERROR) << "Failed to remove package content";
83 return Status::APP_DIR_ERROR;
88 Step::Status StepMountInstall::precheck() {
89 if (context_->root_application_path.get().empty()) {
90 LOG(ERROR) << "root_application_path attribute is empty";
91 return Step::Status::INVALID_VALUE;
93 if (!fs::exists(context_->root_application_path.get())) {
94 LOG(ERROR) << "root_application_path ("
95 << context_->root_application_path.get()
96 << ") path does not exist";
97 return Step::Status::INVALID_VALUE;
100 if (context_->unpacked_dir_path.get().empty()) {
101 LOG(ERROR) << "unpacked_dir_path attribute is empty";
102 return Step::Status::INVALID_VALUE;
104 if (!fs::exists(context_->unpacked_dir_path.get())) {
105 LOG(ERROR) << "unpacked_dir_path ("
106 << context_->unpacked_dir_path.get()
107 << ") path does not exist";
108 return Step::Status::INVALID_VALUE;
111 if (context_->pkgid.get().empty()) {
112 LOG(ERROR) << "pkgid attribute is empty";
113 return Step::Status::PACKAGE_NOT_FOUND;
116 return Step::Status::OK;
119 std::unique_ptr<IZipInterface> StepMountInstall::CreateZipInterface(
120 const fs::path& mount_path) {
121 std::unique_ptr<IZipInterface> zip_interface(
122 new TzipInterface(mount_path));
123 return zip_interface;
127 } // namespace common_installer