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