• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 package ohos;
17 
18 import java.io.File;
19 import java.util.Locale;
20 
21 /**
22  * uncompress command verify.
23  *
24  */
25 class UncompressVerify {
26     private static final String HAP_SUFFIX = ".hap";
27     private static final String HAR_SUFFIX = ".har";
28     private static final String APP_SUFFIX = ".app";
29     private static final String APPQF_SUFFIX = ".appqf";
30     private static final String HSP_SUFFIX = ".hsp";
31     private static final String FALSE = "false";
32     private static final String RPCID_SC = "rpcid.sc";
33     private static final String TRUE = "true";
34     private static final Log LOG = new Log(UncompressVerify.class.toString());
35 
36     /**
37      * is args valid.
38      *
39      * @param utility common data
40      * @return commandVerify if verify valid.
41      */
commandVerify(Utility utility)42     public static boolean commandVerify(Utility utility) {
43         if (!utility.getForceRewrite().isEmpty() && !"true".equals(utility.getForceRewrite())
44             && !"false".equals(utility.getForceRewrite())) {
45             LOG.error("UncompressVerify::isVerifyVaild forceRewrite is invalid!");
46             return false;
47         }
48 
49         if (!utility.getParseMode().isEmpty() && !UncompressEntrance.PARSE_MODE_HAPLIST.equals(utility.getParseMode())
50                 && !UncompressEntrance.PARSE_MODE_HAPINFO.equals(utility.getParseMode())
51                 && !UncompressEntrance.PARSE_MODE_ALL.equals(utility.getParseMode())) {
52             LOG.error("UncompressVerify::isVerifyVaild parseMode is invalid!");
53             return false;
54         }
55 
56         if (Utility.MODE_HAP.equals(utility.getMode())) {
57             return hapCommandVerify(utility);
58         } else if (Utility.MODE_HAR.equals(utility.getMode())) {
59             return harCommandVerify(utility);
60         } else if (Utility.MODE_APP.equals(utility.getMode())) {
61             return appCommandVerify(utility);
62         } else if (Utility.MODE_APPQF.equals(utility.getMode())) {
63             return appqfVerify(utility);
64         } else if (Utility.MODE_HSP.equals(utility.getMode())) {
65             return hspCommandVerify(utility);
66         } else {
67             LOG.error("UncompressVerify::commandVerify mode is invalid!");
68             return false;
69         }
70     }
71 
72     /**
73      * parse and check args if valid in hap mode.
74      *
75      * @param utility common data
76      * @return isVerifyValidInHapMode if verify valid in hap mode.
77      */
hapCommandVerify(Utility utility)78     private static boolean hapCommandVerify(Utility utility) {
79         utility.setHapPath(utility.getFormattedPath(utility.getHapPath()));
80         File file = new File(utility.getHapPath());
81         if (!file.isFile() || !file.getName().toLowerCase(Locale.ENGLISH).endsWith(HAP_SUFFIX)) {
82             LOG.error("UncompressVerify::isArgsValidInHapMode hap-path must end with.hap!");
83             return false;
84         }
85 
86         if (!(TRUE.equals(utility.getRpcid()) || FALSE.equals(utility.getRpcid()))) {
87             LOG.error("UncompressVerify::isArgsValidInHapMode --rpcid must be true or false");
88             return false;
89         }
90         if (TRUE.equals(utility.getRpcid())) {
91             return rpcidCommandVerify(utility);
92         }
93 
94         return verifyOutPath(utility, file);
95     }
96 
hspCommandVerify(Utility utility)97     private static boolean hspCommandVerify(Utility utility) {
98         utility.setHspPath(utility.getFormattedPath(utility.getHspPath()));
99         File file = new File(utility.getHspPath());
100         if (!file.isFile() || !file.getName().toLowerCase(Locale.ENGLISH).endsWith(HSP_SUFFIX)) {
101             LOG.error("UncompressVerify::isArgsValidInHspMode hsp-path must end with.hsp!");
102             return false;
103         }
104         return verifyOutPath(utility, file);
105     }
106 
107     /**
108      * parse and check args if valid in hap mode.
109      *
110      * @param utility common data
111      * @return isVerifyValidInRpcidMode if verify valid in hap mode.
112      */
rpcidCommandVerify(Utility utility)113     private static boolean rpcidCommandVerify(Utility utility) {
114         File outPath = new File(utility.getOutPath(), RPCID_SC);
115         if (outPath.exists() && FALSE.equals(utility.getForceRewrite())) {
116             LOG.error("UncompressVerify::rpcidCommandVerify outPath already exists");
117             return false;
118         }
119         return true;
120     }
121 
122     /**
123      * parse and check args if valid in har mode.
124      *
125      * @param utility common data
126      * @return isVerifyValidInHapMode if verify valid in hap mode.
127      */
harCommandVerify(Utility utility)128     private static boolean harCommandVerify(Utility utility) {
129         utility.setHarPath(utility.getFormattedPath(utility.getHarPath()));
130         File file = new File(utility.getHarPath());
131         if (!file.isFile() || !file.getName().toLowerCase(Locale.ENGLISH).endsWith(HAR_SUFFIX)) {
132             LOG.error("UncompressVerify::isArgsValidInHarMode har-path must end with.har!");
133             return false;
134         }
135 
136         if (utility.getOutPath().isEmpty()) {
137             LOG.error("UncompressVerify::isVerifyVaild outPath is invalid!");
138             return false;
139         }
140 
141         return verifyOutPath(utility, file);
142     }
143 
144     /**
145      * parse and check args if valid in app mode.
146      *
147      * @param utility common data
148      * @return true if input app file is valid.
149      */
appCommandVerify(Utility utility)150     private static boolean appCommandVerify(Utility utility) {
151         if (UncompressEntrance.PARSE_MODE_HAPINFO.equals(utility.getParseMode())
152                 && utility.getHapName().isEmpty()) {
153             LOG.error("UncompressVerify::isArgsVaildInParseMode hapName can't be empty!");
154             return false;
155         }
156 
157         utility.setAppPath(utility.getFormattedPath(utility.getAppPath()));
158         File file = new File(utility.getAppPath());
159         if (!file.isFile() || !file.getName().toLowerCase(Locale.ENGLISH).endsWith(APP_SUFFIX)) {
160             LOG.error("UncompressVerify::appCommandVerify app-path must end with.app!");
161             return false;
162         }
163 
164         return verifyOutPath(utility, file);
165     }
166 
167     /**
168      * parse and check args if valid in appqf mode.
169      *
170      * @param utility common data
171      * @return true if input appqf path is valid.
172      */
appqfVerify(Utility utility)173     private static boolean appqfVerify(Utility utility) {
174         utility.setAPPQFPath(utility.getFormattedPath(utility.getAPPQFPath()));
175         File file = new File(utility.getAPPQFPath());
176         if (!file.isFile() || !file.getName().toLowerCase(Locale.ENGLISH).endsWith(APPQF_SUFFIX)) {
177             LOG.error("UncompressVerify::appqfVerify appqf-path is invalid,please" +
178                     " check input file is exist or this file is not end with .appqf!");
179             return false;
180         }
181 
182         return verifyOutPath(utility, file);
183     }
184 
185     /**
186      * parse and check the outpath args.
187      *
188      * @param utility common data
189      * @param file file to be verified
190      * @return isVerifyValidInHapMode if verify valid in hap mode.
191      */
verifyOutPath(Utility utility, File file)192     private static boolean verifyOutPath(Utility utility, File file) {
193         if (utility.getOutPath().isEmpty() && !utility.getIsParse()) {
194             LOG.error("--out-path is empty!");
195             return false;
196         }
197 
198         File outFile = new File(utility.getOutPath());
199         if (("false".equals(utility.getForceRewrite())) && (outFile.exists())) {
200             LOG.error("UncompressVerify::isArgsValidInHapMode out file already existed!");
201             return false;
202         }
203         return true;
204     }
205 
206     /**
207      * check path if valid
208      *
209      * @param path path input
210      * @param isFile type input
211      * @param flag flag input
212      * @return isPathValid if path verify
213      */
isPathValid(String path, boolean isFile, String flag)214     public static boolean isPathValid(String path, boolean isFile, String flag) {
215         if (!FileUtils.matchPattern(path)) {
216             LOG.error("Input invalid file " + path);
217             return false;
218         }
219         File file = new File(path);
220         if (isFile && (file.isFile()) && file.getName().toLowerCase(Locale.ENGLISH).endsWith(flag)) {
221             return true;
222         }
223         return (!isFile) && file.isDirectory();
224     }
225 
226     /**
227      * check path if valid.
228      *
229      * @param parseMode indicates the parse mode of parse app.
230      * @param hapName type indicates the hap name if the mode is HAP_INFO.
231      * @return isParseAppModeValid if path verify.
232      */
isParseAppModeValid(String parseMode, String hapName)233     public static boolean isParseAppModeValid(String parseMode, String hapName) {
234         if (!UncompressEntrance.PARSE_MODE_HAPLIST.equals(parseMode) &&
235                 !UncompressEntrance.PARSE_MODE_HAPINFO.equals(parseMode) &&
236                 !UncompressEntrance.PARSE_MODE_ALL.equals(parseMode)) {
237             LOG.error("uncompressVerify parse mode " + parseMode + " is invalid!");
238             return false;
239         }
240         if (UncompressEntrance.PARSE_MODE_HAPINFO.equals(parseMode) && hapName.isEmpty()) {
241             LOG.error("uncompressVerify hapName should not empty when parse mode is hap-info!");
242             return false;
243         }
244         return true;
245     }
246 }