• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "hap_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 REQUEST_PERMISSIONS = "requestPermissions";
29 const std::string PERMISSION_SUPPORT_PLUGIN = "ohos.permission.kernel.SUPPORT_PLUGIN";
30 }
HapPackager(const std::map<std::string,std::string> & parameterMap,std::string & resultReceiver)31 HapPackager::HapPackager(const std::map<std::string, std::string> &parameterMap, std::string &resultReceiver)
32     : Packager(parameterMap, resultReceiver)
33 {}
34 
InitAllowedParam()35 int32_t HapPackager::InitAllowedParam()
36 {
37     allowedParameters_ = {
38         {}
39     };
40     return ERR_OK;
41 }
42 
PreProcess()43 int32_t HapPackager::PreProcess()
44 {
45     if (!CheckForceFlag()) {
46         return ERR_INVALID_VALUE;
47     }
48 
49     std::map<std::string, std::string>::const_iterator it = parameterMap_.find(Constants::PARAM_JSON_PATH);
50     std::string jsonPath;
51     if (it != parameterMap_.end()) {
52         jsonPath = it->second;
53     }
54     it = parameterMap_.find(Constants::PARAM_BIN_PATH);
55     if (it != parameterMap_.end() && !it->second.empty() && jsonPath.empty()) {
56         std::string outPath = "";
57         std::string forceRewrite = "";
58         it = parameterMap_.find(Constants::PARAM_OUT_PATH);
59         if (it != parameterMap_.end()) {
60             outPath = it->second;
61         }
62 
63         it = parameterMap_.find(Constants::PARAM_FORCE);
64         if (it != parameterMap_.end()) {
65             forceRewrite = it->second;
66         }
67 
68         return IsOutPathValid(outPath, forceRewrite, Constants::HAP_SUFFIX);
69     } else {
70         bool ret = IsVerifyValidInHapCommonMode() && IsVerifyValidInHapMode();
71         if (!ret) {
72             return ERR_INVALID_VALUE;
73         }
74     }
75     return ERR_OK;
76 }
77 
Process()78 int32_t HapPackager::Process()
79 {
80     if (!CompressHap()) {
81         std::string outPath;
82         if (parameterMap_.find(Constants::PARAM_OUT_PATH) != parameterMap_.end()) {
83             outPath = parameterMap_.at(Constants::PARAM_OUT_PATH);
84         }
85         if (fs::exists(outPath)) {
86             fs::remove_all(outPath);
87         }
88         LOGE("Hap Process failed.");
89         return ERR_INVALID_VALUE;
90     }
91     return ERR_OK;
92 }
93 
PostProcess()94 int32_t HapPackager::PostProcess()
95 {
96     if (generateBuildHash_) {
97         if (!CompressHap()) {
98             std::string outPath;
99             if (parameterMap_.find(Constants::PARAM_OUT_PATH) != parameterMap_.end()) {
100                 outPath = parameterMap_.at(Constants::PARAM_OUT_PATH);
101             }
102             if (fs::exists(outPath)) {
103                 fs::remove_all(outPath);
104             }
105             LOGE("sencond CompressHap failed.");
106             return ERR_INVALID_VALUE;
107         }
108     }
109     return ERR_OK;
110 }
111 
IsVerifyValidInHapCommonMode()112 bool HapPackager::IsVerifyValidInHapCommonMode()
113 {
114     std::map<std::string, std::string>::const_iterator it = parameterMap_.find(Constants::PARAM_JSON_PATH);
115     if (it == parameterMap_.end() || it->second.empty()) {
116         LOGE("HapPackager::commandPathVerify json-path is empty.");
117         return false;
118     }
119     jsonPath_ = it->second;
120     if (!IsPathValid(it->second, true, Constants::CONFIG_JSON)
121         && !IsPathValid(it->second, true, Constants::MODULE_JSON)) {
122         LOGE("HapPackager::isArgsValidInHarMode json-path must be"
123             " config.json or module.json file.");
124         return false;
125     }
126     if (!IsValidRpcid() || !IsValidPackInfo()) {
127         return false;
128     }
129     it = parameterMap_.find(Constants::PARAM_PROFILE_PATH);
130     if (it != parameterMap_.end()) {
131         const std::string filePath = it->second;
132         if (!fs::is_regular_file(filePath) ||
133             fs::path(filePath).filename().string() != Constants::PROFILE_NAME) {
134             LOGE("HapPackager::isArgsValidInHapMode profile-path"
135                 " must be CAPABILITY.profile file.");
136             return false;
137         }
138     }
139     if (!Compatible(Constants::PARAM_ABC_PATH, formattedAbcPathList_, Constants::ABC_SUFFIX)) {
140         return false;
141     }
142     it = parameterMap_.find(Constants::PARAM_DIR_LIST);
143     if (it != parameterMap_.end() && !SplitDirList(it->second, formatedDirList_)) {
144         LOGE("HapPackager::isArgsValidInHapMode --dir-list is invalid.");
145         return false;
146     }
147     it = parameterMap_.find(Constants::PARAM_PKG_CONTEXT_PATH);
148     if (it != parameterMap_.end()) {
149         const std::string filePath = it->second;
150         if (!fs::is_regular_file(filePath) ||
151             fs::path(filePath).filename().string() != Constants::PKG_CONTEXT_JSON) {
152             LOGE("HapPackager::isArgsValidInHapMode --pkg-context-path"
153                 " file must be pkgContextInfo.json file.");
154             return false;
155         }
156     }
157     return true;
158 }
159 
Compatible(const std::string & paramPath,std::list<std::string> & fileList,const std::string & suffix)160 bool HapPackager::Compatible(const std::string &paramPath, std::list<std::string> &fileList,
161     const std::string &suffix)
162 {
163     std::map<std::string, std::string>::const_iterator it = parameterMap_.find(paramPath);
164     if (it != parameterMap_.end() && !it->second.empty() && !CompatibleProcess(it->second,
165         fileList, suffix)) {
166         LOGE("HapPackager::isArgsValidInHapMode %s is invalid.", paramPath.c_str());
167         return false;
168     }
169     return true;
170 }
171 
IsVerifyValidInHapMode()172 bool HapPackager::IsVerifyValidInHapMode()
173 {
174     std::map<std::string, std::string>::const_iterator it = parameterMap_.find(Constants::PARAM_INDEX_PATH);
175     if (it != parameterMap_.end()) {
176         const std::string filePath = it->second;
177         if (!fs::is_regular_file(filePath) ||
178             fs::path(filePath).filename().string() != Constants::RESOURCES_INDEX) {
179             LOGE("HapPackager::isArgsValidInHapMode index-path must be resources.index file.");
180             return false;
181         }
182     }
183 
184     if (!Compatible(Constants::PARAM_MAPLE_SO_PATH, formattedSoPathList_, Constants::SO_SUFFIX) ||
185         !Compatible(Constants::PARAM_ABILITY_SO_PATH, formattedAbilitySoPathList_, Constants::SO_SUFFIX) ||
186         !Compatible(Constants::PARAM_JAR_PATH, formattedJarPathList_, Constants::JAR_SUFFIX) ||
187         !Compatible(Constants::PARAM_TXT_PATH, formattedTxtPathList_, Constants::TXT_SUFFIX)) {
188         return false;
189     }
190 
191     if (!IsHapPathValid()) {
192         return false;
193     }
194 
195     it = parameterMap_.find(Constants::PARAM_ETS_PATH);
196     if (it != parameterMap_.end()) {
197         const std::string filePath = it->second;
198         if (!filePath.empty() && !fs::exists(filePath)) {
199             LOGE("HapPackager::IsVerifyValidInHapMode --ets-path is invalid.");
200             return false;
201         }
202     }
203 
204     std::string outPath = "";
205     std::string forceRewrite = "";
206     it = parameterMap_.find(Constants::PARAM_OUT_PATH);
207     if (it != parameterMap_.end()) {
208         outPath = it->second;
209     }
210 
211     it = parameterMap_.find(Constants::PARAM_FORCE);
212     if (it != parameterMap_.end()) {
213         forceRewrite = it->second;
214     }
215 
216     return IsOutPathValid(outPath, forceRewrite, Constants::HAP_SUFFIX);
217 }
218 
219 
IsValidRpcid()220 bool HapPackager::IsValidRpcid()
221 {
222     std::map<std::string, std::string>::const_iterator it = parameterMap_.find(Constants::PARAM_RPCID_PATH);
223     if (it != parameterMap_.end()) {
224         const std::string filePath = it->second;
225         if (!fs::is_regular_file(filePath)) {
226             LOGE("HapPackager::isValidRpcid rpcid-path is not a file.");
227             return false;
228         }
229         if (fs::path(filePath).filename().string() != Constants::RPCID_SC) {
230             LOGE("HapPackager::isValidRpcid rpcid-path must be rpcid.sc file.");
231             return false;
232         }
233     }
234     return true;
235 }
236 
IsValidPackInfo()237 bool HapPackager::IsValidPackInfo()
238 {
239     std::map<std::string, std::string>::const_iterator it = parameterMap_.find(Constants::PARAM_PACK_INFO_PATH);
240     if (it != parameterMap_.end()) {
241         const std::string filePath = it->second;
242         if (!fs::is_regular_file(filePath)) {
243             LOGE("HapPackager::isValidPackInfo --pack-info-path is not a file.");
244             return false;
245         }
246         if (fs::path(filePath).filename().string() != Constants::PACK_INFO) {
247             LOGE("HapPackager::isValidPackInfo --pack-info-path must be pack.info file.");
248             return false;
249         }
250     }
251     return true;
252 }
253 
IsHapPathValid()254 bool HapPackager::IsHapPathValid()
255 {
256     if (IsHapPathValid(Constants::PARAM_MAPLE_SO_DIR)) {
257         LOGE("HapPackager::isArgsValidInHapMode maple-so-dir is invalid.");
258         return false;
259     }
260     if (IsHapPathValid(Constants::PARAM_LIB_PATH)) {
261         LOGE("HapPackager::isArgsValidInHapMode lib-path is invalid.");
262         return false;
263     }
264     if (IsHapPathValid(Constants::PARAM_HNP_PATH)) {
265         LOGE("HapPackager::isArgsValidInHapMode hnp-path is invalid.");
266         return false;
267     }
268     if (IsHapPathValid(Constants::PARAM_RES_PATH)) {
269         LOGE("HapPackager::isArgsValidInHapMode res-path is invalid.");
270         return false;
271     }
272     if (IsHapPathValid(Constants::PARAM_RESOURCES_PATH)) {
273         LOGE("HapPackager::isArgsValidInHapMode resources-path is invalid.");
274         return false;
275     }
276     if (IsHapPathValid(Constants::PARAM_ASSETS_PATH)) {
277         LOGE("HapPackager::isArgsValidInHapMode assets-path is invalid.");
278         return false;
279     }
280     if (IsHapPathValid(Constants::PARAM_SHAREDLIBS_PATH)) {
281         LOGE("HapPackager::isArgsValidInHapMode shared-libs-path is invalid.");
282         return false;
283     }
284     if (IsHapPathValid(Constants::PARAM_AN_PATH)) {
285         LOGE("HapPackager::isArgsValidInHapMode an-path is invalid.");
286         return false;
287     }
288     return true;
289 }
290 
IsHapPathValid(const std::string & parameterMapKey)291 bool HapPackager::IsHapPathValid(const std::string &parameterMapKey)
292 {
293     std::map<std::string, std::string>::const_iterator it = parameterMap_.find(parameterMapKey);
294     if (it == parameterMap_.end()) {
295         return false;
296     }
297     const std::string path = it->second;
298     return (!path.empty() && !IsPathValid(path, false));
299 }
300 
CompressHap()301 bool HapPackager::CompressHap()
302 {
303     std::map<std::string, std::string>::const_iterator it = parameterMap_.find(Constants::PARAM_BIN_PATH);
304     if (jsonPath_.empty() && it != parameterMap_.end() && !it->second.empty()) {
305         CompressHapMode();
306         return true;
307     }
308     if (!SetGenerateBuildHash(jsonPath_, generateBuildHash_, buildHashFinish_)) {
309         return false;
310     }
311     if (!moduleJson_.ParseFromFile(jsonPath_)) {
312         LOGE("ParseFromFile failed");
313         return false;
314     }
315     if (JsonUtils::IsModuleJson(jsonPath_)) {
316         if (!CheckStageHap(jsonPath_)) {
317             LOGE("CheckStageHap failed.");
318             return false;
319         }
320         std::string moduleType;
321         if (!moduleJson_.GetStageModuleType(moduleType)) {
322             LOGW("GetStageModuleType failed");
323         }
324         if (Constants::TYPE_SHARED == moduleType) {
325             LOGW("Compress mode is hap, but module type is shared.");
326         }
327 
328         std::string bundleType;
329         if (!moduleJson_.GetStageBundleType(bundleType)) {
330             LOGW("GetStageBundleType failed");
331         }
332         if (Constants::TYPE_SHARED == bundleType) {
333             LOGW("warning:Compress mode is hap, but app type is shared.");
334         }
335         if (!CompressHapModeForModule(jsonPath_) || !BuildHash(buildHashFinish_, generateBuildHash_,
336             parameterMap_, jsonPath_)) {
337             return false;
338         }
339         if (Constants::TYPE_APP_PLUGIN == bundleType) {
340             LOGE("hap can not plugin.");
341             return false;
342         } else {
343             if (!IsPluginHost()) {
344                 LOGE("plugin package cannot be the host.");
345                 return false;
346             }
347         }
348     } else {
349         if (!CompressHapMode() || !BuildHash(buildHashFinish_, generateBuildHash_, parameterMap_, jsonPath_)) {
350             return false;
351         }
352     }
353     return true;
354 }
355 
IsPluginHost()356 bool HapPackager::IsPluginHost()
357 {
358     std::unique_ptr<PtJson> moduleObj;
359     if (!moduleJson_.GetModuleObject(moduleObj)) {
360         LOGE("GetModuleObject failed!");
361         return false;
362     }
363     if (moduleObj->Contains(REQUEST_PERMISSIONS.c_str())) {
364         std::unique_ptr<PtJson> requestPermissionsObj;
365         if (moduleObj->GetArray(REQUEST_PERMISSIONS.c_str(), &requestPermissionsObj) != Result::SUCCESS) {
366             LOGW("Module node get %s array node failed!", REQUEST_PERMISSIONS.c_str());
367             return true;
368         }
369         if (IsPermissionSupportPlugin(requestPermissionsObj)) {
370             return CheckPkgContext();
371         }
372     }
373     return true;
374 }
375 
IsPermissionSupportPlugin(std::unique_ptr<PtJson> & requestPermissionsObj)376 bool HapPackager::IsPermissionSupportPlugin(std::unique_ptr<PtJson>& requestPermissionsObj)
377 {
378     if (requestPermissionsObj == nullptr) {
379         LOGE("requestPermissionsObj nullptr!");
380         return false;
381     }
382     for (int32_t i = 0; i < requestPermissionsObj->GetSize(); i++) {
383         std::unique_ptr<PtJson> requestPermissionObj = requestPermissionsObj->Get(i);
384         if (requestPermissionObj->Contains(NAME.c_str())) {
385             std::string requestPermissionName;
386             if (requestPermissionObj->GetString(NAME.c_str(), &requestPermissionName) != Result::SUCCESS) {
387                 LOGW("get %s failed!", NAME.c_str());
388                 continue;
389             }
390             if (requestPermissionName == PERMISSION_SUPPORT_PLUGIN) {
391                 return true;
392             }
393         }
394     }
395     return false;
396 }
397 
CheckPkgContext()398 bool HapPackager::CheckPkgContext()
399 {
400     std::map<std::string, std::string>::const_iterator it = parameterMap_.find(Constants::PARAM_PKG_CONTEXT_PATH);
401     if (it != parameterMap_.end()) {
402         const std::string filePath = it->second;
403         if (!fs::is_regular_file(filePath) ||
404             fs::path(filePath).filename().string() != Constants::PKG_CONTEXT_JSON) {
405             LOGE("host must include pkgContextInfo.json");
406             return false;
407         }
408         return true;
409     }
410     LOGE("no have pkgContextInfo.json.");
411     return false;
412 }
413 
CheckStageHap(const std::string & jsonPath)414 bool HapPackager::CheckStageHap(const std::string &jsonPath)
415 {
416     if (!moduleJson_.CheckStageAsanTsanEnabledValid()) {
417         LOGE("CheckStageAsanTsanEnabledValid failed.");
418         return false;
419     }
420 
421     if (!moduleJson_.CheckStageAtomicService()) {
422         LOGE("CheckStageAtomicService failed.");
423         return false;
424     }
425 
426     return true;
427 }
428 
CompressHapModeForModule(const std::string & jsonPath)429 bool HapPackager::CompressHapModeForModule(const std::string &jsonPath)
430 {
431     if (!moduleJson_.GetStageModuleName(moduleName_)) {
432         LOGE("HapPackager::Process: GetStageModuleName failed!");
433     }
434     if (!moduleJson_.GetStageDeviceTypes(deviceTypes_)) {
435         LOGE("HapPackager::Process: GetStageDeviceTypes failed!");
436     }
437     if (!OpenZipWrapper()) {
438         return false;
439     }
440 
441     std::string jsonString = moduleJson_.ToString();
442     if (!jsonString.empty()) {
443         if (zipWrapper_.WriteStringToZip(jsonString, Constants::MODULE_JSON) != ZipErrCode::ZIP_ERR_SUCCESS) {
444             LOGE("HapPackager::Process: zipWrapper WriteStringToZip failed!");
445             return false;
446         }
447     }
448 
449     std::map<std::string, std::string> paramFileMap = {
450         {Constants::PARAM_PROFILE_PATH, Constants::PROFILE_NAME},
451         {Constants::PARAM_INDEX_PATH, Constants::RESOURCES_INDEX},
452         {Constants::PARAM_LIB_PATH, Constants::LIB_PATH},
453         {Constants::PARAM_AN_PATH, Constants::AN_PATH},
454         {Constants::PARAM_AP_PATH, Constants::AP_PATH},
455         {Constants::PARAM_RESOURCES_PATH, Constants::RESOURCES_PATH},
456         {Constants::PARAM_JS_PATH, Constants::JS_PATH},
457         {Constants::PARAM_ETS_PATH, Constants::ETS_PATH},
458         {Constants::PARAM_HNP_PATH, Constants::HNP_PATH},
459         {Constants::PARAM_RPCID_PATH, Constants::RPCID_SC},
460         {Constants::PARAM_ASSETS_PATH, Constants::ASSETS_PATH},
461         {Constants::PARAM_PACK_INFO_PATH, Constants::PACK_INFO}
462     };
463     for (auto& item : paramFileMap) {
464         if (!AddCommonFileOrDirectoryToZip(item.first, item.second)) {
465             return false;
466         }
467     }
468 
469     if (!AddParamFileToZip() || !AddResFileAndDirLsitToZip() || !AddPkgAndBinFileToZipForStageMaode()) {
470         return false;
471     }
472     return CompressHapModeMultiple();
473 }
474 
CompressHapMode()475 bool HapPackager::CompressHapMode()
476 {
477     if (!moduleJson_.GetFaModuleName(moduleName_)) {
478         LOGE("HapPackager::Process: GetFaModuleName failed!");
479     }
480     if (!moduleJson_.GetFaDeviceTypes(deviceTypes_)) {
481         LOGE("HapPackager::Process: GetFaDeviceTypes failed!");
482     }
483 
484     if (!OpenZipWrapper()) {
485         return false;
486     }
487 
488     std::string jsonString = moduleJson_.ToString();
489     if (!jsonString.empty()) {
490         if (zipWrapper_.WriteStringToZip(jsonString, Constants::CONFIG_JSON) != ZipErrCode::ZIP_ERR_SUCCESS) {
491             LOGE("HapPackager::Process: zipWrapper WriteStringToZip failed!");
492             return false;
493         }
494     }
495 
496     std::map<std::string, std::string> paramFileMap = {
497         {Constants::PARAM_PROFILE_PATH, Constants::PROFILE_NAME},
498         {Constants::PARAM_LIB_PATH, Constants::LIB_PATH},
499         {Constants::PARAM_RPCID_PATH, Constants::RPCID_SC},
500         {Constants::PARAM_PACK_INFO_PATH, Constants::PACK_INFO},
501         {Constants::PARAM_ASSETS_PATH, Constants::ASSETS_PATH}
502     };
503     for (auto& item : paramFileMap) {
504         if (!AddCommonFileOrDirectoryToZip(item.first, item.second)) {
505             return false;
506         }
507     }
508 
509     if (!AddIndexToZipForFaMaode() || !AddParamFileToZip() || !AddResFileAndDirLsitToZip()) {
510         return false;
511     }
512 
513     std::map<std::string, std::string>::const_iterator it = parameterMap_.find(Constants::PARAM_RESOURCES_PATH);
514     if (it != parameterMap_.end() && !it->second.empty() && !moduleName_.empty()) {
515         std::string resourcesPath = Constants::ASSETS_PATH + Constants::LINUX_FILE_SEPARATOR + moduleName_ +
516             Constants::LINUX_FILE_SEPARATOR + Constants::RESOURCES_PATH;
517         if (zipWrapper_.AddFileOrDirectoryToZip(it->second, resourcesPath) != ZipErrCode::ZIP_ERR_SUCCESS) {
518             LOGE("HapPackager::Process: zipWrapper AddFileOrDirectoryToZip failed!");
519             return false;
520         }
521     }
522     return CompressHapModeMultiple();
523 }
524 
CompressHapModeMultiple()525 bool HapPackager::CompressHapModeMultiple()
526 {
527     for (std::string soPathItem : formattedSoPathList_) {
528         std::string zipPath = Constants::SO_ARM64_DIR + fs::path(soPathItem).filename().string();
529         if (zipWrapper_.AddFileOrDirectoryToZip(soPathItem, zipPath) != ZipErrCode::ZIP_ERR_SUCCESS) {
530             LOGE("HapPackager::Process: zipWrapper AddFileOrDirectoryToZip failed!");
531             return false;
532         }
533     }
534 
535     std::map<std::string, std::string>::const_iterator it = parameterMap_.find(Constants::PARAM_MAPLE_SO_DIR);
536     if (it != parameterMap_.end() && formattedSoPathList_.size() == 0 && !it->second.empty()) {
537         if (zipWrapper_.AddFileOrDirectoryToZip(it->second, Constants::SO_DIR) != ZipErrCode::ZIP_ERR_SUCCESS) {
538             LOGE("HapPackager::Process: zipWrapper AddFileOrDirectoryToZip failed!");
539             return false;
540         }
541     }
542 
543     for (auto& pathList : {formattedAbilitySoPathList_, formattedAbcPathList_, formattedJarPathList_,
544         formattedTxtPathList_}) {
545         if (!AddFileListToZip(pathList)) {
546             return false;
547         }
548     }
549 
550     if (!AddCommonFileOrDirectoryToZip(Constants::PARAM_SHAREDLIBS_PATH, Constants::SHARED_LIBS_DIR)) {
551         return false;
552     }
553     zipWrapper_.Close();
554     return true;
555 }
556 
AddFileListToZip(const std::list<std::string> & pathList_)557 bool HapPackager::AddFileListToZip(const std::list<std::string> &pathList_)
558 {
559     for (auto Item : pathList_) {
560         std::string zipPath = fs::path(Item).filename().string();
561         if (zipWrapper_.AddFileOrDirectoryToZip(Item, zipPath) != ZipErrCode::ZIP_ERR_SUCCESS) {
562             LOGE("HapPackager::Process: zipWrapper AddFileOrDirectoryToZip failed!");
563             return false;
564         }
565     }
566     return true;
567 }
568 
OpenZipWrapper()569 bool HapPackager::OpenZipWrapper()
570 {
571     std::map<std::string, std::string>::const_iterator it = parameterMap_.find(Constants::PARAM_OUT_PATH);
572     std::string outPath;
573     if (it != parameterMap_.end()) {
574         outPath = it->second;
575     }
576 
577     zipWrapper_.Open(outPath);
578     if (!zipWrapper_.IsOpen()) {
579         LOGE("HapPackager::Process: zipWrapper Open failed!");
580         return false;
581     }
582     return true;
583 }
584 
AddCommonFileOrDirectoryToZip(const std::string & paramPath,const std::string & targetPath)585 bool HapPackager::AddCommonFileOrDirectoryToZip(const std::string &paramPath, const std::string &targetPath)
586 {
587     std::map<std::string, std::string>::const_iterator it = parameterMap_.find(paramPath);
588     if (it != parameterMap_.end() && !it->second.empty()) {
589         if (zipWrapper_.AddFileOrDirectoryToZip(it->second, targetPath) != ZipErrCode::ZIP_ERR_SUCCESS) {
590             LOGE("HapPackager::Process: zipWrapper AddFileOrDirectoryToZip failed!");
591             return false;
592         }
593     }
594     return true;
595 }
596 
AddParamFileToZip()597 bool HapPackager::AddParamFileToZip()
598 {
599     std::map<std::string, std::string>::const_iterator it = parameterMap_.find(Constants::PARAM_FILE_PATH);
600     if (it != parameterMap_.end() && !it->second.empty()) {
601         fs::path filePath = fs::path(it->second);
602         std::string zipPath = Constants::NULL_DIR_NAME;
603         if (!fs::is_directory(filePath)) {
604             zipPath = (filePath).filename().string();
605         }
606         if (zipWrapper_.AddFileOrDirectoryToZip(it->second, zipPath) != ZipErrCode::ZIP_ERR_SUCCESS) {
607             LOGE("HapPackager::Process: zipWrapper AddFileOrDirectoryToZip failed!");
608             return false;
609         }
610     }
611     return true;
612 }
613 
AddResFileAndDirLsitToZip()614 bool HapPackager::AddResFileAndDirLsitToZip()
615 {
616     std::map<std::string, std::string>::const_iterator it = parameterMap_.find(Constants::PARAM_RES_PATH);
617     if (it != parameterMap_.end() && !it->second.empty() && !moduleName_.empty()) {
618         std::string resPath = Constants::ASSETS_PATH + Constants::LINUX_FILE_SEPARATOR + moduleName_ +
619             Constants::LINUX_FILE_SEPARATOR + Constants::RESOURCES_PATH;
620         std::string deviceType;
621         if (!deviceTypes_.empty()) {
622             deviceType = deviceTypes_.front();
623         }
624         if (Constants::DEVICE_TYPE_FITNESSWATCH == deviceType ||
625             Constants::DEVICE_TYPE_FITNESSWATCH_NEW == deviceType) {
626             resPath = Constants::RES_PATH;
627         }
628         if (zipWrapper_.AddFileOrDirectoryToZip(it->second, resPath) != ZipErrCode::ZIP_ERR_SUCCESS) {
629             LOGE("HapPackager::Process: zipWrapper AddFileOrDirectoryToZip failed!");
630             return false;
631         }
632     }
633 
634     if (!formatedDirList_.empty()) {
635         for (const auto& dirPath : formatedDirList_) {
636             std::string baseDir = fs::path(dirPath).filename().string();
637             if (zipWrapper_.AddFileOrDirectoryToZip(dirPath, baseDir) != ZipErrCode::ZIP_ERR_SUCCESS) {
638                 LOGE("HapPackager::Process: zipWrapper AddFileOrDirectoryToZip failed!");
639                 return false;
640             }
641         }
642     }
643     return true;
644 }
645 
AddIndexToZipForFaMaode()646 bool HapPackager::AddIndexToZipForFaMaode()
647 {
648     std::map<std::string, std::string>::const_iterator it = parameterMap_.find(Constants::PARAM_INDEX_PATH);
649     if (it != parameterMap_.end() && !it->second.empty() && !moduleName_.empty()) {
650         std::string assetsPath = Constants::ASSETS_PATH + Constants::LINUX_FILE_SEPARATOR + moduleName_ +
651             Constants::LINUX_FILE_SEPARATOR + fs::path(it->second).filename().string();
652         if (zipWrapper_.AddFileOrDirectoryToZip(it->second, assetsPath) != ZipErrCode::ZIP_ERR_SUCCESS) {
653             LOGE("HapPackager::Process: zipWrapper AddFileOrDirectoryToZip failed!");
654             return false;
655         }
656     }
657     return true;
658 }
659 
AddPkgAndBinFileToZipForStageMaode()660 bool HapPackager::AddPkgAndBinFileToZipForStageMaode()
661 {
662     std::map<std::string, std::string>::const_iterator it = parameterMap_.find(Constants::PARAM_BIN_PATH);
663     if (it != parameterMap_.end() && !it->second.empty()) {
664         fs::path filePath = fs::path(it->second);
665         std::string zipPath = Constants::NULL_DIR_NAME;
666         if (!fs::is_directory(filePath)) {
667             zipPath = (filePath).filename().string();
668         }
669         if (zipWrapper_.AddFileOrDirectoryToZip(it->second, zipPath) != ZipErrCode::ZIP_ERR_SUCCESS) {
670             LOGE("HapPackager::Process: zipWrapper AddFileOrDirectoryToZip failed!");
671             return false;
672         }
673     }
674     it = parameterMap_.find(Constants::PARAM_PKG_CONTEXT_PATH);
675     if (it != parameterMap_.end() && !it->second.empty()) {
676         ModuleJson moduleJson;
677         if (!moduleJson.ParseFromFile(it->second)) {
678             LOGE("HapPackager::Process: moduleJson Read failed!");
679             return false;
680         }
681         std::string jsonString = moduleJson.ToString();
682         if (!jsonString.empty()) {
683             if (zipWrapper_.WriteStringToZip(jsonString, Constants::PKG_CONTEXT_JSON) != ZipErrCode::ZIP_ERR_SUCCESS) {
684                 LOGE("HapPackager::Process: zipWrapper WriteStringToZip failed!");
685                 return false;
686             }
687         } else {
688             LOGE("HapPackager::Process: jsonFile error!");
689             return false;
690         }
691     }
692     return true;
693 }
694 } // namespace AppPackingTool
695 } // namespace OHOS