1 /* 2014, Copyright © Intel Coporation, APACHE-2.0, see LICENSE file */
2 // Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3 // Use of this source code is governed by a apache 2.0 license that can be
4 // found in the LICENSE file.
6 #include "common/step/filesystem/step_unzip.h"
8 #include <tzplatform_config.h>
19 #include "common/utils/file_util.h"
21 namespace fs = std::filesystem;
25 const char kPackageUnpackDirPath[] = UNPACKDIR;
29 namespace common_installer {
30 namespace filesystem {
32 Step::Status StepUnzip::precheck() {
33 if (context_->file_path.get().empty()) {
34 LOG(ERROR) << "file_path attribute is empty";
35 return Step::Status::INVALID_VALUE;
37 if (!std::filesystem::exists(context_->file_path.get())) {
38 LOG(ERROR) << "file_path ("
39 << context_->file_path.get()
40 << ") path does not exist";
41 return Step::Status::INVALID_VALUE;
44 if (context_->root_application_path.get().empty()) {
45 LOG(ERROR) << "root_application_path attribute is empty";
46 return Step::Status::INVALID_VALUE;
48 if (!std::filesystem::exists(context_->root_application_path.get())) {
49 LOG(ERROR) << "root_application_path ("
50 << context_->root_application_path.get()
51 << ") path does not exist";
52 return Step::Status::INVALID_VALUE;
55 return Step::Status::OK;
58 Step::Status StepUnzip::process() {
59 fs::path tmp_dir = GenerateTmpDir(kPackageUnpackDirPath);
61 int64_t required_size =
62 GetUnpackedPackageSize(context_->file_path.get());
64 if (required_size == -1) {
65 LOG(ERROR) << "Couldn't get uncompressed size for package: "
66 << context_->file_path.get();
67 return Step::Status::UNZIP_ERROR;
70 LOG(DEBUG) << "Required size for application: " << required_size << "B";
72 if (!CheckFreeSpaceAtPath(required_size, tmp_dir)) {
73 LOG(ERROR) << "There is not enough space to unpack application files";
74 return Step::Status::OUT_OF_SPACE;
77 if (!CheckFreeSpaceAtPath(required_size,
78 fs::path(context_->root_application_path.get()))) {
79 LOG(ERROR) << "There is not enough space to install application files";
80 return Step::Status::OUT_OF_SPACE;
83 // write unpacked directory for recovery file
84 if (context_->recovery_info.get().recovery_file) {
85 context_->recovery_info.get().recovery_file->set_unpacked_dir(tmp_dir);
86 context_->recovery_info.get().recovery_file->WriteAndCommitFileContent();
89 if (!CreateDir(tmp_dir)) {
90 LOG(ERROR) << "Failed to create temp directory: " << tmp_dir;
91 return Step::Status::APP_DIR_ERROR;
95 if (!ExtractToTmpDir(context_->file_path.get().c_str(), tmp_dir)) {
96 LOG(ERROR) << "Failed to process unpack step";
98 return Step::Status::UNZIP_ERROR;
100 context_->unpacked_dir_path.set(tmp_dir);
102 LOG(INFO) << context_->file_path.get() << " was successfully unzipped into "
103 << context_->unpacked_dir_path.get();
107 Step::Status StepUnzip::undo() {
108 if (access(context_->unpacked_dir_path.get().c_str(), F_OK) == 0) {
109 RemoveAll(context_->unpacked_dir_path.get());
110 LOG(DEBUG) << "remove temp dir: " << context_->unpacked_dir_path.get();
116 } // namespace filesystem
117 } // namespace common_installer