• 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 "res_packager.h"
17 
18 #include <filesystem>
19 #include <string>
20 
21 #include "constants.h"
22 #include "log.h"
23 
24 namespace OHOS {
25 namespace AppPackingTool {
ResPackager(const std::map<std::string,std::string> & parameterMap,std::string & resultReceiver)26 ResPackager::ResPackager(const std::map<std::string, std::string> &parameterMap, std::string &resultReceiver)
27     : Packager(parameterMap, resultReceiver)
28 {}
29 
InitAllowedParam()30 int32_t ResPackager::InitAllowedParam()
31 {
32     allowedParameters_ = {
33         {}
34     };
35     return ERR_OK;
36 }
37 
PreProcess()38 int32_t ResPackager::PreProcess()
39 {
40     if (!CheckForceFlag()) {
41         return ERR_INVALID_VALUE;
42     }
43     if (!IsVerifyValidInResMode()) {
44         return ERR_INVALID_VALUE;
45     }
46     return ERR_OK;
47 }
48 
Process()49 int32_t ResPackager::Process()
50 {
51     if (!CompressPackResMode()) {
52         if (fs::exists(outPath_)) {
53             fs::remove_all(outPath_);
54         }
55         LOGE("ResPackager::Process failed.");
56         return ERR_INVALID_VALUE;
57     }
58     return ERR_OK;
59 }
60 
PostProcess()61 int32_t ResPackager::PostProcess()
62 {
63     return ERR_OK;
64 }
65 
IsVerifyValidInResMode()66 bool ResPackager::IsVerifyValidInResMode()
67 {
68     std::map<std::string, std::string>::const_iterator it = parameterMap_.find(Constants::PARAM_PACK_INFO_PATH);
69     // check pack-info-path
70     if (it != parameterMap_.end()) {
71         packInfoPath_ = it->second;
72         if (!fs::is_regular_file(packInfoPath_)) {
73             LOGE("ResPackager::IsVerifyValidInResMode --pack-info-path is not a file.");
74             return false;
75         }
76         if (fs::path(packInfoPath_).filename().string() != Constants::PACK_INFO) {
77             LOGE("ResPackager::IsVerifyValidInResMode --pack-info-path must be pack.info file.");
78             return false;
79         }
80     }
81     // check entrycard-path
82     it = parameterMap_.find(Constants::PARAM_ENTRYCARD_PATH);
83     if (it != parameterMap_.end()) {
84         entryCardPath_ = it->second;
85         if (!fs::is_directory(entryCardPath_)) {
86             LOGE("ResPackager::IsVerifyValidInResMode --entrycard-path is not a directory.");
87             return false;
88         }
89         if (fs::path(entryCardPath_).filename().string() != Constants::ENTRYCARD_NAME) {
90             LOGE("ResPackager::IsVerifyValidInResMode the level-1 directory name must be EntryCard, current is %s",
91                 entryCardPath_.c_str());
92             return false;
93         }
94         if (!CompatibleProcess(entryCardPath_, formattedEntryCardPathList_, Constants::PNG_SUFFIX)) {
95             LOGE("ResPackager::IsVerifyValidInResMode --entrycard-path is invalid.");
96             return false;
97         }
98     }
99     // check out-path
100     it = parameterMap_.find(Constants::PARAM_OUT_PATH);
101     if (it != parameterMap_.end()) {
102         outPath_ = it->second;
103     }
104     std::string forceRewrite = "";
105     it = parameterMap_.find(Constants::PARAM_FORCE);
106     if (it != parameterMap_.end()) {
107         forceRewrite = it->second;
108     }
109     return IsOutPathValid(outPath_, forceRewrite, Constants::RES_SUFFIX);
110 }
111 
CompressPackResMode()112 bool ResPackager::CompressPackResMode()
113 {
114     zipWrapper_.Open(outPath_);
115     if (!zipWrapper_.IsOpen()) {
116         LOGE("ResPackager::Process: zipWrapper Open failed!");
117         return false;
118     }
119     if (zipWrapper_.AddFileOrDirectoryToZip(entryCardPath_, Constants::ENTRYCARD_NAME) !=
120         ZipErrCode::ZIP_ERR_SUCCESS) {
121         LOGE("ResPackager::Process: zipWrapper AddFileOrDirectoryToZip failed!");
122         return false;
123     }
124     zipWrapper_.Close();
125 
126     return true;
127 }
128 } // namespace AppPackingTool
129 } // namespace OHOS