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>
10 #include <boost/filesystem.hpp>
11 #include <boost/chrono/detail/system.hpp>
20 #include "common/utils/file_util.h"
22 namespace bf = boost::filesystem;
23 namespace bs = boost::system;
27 const char kPackageUnpackDirPath[] = UNPACKDIR;
31 namespace common_installer {
32 namespace filesystem {
34 Step::Status StepUnzip::precheck() {
35 if (context_->file_path.get().empty()) {
36 LOG(ERROR) << "file_path attribute is empty";
37 return Step::Status::INVALID_VALUE;
39 if (!boost::filesystem::exists(context_->file_path.get())) {
40 LOG(ERROR) << "file_path ("
41 << context_->file_path.get()
42 << ") path does not exist";
43 return Step::Status::INVALID_VALUE;
46 if (context_->root_application_path.get().empty()) {
47 LOG(ERROR) << "root_application_path attribute is empty";
48 return Step::Status::INVALID_VALUE;
50 if (!boost::filesystem::exists(context_->root_application_path.get())) {
51 LOG(ERROR) << "root_application_path ("
52 << context_->root_application_path.get()
53 << ") path does not exist";
54 return Step::Status::INVALID_VALUE;
57 return Step::Status::OK;
60 Step::Status StepUnzip::process() {
61 bf::path tmp_dir = GenerateTmpDir(kPackageUnpackDirPath);
63 int64_t required_size =
64 GetUnpackedPackageSize(context_->file_path.get());
66 if (required_size == -1) {
67 LOG(ERROR) << "Couldn't get uncompressed size for package: "
68 << context_->file_path.get();
69 return Step::Status::UNZIP_ERROR;
72 LOG(DEBUG) << "Required size for application: " << required_size << "B";
74 if (!CheckFreeSpaceAtPath(required_size, tmp_dir)) {
75 LOG(ERROR) << "There is not enough space to unpack application files";
76 return Step::Status::OUT_OF_SPACE;
79 if (!CheckFreeSpaceAtPath(required_size,
80 bf::path(context_->root_application_path.get()))) {
81 LOG(ERROR) << "There is not enough space to install application files";
82 return Step::Status::OUT_OF_SPACE;
85 // write unpacked directory for recovery file
86 if (context_->recovery_info.get().recovery_file) {
87 context_->recovery_info.get().recovery_file->set_unpacked_dir(tmp_dir);
88 context_->recovery_info.get().recovery_file->WriteAndCommitFileContent();
91 if (!CreateDir(tmp_dir)) {
92 LOG(ERROR) << "Failed to create temp directory: " << tmp_dir;
93 return Step::Status::APP_DIR_ERROR;
97 if (!ExtractToTmpDir(context_->file_path.get().string().c_str(), tmp_dir)) {
98 LOG(ERROR) << "Failed to process unpack step";
100 return Step::Status::UNZIP_ERROR;
102 context_->unpacked_dir_path.set(tmp_dir);
104 LOG(INFO) << context_->file_path.get() << " was successfully unzipped into "
105 << context_->unpacked_dir_path.get();
109 Step::Status StepUnzip::undo() {
110 if (access(context_->unpacked_dir_path.get().string().c_str(), F_OK) == 0) {
111 RemoveAll(context_->unpacked_dir_path.get());
112 LOG(DEBUG) << "remove temp dir: " << context_->unpacked_dir_path.get();
118 } // namespace filesystem
119 } // namespace common_installer