Remove boost dependency
[platform/core/appfw/app-installers.git] / src / common / step / filesystem / step_unzip.cc
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.
5
6 #include "common/step/filesystem/step_unzip.h"
7
8 #include <tzplatform_config.h>
9
10 #include <cassert>
11 #include <cstdio>
12 #include <cstdlib>
13 #include <cstdint>
14 #include <climits>
15 #include <cstring>
16 #include <filesystem>
17 #include <string>
18
19 #include "common/utils/file_util.h"
20
21 namespace fs = std::filesystem;
22
23 namespace {
24
25 const char kPackageUnpackDirPath[] = UNPACKDIR;
26
27 }  // namespace
28
29 namespace common_installer {
30 namespace filesystem {
31
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;
36   }
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;
42   }
43
44   if (context_->root_application_path.get().empty()) {
45     LOG(ERROR) << "root_application_path attribute is empty";
46     return Step::Status::INVALID_VALUE;
47   }
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;
53   }
54
55   return Step::Status::OK;
56 }
57
58 Step::Status StepUnzip::process() {
59   fs::path tmp_dir = GenerateTmpDir(kPackageUnpackDirPath);
60
61   int64_t required_size =
62       GetUnpackedPackageSize(context_->file_path.get());
63
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;
68   }
69
70   LOG(DEBUG) << "Required size for application: " << required_size << "B";
71
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;
75   }
76
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;
81   }
82
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();
87   }
88
89   if (!CreateDir(tmp_dir)) {
90     LOG(ERROR) << "Failed to create temp directory: " << tmp_dir;
91     return Step::Status::APP_DIR_ERROR;
92   }
93
94
95   if (!ExtractToTmpDir(context_->file_path.get().c_str(), tmp_dir)) {
96     LOG(ERROR) << "Failed to process unpack step";
97     RemoveAll(tmp_dir);
98     return Step::Status::UNZIP_ERROR;
99   }
100   context_->unpacked_dir_path.set(tmp_dir);
101
102   LOG(INFO) << context_->file_path.get() << " was successfully unzipped into "
103       << context_->unpacked_dir_path.get();
104   return Status::OK;
105 }
106
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();
111   }
112   return Status::OK;
113 }
114
115
116 }  // namespace filesystem
117 }  // namespace common_installer