• 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 #ifndef DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_PACKAGER_H
17 #define DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_PACKAGER_H
18 
19 #include <filesystem>
20 #include <functional>
21 #include <iostream>
22 #include <list>
23 #include <map>
24 #include <regex>
25 #include <string>
26 #include <vector>
27 
28 #include "constants.h"
29 #include "json/module_json.h"
30 
31 namespace fs = std::filesystem;
32 
33 namespace OHOS {
34 namespace AppPackingTool {
35 
36 enum ErrCode {
37     ERR_OK = 0,
38     ERR_INVALID_VALUE,
39 };
40 
41 struct Parameter {
42     std::string paramName;
43     bool required;
44     Constants::PARAM_TYPE paramType;
45     std::string paramRegrex;
46     std::string zipEntry;
47 };
48 
49 class Packager {
50 public:
51     Packager(const std::map<std::string, std::string> &parameterMap, std::string &resultReceiver);
52     virtual ~Packager();
53 
54     int32_t MakePackage();
55 
56     virtual int32_t InitAllowedParam() = 0;
57     virtual int32_t PreProcess() = 0;
58     virtual int32_t Process() = 0;
59     virtual int32_t PostProcess() = 0;
60 
61 protected:
62     const std::map<std::string, std::string> &parameterMap_;
63     std::string &resultReceiver_;
64     std::vector<Parameter> allowedParameters_;
65     bool buildHashFinish_ = false;
66     bool generateBuildHash_ = false;
67 
68     bool CheckForceFlag();
69     bool IsPathValid(const std::string &path, const bool &isFile, const std::string suffix = "");
70     bool SplitDirList(const std::string &dirList, std::list<std::string> &fileList);
71     void RemoveDuplicatePath(const std::string &path, std::list<std::string> &pathList);
72     bool CompatibleProcess(const std::string &inputPath, std::list<std::string> &fileList, const std::string &suffix);
73     bool CompatibleProcess(const std::string &inputPath, std::list<std::string> &fileList,
74         const std::string &suffix, const std::string &extraSuffix);
75     bool IsOutPathValid(const std::string &outPath, const std::string &forceRewrite, const std::string &suffix);
76     bool SetGenerateBuildHash(std::string &jsonPath, bool &generateBuildHash, bool &buildHashFinish);
77     bool CopyFileToTempDir(std::string &jsonPath);
78     bool BuildHash(bool &buildHashFinish, const bool &generateBuildHash,
79         const std::map<std::string, std::string> &parameterMap, const std::string &jsonPath);
80     bool PutBuildHash(const std::string &jsonPath, const std::string &hash, bool &buildHashFinish);
81     bool IsOutDirectoryValid();
82     bool IsModuleHap(const std::string& hapPath);
83     void CompressPackinfoIntoHap(const std::string& hapPathItem, const std::string& unzipPathString,
84         const std::string& outPathString, const std::string& packInfoPath);
85 };
86 
87 }  // namespace AppExecFwk
88 }  // namespace OHOS
89 
90 #endif  // DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_PACKAGER_H