1 /*
2 * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "binary_file_packer.h"
17
18 #include "compression_parser.h"
19 #include "restool_errors.h"
20
21 namespace OHOS {
22 namespace Global {
23 namespace Restool {
24 using namespace std;
25
BinaryFilePacker(const PackageParser & packageParser,const std::string & moduleName)26 BinaryFilePacker::BinaryFilePacker(const PackageParser &packageParser, const std::string &moduleName)
27 : packageParser_(packageParser), moduleName_(moduleName)
28 {
29 }
30
~BinaryFilePacker()31 BinaryFilePacker::~BinaryFilePacker()
32 {
33 }
34
StopCopy()35 void BinaryFilePacker::StopCopy()
36 {
37 stopCopy_.store(true);
38 }
39
CopyBinaryFileAsync(const std::vector<std::string> & inputs)40 std::future<uint32_t> BinaryFilePacker::CopyBinaryFileAsync(const std::vector<std::string> &inputs)
41 {
42 auto func = [this](const vector<string> &inputs) { return this->CopyBinaryFile(inputs); };
43 std::future<uint32_t> res = ThreadPool::GetInstance().Enqueue(func, inputs);
44 return res;
45 }
46
CopyBinaryFile(const vector<string> & inputs)47 uint32_t BinaryFilePacker::CopyBinaryFile(const vector<string> &inputs)
48 {
49 for (const auto &input : inputs) {
50 CopyBinaryFile(input);
51 }
52 if (CheckCopyResults() != RESTOOL_SUCCESS) {
53 return RESTOOL_ERROR;
54 }
55 return RESTOOL_SUCCESS;
56 }
57
CopyBinaryFile(const string & input)58 uint32_t BinaryFilePacker::CopyBinaryFile(const string &input)
59 {
60 string rawfilePath = FileEntry::FilePath(input).Append(RAW_FILE_DIR).GetPath();
61 if (CopyBinaryFile(rawfilePath, RAW_FILE_DIR) == RESTOOL_ERROR) {
62 return RESTOOL_ERROR;
63 }
64 string resfilePath = FileEntry::FilePath(input).Append(RES_FILE_DIR).GetPath();
65 if (CopyBinaryFile(resfilePath, RES_FILE_DIR) == RESTOOL_ERROR) {
66 return RESTOOL_ERROR;
67 }
68 return RESTOOL_SUCCESS;
69 }
70
CopyBinaryFile(const string & filePath,const string & fileType)71 uint32_t BinaryFilePacker::CopyBinaryFile(const string &filePath, const string &fileType)
72 {
73 if (!ResourceUtil::FileExist(filePath)) {
74 return RESTOOL_SUCCESS;
75 }
76
77 if (!FileEntry::IsDirectory(filePath)) {
78 PrintError(GetError(ERR_CODE_INVALID_RESOURCE_PATH).FormatCause(filePath.c_str(), "not a directory"));
79 return RESTOOL_ERROR;
80 }
81
82 if (ResourceUtil::IsIgnoreFile(fileType, false)) {
83 return RESTOOL_SUCCESS;
84 }
85
86 string dst = FileEntry::FilePath(packageParser_.GetOutput()).Append(RESOURCES_DIR).Append(fileType).GetPath();
87 if (CopyBinaryFileImpl(filePath, dst) != RESTOOL_SUCCESS) {
88 return RESTOOL_ERROR;
89 }
90 return RESTOOL_SUCCESS;
91 }
92
CopyBinaryFileImpl(const string & src,const string & dst)93 uint32_t BinaryFilePacker::CopyBinaryFileImpl(const string &src, const string &dst)
94 {
95 if (!ResourceUtil::CreateDirs(dst)) {
96 return RESTOOL_ERROR;
97 }
98
99 FileEntry f(src);
100 if (!f.Init()) {
101 return RESTOOL_ERROR;
102 }
103 for (const auto &entry : f.GetChilds()) {
104 string filename = entry->GetFilePath().GetFilename();
105 if (ResourceUtil::IsIgnoreFile(filename, entry->IsFile())) {
106 continue;
107 }
108
109 string subPath = FileEntry::FilePath(dst).Append(filename).GetPath();
110 if (!entry->IsFile()) {
111 if (CopyBinaryFileImpl(entry->GetFilePath().GetPath(), subPath) != RESTOOL_SUCCESS) {
112 return RESTOOL_ERROR;
113 }
114 continue;
115 }
116
117 if (IsDuplicated(entry, subPath)) {
118 continue;
119 }
120
121 if (stopCopy_.load()) {
122 cout << "Info: CopyBinaryFileImpl: stop copy binary file." << endl;
123 return RESTOOL_ERROR;
124 }
125
126 string path = entry->GetFilePath().GetPath();
127 auto copyFunc = [this](const string path, string subPath) { return this->CopySingleFile(path, subPath); };
128 std::future<uint32_t> res = ThreadPool::GetInstance().Enqueue(copyFunc, path, subPath);
129 copyResults_.push_back(std::move(res));
130 }
131 return RESTOOL_SUCCESS;
132 }
133
IsDuplicated(const unique_ptr<FileEntry> & entry,string subPath)134 bool BinaryFilePacker::IsDuplicated(const unique_ptr<FileEntry> &entry, string subPath)
135 {
136 lock_guard<mutex> lock(mutex_);
137 if (g_hapResourceSet.count(subPath)) {
138 g_hapResourceSet.erase(subPath);
139 } else if (!g_resourceSet.emplace(subPath).second) {
140 cout << "Warning: '" << entry->GetFilePath().GetPath() << "' is defined repeatedly." << endl;
141 return true;
142 }
143 return false;
144 }
145
CopySingleFile(const std::string & path,std::string & subPath)146 uint32_t BinaryFilePacker::CopySingleFile(const std::string &path, std::string &subPath)
147 {
148 if (moduleName_ == "har" || CompressionParser::GetCompressionParser()->GetDefaultCompress()) {
149 if (!ResourceUtil::CopyFileInner(path, subPath)) {
150 return RESTOOL_ERROR;
151 }
152 return RESTOOL_SUCCESS;
153 }
154 if (!CompressionParser::GetCompressionParser()->CopyAndTranscode(path, subPath, true)) {
155 return RESTOOL_ERROR;
156 }
157 return RESTOOL_SUCCESS;
158 }
159
CheckCopyResults()160 uint32_t BinaryFilePacker::CheckCopyResults()
161 {
162 for (auto &res : copyResults_) {
163 if (stopCopy_.load()) {
164 cout << "Info: CopyBinaryFile: stop copy binary file." << endl;
165 return RESTOOL_ERROR;
166 }
167 uint32_t ret = res.get();
168 if (ret != RESTOOL_SUCCESS) {
169 return RESTOOL_ERROR;
170 }
171 }
172 return RESTOOL_SUCCESS;
173 }
174 } // namespace Restool
175 } // namespace Global
176 } // namespace OHOS
177