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/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 StepMountUpdate::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 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 ci::RemoveRWDirectories(zip_destination_path);
46 context_->manifest_data.get()->zip_mount_file =
47 strdup(zip_destination_path.c_str());
49 bf::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 StepMountUpdate::UmountPackagePath() {
61 bf::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 StepMountUpdate::clean() {
72 bf::path backup_zip_location =
73 GetBackupPathForZipFile(GetZipPackageLocation(
74 context_->GetPkgPath(), context_->pkgid.get()));
75 Remove(backup_zip_location);
80 Step::Status StepMountUpdate::undo() {
83 bf::path zip_location = GetZipPackageLocation(
84 context_->GetPkgPath(), context_->pkgid.get());
85 bf::path backup_zip_location = GetBackupPathForZipFile(zip_location);
87 if (bf::exists(backup_zip_location)) {
88 if (!Remove(zip_location))
89 return Status::APP_DIR_ERROR;
90 if (!MoveFile(backup_zip_location, zip_location)) {
91 LOG(ERROR) << "Failed to restore backup of zip file: "
92 << backup_zip_location;
93 return Status::APP_DIR_ERROR;
96 // mount previous file for re-registration of trust anchor
97 bf::path mount_point = GetMountLocation(context_->GetPkgPath());
98 auto zip_final = CreateZipInterface(mount_point);
99 bf::path zip_destination_path =
100 GetZipPackageLocation(context_->GetPkgPath(), context_->pkgid.get());
101 if (!zip_final->MountZip(zip_destination_path)) {
102 LOG(ERROR) << "Failed to mount zip package in installation path";
103 return Status::APP_DIR_ERROR;
109 Step::Status StepMountUpdate::precheck() {
110 if (context_->root_application_path.get().empty()) {
111 LOG(ERROR) << "root_application_path attribute is empty";
112 return Step::Status::INVALID_VALUE;
114 if (!boost::filesystem::exists(context_->root_application_path.get())) {
115 LOG(ERROR) << "root_application_path ("
116 << context_->root_application_path.get()
117 << ") path does not exist";
118 return Step::Status::INVALID_VALUE;
121 if (context_->unpacked_dir_path.get().empty()) {
122 LOG(ERROR) << "unpacked_dir_path attribute is empty";
123 return Step::Status::INVALID_VALUE;
125 if (!boost::filesystem::exists(context_->unpacked_dir_path.get())) {
126 LOG(ERROR) << "unpacked_dir_path ("
127 << context_->unpacked_dir_path.get()
128 << ") path does not exist";
129 return Step::Status::INVALID_VALUE;
132 if (context_->pkgid.get().empty()) {
133 LOG(ERROR) << "pkgid attribute is empty";
134 return Step::Status::PACKAGE_NOT_FOUND;
137 return Step::Status::OK;
140 std::unique_ptr<IZipInterface> StepMountUpdate::CreateZipInterface(
141 const boost::filesystem::path& mount_path) {
142 std::unique_ptr<IZipInterface> zip_interface(
143 new TzipInterface(mount_path));
144 return zip_interface;
148 } // namespace common_installer