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.io.InputStream; 20 import java.util.Locale; 21 22 /** 23 * uncompress command verify. 24 * 25 */ 26 public class UncompressVerify { 27 private static final String HAP_SUFFIX = ".hap"; 28 private static final String HAR_SUFFIX = ".har"; 29 private static final String APP_SUFFIX = ".app"; 30 private static final Log LOG = new Log(UncompressVerify.class.toString()); 31 32 /** 33 * is args valid. 34 * 35 * @param utility common data 36 * @return commandVerify if verify valid. 37 */ commandVerify(Utility utility)38 public static boolean commandVerify(Utility utility) { 39 if (!utility.getForceRewrite().isEmpty() && !"true".equals(utility.getForceRewrite()) 40 && !"false".equals(utility.getForceRewrite())) { 41 LOG.error("UncompressVerify::isVerifyVaild forceRewrite is invalid!"); 42 return false; 43 } 44 45 if (!utility.getParseMode().isEmpty() && !UncompressEntrance.PARSE_MODE_HAPLIST.equals(utility.getParseMode()) 46 && !UncompressEntrance.PARSE_MODE_HAPINFO.equals(utility.getParseMode()) 47 && !UncompressEntrance.PARSE_MODE_ALL.equals(utility.getParseMode())) { 48 LOG.error("UncompressVerify::isVerifyVaild parseMode is invalid!"); 49 return false; 50 } 51 52 if (Utility.MODE_HAP.equals(utility.getMode())) { 53 return hapCommandVerify(utility); 54 } else if (Utility.MODE_HAR.equals(utility.getMode())) { 55 return harCommandVerify(utility); 56 } else if (Utility.MODE_APP.equals(utility.getMode())) { 57 return appCommandVerify(utility); 58 } else { 59 LOG.error("UncompressVerify::commandVerify mode is invalid!"); 60 return false; 61 } 62 } 63 64 /** 65 * is args valid. 66 * 67 * @param utility common data 68 * @param input the InputStream about the app package. 69 * @return commandVerify if verify valid. 70 */ inputParseVerify(Utility utility, InputStream input)71 public static boolean inputParseVerify(Utility utility, InputStream input) { 72 if (input == null) { 73 LOG.error("UncompressVerify::isInputVerifyVaild input is invalid!"); 74 return false; 75 } 76 77 if (!utility.getForceRewrite().isEmpty() && !"true".equals(utility.getForceRewrite()) 78 && !"false".equals(utility.getForceRewrite())) { 79 LOG.error("UncompressVerify::isInputVerifyVaild forceRewrite is invalid!"); 80 return false; 81 } 82 83 if (utility.getParseMode().isEmpty() || (!UncompressEntrance.PARSE_MODE_HAPLIST.equals(utility.getParseMode()) 84 && !UncompressEntrance.PARSE_MODE_HAPINFO.equals(utility.getParseMode())) 85 && !UncompressEntrance.PARSE_MODE_ALL.equals(utility.getParseMode())) { 86 LOG.error("UncompressVerify::isInputVerifyVaild parseMode is invalid!"); 87 return false; 88 } 89 90 if (Utility.MODE_HAP.equals(utility.getMode())) { 91 return true; 92 } else if (Utility.MODE_HAR.equals(utility.getMode())) { 93 return true; 94 } else if (Utility.MODE_APP.equals(utility.getMode())) { 95 if (UncompressEntrance.PARSE_MODE_HAPINFO.equals(utility.getParseMode()) 96 && utility.getHapName().isEmpty()) { 97 LOG.error("UncompressVerify::isArgsVaildInParseMode hapName can't be empty!"); 98 return false; 99 } 100 101 if (!utility.getUnpackApk().isEmpty() && !"true".equals(utility.getUnpackApk()) 102 && !"false".equals(utility.getUnpackApk())) { 103 LOG.error("UncompressVerify::isVerifyVaild unpackapk is invalid!"); 104 return false; 105 } 106 107 return true; 108 } else { 109 LOG.error("UncompressVerify::InputcommandVerify mode is invalid!"); 110 return false; 111 } 112 } 113 114 /** 115 * parse and check args if valid in hap mode. 116 * 117 * @param utility common data 118 * @return isVerifyValidInHapMode if verify valid in hap mode. 119 */ hapCommandVerify(Utility utility)120 private static boolean hapCommandVerify(Utility utility) { 121 utility.setHapPath(utility.getFormattedPath(utility.getHapPath())); 122 File file = new File(utility.getHapPath()); 123 if (!file.isFile() || !file.getName().toLowerCase(Locale.ENGLISH).endsWith(HAP_SUFFIX)) { 124 LOG.error("UncompressVerify::isArgsValidInHapMode hap-path must end with.hap!"); 125 return false; 126 } 127 128 return verifyOutPath(utility, file); 129 } 130 131 /** 132 * parse and check args if valid in har mode. 133 * 134 * @param utility common data 135 * @return isVerifyValidInHapMode if verify valid in hap mode. 136 */ harCommandVerify(Utility utility)137 private static boolean harCommandVerify(Utility utility) { 138 utility.setHarPath(utility.getFormattedPath(utility.getHarPath())); 139 File file = new File(utility.getHarPath()); 140 if (!file.isFile() || !file.getName().toLowerCase(Locale.ENGLISH).endsWith(HAR_SUFFIX)) { 141 LOG.error("UncompressVerify::isArgsValidInHarMode har-path must end with.har!"); 142 return false; 143 } 144 145 if (utility.getOutPath().isEmpty()) { 146 LOG.error("UncompressVerify::isVerifyVaild outPath is invalid!"); 147 return false; 148 } 149 150 return verifyOutPath(utility, file); 151 } 152 153 /** 154 * parse and check args if valid in app mode. 155 * 156 * @param utility common data 157 * @return isVerifyValidInHapMode if verify valid in hap mode. 158 */ appCommandVerify(Utility utility)159 private static boolean appCommandVerify(Utility utility) { 160 if (UncompressEntrance.PARSE_MODE_HAPINFO.equals(utility.getParseMode()) 161 && utility.getHapName().isEmpty()) { 162 LOG.error("UncompressVerify::isArgsVaildInParseMode hapName can't be empty!"); 163 return false; 164 } 165 166 utility.setAppPath(utility.getFormattedPath(utility.getAppPath())); 167 File file = new File(utility.getAppPath()); 168 if (!file.isFile() || !file.getName().toLowerCase(Locale.ENGLISH).endsWith(APP_SUFFIX)) { 169 LOG.error("UncompressVerify::isArgsValidInAppMode app-path must end with.app!"); 170 return false; 171 } 172 173 return verifyOutPath(utility, file); 174 } 175 176 177 /** 178 * parse and check the outpath args. 179 * 180 * @param utility common data 181 * @param file file to be verified 182 * @return isVerifyValidInHapMode if verify valid in hap mode. 183 */ verifyOutPath(Utility utility, File file)184 private static boolean verifyOutPath(Utility utility, File file) { 185 if (utility.getOutPath().isEmpty() && utility.getParseMode().isEmpty()) { 186 String tempStr = file.getName(); 187 File parentFile = file.getParentFile(); 188 if (parentFile == null) { 189 utility.setOutPath(tempStr.substring(0, tempStr.lastIndexOf("."))); 190 } else { 191 utility.setOutPath(parentFile.toString() + File.separator 192 + tempStr.substring(0, tempStr.lastIndexOf("."))); 193 } 194 } 195 196 File outFile = new File(utility.getOutPath()); 197 if (("false".equals(utility.getForceRewrite())) && (outFile.exists())) { 198 LOG.error("UncompressVerify::isArgsValidInHapMode out file already existed!"); 199 return false; 200 } 201 return true; 202 } 203 204 /** 205 * check path if valid 206 * 207 * @param path path input 208 * @param isFile type input 209 * @param flag flag input 210 * @return isPathValid if path verify 211 */ isPathValid(String path, boolean isFile, String flag)212 public static boolean isPathValid(String path, boolean isFile, String flag) { 213 File file = new File(path); 214 if (isFile && (file.isFile()) && file.getName().toLowerCase(Locale.ENGLISH).endsWith(flag)) { 215 return true; 216 } 217 return (!isFile) && file.isDirectory(); 218 } 219 }