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 <filesystem>
17 #include <fstream>
18 #include <iostream>
19 #include <ostream>
20 #include <regex>
21 #include <string>
22
23 #include "hap_packager.h"
24 #include "constants.h"
25 #include "contrib/minizip/zip.h"
26 #include "nlohmann/json.hpp"
27 #include "packager.h"
28
29 namespace OHOS {
30 namespace AppPackingTool {
31 namespace {}
32
HapPackager(const std::map<std::string,std::string> & parameterMap,std::string & resultReceiver)33 HapPackager::HapPackager(const std::map<std::string, std::string> ¶meterMap, std::string &resultReceiver)
34 : Packager(parameterMap, resultReceiver)
35 {}
36
InitAllowedParam()37 int HapPackager::InitAllowedParam()
38 {
39 allowedParameters_ = {
40 {}
41 };
42 return ERR_OK;
43 }
44
PreProcess()45 int HapPackager::PreProcess()
46 {
47 return ERR_OK;
48 }
49
Process()50 int HapPackager::Process()
51 {
52 std::cout << "Hap DoPackage" << std::endl;
53 std::string outPath = parameterMap_.at(Constants::PARAM_OUT_PATH);
54 zipFile zf = zipOpen64(outPath.c_str(), APPEND_STATUS_CREATE);
55 if (zf == nullptr) {
56 std::cout << "err zipOpen64 null" << std::endl;
57 return ERR_INVALID_VALUE;
58 }
59 zip_fileinfo fi = {};
60 std::map<std::string, std::string>::const_iterator it = parameterMap_.find(Constants::PARAM_JSON_PATH);
61 if (it != parameterMap_.end()) {
62 if (ParseJsonFile(moduleJson, it->second)) {
63 WriteStringToZip(zf, moduleJson.dump(), fs::path(Constants::MODULE_JSON), fi);
64 }
65 }
66 it = parameterMap_.find(Constants::PARAM_LIB_PATH);
67 if (it != parameterMap_.end()) {
68 AddFileToZip(zf, fs::path(it->second), fs::path(Constants::LIB_PATH), fi);
69 }
70 it = parameterMap_.find(Constants::PARAM_RESOURCES_PATH);
71 if (it != parameterMap_.end()) {
72 AddFileToZip(zf, fs::path(it->second), fs::path(Constants::RESOURCES_PATH), fi);
73 }
74 it = parameterMap_.find(Constants::PARAM_INDEX_PATH);
75 if (it != parameterMap_.end()) {
76 AddFileToZip(zf, fs::path(it->second), fs::path(Constants::RESOURCES_INDEX), fi);
77 }
78 it = parameterMap_.find(Constants::PARAM_PACK_INFO_PATH);
79 if (it != parameterMap_.end()) {
80 AddFileToZip(zf, fs::path(it->second), fs::path(Constants::PACK_INFO), fi);
81 }
82 it = parameterMap_.find(Constants::PARAM_ETS_PATH);
83 if (it != parameterMap_.end()) {
84 AddFileToZip(zf, fs::path(it->second), fs::path(Constants::ETS_PATH), fi);
85 }
86 it = parameterMap_.find(Constants::PARAM_RPCID_PATH);
87 if (it != parameterMap_.end()) {
88 AddFileToZip(zf, fs::path(it->second), fs::path(Constants::RPCID_SC), fi);
89 }
90 it = parameterMap_.find(Constants::PARAM_PKG_CONTEXT_PATH);
91 if (it != parameterMap_.end()) {
92 AddFileToZip(zf, fs::path(it->second), fs::path(Constants::PKG_CONTEXT_JSON), fi);
93 }
94 zipClose(zf, nullptr);
95 return ERR_OK;
96 }
97
PostProcess()98 int HapPackager::PostProcess()
99 {
100 return ERR_OK;
101 }
102
103 } // namespace AppPackingTool
104 } // namespace OHOS