1 /*
2 * Copyright (c) 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 "hsp_packager.h"
17
18 #include <string>
19
20 #include "constants.h"
21 #include "json/json_utils.h"
22 #include "log.h"
23
24 namespace OHOS {
25 namespace AppPackingTool {
26 namespace {
27 const std::string NAME = "name";
28 const std::string EXTENSION_ABILITIES = "extensionAbilities";
29 const std::string REQUEST_PERMISSIONS = "requestPermissions";
30 const std::string PERMISSION_SUPPORT_PLUGIN = "ohos.permission.kernel.SUPPORT_PLUGIN";
31 }
HspPackager(const std::map<std::string,std::string> & parameterMap,std::string & resultReceiver)32 HspPackager::HspPackager(const std::map<std::string, std::string> ¶meterMap, std::string &resultReceiver)
33 : Packager(parameterMap, resultReceiver)
34 {}
35
InitAllowedParam()36 int32_t HspPackager::InitAllowedParam()
37 {
38 return ERR_OK;
39 }
PreProcess()40 int32_t HspPackager::PreProcess()
41 {
42 if (!CheckForceFlag()) {
43 return ERR_INVALID_VALUE;
44 }
45
46 bool ret = IsVerifyValidInHspCommonMode() && IsVerifyValidInHspMode();
47 if (!ret) {
48 return ERR_INVALID_VALUE;
49 }
50 return ERR_OK;
51 }
Process()52 int32_t HspPackager::Process()
53 {
54 if (!CompressHsp()) {
55 std::string outPath;
56 if (parameterMap_.find(Constants::PARAM_OUT_PATH) != parameterMap_.end()) {
57 outPath = parameterMap_.at(Constants::PARAM_OUT_PATH);
58 }
59 if (fs::exists(outPath)) {
60 fs::remove_all(outPath);
61 }
62 LOGE("Hsp Process failed!");
63 return ERR_INVALID_VALUE;
64 }
65 return ERR_OK;
66 }
PostProcess()67 int32_t HspPackager::PostProcess()
68 {
69 if (generateBuildHash_) {
70 if (!CompressHsp()) {
71 std::string outPath;
72 if (parameterMap_.find(Constants::PARAM_OUT_PATH) != parameterMap_.end()) {
73 outPath = parameterMap_.at(Constants::PARAM_OUT_PATH);
74 }
75 if (fs::exists(outPath)) {
76 fs::remove_all(outPath);
77 }
78 LOGE("sencond CompressHsp failed!");
79 return ERR_INVALID_VALUE;
80 }
81 }
82 return ERR_OK;
83 }
84
IsVerifyValidInHspCommonMode()85 bool HspPackager::IsVerifyValidInHspCommonMode()
86 {
87 std::map<std::string, std::string>::const_iterator it = parameterMap_.find(Constants::PARAM_JSON_PATH);
88 if (it == parameterMap_.end() || it->second.empty()) {
89 LOGE("HspPackager::isArgsValidInHspMode json-path is empty.");
90 return false;
91 }
92 jsonPath_ = it->second;
93 if (!IsPathValid(it->second, true, Constants::MODULE_JSON)) {
94 LOGE("HspPackager::isArgsValidInHspMode json-path must be module.json file.");
95 return false;
96 }
97 if (!Compatible(Constants::PARAM_JAR_PATH, formattedJarPathList_, Constants::JAR_SUFFIX) ||
98 !Compatible(Constants::PARAM_TXT_PATH, formattedTxtPathList_, Constants::TXT_SUFFIX)) {
99 return false;
100 }
101 if (!IsHspPathValid()) {
102 return false;
103 }
104 it = parameterMap_.find(Constants::PARAM_DIR_LIST);
105 if (it != parameterMap_.end() && !it->second.empty() &&
106 !SplitDirList(it->second, formatedDirList_)) {
107 LOGE("HspPackager::isArgsValidInHspMode --dir-list is invalid.");
108 return false;
109 }
110 it = parameterMap_.find(Constants::PARAM_PROFILE_PATH);
111 if (it != parameterMap_.end()) {
112 const std::string filePath = it->second;
113 if (!fs::is_regular_file(filePath) ||
114 fs::path(filePath).filename().string() != Constants::PROFILE_NAME) {
115 LOGE("HspPackager::isArgsValidInHspMode profile-path"
116 " must be CAPABILITY.profile file.");
117 return false;
118 }
119 }
120 it = parameterMap_.find(Constants::PARAM_PKG_CONTEXT_PATH);
121 if (it != parameterMap_.end() && !it ->second.empty()) {
122 const std::string filePath = it->second;
123 if (!fs::is_regular_file(filePath) ||
124 fs::path(filePath).filename().string() != Constants::PKG_CONTEXT_JSON) {
125 LOGE("HspPackager::isArgsValidInHspMode --pkg-context-path file"
126 " must be pkgContextInfo.json file.");
127 return false;
128 }
129 }
130 return true;
131 }
132
IsVerifyValidInHspMode()133 bool HspPackager::IsVerifyValidInHspMode()
134 {
135 std::map<std::string, std::string>::const_iterator it = parameterMap_.find(Constants::PARAM_ETS_PATH);
136 if (it != parameterMap_.end()) {
137 const std::string filePath = it->second;
138 if (!filePath.empty() && !fs::exists(filePath)) {
139 LOGE("HspPackager::IsVerifyValidInHspMode --ets-path is invalid.");
140 return false;
141 }
142 }
143
144 std::string outPath = "";
145 std::string forceRewrite = "";
146 it = parameterMap_.find(Constants::PARAM_OUT_PATH);
147 if (it != parameterMap_.end()) {
148 outPath = it->second;
149 }
150
151 it = parameterMap_.find(Constants::PARAM_FORCE);
152 if (it != parameterMap_.end()) {
153 forceRewrite = it->second;
154 }
155
156 return IsOutPathValid(outPath, forceRewrite, Constants::HSP_SUFFIX);
157 }
158
Compatible(const std::string & paramPath,std::list<std::string> & fileList,const std::string & suffix)159 bool HspPackager::Compatible(const std::string ¶mPath, std::list<std::string> &fileList,
160 const std::string &suffix)
161 {
162 std::map<std::string, std::string>::const_iterator it = parameterMap_.find(paramPath);
163 if (it != parameterMap_.end() && !it->second.empty() && !CompatibleProcess(it->second,
164 fileList, suffix)) {
165 LOGE("HspPackager::isArgsValidInHapMode %s is invalid.", paramPath.c_str());
166 return false;
167 }
168 return true;
169 }
170
IsHspPathValid()171 bool HspPackager::IsHspPathValid()
172 {
173 if (IsHspPathValid(Constants::PARAM_LIB_PATH)) {
174 LOGE("HspPackager::isArgsValidInHspMode lib-path is invalid.");
175 return false;
176 }
177 if (IsHspPathValid(Constants::PARAM_RES_PATH)) {
178 LOGE("HspPackager::isArgsValidInHspMode res-path is invalid.");
179 return false;
180 }
181 if (IsHspPathValid(Constants::PARAM_RESOURCES_PATH)) {
182 LOGE("HspPackager::isArgsValidInHspMode resources-path is invalid.");
183 return false;
184 }
185 if (IsHspPathValid(Constants::PARAM_ASSETS_PATH)) {
186 LOGE("HspPackager::isArgsValidInHspMode assets-path is invalid.");
187 return false;
188 }
189 if (IsHspPathValid(Constants::PARAM_AP_PATH)) {
190 LOGE("HspPackager::isArgsValidInHspMode ap-path is invalid.");
191 return false;
192 }
193 if (IsHspPathValid(Constants::PARAM_AN_PATH)) {
194 LOGE("HspPackager::isArgsValidInHspMode an-path is invalid.");
195 return false;
196 }
197 return true;
198 }
199
IsHspPathValid(const std::string & parameterMapKey)200 bool HspPackager::IsHspPathValid(const std::string ¶meterMapKey)
201 {
202 std::map<std::string, std::string>::const_iterator it = parameterMap_.find(parameterMapKey);
203 if (it == parameterMap_.end()) {
204 return false;
205 }
206 const std::string path = it->second;
207 return (!path.empty() && !IsPathValid(path, false));
208 }
209
CompressHsp()210 bool HspPackager::CompressHsp()
211 {
212 if (!SetGenerateBuildHash(jsonPath_, generateBuildHash_, buildHashFinish_)) {
213 return false;
214 }
215 if (!moduleJson_.ParseFromFile(jsonPath_)) {
216 LOGE("ParseFromFile failed");
217 return false;
218 }
219 if (JsonUtils::IsModuleJson(jsonPath_)) {
220 if (!moduleJson_.CheckStageAsanTsanEnabledValid()) {
221 LOGE("CheckStageAsanTsanEnabledValid failed.");
222 return false;
223 }
224 if (!moduleJson_.CheckStageAtomicService()) {
225 LOGE("CheckStageAtomicService failed.");
226 return false;
227 }
228 if (!moduleJson_.CheckStageOverlayCfg()) {
229 LOGE("checkStageOverlayCfg failed.");
230 return false;
231 }
232 std::string moduleType;
233 if (!moduleJson_.GetStageModuleType(moduleType)) {
234 LOGW("GetStageModuleType failed.");
235 }
236 if (moduleType != Constants::TYPE_SHARED) {
237 LOGE("module type must be shared.");
238 return false;
239 }
240 }
241 if (!CompressHspMode(jsonPath_) || !BuildHash(buildHashFinish_, generateBuildHash_, parameterMap_, jsonPath_)) {
242 return false;
243 }
244 return true;
245 }
246
CompressHspMode(const std::string & jsonPath)247 bool HspPackager::CompressHspMode(const std::string &jsonPath)
248 {
249 std::map<std::string, std::string>::const_iterator it = parameterMap_.find(Constants::PARAM_OUT_PATH);
250 std::string outPath;
251 if (it != parameterMap_.end()) {
252 outPath = it->second;
253 }
254 zipWrapper_.Open(outPath);
255 if (!zipWrapper_.IsOpen()) {
256 LOGE("HspPackager::Process: zipWrapper Open failed!");
257 return false;
258 }
259 std::string jsonString = moduleJson_.ToString();
260 if (!jsonString.empty()) {
261 std::string jsonType;
262 if (JsonUtils::IsModuleJson(jsonPath)) {
263 jsonType = Constants::MODULE_JSON;
264 if (!moduleJson_.GetStageModuleName(moduleName_) || moduleJson_.GetStageDeviceTypes(deviceTypes_)) {
265 LOGW("GetStageModuleName or GetStageDeviceTypes failed!");
266 }
267 } else {
268 jsonType = Constants::CONFIG_JSON;
269 if (!moduleJson_.GetFaModuleName(moduleName_) || moduleJson_.GetFaDeviceTypes(deviceTypes_)) {
270 LOGW("GetStageModuleName or GetStageDeviceTypes failed!");
271 }
272 }
273 if (zipWrapper_.WriteStringToZip(jsonString, jsonType) != ZipErrCode::ZIP_ERR_SUCCESS) {
274 LOGE("HspPackager::Process: zipWrapper WriteStringToZip failed!");
275 return false;
276 }
277 }
278
279 if (!AddCommonFileOrDirectoryToZip(Constants::PARAM_PROFILE_PATH, Constants::PROFILE_NAME)) {
280 return false;
281 }
282 it = parameterMap_.find(Constants::PARAM_INDEX_PATH);
283 if (it != parameterMap_.end() && !it->second.empty() && JsonUtils::IsModuleJson(jsonPath)) {
284 if (zipWrapper_.AddFileOrDirectoryToZip(it->second, Constants::RESOURCES_INDEX) !=
285 ZipErrCode::ZIP_ERR_SUCCESS) {
286 LOGE("HspPackager::Process: zipWrapper AddFileOrDirectoryToZip failed!");
287 return false;
288 }
289 }
290 if (!CheckAppPlugin()) {
291 LOGE("CheckAppPlugin failed.");
292 return false;
293 }
294 return CompressHspModePartSecond(jsonPath);
295 }
296
CheckAppPlugin()297 bool HspPackager::CheckAppPlugin()
298 {
299 std::string bundleType;
300 if (!moduleJson_.GetStageBundleType(bundleType)) {
301 LOGW("GetStageBundleType failed");
302 return true;
303 }
304 if (Constants::TYPE_APP_PLUGIN != bundleType) {
305 if (IsPluginHost()) {
306 return true;
307 }
308 if (!CheckPkgContext()) {
309 LOGE("CheckPkgContext failed.");
310 return false;
311 }
312 return true;
313 }
314 std::unique_ptr<PtJson> moduleObj;
315 if (!moduleJson_.GetModuleObject(moduleObj)) {
316 LOGE("GetModuleObject failed!");
317 return false;
318 }
319 if (moduleObj->Contains(EXTENSION_ABILITIES.c_str())) {
320 std::unique_ptr<PtJson> extensionAbilitiesObj;
321 if (moduleObj->GetArray(EXTENSION_ABILITIES.c_str(), &extensionAbilitiesObj) != Result::SUCCESS) {
322 LOGW("Module node get %s array node failed!", EXTENSION_ABILITIES.c_str());
323 }
324 if (extensionAbilitiesObj) {
325 if (!IsExtensionAbility(extensionAbilitiesObj)) {
326 LOGE("IsExtensionAbility failed.");
327 return false;
328 }
329 }
330 }
331 if (!CheckPkgContext()) {
332 LOGE("CheckPkgContext failed.");
333 return false;
334 }
335 if (moduleObj->Contains(REQUEST_PERMISSIONS.c_str())) {
336 std::unique_ptr<PtJson> requestPermissionsObj;
337 if (moduleObj->GetArray(REQUEST_PERMISSIONS.c_str(), &requestPermissionsObj) != Result::SUCCESS) {
338 LOGW("Module node get %s array node failed!", REQUEST_PERMISSIONS.c_str());
339 return true;
340 }
341 if (IsPermissionSupportPlugin(requestPermissionsObj)) {
342 LOGE("plugin package cannot be PERMISSION_SUPPORT_PLUGIN");
343 return false;
344 }
345 }
346 return true;
347 }
348
IsPluginHost()349 bool HspPackager::IsPluginHost()
350 {
351 std::unique_ptr<PtJson> moduleObj;
352 if (!moduleJson_.GetModuleObject(moduleObj)) {
353 LOGE("GetModuleObject failed!");
354 return false;
355 }
356 if (moduleObj->Contains(REQUEST_PERMISSIONS.c_str())) {
357 std::unique_ptr<PtJson> requestPermissionsObj;
358 if (moduleObj->GetArray(REQUEST_PERMISSIONS.c_str(), &requestPermissionsObj) != Result::SUCCESS) {
359 LOGW("Module node get %s array node failed!", REQUEST_PERMISSIONS.c_str());
360 return true;
361 }
362 if (IsPermissionSupportPlugin(requestPermissionsObj)) {
363 LOGW("requestPermission is PERMISSION_SUPPORT_PLUGIN");
364 return false;
365 }
366 }
367 return true;
368 }
369
IsExtensionAbility(std::unique_ptr<PtJson> & extensionAbilitiesObj)370 bool HspPackager::IsExtensionAbility(std::unique_ptr<PtJson>& extensionAbilitiesObj)
371 {
372 for (int32_t i = 0; i < extensionAbilitiesObj->GetSize(); i++) {
373 std::unique_ptr<PtJson> extensionAbilityObj = extensionAbilitiesObj->Get(i);
374 if (extensionAbilityObj->Contains(NAME.c_str())) {
375 std::string extensionAbilityName;
376 if (extensionAbilityObj->GetString(NAME.c_str(), &extensionAbilityName) == Result::SUCCESS) {
377 LOGE("extendAbilities of plugin package must empty");
378 return false;
379 }
380 }
381 }
382 return true;
383 }
384
IsPermissionSupportPlugin(std::unique_ptr<PtJson> & requestPermissionsObj)385 bool HspPackager::IsPermissionSupportPlugin(std::unique_ptr<PtJson>& requestPermissionsObj)
386 {
387 if (requestPermissionsObj == nullptr) {
388 LOGE("requestPermissionsObj nullptr!");
389 return false;
390 }
391 for (int32_t i = 0; i < requestPermissionsObj->GetSize(); i++) {
392 std::unique_ptr<PtJson> requestPermissionObj = requestPermissionsObj->Get(i);
393 if (requestPermissionObj->Contains(NAME.c_str())) {
394 std::string requestPermissionName;
395 if (requestPermissionObj->GetString(NAME.c_str(), &requestPermissionName) != Result::SUCCESS) {
396 LOGW("get %s failed!", NAME.c_str());
397 continue;
398 }
399 if (requestPermissionName == PERMISSION_SUPPORT_PLUGIN) {
400 return true;
401 }
402 }
403 }
404 return false;
405 }
406
CheckPkgContext()407 bool HspPackager::CheckPkgContext()
408 {
409 std::map<std::string, std::string>::const_iterator it = parameterMap_.find(Constants::PARAM_PKG_CONTEXT_PATH);
410 if (it != parameterMap_.end()) {
411 const std::string filePath = it->second;
412 if (!fs::is_regular_file(filePath) ||
413 fs::path(filePath).filename().string() != Constants::PKG_CONTEXT_JSON) {
414 LOGE("host must include pkgContextInfo.json");
415 return false;
416 }
417 return true;
418 }
419 LOGE("host must include pkgContextInfo.json");
420 return false;
421 }
422
CompressHspModePartSecond(const std::string & jsonPath)423 bool HspPackager::CompressHspModePartSecond(const std::string &jsonPath)
424 {
425 std::map<std::string, std::string> paramFileMap = {
426 {Constants::PARAM_LIB_PATH, Constants::LIB_PATH},
427 {Constants::PARAM_AN_PATH, Constants::AN_PATH},
428 {Constants::PARAM_AP_PATH, Constants::AP_PATH},
429 {Constants::PARAM_RPCID_PATH, Constants::RPCID_SC},
430 {Constants::PARAM_ASSETS_PATH, Constants::ASSETS_PATH}
431 };
432 for (auto& item : paramFileMap) {
433 if (!AddCommonFileOrDirectoryToZip(item.first, item.second)) {
434 return false;
435 }
436 }
437
438 std::map<std::string, std::string>::const_iterator it = parameterMap_.find(Constants::PARAM_FILE_PATH);
439 if (it != parameterMap_.end() && !it->second.empty()) {
440 fs::path filePath = fs::path(it->second);
441 std::string zipPath = Constants::NULL_DIR_NAME;
442 if (!fs::is_directory(filePath)) {
443 zipPath = (filePath).filename().string();
444 }
445 if (zipWrapper_.AddFileOrDirectoryToZip(it->second, zipPath) != ZipErrCode::ZIP_ERR_SUCCESS) {
446 LOGE("HspPackager::Process: zipWrapper AddFileOrDirectoryToZip failed!");
447 return false;
448 }
449 }
450
451 it = parameterMap_.find(Constants::PARAM_RESOURCES_PATH);
452 if (it != parameterMap_.end() && !it->second.empty() && JsonUtils::IsModuleJson(jsonPath)) {
453 if (zipWrapper_.AddFileOrDirectoryToZip(it->second, Constants::RESOURCES_PATH) != ZipErrCode::ZIP_ERR_SUCCESS) {
454 LOGE("HspPackager::Process: zipWrapper AddFileOrDirectoryToZip failed!");
455 return false;
456 }
457 }
458 return CompressHspModePartThird(jsonPath);
459 }
460
CompressHspModePartThird(const std::string & jsonPath)461 bool HspPackager::CompressHspModePartThird(const std::string &jsonPath)
462 {
463 std::map<std::string, std::string>::const_iterator it = parameterMap_.find(Constants::PARAM_RES_PATH);
464 if (it != parameterMap_.end() && !it->second.empty() && !moduleName_.empty()) {
465 std::string resPath = Constants::ASSETS_PATH + Constants::LINUX_FILE_SEPARATOR + moduleName_ +
466 Constants::LINUX_FILE_SEPARATOR + Constants::RESOURCES_PATH;
467 std::string deviceType;
468 if (!deviceTypes_.empty()) {
469 deviceType = deviceTypes_.front();
470 }
471 if (Constants::DEVICE_TYPE_FITNESSWATCH == deviceType ||
472 Constants::DEVICE_TYPE_FITNESSWATCH_NEW == deviceType) {
473 resPath = Constants::RES_PATH;
474 }
475 if (zipWrapper_.AddFileOrDirectoryToZip(it->second, resPath) != ZipErrCode::ZIP_ERR_SUCCESS) {
476 LOGE("HspPackager::Process: zipWrapper AddFileOrDirectoryToZip failed!");
477 return false;
478 }
479 }
480 it = parameterMap_.find(Constants::PARAM_JS_PATH);
481 if (it != parameterMap_.end() && !it->second.empty() && JsonUtils::IsModuleJson(jsonPath)) {
482 if (zipWrapper_.AddFileOrDirectoryToZip(it->second, Constants::JS_PATH) != ZipErrCode::ZIP_ERR_SUCCESS) {
483 LOGE("HspPackager::Process: zipWrapper AddFileOrDirectoryToZip failed!");
484 return false;
485 }
486 }
487 it = parameterMap_.find(Constants::PARAM_ETS_PATH);
488 if (it != parameterMap_.end() && !it->second.empty() && JsonUtils::IsModuleJson(jsonPath)) {
489 if (zipWrapper_.AddFileOrDirectoryToZip(it->second, Constants::ETS_PATH) != ZipErrCode::ZIP_ERR_SUCCESS) {
490 LOGE("HspPackager::Process: zipWrapper AddFileOrDirectoryToZip failed!");
491 return false;
492 }
493 }
494 return CompressHspModePartFourth();
495 }
496
CompressHspModePartFourth()497 bool HspPackager::CompressHspModePartFourth()
498 {
499 std::map<std::string, std::string>::const_iterator it = parameterMap_.find(Constants::PARAM_BIN_PATH);
500 if (it != parameterMap_.end() && !it->second.empty()) {
501 fs::path filePath = fs::path(it->second);
502 std::string zipPath = Constants::NULL_DIR_NAME;
503 if (!fs::is_directory(filePath)) {
504 zipPath = (filePath).filename().string();
505 }
506 if (zipWrapper_.AddFileOrDirectoryToZip(it->second, zipPath) != ZipErrCode::ZIP_ERR_SUCCESS) {
507 LOGE("HspPackager::Process: zipWrapper AddFileOrDirectoryToZip failed!");
508 return false;
509 }
510 }
511 if (!AddCommonFileOrDirectoryToZip(Constants::PARAM_PACK_INFO_PATH, Constants::PACK_INFO)) {
512 return false;
513 }
514 if (!formatedDirList_.empty()) {
515 for (const auto& dirPath : formatedDirList_) {
516 std::string baseDir = fs::path(dirPath).filename().string();
517 if (zipWrapper_.AddFileOrDirectoryToZip(dirPath, baseDir) != ZipErrCode::ZIP_ERR_SUCCESS) {
518 LOGE("HspPackager::Process: zipWrapper AddFileOrDirectoryToZip failed!");
519 return false;
520 }
521 }
522 }
523 it = parameterMap_.find(Constants::PARAM_PKG_CONTEXT_PATH);
524 if (it != parameterMap_.end() && !it->second.empty()) {
525 ModuleJson moduleJson;
526 if (!moduleJson.ParseFromFile(it->second)) {
527 LOGE("HspPackager::Process: moduleJson Read failed!");
528 return false;
529 }
530 std::string jsonString = moduleJson.ToString();
531 if (!jsonString.empty()) {
532 if (zipWrapper_.WriteStringToZip(jsonString, Constants::PKG_CONTEXT_JSON) != ZipErrCode::ZIP_ERR_SUCCESS) {
533 LOGE("HspPackager::Process: zipWrapper WriteStringToZip failed!");
534 return false;
535 }
536 } else {
537 LOGE("HspPackager::Process: jsonFile error!");
538 return false;
539 }
540 }
541 return CompressHspModeMultiple();
542 }
543
CompressHspModeMultiple()544 bool HspPackager::CompressHspModeMultiple()
545 {
546 std::map<std::string, std::string>::const_iterator it = parameterMap_.find(Constants::PARAM_MAPLE_SO_DIR);
547 if (it != parameterMap_.end() && formattedSoPathList_.size() == 0 && !it->second.empty()) {
548 if (zipWrapper_.AddFileOrDirectoryToZip(it->second, Constants::SO_DIR) != ZipErrCode::ZIP_ERR_SUCCESS) {
549 LOGE("HapPackager::Process: zipWrapper AddFileOrDirectoryToZip failed!");
550 return false;
551 }
552 }
553
554 for (auto jarPathItem : formattedJarPathList_) {
555 std::string zipPath = fs::path(jarPathItem).filename().string();
556 if (zipWrapper_.AddFileOrDirectoryToZip(jarPathItem, zipPath) != ZipErrCode::ZIP_ERR_SUCCESS) {
557 LOGE("HspPackager::Process: zipWrapper AddFileOrDirectoryToZip failed!");
558 return false;
559 }
560 }
561
562 for (auto txtPathItem : formattedTxtPathList_) {
563 std::string zipPath = fs::path(txtPathItem).filename().string();
564 if (zipWrapper_.AddFileOrDirectoryToZip(txtPathItem, zipPath) != ZipErrCode::ZIP_ERR_SUCCESS) {
565 LOGE("HspPackager::Process: zipWrapper AddFileOrDirectoryToZip failed!");
566 return false;
567 }
568 }
569
570 it = parameterMap_.find(Constants::PARAM_SHAREDLIBS_PATH);
571 if (it != parameterMap_.end() && !it->second.empty()) {
572 if (zipWrapper_.AddFileOrDirectoryToZip(it->second, Constants::SHARED_LIBS_DIR) !=
573 ZipErrCode::ZIP_ERR_SUCCESS) {
574 LOGE("HspPackager::Process: zipWrapper AddFileOrDirectoryToZip failed!");
575 return false;
576 }
577 }
578 zipWrapper_.Close();
579 return true;
580 }
581
AddCommonFileOrDirectoryToZip(const std::string & paramPath,const std::string & targetPath)582 bool HspPackager::AddCommonFileOrDirectoryToZip(const std::string ¶mPath, const std::string &targetPath)
583 {
584 std::map<std::string, std::string>::const_iterator it = parameterMap_.find(paramPath);
585 if (it != parameterMap_.end() && !it->second.empty()) {
586 if (zipWrapper_.AddFileOrDirectoryToZip(it->second, targetPath) != ZipErrCode::ZIP_ERR_SUCCESS) {
587 LOGE("HspPackager::Process: zipWrapper AddFileOrDirectoryToZip failed!");
588 return false;
589 }
590 }
591 return true;
592 }
593 } // namespace AppPackingTool
594 } // namespace OHOS