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 if (!(TRUE.equals(utility.getLibs()) || FALSE.equals(utility.getLibs()))) { 86 LOG.error("UncompressVerify::isArgsValidInHapMode --libs must be true or false"); 87 return false; 88 } 89 if (TRUE.equals(utility.getLibs())) { 90 utility.setRpcid(Utility.FALSE_STRING); 91 return libsCommandVerify(utility); 92 } 93 if (!(TRUE.equals(utility.getRpcid()) || FALSE.equals(utility.getRpcid()))) { 94 LOG.error("UncompressVerify::isArgsValidInHapMode --rpcid must be true or false"); 95 return false; 96 } 97 if (TRUE.equals(utility.getRpcid())) { 98 return rpcidCommandVerify(utility); 99 } 100 101 return verifyOutPath(utility, file); 102 } 103 hspCommandVerify(Utility utility)104 private static boolean hspCommandVerify(Utility utility) { 105 utility.setHspPath(utility.getFormattedPath(utility.getHspPath())); 106 File file = new File(utility.getHspPath()); 107 if (!file.isFile() || !file.getName().toLowerCase(Locale.ENGLISH).endsWith(HSP_SUFFIX)) { 108 LOG.error("UncompressVerify::isArgsValidInHspMode hsp-path must end with.hsp!"); 109 return false; 110 } 111 112 if (!(TRUE.equals(utility.getLibs()) || FALSE.equals(utility.getLibs()))) { 113 LOG.error("UncompressVerify::isArgsValidInHapMode --libs must be true or false"); 114 return false; 115 } 116 if (TRUE.equals(utility.getLibs())) { 117 return libsCommandVerify(utility); 118 } 119 return verifyOutPath(utility, file); 120 } 121 122 /** 123 * parse and check args if valid in hap mode. 124 * 125 * @param utility common data 126 * @return isVerifyValidInRpcidMode if verify valid in hap mode. 127 */ rpcidCommandVerify(Utility utility)128 private static boolean rpcidCommandVerify(Utility utility) { 129 File outPath = new File(utility.getOutPath(), RPCID_SC); 130 if (outPath.exists() && FALSE.equals(utility.getForceRewrite())) { 131 LOG.error("UncompressVerify::rpcidCommandVerify outPath already exists"); 132 return false; 133 } 134 return true; 135 } 136 137 /** 138 * parse and check args if valid in hap or hsp mode. 139 * 140 * @param utility common data 141 * @return isVerifyValidInLibsMode if verify valid in hap or hsp mode. 142 */ libsCommandVerify(Utility utility)143 private static boolean libsCommandVerify(Utility utility) { 144 if (utility.getOutPath().isEmpty()) { 145 LOG.error("--out-path is empty!"); 146 return false; 147 } 148 149 File outFile = new File(utility.getOutPath()); 150 if (("false".equals(utility.getForceRewrite())) && (outFile.exists())) { 151 LOG.error("UncompressVerify::libsCommandVerify out file already existed!"); 152 return false; 153 } 154 155 if (!utility.getCpuAbis().isEmpty()) { 156 String[] array = utility.getCpuAbis().split(","); 157 for (String item : array) { 158 if (!utility.getFormattedCpuAbiList().contains(item)) { 159 utility.getFormattedCpuAbiList().add(item); 160 } 161 } 162 } 163 return true; 164 } 165 166 /** 167 * parse and check args if valid in har mode. 168 * 169 * @param utility common data 170 * @return isVerifyValidInHapMode if verify valid in hap mode. 171 */ harCommandVerify(Utility utility)172 private static boolean harCommandVerify(Utility utility) { 173 utility.setHarPath(utility.getFormattedPath(utility.getHarPath())); 174 File file = new File(utility.getHarPath()); 175 if (!file.isFile() || !file.getName().toLowerCase(Locale.ENGLISH).endsWith(HAR_SUFFIX)) { 176 LOG.error("UncompressVerify::isArgsValidInHarMode har-path must end with.har!"); 177 return false; 178 } 179 180 if (utility.getOutPath().isEmpty()) { 181 LOG.error("UncompressVerify::isVerifyVaild outPath is invalid!"); 182 return false; 183 } 184 185 return verifyOutPath(utility, file); 186 } 187 188 /** 189 * parse and check args if valid in app mode. 190 * 191 * @param utility common data 192 * @return true if input app file is valid. 193 */ appCommandVerify(Utility utility)194 private static boolean appCommandVerify(Utility utility) { 195 if (UncompressEntrance.PARSE_MODE_HAPINFO.equals(utility.getParseMode()) 196 && utility.getHapName().isEmpty()) { 197 LOG.error("UncompressVerify::isArgsVaildInParseMode hapName can't be empty!"); 198 return false; 199 } 200 201 utility.setAppPath(utility.getFormattedPath(utility.getAppPath())); 202 File file = new File(utility.getAppPath()); 203 if (!file.isFile() || !file.getName().toLowerCase(Locale.ENGLISH).endsWith(APP_SUFFIX)) { 204 LOG.error("UncompressVerify::appCommandVerify app-path must end with.app!"); 205 return false; 206 } 207 208 return verifyOutPath(utility, file); 209 } 210 211 /** 212 * parse and check args if valid in appqf mode. 213 * 214 * @param utility common data 215 * @return true if input appqf path is valid. 216 */ appqfVerify(Utility utility)217 private static boolean appqfVerify(Utility utility) { 218 utility.setAPPQFPath(utility.getFormattedPath(utility.getAPPQFPath())); 219 File file = new File(utility.getAPPQFPath()); 220 if (!file.isFile() || !file.getName().toLowerCase(Locale.ENGLISH).endsWith(APPQF_SUFFIX)) { 221 LOG.error("UncompressVerify::appqfVerify appqf-path is invalid,please" + 222 " check input file is exist or this file is not end with .appqf!"); 223 return false; 224 } 225 226 return verifyOutPath(utility, file); 227 } 228 229 /** 230 * parse and check the outpath args. 231 * 232 * @param utility common data 233 * @param file file to be verified 234 * @return isVerifyValidInHapMode if verify valid in hap mode. 235 */ verifyOutPath(Utility utility, File file)236 private static boolean verifyOutPath(Utility utility, File file) { 237 if (utility.getOutPath().isEmpty() && !utility.getIsParse()) { 238 LOG.error("--out-path is empty!"); 239 return false; 240 } 241 242 File outFile = new File(utility.getOutPath()); 243 if (("false".equals(utility.getForceRewrite())) && (outFile.exists())) { 244 LOG.error("UncompressVerify::isArgsValidInHapMode out file already existed!"); 245 return false; 246 } 247 return true; 248 } 249 250 /** 251 * check path if valid 252 * 253 * @param path path input 254 * @param isFile type input 255 * @param flag flag input 256 * @return isPathValid if path verify 257 */ isPathValid(String path, boolean isFile, String flag)258 public static boolean isPathValid(String path, boolean isFile, String flag) { 259 if (!FileUtils.matchPattern(path)) { 260 LOG.error("Input invalid file " + path); 261 return false; 262 } 263 File file = new File(path); 264 if (isFile && (file.isFile()) && file.getName().toLowerCase(Locale.ENGLISH).endsWith(flag)) { 265 return true; 266 } 267 return (!isFile) && file.isDirectory(); 268 } 269 270 /** 271 * check path if valid. 272 * 273 * @param parseMode indicates the parse mode of parse app. 274 * @param hapName type indicates the hap name if the mode is HAP_INFO. 275 * @return isParseAppModeValid if path verify. 276 */ isParseAppModeValid(String parseMode, String hapName)277 public static boolean isParseAppModeValid(String parseMode, String hapName) { 278 if (!UncompressEntrance.PARSE_MODE_HAPLIST.equals(parseMode) && 279 !UncompressEntrance.PARSE_MODE_HAPINFO.equals(parseMode) && 280 !UncompressEntrance.PARSE_MODE_ALL.equals(parseMode)) { 281 LOG.error("uncompressVerify parse mode " + parseMode + " is invalid!"); 282 return false; 283 } 284 if (UncompressEntrance.PARSE_MODE_HAPINFO.equals(parseMode) && hapName.isEmpty()) { 285 LOG.error("uncompressVerify hapName should not empty when parse mode is hap-info!"); 286 return false; 287 } 288 return true; 289 } 290 }