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"
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 StepMountUpdate::process() {
24 std::error_code error;
26 if (!fs::exists(backup_path_)) {
27 fs::create_directories(backup_path_, error);
29 LOG(ERROR) << "Failed to create backup directory: " << backup_path_;
30 return Status::APP_DIR_ERROR;
34 auto zip_unpack = CreateZipInterface(context_->unpacked_dir_path.get());
35 if (!zip_unpack->UnmountZip()) {
36 LOG(ERROR) << "Failed to unmount zip package from temporary path";
37 return Status::APP_DIR_ERROR;
40 fs::path zip_destination_path =
41 GetZipPackageLocation(context_->GetPkgPath(), context_->pkgid.get());
42 fs::path backup_zip_location = GetBackupPathForZipFile(zip_destination_path);
44 if (!MoveFile(zip_destination_path, backup_zip_location)) {
45 LOG(ERROR) << "Files to create backup of zip package file";
46 return Status::APP_DIR_ERROR;
49 if (!CopyFile(context_->file_path.get(), zip_destination_path)) {
50 return Status::APP_DIR_ERROR;
52 ci::RemoveRWDirectories(zip_destination_path);
53 context_->manifest_data.get()->zip_mount_file =
54 strdup(zip_destination_path.c_str());
56 fs::path mount_point = GetMountLocation(context_->GetPkgPath());
57 auto zip_final = CreateZipInterface(mount_point);
58 if (!zip_final->MountZip(zip_destination_path)) {
59 LOG(ERROR) << "Failed to mount zip package in installation path";
60 return Status::APP_DIR_ERROR;
63 LOG(INFO) << "Successfully mount zip package in: " << mount_point;
67 Step::Status StepMountUpdate::UmountPackagePath() {
68 fs::path mount_point = GetMountLocation(context_->GetPkgPath());
69 auto zip_final = CreateZipInterface(mount_point);
70 if (!zip_final->UnmountZip()) {
71 LOG(ERROR) << "Failed to unmount zip package after installation";
72 return Status::APP_DIR_ERROR;
78 Step::Status StepMountUpdate::clean() {
79 fs::path backup_zip_location =
80 GetBackupPathForZipFile(GetZipPackageLocation(
81 context_->GetPkgPath(), context_->pkgid.get()));
82 Remove(backup_zip_location);
84 if (fs::exists(backup_path_))
85 RemoveAll(backup_path_);
90 Step::Status StepMountUpdate::undo() {
93 fs::path zip_location = GetZipPackageLocation(
94 context_->GetPkgPath(), context_->pkgid.get());
95 fs::path backup_zip_location = GetBackupPathForZipFile(zip_location);
97 if (fs::exists(backup_zip_location)) {
98 if (!Remove(zip_location))
99 return Status::APP_DIR_ERROR;
100 if (!MoveFile(backup_zip_location, zip_location)) {
101 LOG(ERROR) << "Failed to restore backup of zip file: "
102 << backup_zip_location;
103 return Status::APP_DIR_ERROR;
106 // mount previous file for re-registration of trust anchor
107 fs::path mount_point = GetMountLocation(context_->GetPkgPath());
108 auto zip_final = CreateZipInterface(mount_point);
109 fs::path zip_destination_path =
110 GetZipPackageLocation(context_->GetPkgPath(), context_->pkgid.get());
111 if (!zip_final->MountZip(zip_destination_path)) {
112 LOG(ERROR) << "Failed to mount zip package in installation path";
113 return Status::APP_DIR_ERROR;
117 if (fs::exists(backup_path_))
118 RemoveAll(backup_path_);
123 Step::Status StepMountUpdate::precheck() {
124 if (context_->root_application_path.get().empty()) {
125 LOG(ERROR) << "root_application_path attribute is empty";
126 return Step::Status::INVALID_VALUE;
128 if (!std::filesystem::exists(context_->root_application_path.get())) {
129 LOG(ERROR) << "root_application_path ("
130 << context_->root_application_path.get()
131 << ") path does not exist";
132 return Step::Status::INVALID_VALUE;
135 if (context_->unpacked_dir_path.get().empty()) {
136 LOG(ERROR) << "unpacked_dir_path attribute is empty";
137 return Step::Status::INVALID_VALUE;
139 if (!std::filesystem::exists(context_->unpacked_dir_path.get())) {
140 LOG(ERROR) << "unpacked_dir_path ("
141 << context_->unpacked_dir_path.get()
142 << ") path does not exist";
143 return Step::Status::INVALID_VALUE;
146 if (context_->pkgid.get().empty()) {
147 LOG(ERROR) << "pkgid attribute is empty";
148 return Step::Status::PACKAGE_NOT_FOUND;
151 backup_path_ = GetBackupPathForPackagePath(context_->GetPkgPath());
153 return Step::Status::OK;
156 std::unique_ptr<IZipInterface> StepMountUpdate::CreateZipInterface(
157 const fs::path& mount_path) {
158 std::unique_ptr<IZipInterface> zip_interface(
159 new TzipInterface(mount_path));
160 return zip_interface;
164 } // namespace common_installer