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 "common/step/mount/step_mount_update.h"
7 #include <boost/filesystem/operations.hpp>
8 #include <boost/filesystem/path.hpp>
12 #include "common/backup_paths.h"
13 #include "common/request.h"
14 #include "common/tzip_interface.h"
15 #include "common/utils/file_util.h"
17 namespace bf = boost::filesystem;
18 namespace bs = boost::system;
20 namespace common_installer {
23 Step::Status StepMountUpdate::process() {
24 context_->pkg_path.set(
25 context_->root_application_path.get() / context_->pkgid.get());
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;
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);
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;
42 if (!CopyFile(context_->file_path.get(), zip_destination_path)) {
43 return Status::APP_DIR_ERROR;
45 context_->manifest_data.get()->zip_mount_file =
46 strdup(zip_destination_path.c_str());
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;
55 LOG(INFO) << "Successfully mount zip package in: " << mount_point;
59 Step::Status StepMountUpdate::clean() {
60 bf::path backup_zip_location =
61 GetBackupPathForZipFile(GetZipPackageLocation(
62 context_->pkg_path.get(), context_->pkgid.get()));
64 bf::remove(backup_zip_location, error);
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;
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);
80 if (bf::exists(backup_zip_location)) {
82 bf::remove(zip_location, error);
84 LOG(ERROR) << "Failed to remove: " << zip_location;
85 return Status::APP_DIR_ERROR;
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;
94 bf::remove(context_->pkg_path.get(), error);
96 LOG(ERROR) << "Failed to remove package content";
97 return Status::APP_DIR_ERROR;
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;
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;
114 if (context_->unpacked_dir_path.get().empty()) {
115 LOG(ERROR) << "unpacked_dir_path attribute is empty";
116 return Step::Status::INVALID_VALUE;
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;
125 if (context_->pkgid.get().empty()) {
126 LOG(ERROR) << "pkgid attribute is empty";
127 return Step::Status::PACKAGE_NOT_FOUND;
130 return Step::Status::OK;
134 } // namespace common_installer