From 59fdbfbdfeda6370a94ea370ecc91aa3c10a5b24 Mon Sep 17 00:00:00 2001 From: hupeixi Date: Wed, 29 Nov 2023 09:57:45 +0800 Subject: [PATCH 1/6] =?UTF-8?q?=E6=89=AB=E6=8F=8F=E5=B7=A5=E5=85=B7?= =?UTF-8?q?=E5=89=AF=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: hupeixi --- adapter/ohos/CommandParser.java | 20 ++ adapter/ohos/ScanEntrance.java | 61 +++++ adapter/ohos/ScanStatDuplicate.java | 389 ++++++++++++++++++++++++++++ adapter/ohos/ScanStatFileSize.java | 330 +++++++++++++++++++++++ adapter/ohos/ScanVerify.java | 100 +++++++ adapter/ohos/ShowHelp.java | 34 +++ adapter/ohos/Utility.java | 39 ++- 7 files changed, 970 insertions(+), 3 deletions(-) create mode 100644 adapter/ohos/ScanEntrance.java create mode 100644 adapter/ohos/ScanStatDuplicate.java create mode 100644 adapter/ohos/ScanStatFileSize.java create mode 100644 adapter/ohos/ScanVerify.java diff --git a/adapter/ohos/CommandParser.java b/adapter/ohos/CommandParser.java index 4b0f7077..9160429a 100644 --- a/adapter/ohos/CommandParser.java +++ b/adapter/ohos/CommandParser.java @@ -86,6 +86,10 @@ public class CommandParser { private static final String VERSION_CODE = "--version-code"; private static final String VERSION_NAME = "--version-name"; private static final String INPUT_LIST = "--input-list"; + private static final String INPUT = "--input"; + private static final String STAT_DUPLICATE = "--stat-duplicate"; + private static final String STAT_SUFFIX = "--stat-suffix"; + private static final String STAT_FILE_SIZE = "--stat-file-size"; private static final int PARSE_MODE_VALUE_LENGTH = 2; private static final Log LOG = new Log(CommandParser.class.toString()); private static final Map, Boolean>> commandFuncs = new HashMap<>(); @@ -295,6 +299,22 @@ public class CommandParser { entry.getKey().setInputList(entry.getValue()); return true; }); + commandFuncs.put(INPUT, entry -> { + entry.getKey().setInput(entry.getValue()); + return true; + }); + commandFuncs.put(STAT_DUPLICATE, entry -> { + entry.getKey().setStatDuplicate(entry.getValue()); + return true; + }); + commandFuncs.put(STAT_SUFFIX, entry -> { + entry.getKey().setStatSuffix(entry.getValue()); + return true; + }); + commandFuncs.put(STAT_FILE_SIZE, entry -> { + entry.getKey().setStatFileSize(entry.getValue()); + return true; + }); } diff --git a/adapter/ohos/ScanEntrance.java b/adapter/ohos/ScanEntrance.java new file mode 100644 index 00000000..8857a577 --- /dev/null +++ b/adapter/ohos/ScanEntrance.java @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2023-2023 Huawei Device Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ohos; + +/** + * scanEntrance info + * + * @since 2023/11/23 + */ + +public class ScanEntrance { + private static final int EXIT_STATUS_NORMAL = 0; + private static final int EXIT_STATUS_EXCEPTION = 1; + private static final Log LOG = new Log(ScanEntrance.class.toString()); + + + /** + * scan tool main function. + * + * @param args command line + */ + + public static void main(String[] args) { + Utility utility = new Utility(); + + if (!CommandParser.commandParser(utility, args)) { + LOG.error("ScanEntrance::main exit, parser failed"); + ShowHelp.scanHelp(); + System.exit(EXIT_STATUS_EXCEPTION); + } + + if (!ScanVerify.commandVerify(utility)) { + LOG.error("ScanEntrance::main exit, verify failed"); + ShowHelp.scanHelp(); + System.exit(EXIT_STATUS_EXCEPTION); + } + + Scan scan = new Scan(); + if (!scan.scanProcess(utility)) { + LOG.error("ScanEntrance::main exit, compress failed"); + ShowHelp.scanHelp(); + System.exit(EXIT_STATUS_EXCEPTION); + } + + System.exit(EXIT_STATUS_NORMAL); + } +} \ No newline at end of file diff --git a/adapter/ohos/ScanStatDuplicate.java b/adapter/ohos/ScanStatDuplicate.java new file mode 100644 index 00000000..84fe5e17 --- /dev/null +++ b/adapter/ohos/ScanStatDuplicate.java @@ -0,0 +1,389 @@ +/* + * Copyright (c) 2023-2023 Huawei Device Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ohos; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.serializer.SerializerFeature; + +import java.io.BufferedInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.math.BigInteger; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.List; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; + +/** + * scanDuplicate info + * + * @since 2023/11/27 + */ + +public class ScanStatDuplicate { + private static final String LINUX_FILE_SEPARATOR = "/"; + private static final String DUPLICATE_FOLDER_NAME = "duplicate"; + private static final String BACKUPS = "backups"; + private static final String EMPTY_STRING = ""; + private static final String SHA_256 = "SHA-256"; + private static final String RESULT_MD5 = "md5"; + private static final String RESULT_SIZE = "size"; + private static final String RESULT_FILES = "files"; + private static final String DUPLICATE_DESC = "find the duplicated files"; + private static final String DUPLICATE_PARAM = "--stat-duplicate"; + private static final String TASK_TYPE = "taskType"; + private static final String TASK_DESC = "taskDesc"; + private static final String PARAM = "param"; + private static final String START_TIME = "startTime"; + private static final String STOP_TIME = "stopTime"; + private static final String RESULT = "result"; + private static final String HAP = ".hap"; + private static final String HSP = ".hsp"; + private static final int BUFFER_SIZE = 10 * 1024; + private static final int MD5_BUFFER_SIZE = 1024; + private static final long ZERO = 0L; + private static final long DUPLICATE_TYPE = 1L; + private static final int MINUS_ONE = -1; + private static final int MD5_LENGTH = 16; + private static final long SHOW_SIZE = 10L; + private static final String HTML_BUTTON_SHOW = ""; + private static final String HTML_BUTTON_HIDE = ""; + private static final Log LOG = new Log(ScanStatDuplicate.class.toString()); + + private static class ParamModel { + private String md5 = EMPTY_STRING; + private long size = ZERO; + private List files = new ArrayList<>(); + + public String getMd5() { + return this.md5; + } + public void setMd5(String md5) { + this.md5 = md5; + } + public long getSize() { + return this.size; + } + public void setSize(long size) { + this.size = size; + } + public List getFiles() { + return this.files; + } + public void setFiles(List files) { + this.files = files; + } + } + + private static class DuplicateResult { + private long taskType = DUPLICATE_TYPE; + private String taskDesc = DUPLICATE_DESC; + private String param = DUPLICATE_PARAM; + private String startTime = EMPTY_STRING; + private String stopTime = EMPTY_STRING; + private List result = new ArrayList<>(); + + public long getTaskType() { + return this.taskType; + } + public void setTaskType(long taskType) { + this.taskType = taskType; + } + public String getTaskDesc() { + return this.taskDesc; + } + public void setTaskDesc(String taskDesc) { + this.taskDesc = taskDesc; + } + public String getParam() { + return this.param; + } + public void setParam(String param) { + this.param = param; + } + public String getStartTime() { + return this.startTime; + } + public void setStartTime(String startTime) { + this.startTime = startTime; + } + public String getStopTime() { + return this.stopTime; + } + public void setStopTime(String stopTime) { + this.stopTime = stopTime; + } + public List getResult() { + return this.result; + } + public void setResult(List result) { + this.result = result; + } + } + + /** + * scan statDuplicate. + * + * @param utility utility data + * @param jsonList List data + * @return duplicate html + */ + public String statDuplicate(Utility utility, List jsonList) + throws BundleException, IOException, NoSuchAlgorithmException { + DuplicateResult duplicateResult = new DuplicateResult(); + duplicateResult.setStartTime(getCurrentTime()); + String currentDir = System.getProperty("user.dir"); + String targetPath = currentDir + LINUX_FILE_SEPARATOR + DUPLICATE_FOLDER_NAME; + List fileList = getAllInputFileList(utility, targetPath); + List resList = getDuplicateResList(fileList); + File parentFile = new File(utility.getOutPath()); + if (!parentFile.exists() && !parentFile.mkdirs()) { + LOG.error("Compressor::compressHapAddition create target file parent directory failed."); + } + List filterList = new ArrayList<>(); + for (ParamModel m : resList) { + List files = m.getFiles(); + if (files.size() > 1) { + filterList.add(m); + } + } + duplicateResult.setResult(filterList); + duplicateResult.setStopTime(getCurrentTime()); + String taskTypeHtml = getHtmlRow(TASK_TYPE, duplicateResult.getTaskType()); + String taskDescHtml = getHtmlRow(TASK_DESC, duplicateResult.getTaskDesc()); + String paramHtml = getHtmlRow(PARAM, duplicateResult.getParam()); + String startTimeHtml = getHtmlRow(START_TIME, duplicateResult.getStartTime()); + String stopTimeHtml = getHtmlRow(STOP_TIME, duplicateResult.getStopTime()); + String resultValue = getResultHtml(duplicateResult.getResult()); + String resultHtml = getHtmlRowResultClass(RESULT, resultValue); + String htmlStr = "" + taskTypeHtml + taskDescHtml + paramHtml + + startTimeHtml + stopTimeHtml + resultHtml + "
"; + deleteFile(targetPath); + String jsonStr = JSON.toJSONString(duplicateResult, new SerializerFeature[]{ + SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue, + SerializerFeature.WriteDateUseDateFormat}); + jsonList.add(jsonStr); + return htmlStr; + } + + private static String getCurrentTime() { + long currentTimeMillis = System.currentTimeMillis(); + return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS").format(currentTimeMillis); + } + + private List getAllInputFileList(Utility utility, String path) throws BundleException, IOException { + ArrayList fileList = new ArrayList<>(); + unpackHap(utility.getInput(), path); + File file = new File(path); + File[] files = file.listFiles(); + if (files == null) { + LOG.error("getAllInputFileList: no file in this file path."); + return fileList; + } + String copyPath = path + LINUX_FILE_SEPARATOR + BACKUPS; + for (File f : files) { + String fName = f.getName(); + if (fName.endsWith(HSP) || fName.endsWith(HAP)) { + String absolutePath = f.getCanonicalPath(); + File destDir = new File(copyPath); + if (!destDir.exists()) { + destDir.mkdirs(); + } + String targetPath = copyPath + LINUX_FILE_SEPARATOR + fName; + File targetFile = new File(targetPath); + File sourceFile = new File(absolutePath); + FileUtils.copyFile(sourceFile, targetFile); + deleteFile(absolutePath); + String outPath = path + LINUX_FILE_SEPARATOR + fName; + File outDir = new File(outPath); + if (!outDir.exists()) { + outDir.mkdirs(); + } + unpackHap(targetPath, outPath); + } + } + deleteFile(copyPath); + FileUtils.getFileList(path, fileList); + return fileList; + } + + private static void unpackHap(String srcPath, String outPath) throws BundleException { + try (FileInputStream fis = new FileInputStream(srcPath); + ZipInputStream zipInputStream = new ZipInputStream(new BufferedInputStream(fis))) { + File destDir = new File(outPath); + if (!destDir.exists()) { + destDir.mkdirs(); + } + ZipEntry entry; + while ((entry = zipInputStream.getNextEntry()) != null) { + String entryName = entry.getName(); + File entryFile = new File(outPath, entryName); + + if (entry.isDirectory()) { + entryFile.mkdirs(); + zipInputStream.closeEntry(); + continue; + } + File parent = entryFile.getParentFile(); + if (!parent.exists()) { + parent.mkdirs(); + } + + FileOutputStream fos = new FileOutputStream(entryFile); + byte[] buffer = new byte[BUFFER_SIZE]; + int bytesRead; + while ((bytesRead = zipInputStream.read(buffer)) != MINUS_ONE) { + fos.write(buffer, 0, bytesRead); + } + fos.close(); + zipInputStream.closeEntry(); + } + } catch (IOException e) { + LOG.error("unpack hap failed IOException " + e.getMessage()); + throw new BundleException("unpack hap failed IOException " + e.getMessage()); + } + } + + public List getDuplicateResList(List fileList) throws IOException, NoSuchAlgorithmException { + List resList = new ArrayList<>(); + for (String filePath : fileList) { + boolean addFlag = true; + String md5 = md5HashCode(filePath); + for (ParamModel element : resList) { + String eleMd5 = element.getMd5(); + if (eleMd5.equals(md5)) { + List eleFiles = element.getFiles(); + eleFiles.add(cutPath(filePath, DUPLICATE_FOLDER_NAME)); + element.setFiles(eleFiles); + addFlag = false; + } + } + if (addFlag) { + ParamModel model = new ParamModel(); + long size = FileUtils.getFileSize(filePath); + model.setMd5(md5); + model.setSize(size); + List files = model.getFiles(); + files.add(cutPath(filePath, DUPLICATE_FOLDER_NAME)); + resList.add(model); + } + } + return resList; + } + + private static String getHtmlRow(String key, long valve) { + return "" + key + "" + valve + ""; + } + + private static String getHtmlRow(String key, String valve) { + return "" + key + "" + valve + ""; + } + + private static String getHtmlRowResultClass(String key, String valve) { + return "" + key + "" + valve + ""; + } + + private static String getHtmlRowResult(String key, long valve, + String trClass, String tdClassKey, String tdClassValue) { + return String.format("" + key + "" + valve + "", + trClass, tdClassKey, tdClassValue); + } + + private static String getHtmlRowResult(String key, String valve, + String trClass, String tdClassKey, String tdClassValue) { + return String.format("" + key + "" + valve + "", + trClass, tdClassKey, tdClassValue); + } + + private static String getHtmlRowResult(String key, List valve) { + StringBuilder resValve = new StringBuilder(EMPTY_STRING); + for (String ele : valve) { + resValve.insert(0, "
  • " + ele + "
  • "); + } + return "" + key + + "
      " + resValve + "
    "; + } + + private static String getResultHtml(List models) { + StringBuilder resultHtml = new StringBuilder(EMPTY_STRING); + resultHtml.append("
      "); + for (int i = 0; i < models.size(); i++) { + ParamModel model = models.get(i); + String md5Html = getHtmlRowResult(RESULT_MD5, model.getMd5(), + " class=\"duplicateLayout\"", " class=\"duplicateKey\"", " class=\"duplicateValue\""); + String sizeHtml = getHtmlRowResult(RESULT_SIZE, model.getSize(), + " class=\"duplicateLayout\"", " class=\"duplicateKey\"", " class=\"duplicateValue\""); + String filesHtml = getHtmlRowResult(RESULT_FILES, model.getFiles()); + String liHtml; + if (SHOW_SIZE > i) { + liHtml = "
    • "; + } else { + liHtml = "
    • "; + } + String modelHtml = liHtml + "" + + md5Html + sizeHtml + filesHtml + "
    • "; + resultHtml.append(modelHtml); + } + resultHtml.append("
    "); + if (models.size() > SHOW_SIZE) { + resultHtml.append(String.format(HTML_BUTTON_SHOW, DUPLICATE_FOLDER_NAME, DUPLICATE_FOLDER_NAME)); + resultHtml.append(String.format(HTML_BUTTON_HIDE, DUPLICATE_FOLDER_NAME, DUPLICATE_FOLDER_NAME)); + } + return resultHtml.toString(); + } + + private static void deleteFile(final String path) { + File file = new File(path); + if (file.exists()) { + if (file.isDirectory()) { + File[] files = file.listFiles(); + for (int i = 0; i < files.length; i++) { + deleteFile(files[i].toString()); + } + } + file.delete(); + } + } + + private String md5HashCode(String filePath) throws IOException, NoSuchAlgorithmException { + MessageDigest md = MessageDigest.getInstance(SHA_256); + InputStream fis = new FileInputStream(filePath); + byte[] buffer = new byte[MD5_BUFFER_SIZE]; + int length = MINUS_ONE; + while ((length = fis.read(buffer, 0, MD5_BUFFER_SIZE)) != MINUS_ONE) { + md.update(buffer, 0, length); + } + fis.close(); + byte[] md5Bytes = md.digest(); + BigInteger bigInt = new BigInteger(1, md5Bytes); + return bigInt.toString(MD5_LENGTH); + } + + private String cutPath(String path, String packageName) { + String[] split = path.split(packageName); + return split[1]; + } + +} diff --git a/adapter/ohos/ScanStatFileSize.java b/adapter/ohos/ScanStatFileSize.java new file mode 100644 index 00000000..c9b4cc95 --- /dev/null +++ b/adapter/ohos/ScanStatFileSize.java @@ -0,0 +1,330 @@ +/* + * Copyright (c) 2023-2023 Huawei Device Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ohos; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.serializer.SerializerFeature; + +import java.io.BufferedInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; + +/** + * scanFileSize info + * + * @since 2023/11/27 + */ + +public class ScanStatFileSize { + private static final String LINUX_FILE_SEPARATOR = "/"; + private static final String BACKUPS = "backups"; + private static final String FILE_SIZE_FOLDER_NAME = "fileSize"; + private static final String EMPTY_STRING = ""; + private static final String FILE_SIZE_DESC = "find files whose size exceed the limit size"; + private static final String FILE_SIZE_PARAM = "--stat-file-size"; + private static final String FILE_SIZE_PARAM_SEPARATOR = " "; + private static final String FILE_SIZE_PARAM_VALUE = "%s"; + private static final String FILE_SIZE_RESULT_SIZE = "size"; + private static final String FILE_SIZE_RESULT_FILE = "file"; + private static final String TASK_TYPE = "taskType"; + private static final String TASK_DESC = "taskDesc"; + private static final String PARAM = "param"; + private static final String START_TIME = "startTime"; + private static final String STOP_TIME = "stopTime"; + private static final String RESULT = "result"; + private static final String HAP = ".hap"; + private static final String HSP = ".hsp"; + private static final int BUFFER_SIZE = 10 * 1024; + private static final long ZERO = 0L; + private static final long FILE_SIZE_TYPE = 2L; + private static final int MINUS_ONE = -1; + private static final long SHOW_SIZE = 10L; + private static final String HTML_BUTTON_SHOW = ""; + private static final String HTML_BUTTON_HIDE = ""; + + private static final Log LOG = new Log(ScanStatFileSize.class.toString()); + + private static class ParamModelFileSize { + private String file = EMPTY_STRING; + private long size = ZERO; + + public long getSize() { + return this.size; + } + public void setSize(long size) { + this.size = size; + } + public String getFile() { + return file; + } + public void setFile(String file) { + this.file = file; + } + } + + private static class FileSizeResult { + private long taskType = FILE_SIZE_TYPE; + private String taskDesc = FILE_SIZE_DESC; + private String param = FILE_SIZE_PARAM + FILE_SIZE_PARAM_SEPARATOR + FILE_SIZE_PARAM_VALUE; + private String startTime = EMPTY_STRING; + private String stopTime = EMPTY_STRING; + private List result = new ArrayList<>(); + + public long getTaskType() { + return this.taskType; + } + public void setTaskType(long taskType) { + this.taskType = taskType; + } + public String getTaskDesc() { + return this.taskDesc; + } + public void setTaskDesc(String taskDesc) { + this.taskDesc = taskDesc; + } + public String getParam() { + return this.param; + } + public void setParam(String param) { + this.param = param; + } + public String getStartTime() { + return this.startTime; + } + public void setStartTime(String startTime) { + this.startTime = startTime; + } + public String getStopTime() { + return this.stopTime; + } + public void setStopTime(String stopTime) { + this.stopTime = stopTime; + } + public List getResult() { + return this.result; + } + public void setResult(List result) { + this.result = result; + } + } + + /** + * scan statFileSize. + * + * @param utility utility data + * @param jsonList List data + * @return fileSize html + */ + public String statFileSize(Utility utility, List jsonList) + throws BundleException, IOException { + FileSizeResult fileSizeResult = new FileSizeResult(); + fileSizeResult.setStartTime(getCurrentTime()); + String currentDir = System.getProperty("user.dir"); + String targetPath = currentDir + LINUX_FILE_SEPARATOR + FILE_SIZE_FOLDER_NAME; + List fileList = getAllInputFileList(utility, targetPath); + List resList = new ArrayList<>(); + for (String filePath : fileList) { + long statFileSize = Long.parseLong(utility.getStatFileSize()); + long size = FileUtils.getFileSize(filePath); + if (size > statFileSize) { + ParamModelFileSize model = new ParamModelFileSize(); + model.setFile(cutPath(filePath, FILE_SIZE_FOLDER_NAME)); + model.setSize(size); + resList.add(model); + } + } + resList.sort(Comparator.comparing(ParamModelFileSize::getSize).reversed()); + fileSizeResult.setResult(resList); + fileSizeResult.setParam(String.format(fileSizeResult.getParam(), utility.getStatFileSize())); + fileSizeResult.setStopTime(getCurrentTime()); + File parentFile = new File(utility.getOutPath()); + if (!parentFile.exists() && !parentFile.mkdirs()) { + LOG.error("Compressor::compressHapAddition create target file parent directory failed."); + } + String jsonStr = JSON.toJSONString(fileSizeResult, new SerializerFeature[]{ + SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue, + SerializerFeature.WriteDateUseDateFormat}); + String taskTypeHtml = getHtmlRow(TASK_TYPE, fileSizeResult.getTaskType()); + String taskDescHtml = getHtmlRow(TASK_DESC, fileSizeResult.getTaskDesc()); + String paramHtml = getHtmlRow(PARAM, fileSizeResult.getParam()); + String startTimeHtml = getHtmlRow(START_TIME, fileSizeResult.getStartTime()); + String stopTimeHtml = getHtmlRow(STOP_TIME, fileSizeResult.getStopTime()); + String resultValue = getResultHtmlFileSize(fileSizeResult.getResult()); + String resultHtml = getHtmlRowResultClass(RESULT, resultValue); + String htmlStr = "" + taskTypeHtml + taskDescHtml + paramHtml + + startTimeHtml + stopTimeHtml + resultHtml + "
    "; + deleteFile(targetPath); + jsonList.add(jsonStr); + return htmlStr; + } + + private List getAllInputFileList(Utility utility, String path) throws BundleException, IOException { + ArrayList fileList = new ArrayList<>(); + unpackHap(utility.getInput(), path); + File file = new File(path); + File[] files = file.listFiles(); + if (files == null) { + LOG.error("getAllInputFileList: no file in this file path."); + return fileList; + } + String copyPath = path + LINUX_FILE_SEPARATOR + BACKUPS; + for (File f : files) { + String fName = f.getName(); + if (fName.endsWith(HSP) || fName.endsWith(HAP)) { + String absolutePath = f.getCanonicalPath(); + File destDir = new File(copyPath); + if (!destDir.exists()) { + destDir.mkdirs(); + } + String targetPath = copyPath + LINUX_FILE_SEPARATOR + fName; + File targetFile = new File(targetPath); + File sourceFile = new File(absolutePath); + FileUtils.copyFile(sourceFile, targetFile); + deleteFile(absolutePath); + String outPath = path + LINUX_FILE_SEPARATOR + fName; + File outDir = new File(outPath); + if (!outDir.exists()) { + outDir.mkdirs(); + } + unpackHap(targetPath, outPath); + } + } + deleteFile(copyPath); + FileUtils.getFileList(path, fileList); + return fileList; + } + + private String cutPath(String path, String packageName) { + String[] split = path.split(packageName); + return split[1]; + } + + private static String getResultHtmlFileSize(List models) { + StringBuilder resultHtml = new StringBuilder(EMPTY_STRING); + resultHtml.append(""); + String resultFieldHtml = getHtmlRowResult(FILE_SIZE_RESULT_FILE, FILE_SIZE_RESULT_SIZE, + " class=\"fileSizeLayout\"", " class=\"fileSizeKey\"", " class=\"fileSizeValue\""); + resultHtml.append(resultFieldHtml); + for (int i = 0; i < models.size(); i++) { + ParamModelFileSize model = models.get(i); + String resultRowHtml; + if (SHOW_SIZE > i) { + resultRowHtml = getHtmlRowResult(model.getFile(), model.getSize(), + " class=\"fileSizeLayout\"", " class=\"fileSizeKey\"", " class=\"fileSizeValue\""); + } else { + resultRowHtml = getHtmlRowResult(model.getFile(), model.getSize(), + " class=\"fileSize\"", " class=\"fileSizeKey\"", " class=\"fileSizeValue\""); + } + resultHtml.append(resultRowHtml); + } + resultHtml.append("
    "); + if (models.size() > SHOW_SIZE) { + resultHtml.append(String.format(HTML_BUTTON_SHOW, FILE_SIZE_FOLDER_NAME, FILE_SIZE_FOLDER_NAME)); + resultHtml.append(String.format(HTML_BUTTON_HIDE, FILE_SIZE_FOLDER_NAME, FILE_SIZE_FOLDER_NAME)); + } + return resultHtml.toString(); + } + + private static String getHtmlRow(String key, String valve) { + return "" + key + "" + valve + ""; + } + + private static String getHtmlRow(String key, long valve) { + return "" + key + "" + valve + ""; + } + + private static String getHtmlRowResultClass(String key, String valve) { + return "" + key + "" + valve + ""; + } + + private static String getHtmlRowResult(String key, String valve, + String trClass, String tdClassKey, String tdClassValue) { + return String.format("" + key + "" + valve + "", + trClass, tdClassKey, tdClassValue); + } + + private static String getHtmlRowResult(String key, long valve, + String trClass, String tdClassKey, String tdClassValue) { + return String.format("" + key + "" + valve + "", + trClass, tdClassKey, tdClassValue); + } + + private static String getCurrentTime() { + long currentTimeMillis = System.currentTimeMillis(); + return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS").format(currentTimeMillis); + } + + private static void unpackHap(String srcPath, String outPath) throws BundleException { + try (FileInputStream fis = new FileInputStream(srcPath); + ZipInputStream zipInputStream = new ZipInputStream(new BufferedInputStream(fis))) { + File destDir = new File(outPath); + if (!destDir.exists()) { + destDir.mkdirs(); + } + ZipEntry entry; + while ((entry = zipInputStream.getNextEntry()) != null) { + String entryName = entry.getName(); + File entryFile = new File(outPath, entryName); + + if (entry.isDirectory()) { + entryFile.mkdirs(); + zipInputStream.closeEntry(); + continue; + } + File parent = entryFile.getParentFile(); + if (!parent.exists()) { + parent.mkdirs(); + } + + FileOutputStream fos = new FileOutputStream(entryFile); + byte[] buffer = new byte[BUFFER_SIZE]; + int bytesRead; + while ((bytesRead = zipInputStream.read(buffer)) != MINUS_ONE) { + fos.write(buffer, 0, bytesRead); + } + fos.close(); + zipInputStream.closeEntry(); + } + } catch (IOException e) { + LOG.error("unpack hap failed IOException " + e.getMessage()); + throw new BundleException("unpack hap failed IOException " + e.getMessage()); + } + } + + private static void deleteFile(final String path) { + File file = new File(path); + if (file.exists()) { + if (file.isDirectory()) { + File[] files = file.listFiles(); + for (int i = 0; i < files.length; i++) { + deleteFile(files[i].toString()); + } + } + file.delete(); + } + } +} diff --git a/adapter/ohos/ScanVerify.java b/adapter/ohos/ScanVerify.java new file mode 100644 index 00000000..c47a65e8 --- /dev/null +++ b/adapter/ohos/ScanVerify.java @@ -0,0 +1,100 @@ +/* + * Copyright (c) 2023-2023 Huawei Device Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ohos; + +import java.math.BigDecimal; +import java.util.regex.Pattern; + +/** + * scanVerify info + * + * @since 2023/11/23 + */ + +public class ScanVerify { + + static final String HAP = ".hap"; + static final String HSP = ".hsp"; + static final String APP = ".app"; + private static final String TRUE = "true"; + private static final String FALSE = "false"; + private static final String EMPTY_STRING = ""; + private static final Pattern PATTERN = Pattern.compile("[0-9]*"); + + private static final Log LOG = new Log(ScanVerify.class.toString()); + + /** + * if args valid. + * + * @param utility common data + * @return commandVerify if command valid. + */ + public static boolean commandVerify(Utility utility) { + if (utility == null) { + LOG.error("ScanVerify::commandVerify utility is null."); + return false; + } + + if (utility.getInput().isEmpty()) { + LOG.error("ScanVerify::commandVerify input is null."); + return false; + } + + if (!(utility.getInput().endsWith(HAP) || utility.getInput().endsWith(HSP) + || utility.getInput().endsWith(APP))) { + LOG.error("ScanVerify::commandVerify input is invalid!"); + return false; + } + + if (utility.getOutPath().isEmpty()) { + LOG.error("ScanVerify::commandVerify outPath is null."); + return false; + } + + if (!(TRUE.equals(utility.getStatDuplicate()) || FALSE.equals(utility.getStatDuplicate()))) { + LOG.error("ScanVerify::commandVerify statDuplicate is invalid! Must be true or false."); + return false; + } + + if (!utility.getStatFileSize().isEmpty()) { + if (!PATTERN.matcher(utility.getStatFileSize()).matches()) { + LOG.error("ScanVerify::commandVerify statFileSize is invalid!" + + " Must be integer in [0, Integer.MAX]"); + return false; + } + if (new BigDecimal(utility.getStatFileSize()).compareTo(BigDecimal.ZERO) < 0 + || new BigDecimal(utility.getStatFileSize()).compareTo(new BigDecimal(Integer.MAX_VALUE)) > 0) { + LOG.error("ScanVerify::commandVerify statFileSize is invalid!" + + " Must be integer in [0, Integer.MAX]"); + return false; + } + } + + if (!(TRUE.equals(utility.getStatSuffix()) || FALSE.equals(utility.getStatSuffix()))) { + LOG.error("ScanVerify::commandVerify statSuffix is invalid! Must be true or false."); + return false; + } + + if (FALSE.equals(utility.getStatSuffix()) && FALSE.equals(utility.getStatDuplicate()) + && EMPTY_STRING.equals(utility.getStatFileSize())) { + LOG.error("ScanVerify::commandVerify stat parameter is null."); + } + + + return true; + } +} diff --git a/adapter/ohos/ShowHelp.java b/adapter/ohos/ShowHelp.java index a0afce8c..5da1a16b 100644 --- a/adapter/ohos/ShowHelp.java +++ b/adapter/ohos/ShowHelp.java @@ -127,4 +127,38 @@ public interface ShowHelp { " --unpackapk default false; if true, unpack apk files from hap\n" + " in the app."); } + + /** + * content of scan command help menu. + */ + static void scanHelp() { + log.info(System.lineSeparator() + "STAT DUPLICATE USAGE:" + System.lineSeparator() + + "java -jar app_check_tool.jar --input [options] --out-path [option]" + System.lineSeparator() + + "--stat-duplicate [option]" + System.lineSeparator() + + "OPTIONS:" + System.lineSeparator() + + " --input not null must be hap or hsp or app." + System.lineSeparator() + + " --out-path not null must be folder." + System.lineSeparator() + + " --stat-duplicate not null default false; must be true or false." + System.lineSeparator() + + " if true, count duplicate files." + System.lineSeparator() + + "----------------------------------------------------------------------------------" + + System.lineSeparator() + + "STAT FILE SIZE USAGE:" + System.lineSeparator() + + "java -jar app_check_tool.jar --input [options] --out-path [option]" + System.lineSeparator() + + "--stat-file-size [option]" + System.lineSeparator() + + "OPTIONS:" + System.lineSeparator() + + " --input not null must be hap or hsp or app." + System.lineSeparator() + + " --out-path not null must be folder." + System.lineSeparator() + + " --stat-file-size not null must be an number." + System.lineSeparator() + + " count files exceeding the specified size." + System.lineSeparator() + + "----------------------------------------------------------------------------------" + + System.lineSeparator() + + "STAT SUFFIX USAGE:" + System.lineSeparator() + + "java -jar app_check_tool.jar --input [options] --out-path [option]" + System.lineSeparator() + + "--stat-suffix [option]" + System.lineSeparator() + + "OPTIONS:" + System.lineSeparator() + + " --input not null must be hap or hsp or app." + System.lineSeparator() + + " --out-path not null must be folder." + System.lineSeparator() + + " --stat-suffix not null default false; must be true or false;" + System.lineSeparator() + + " if true, calculate the proportion of various types of files."); + } } diff --git a/adapter/ohos/Utility.java b/adapter/ohos/Utility.java index 4461fe3e..dfba31d6 100644 --- a/adapter/ohos/Utility.java +++ b/adapter/ohos/Utility.java @@ -124,6 +124,10 @@ public class Utility { private String hqfList = ""; private String hspList = ""; private String inputList = ""; + private String input = ""; + private String statDuplicate = "false"; + private String statSuffix = "false"; + private String statFileSize = ""; public void setIsParse(boolean isParse) { this.isParse = isParse; @@ -270,9 +274,7 @@ public class Utility { public void setHapPath(String hapPath) { if (!hapPath.startsWith(CMD_PREFIX)) { this.hapPath = hapPath; - if (MODE_HAPADDITION.equals(this.getMode())) { - this.absoluteHapPath = getFormattedPath(hapPath); - } + this.absoluteHapPath = getFormattedPath(hapPath); } } @@ -806,4 +808,35 @@ public class Utility { public void setInputList(String inputList) { this.inputList = inputList; } + + public String getInput() { + return input; + } + + public void setInput(String input) { + this.input = getFormattedPath(input); + } + + public String getStatDuplicate() { + return statDuplicate; + } + + public void setStatDuplicate(String statDuplicate) { + this.statDuplicate = statDuplicate; + } + + public String getStatSuffix() { + return statSuffix; + } + + public void setStatSuffix(String statSuffix) { + this.statSuffix = statSuffix; + } + public String getStatFileSize() { + return statFileSize; + } + + public void setStatFileSize(String statFileSize) { + this.statFileSize = statFileSize; + } } -- Gitee From 508d67014a5c355aaec9c704ccc48b3a3a5ad9ed Mon Sep 17 00:00:00 2001 From: hupeixi Date: Wed, 29 Nov 2023 10:11:54 +0800 Subject: [PATCH 2/6] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E8=AF=B4=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: hupeixi --- adapter/ohos/ScanEntrance.java | 61 ----------------------------- adapter/ohos/ScanStatDuplicate.java | 2 +- adapter/ohos/ScanStatFileSize.java | 2 +- adapter/ohos/ScanVerify.java | 2 +- 4 files changed, 3 insertions(+), 64 deletions(-) delete mode 100644 adapter/ohos/ScanEntrance.java diff --git a/adapter/ohos/ScanEntrance.java b/adapter/ohos/ScanEntrance.java deleted file mode 100644 index 8857a577..00000000 --- a/adapter/ohos/ScanEntrance.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright (c) 2023-2023 Huawei Device Co., Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package ohos; - -/** - * scanEntrance info - * - * @since 2023/11/23 - */ - -public class ScanEntrance { - private static final int EXIT_STATUS_NORMAL = 0; - private static final int EXIT_STATUS_EXCEPTION = 1; - private static final Log LOG = new Log(ScanEntrance.class.toString()); - - - /** - * scan tool main function. - * - * @param args command line - */ - - public static void main(String[] args) { - Utility utility = new Utility(); - - if (!CommandParser.commandParser(utility, args)) { - LOG.error("ScanEntrance::main exit, parser failed"); - ShowHelp.scanHelp(); - System.exit(EXIT_STATUS_EXCEPTION); - } - - if (!ScanVerify.commandVerify(utility)) { - LOG.error("ScanEntrance::main exit, verify failed"); - ShowHelp.scanHelp(); - System.exit(EXIT_STATUS_EXCEPTION); - } - - Scan scan = new Scan(); - if (!scan.scanProcess(utility)) { - LOG.error("ScanEntrance::main exit, compress failed"); - ShowHelp.scanHelp(); - System.exit(EXIT_STATUS_EXCEPTION); - } - - System.exit(EXIT_STATUS_NORMAL); - } -} \ No newline at end of file diff --git a/adapter/ohos/ScanStatDuplicate.java b/adapter/ohos/ScanStatDuplicate.java index 84fe5e17..fb53b97f 100644 --- a/adapter/ohos/ScanStatDuplicate.java +++ b/adapter/ohos/ScanStatDuplicate.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023-2023 Huawei Device Co., Ltd. + * Copyright (c) 2023 Huawei Device Co., Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/adapter/ohos/ScanStatFileSize.java b/adapter/ohos/ScanStatFileSize.java index c9b4cc95..57b115b5 100644 --- a/adapter/ohos/ScanStatFileSize.java +++ b/adapter/ohos/ScanStatFileSize.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023-2023 Huawei Device Co., Ltd. + * Copyright (c) 2023 Huawei Device Co., Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/adapter/ohos/ScanVerify.java b/adapter/ohos/ScanVerify.java index c47a65e8..23e60adb 100644 --- a/adapter/ohos/ScanVerify.java +++ b/adapter/ohos/ScanVerify.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023-2023 Huawei Device Co., Ltd. + * Copyright (c) 2021-2023 Huawei Device Co., Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. -- Gitee From 82189b7ed8dd7dae26f3d81e54c1f96ff6cb55fe Mon Sep 17 00:00:00 2001 From: hupeixi Date: Wed, 29 Nov 2023 10:14:20 +0800 Subject: [PATCH 3/6] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=AF=B4=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: hupeixi --- adapter/ohos/ScanEntrance.java | 61 ++++ adapter/ohos/ScanStatSuffix.java | 573 +++++++++++++++++++++++++++++++ adapter/ohos/ScanVerify.java | 2 +- 3 files changed, 635 insertions(+), 1 deletion(-) create mode 100644 adapter/ohos/ScanEntrance.java create mode 100644 adapter/ohos/ScanStatSuffix.java diff --git a/adapter/ohos/ScanEntrance.java b/adapter/ohos/ScanEntrance.java new file mode 100644 index 00000000..299c2ac5 --- /dev/null +++ b/adapter/ohos/ScanEntrance.java @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ohos; + +/** + * scanEntrance info + * + * @since 2023/11/23 + */ + +public class ScanEntrance { + private static final int EXIT_STATUS_NORMAL = 0; + private static final int EXIT_STATUS_EXCEPTION = 1; + private static final Log LOG = new Log(ScanEntrance.class.toString()); + + + /** + * scan tool main function. + * + * @param args command line + */ + + public static void main(String[] args) { + Utility utility = new Utility(); + + if (!CommandParser.commandParser(utility, args)) { + LOG.error("ScanEntrance::main exit, parser failed"); + ShowHelp.scanHelp(); + System.exit(EXIT_STATUS_EXCEPTION); + } + + if (!ScanVerify.commandVerify(utility)) { + LOG.error("ScanEntrance::main exit, verify failed"); + ShowHelp.scanHelp(); + System.exit(EXIT_STATUS_EXCEPTION); + } + + Scan scan = new Scan(); + if (!scan.scanProcess(utility)) { + LOG.error("ScanEntrance::main exit, compress failed"); + ShowHelp.scanHelp(); + System.exit(EXIT_STATUS_EXCEPTION); + } + + System.exit(EXIT_STATUS_NORMAL); + } +} \ No newline at end of file diff --git a/adapter/ohos/ScanStatSuffix.java b/adapter/ohos/ScanStatSuffix.java new file mode 100644 index 00000000..aa02f3a5 --- /dev/null +++ b/adapter/ohos/ScanStatSuffix.java @@ -0,0 +1,573 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ohos; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.serializer.SerializerFeature; + +import java.io.BufferedInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Comparator; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; + +/** + * scan info + * + * @since 2023/11/27 + */ + +public class ScanStatSuffix { + private static final String TRUE = "true"; + private static final String FALSE = "false"; + private static final String LINUX_FILE_SEPARATOR = "/"; + private static final String WIN_FILE_SEPARATOR = "\\"; + private static final String WIN = "Win"; + private static final String OS_NAME = "os.name"; + private static final String BACKUPS = "backups"; + private static final String SUFFIX_FOLDER_NAME = "suffix"; + private static final String EMPTY_STRING = ""; + private static final String RESULT_SIZE = "size"; + private static final String RESULT_COMPRESS = "compress"; + private static final String RESULT_FILES = "files"; + private static final String RESULT_FILE = "file"; + private static final String RESULT_TOTAL_SIZE = "totalSize"; + private static final String SUFFIX_DESC = "show files group by file type[.suffix]"; + private static final String SUFFIX_PARAM = "--stat-suffix"; + private static final String SUFFIX_TYPE_UNKNOWN = "unknownType"; + private static final String TASK_TYPE = "taskType"; + private static final String TASK_DESC = "taskDesc"; + private static final String PARAM = "param"; + private static final String START_TIME = "startTime"; + private static final String STOP_TIME = "stopTime"; + private static final String PATH_LIST = "pathList"; + private static final String RESULT = "result"; + private static final String HAP = ".hap"; + private static final String HSP = ".hsp"; + private static final String FILE_TYPE_SO = "so"; + private static final String TMP_FOLDER_NAME = "temporary"; + private static final String LIBS_NAME = "libs"; + private static final int BUFFER_SIZE = 10 * 1024; + private static final long ZERO = 0L; + private static final long SUFFIX_TYPE = 3L; + private static final long SCAN_LEVEL = 2L; + private static final long SHOW_SIZE = 10L; + + private static final String HTML_BUTTON_SHOW = ""; + private static final String HTML_BUTTON_HIDE = ""; + + private static final Log LOG = new Log(ScanStatSuffix.class.toString()); + + private static class FileInfo { + private String file = EMPTY_STRING; + private long size = ZERO; + + public String getFile() { + return this.file; + } + public void setFile(String file) { + this.file = file; + } + public long getSize() { + return this.size; + } + public void setSize(long size) { + this.size = size; + } + } + + private static class SoFile extends FileInfo { + private String compress = FALSE; + + public String getCompress() { + return this.compress; + } + public void setCompress(String compress) { + this.compress = compress; + } + } + + private static class ParamModelSuffix { + private String suffix = EMPTY_STRING; + private long totalSize = ZERO; + private List files = new ArrayList<>(); + + public String getSuffix() { + return this.suffix; + } + public void setSuffix(String suffix) { + this.suffix = suffix; + } + public long getTotalSize() { + return this.totalSize; + } + public void setTotalSize(long totalSize) { + this.totalSize = totalSize; + } + public List getFiles() { + return this.files; + } + public void setFiles(List files) { + this.files = files; + } + } + + private static class SuffixResult { + private long taskType = SUFFIX_TYPE; + private String taskDesc = SUFFIX_DESC; + private String param = SUFFIX_PARAM; + private String startTime = EMPTY_STRING; + private String stopTime = EMPTY_STRING; + private List pathList = new ArrayList<>(); + private List result = new ArrayList<>(); + + public long getTaskType() { + return this.taskType; + } + public void setTaskType(long taskType) { + this.taskType = taskType; + } + public String getTaskDesc() { + return this.taskDesc; + } + public void setTaskDesc(String taskDesc) { + this.taskDesc = taskDesc; + } + public String getParam() { + return this.param; + } + public void setParam(String param) { + this.param = param; + } + public String getStartTime() { + return this.startTime; + } + public void setStartTime(String startTime) { + this.startTime = startTime; + } + public String getStopTime() { + return this.stopTime; + } + public void setStopTime(String stopTime) { + this.stopTime = stopTime; + } + public List getResult() { + return this.result; + } + public void setResult(List result) { + this.result = result; + } + public List getPathList() { + return this.pathList; + } + public void setPathList(List pathList) { + this.pathList = pathList; + } + } + + /** + * scan statSuffix. + * + * @param utility utility data + * @param jsonList List data + * @return suffix html + * @throws BundleException Throws this exception if unpacking exception + * @throws IOException Throws this exception if read file exception + */ + public String statSuffix(Utility utility, List jsonList) + throws BundleException, IOException { + SuffixResult suffixResult = new SuffixResult(); + suffixResult.setStartTime(getCurrentTime()); + String currentDir = System.getProperty("user.dir"); + String targetPath = currentDir + LINUX_FILE_SEPARATOR + SUFFIX_FOLDER_NAME; + String outPath = currentDir + LINUX_FILE_SEPARATOR + TMP_FOLDER_NAME; + String packageName = utility.getInput(); + unpackHap(packageName, outPath); + ArrayList soList = new ArrayList<>(); + suffixResult.setPathList(getPathListData(outPath, packageName, soList)); + List fileList = getAllInputFileList(utility, targetPath); + List resulList = setMapData(outPath, fileList, soList); + deleteFile(outPath); + deleteFile(targetPath); + resulList.sort(Comparator.comparing(ParamModelSuffix::getTotalSize).reversed()); + suffixResult.setResult(resulList); + File parentFile = new File(utility.getOutPath()); + if (!parentFile.exists() && !parentFile.mkdirs()) { + LOG.error("Compressor::compressHapAddition create target file parent directory failed."); + } + suffixResult.setStopTime(getCurrentTime()); + String htmlStr = setHtmlData(suffixResult); + String jsonStr = JSON.toJSONString(suffixResult, new SerializerFeature[] { + SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue, + SerializerFeature.WriteDateUseDateFormat}); + jsonList.add(jsonStr); + return htmlStr; + } + + private List getAllInputFileList(Utility utility, String path) throws BundleException, IOException { + ArrayList fileList = new ArrayList<>(); + unpackHap(utility.getInput(), path); + File file = new File(path); + File[] files = file.listFiles(); + if (files == null) { + LOG.error("getAllInputFileList: no file in this file path."); + return fileList; + } + String copyPath = path + LINUX_FILE_SEPARATOR + BACKUPS; + for (File fileTmp : files) { + String fName = fileTmp.getName(); + if (fName.endsWith(HSP) || fName.endsWith(HAP)) { + String tmpPath = fileTmp.getPath(); + File destDir = new File(copyPath); + if (!destDir.exists()) { + destDir.mkdirs(); + } + String targetPath = copyPath + LINUX_FILE_SEPARATOR + fName; + File targetFile = new File(targetPath); + File sourceFile = new File(tmpPath); + FileUtils.copyFile(sourceFile, targetFile); + deleteFile(tmpPath); + String outPath = path + LINUX_FILE_SEPARATOR + fName; + File outDir = new File(outPath); + if (!outDir.exists()) { + outDir.mkdirs(); + } + unpackHap(targetPath, outPath); + } + } + deleteFile(copyPath); + FileUtils.getFileList(path, fileList); + return fileList; + } + + private List getPathListData(String outPath, String packageName, ArrayList soList) { + List pathList = new ArrayList<>(); + FileUtils.getFileList(outPath, soList); + File pack = new File(packageName); + for (String file : soList) { + if (file.contains(HAP) || file.contains(HSP)) { + file = splitPath(file, TMP_FOLDER_NAME); + pathList.add(pack.getName() + file); + } + } + return pathList; + } + + private List setMapData(String outPath, List fileList, ArrayList soList) { + HashMap> sameSuffixMap = new HashMap<>(); + List resulList = new ArrayList<>(); + for (String filePath : fileList) { + sameSuffixMap = accountFileType(sameSuffixMap, filePath); + } + String osName = System.getProperty(OS_NAME); + Iterator iterator = sameSuffixMap.keySet().iterator(); + while (iterator.hasNext()) { + String next = iterator.next(); + ParamModelSuffix paramModelSuffix = new ParamModelSuffix(); + paramModelSuffix.setSuffix(next); + if (next.equalsIgnoreCase(FILE_TYPE_SO)) { + List soFiles = new ArrayList<>(); + List fileInfoList = sameSuffixMap.get(next); + long sum = ZERO; + for (FileInfo param : fileInfoList) { + SoFile soFile = new SoFile(); + soFile.setSize(param.getSize()); + String soFilePath = param.getFile(); + soFilePath = splitPath(soFilePath, SUFFIX_FOLDER_NAME); + soFile.setFile(soFilePath); + int index = getIndex(osName, soFilePath); + String hapPath = soFilePath.substring(0, index); + File hapFile = new File(hapPath); + long oldSize = getOldSize(soList, hapFile); + long newSize = getNewSize(fileInfoList, hapFile); + soFile.setCompress(oldSize < newSize ? TRUE : FALSE); + soFiles.add(soFile); + sum += param.getSize(); + } + deleteFile(outPath); + paramModelSuffix.setTotalSize(sum); + soFiles.sort(Comparator.comparing(SoFile::getSize).reversed()); + paramModelSuffix.setFiles(soFiles); + } else { + List fileInfoList = sameSuffixMap.get(next); + for (FileInfo fileInfo : fileInfoList) { + String path = fileInfo.getFile(); + fileInfo.setFile(splitPath(path, SUFFIX_FOLDER_NAME)); + } + fileInfoList.sort(Comparator.comparing(FileInfo::getSize).reversed()); + paramModelSuffix.setFiles(fileInfoList); + long sum = fileInfoList.stream().mapToLong(FileInfo::getSize).sum(); + paramModelSuffix.setTotalSize(sum); + } + resulList.add(paramModelSuffix); + } + return resulList; + } + + private String setHtmlData(SuffixResult suffixResult) { + String pathHtml = EMPTY_STRING; + if (suffixResult.getPathList() != null && !suffixResult.getPathList().isEmpty()) { + pathHtml = getPathListHtml(suffixResult.getPathList()); + } + String pathListHtml = EMPTY_STRING; + if (!pathHtml.isEmpty() || !EMPTY_STRING.equals(pathHtml)) { + pathListHtml = getHtmlRow(PATH_LIST, pathHtml); + } + String taskTypeHtml = getHtmlRow(TASK_TYPE, suffixResult.getTaskType()); + String taskDescHtml = getHtmlRow(TASK_DESC, suffixResult.getTaskDesc()); + String paramHtml = getHtmlRow(PARAM, suffixResult.getParam()); + String startTimeHtml = getHtmlRow(START_TIME, suffixResult.getStartTime()); + String stopTimeHtml = getHtmlRow(STOP_TIME, suffixResult.getStopTime()); + String resultValue = getResultHtmlOfSuffix(suffixResult.getResult()); + String resultHtml = getHtmlRowResultClass(RESULT, resultValue); + return "" + taskTypeHtml + taskDescHtml + paramHtml + + startTimeHtml + stopTimeHtml + pathListHtml + resultHtml + "
    "; + } + + private String splitPath(String path, String packageName) { + String res = EMPTY_STRING; + String[] split = path.split(packageName); + if (split.length > 0) { + res = split[1]; + } + return res; + } + + private int getIndex(String osName, String path) { + if (osName.startsWith(WIN)) { + return path.indexOf(WIN_FILE_SEPARATOR + LIBS_NAME); + } else { + return path.indexOf(LINUX_FILE_SEPARATOR + LIBS_NAME); + } + } + + private long getOldSize(List soList, File hapFile) { + long oldSize = ZERO; + for (String file : soList) { + File tmp = new File(file); + if (tmp.getName().equals(hapFile.getName())) { + oldSize = tmp.length(); + } + } + return oldSize; + } + + private long getNewSize(List fileInfoList, File hapFile) { + long newSize = ZERO; + for (FileInfo fileInfo : fileInfoList) { + if (fileInfo.getFile().contains(hapFile.getPath())) { + File tmp = new File(fileInfo.getFile()); + newSize += tmp.length(); + } + } + return newSize; + } + + private static HashMap> accountFileType(HashMap> map, String path) { + File file = new File(path); + String[] split = file.getName().split("\\."); + if (split.length == SCAN_LEVEL) { + if (map.containsKey(split[1])) { + accountType(map, file, split[1]); + } else { + accountTypeUnknown(map, file, split[1]); + } + } else if (split.length == 1) { // no suffix + if (map.containsKey(SUFFIX_TYPE_UNKNOWN)) { + accountType(map, file, SUFFIX_TYPE_UNKNOWN); + } else { + accountTypeUnknown(map, file, SUFFIX_TYPE_UNKNOWN); + } + } + return map; + } + + private static void accountType(HashMap> hashMap, File file, String key) { + FileInfo fileInfo = getFileInfo(file); + List paramModel2s = hashMap.get(key); + paramModel2s.add(fileInfo); + hashMap.put(key, paramModel2s); + } + + private static void accountTypeUnknown(HashMap> hashMap, + File file, String key) { + FileInfo fileInfo = getFileInfo(file); + List fileNew = new ArrayList<>(); + fileNew.add(fileInfo); + hashMap.put(key, fileNew); + } + + private static FileInfo getFileInfo(File file) { + FileInfo fileInfo = new FileInfo(); + fileInfo.setFile(file.getPath()); + long size = FileUtils.getFileSize(file.getPath()); + fileInfo.setSize(size); + return fileInfo; + } + + private static String getPathListHtml(List models) { + StringBuilder pathListHtml = new StringBuilder(EMPTY_STRING); + for (String strHtml : models) { + pathListHtml.append(strHtml).append("
    "); + } + return pathListHtml.toString(); + } + + private static String getResultHtmlOfSuffix(List models) { + StringBuilder resultHtml = new StringBuilder(EMPTY_STRING); + resultHtml.append("
      "); + for (int i = 0; i < models.size(); i++) { + ParamModelSuffix model = models.get(i); + String filesHtml; + if (model.getSuffix().equalsIgnoreCase(FILE_TYPE_SO)) { + filesHtml = getHtmlRowSonSo(RESULT_FILES, model.getFiles()); + } else { + filesHtml = getHtmlRowSon(RESULT_FILES, model.getFiles()); + } + String liHtml; + if (SHOW_SIZE > i) { + liHtml = "
    • "; + } else { + liHtml = "
    • "; + } + String suffixHtml = getHtmlRowResult(SUFFIX_FOLDER_NAME, model.getSuffix(), + " class=\"suffixLayout\"", " class=\"suffixKey\"", " class=\"suffixValue\""); + String totalSizeHtml = getHtmlRowResult(RESULT_TOTAL_SIZE, model.getTotalSize(), + " class=\"suffixLayout\"", " class=\"suffixKey\"", " class=\"suffixValue\""); + String modelHtml = liHtml + "" + + suffixHtml + totalSizeHtml + filesHtml + "
    • "; + resultHtml.append(modelHtml); + } + resultHtml.append("
    "); + if (models.size() > SHOW_SIZE) { + resultHtml.append(String.format(HTML_BUTTON_SHOW, SUFFIX_FOLDER_NAME, SUFFIX_FOLDER_NAME)); + resultHtml.append(String.format(HTML_BUTTON_HIDE, SUFFIX_FOLDER_NAME, SUFFIX_FOLDER_NAME)); + } + return resultHtml.toString(); + } + + private static String getHtmlRow(String key, String valve) { + return "" + key + "" + valve + ""; + } + + private static String getHtmlRow(String key, long valve) { + return "" + key + "" + valve + ""; + } + + private static String getHtmlRowResultClass(String key, String valve) { + return "" + key + "" + valve + ""; + } + + private static String getHtmlRowResult(String key, String valve, + String trClass, String tdClassKey, String tdClassValue) { + return String.format("" + key + "" + valve + "", + trClass, tdClassKey, tdClassValue); + } + + private static String getHtmlRowResult(String key, long valve, + String trClass, String tdClassKey, String tdClassValue) { + return String.format("" + key + "" + valve + "", + trClass, tdClassKey, tdClassValue); + } + + private static String getHtmlRowSon(String key, List files) { + StringBuilder resValve = new StringBuilder(EMPTY_STRING); + for (FileInfo son : files) { + resValve.insert(0, RESULT_FILE + ":" + son.getFile() + "
    "); + resValve.insert(0, RESULT_SIZE + ":" + son.getSize() + "
    "); + } + return "" + key + + "" + resValve + ""; + } + + private static String getHtmlRowSonSo(String key, List files) { + StringBuilder resValve = new StringBuilder(EMPTY_STRING); + for (SoFile so : files) { + resValve.insert(0, RESULT_FILE + ":" + so.getFile() + "
    "); + resValve.insert(0, RESULT_SIZE + ":" + so.getSize() + "
    "); + resValve.insert(0, RESULT_COMPRESS + ":" + so.getCompress() + "
    "); + } + return "" + key + + "" + resValve + ""; + } + + private static String getCurrentTime() { + long currentTimeMillis = System.currentTimeMillis(); + return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS").format(currentTimeMillis); + } + + private static void unpackHap(String srcPath, String outPath) throws BundleException { + try (FileInputStream fis = new FileInputStream(srcPath); + ZipInputStream zipInputStream = new ZipInputStream(new BufferedInputStream(fis))) { + File destDir = new File(outPath); + if (!destDir.exists()) { + destDir.mkdirs(); + } + ZipEntry entry; + while ((entry = zipInputStream.getNextEntry()) != null) { + String entryName = entry.getName(); + File entryFile = new File(outPath, entryName); + + if (entry.isDirectory()) { + entryFile.mkdirs(); + zipInputStream.closeEntry(); + continue; + } + File parent = entryFile.getParentFile(); + if (!parent.exists()) { + parent.mkdirs(); + } + + FileOutputStream fos = new FileOutputStream(entryFile); + byte[] buffer = new byte[BUFFER_SIZE]; + int bytesRead; + while ((bytesRead = zipInputStream.read(buffer)) != -1) { + fos.write(buffer, 0, bytesRead); + } + fos.close(); + zipInputStream.closeEntry(); + } + } catch (IOException e) { + LOG.error("unpack hap failed IOException " + e.getMessage()); + throw new BundleException("unpack hap failed IOException " + e.getMessage()); + } + } + + private static void deleteFile(final String path) { + File file = new File(path); + if (file.exists()) { + if (file.isDirectory()) { + File[] files = file.listFiles(); + for (int i = 0; i < files.length; i++) { + deleteFile(files[i].toString()); + } + } + file.delete(); + } + } +} diff --git a/adapter/ohos/ScanVerify.java b/adapter/ohos/ScanVerify.java index 23e60adb..6cd9e548 100644 --- a/adapter/ohos/ScanVerify.java +++ b/adapter/ohos/ScanVerify.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2023 Huawei Device Co., Ltd. + * Copyright (c) 2023 Huawei Device Co., Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. -- Gitee From f8ef35fd7cffea929c2d105332b46fe06f60e0be Mon Sep 17 00:00:00 2001 From: hupeixi Date: Wed, 29 Nov 2023 10:25:48 +0800 Subject: [PATCH 4/6] =?UTF-8?q?=E5=8E=BB=E9=99=A4=E7=AC=AC=E4=B8=80?= =?UTF-8?q?=E7=AC=94=E7=9A=84=E5=86=85=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: hupeixi --- adapter/ohos/ScanEntrance.java | 61 ---- adapter/ohos/ScanStatSuffix.java | 573 ------------------------------- 2 files changed, 634 deletions(-) delete mode 100644 adapter/ohos/ScanEntrance.java delete mode 100644 adapter/ohos/ScanStatSuffix.java diff --git a/adapter/ohos/ScanEntrance.java b/adapter/ohos/ScanEntrance.java deleted file mode 100644 index 299c2ac5..00000000 --- a/adapter/ohos/ScanEntrance.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright (c) 2023 Huawei Device Co., Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package ohos; - -/** - * scanEntrance info - * - * @since 2023/11/23 - */ - -public class ScanEntrance { - private static final int EXIT_STATUS_NORMAL = 0; - private static final int EXIT_STATUS_EXCEPTION = 1; - private static final Log LOG = new Log(ScanEntrance.class.toString()); - - - /** - * scan tool main function. - * - * @param args command line - */ - - public static void main(String[] args) { - Utility utility = new Utility(); - - if (!CommandParser.commandParser(utility, args)) { - LOG.error("ScanEntrance::main exit, parser failed"); - ShowHelp.scanHelp(); - System.exit(EXIT_STATUS_EXCEPTION); - } - - if (!ScanVerify.commandVerify(utility)) { - LOG.error("ScanEntrance::main exit, verify failed"); - ShowHelp.scanHelp(); - System.exit(EXIT_STATUS_EXCEPTION); - } - - Scan scan = new Scan(); - if (!scan.scanProcess(utility)) { - LOG.error("ScanEntrance::main exit, compress failed"); - ShowHelp.scanHelp(); - System.exit(EXIT_STATUS_EXCEPTION); - } - - System.exit(EXIT_STATUS_NORMAL); - } -} \ No newline at end of file diff --git a/adapter/ohos/ScanStatSuffix.java b/adapter/ohos/ScanStatSuffix.java deleted file mode 100644 index aa02f3a5..00000000 --- a/adapter/ohos/ScanStatSuffix.java +++ /dev/null @@ -1,573 +0,0 @@ -/* - * Copyright (c) 2023 Huawei Device Co., Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package ohos; - -import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.serializer.SerializerFeature; - -import java.io.BufferedInputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Comparator; -import java.util.zip.ZipEntry; -import java.util.zip.ZipInputStream; - -/** - * scan info - * - * @since 2023/11/27 - */ - -public class ScanStatSuffix { - private static final String TRUE = "true"; - private static final String FALSE = "false"; - private static final String LINUX_FILE_SEPARATOR = "/"; - private static final String WIN_FILE_SEPARATOR = "\\"; - private static final String WIN = "Win"; - private static final String OS_NAME = "os.name"; - private static final String BACKUPS = "backups"; - private static final String SUFFIX_FOLDER_NAME = "suffix"; - private static final String EMPTY_STRING = ""; - private static final String RESULT_SIZE = "size"; - private static final String RESULT_COMPRESS = "compress"; - private static final String RESULT_FILES = "files"; - private static final String RESULT_FILE = "file"; - private static final String RESULT_TOTAL_SIZE = "totalSize"; - private static final String SUFFIX_DESC = "show files group by file type[.suffix]"; - private static final String SUFFIX_PARAM = "--stat-suffix"; - private static final String SUFFIX_TYPE_UNKNOWN = "unknownType"; - private static final String TASK_TYPE = "taskType"; - private static final String TASK_DESC = "taskDesc"; - private static final String PARAM = "param"; - private static final String START_TIME = "startTime"; - private static final String STOP_TIME = "stopTime"; - private static final String PATH_LIST = "pathList"; - private static final String RESULT = "result"; - private static final String HAP = ".hap"; - private static final String HSP = ".hsp"; - private static final String FILE_TYPE_SO = "so"; - private static final String TMP_FOLDER_NAME = "temporary"; - private static final String LIBS_NAME = "libs"; - private static final int BUFFER_SIZE = 10 * 1024; - private static final long ZERO = 0L; - private static final long SUFFIX_TYPE = 3L; - private static final long SCAN_LEVEL = 2L; - private static final long SHOW_SIZE = 10L; - - private static final String HTML_BUTTON_SHOW = ""; - private static final String HTML_BUTTON_HIDE = ""; - - private static final Log LOG = new Log(ScanStatSuffix.class.toString()); - - private static class FileInfo { - private String file = EMPTY_STRING; - private long size = ZERO; - - public String getFile() { - return this.file; - } - public void setFile(String file) { - this.file = file; - } - public long getSize() { - return this.size; - } - public void setSize(long size) { - this.size = size; - } - } - - private static class SoFile extends FileInfo { - private String compress = FALSE; - - public String getCompress() { - return this.compress; - } - public void setCompress(String compress) { - this.compress = compress; - } - } - - private static class ParamModelSuffix { - private String suffix = EMPTY_STRING; - private long totalSize = ZERO; - private List files = new ArrayList<>(); - - public String getSuffix() { - return this.suffix; - } - public void setSuffix(String suffix) { - this.suffix = suffix; - } - public long getTotalSize() { - return this.totalSize; - } - public void setTotalSize(long totalSize) { - this.totalSize = totalSize; - } - public List getFiles() { - return this.files; - } - public void setFiles(List files) { - this.files = files; - } - } - - private static class SuffixResult { - private long taskType = SUFFIX_TYPE; - private String taskDesc = SUFFIX_DESC; - private String param = SUFFIX_PARAM; - private String startTime = EMPTY_STRING; - private String stopTime = EMPTY_STRING; - private List pathList = new ArrayList<>(); - private List result = new ArrayList<>(); - - public long getTaskType() { - return this.taskType; - } - public void setTaskType(long taskType) { - this.taskType = taskType; - } - public String getTaskDesc() { - return this.taskDesc; - } - public void setTaskDesc(String taskDesc) { - this.taskDesc = taskDesc; - } - public String getParam() { - return this.param; - } - public void setParam(String param) { - this.param = param; - } - public String getStartTime() { - return this.startTime; - } - public void setStartTime(String startTime) { - this.startTime = startTime; - } - public String getStopTime() { - return this.stopTime; - } - public void setStopTime(String stopTime) { - this.stopTime = stopTime; - } - public List getResult() { - return this.result; - } - public void setResult(List result) { - this.result = result; - } - public List getPathList() { - return this.pathList; - } - public void setPathList(List pathList) { - this.pathList = pathList; - } - } - - /** - * scan statSuffix. - * - * @param utility utility data - * @param jsonList List data - * @return suffix html - * @throws BundleException Throws this exception if unpacking exception - * @throws IOException Throws this exception if read file exception - */ - public String statSuffix(Utility utility, List jsonList) - throws BundleException, IOException { - SuffixResult suffixResult = new SuffixResult(); - suffixResult.setStartTime(getCurrentTime()); - String currentDir = System.getProperty("user.dir"); - String targetPath = currentDir + LINUX_FILE_SEPARATOR + SUFFIX_FOLDER_NAME; - String outPath = currentDir + LINUX_FILE_SEPARATOR + TMP_FOLDER_NAME; - String packageName = utility.getInput(); - unpackHap(packageName, outPath); - ArrayList soList = new ArrayList<>(); - suffixResult.setPathList(getPathListData(outPath, packageName, soList)); - List fileList = getAllInputFileList(utility, targetPath); - List resulList = setMapData(outPath, fileList, soList); - deleteFile(outPath); - deleteFile(targetPath); - resulList.sort(Comparator.comparing(ParamModelSuffix::getTotalSize).reversed()); - suffixResult.setResult(resulList); - File parentFile = new File(utility.getOutPath()); - if (!parentFile.exists() && !parentFile.mkdirs()) { - LOG.error("Compressor::compressHapAddition create target file parent directory failed."); - } - suffixResult.setStopTime(getCurrentTime()); - String htmlStr = setHtmlData(suffixResult); - String jsonStr = JSON.toJSONString(suffixResult, new SerializerFeature[] { - SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue, - SerializerFeature.WriteDateUseDateFormat}); - jsonList.add(jsonStr); - return htmlStr; - } - - private List getAllInputFileList(Utility utility, String path) throws BundleException, IOException { - ArrayList fileList = new ArrayList<>(); - unpackHap(utility.getInput(), path); - File file = new File(path); - File[] files = file.listFiles(); - if (files == null) { - LOG.error("getAllInputFileList: no file in this file path."); - return fileList; - } - String copyPath = path + LINUX_FILE_SEPARATOR + BACKUPS; - for (File fileTmp : files) { - String fName = fileTmp.getName(); - if (fName.endsWith(HSP) || fName.endsWith(HAP)) { - String tmpPath = fileTmp.getPath(); - File destDir = new File(copyPath); - if (!destDir.exists()) { - destDir.mkdirs(); - } - String targetPath = copyPath + LINUX_FILE_SEPARATOR + fName; - File targetFile = new File(targetPath); - File sourceFile = new File(tmpPath); - FileUtils.copyFile(sourceFile, targetFile); - deleteFile(tmpPath); - String outPath = path + LINUX_FILE_SEPARATOR + fName; - File outDir = new File(outPath); - if (!outDir.exists()) { - outDir.mkdirs(); - } - unpackHap(targetPath, outPath); - } - } - deleteFile(copyPath); - FileUtils.getFileList(path, fileList); - return fileList; - } - - private List getPathListData(String outPath, String packageName, ArrayList soList) { - List pathList = new ArrayList<>(); - FileUtils.getFileList(outPath, soList); - File pack = new File(packageName); - for (String file : soList) { - if (file.contains(HAP) || file.contains(HSP)) { - file = splitPath(file, TMP_FOLDER_NAME); - pathList.add(pack.getName() + file); - } - } - return pathList; - } - - private List setMapData(String outPath, List fileList, ArrayList soList) { - HashMap> sameSuffixMap = new HashMap<>(); - List resulList = new ArrayList<>(); - for (String filePath : fileList) { - sameSuffixMap = accountFileType(sameSuffixMap, filePath); - } - String osName = System.getProperty(OS_NAME); - Iterator iterator = sameSuffixMap.keySet().iterator(); - while (iterator.hasNext()) { - String next = iterator.next(); - ParamModelSuffix paramModelSuffix = new ParamModelSuffix(); - paramModelSuffix.setSuffix(next); - if (next.equalsIgnoreCase(FILE_TYPE_SO)) { - List soFiles = new ArrayList<>(); - List fileInfoList = sameSuffixMap.get(next); - long sum = ZERO; - for (FileInfo param : fileInfoList) { - SoFile soFile = new SoFile(); - soFile.setSize(param.getSize()); - String soFilePath = param.getFile(); - soFilePath = splitPath(soFilePath, SUFFIX_FOLDER_NAME); - soFile.setFile(soFilePath); - int index = getIndex(osName, soFilePath); - String hapPath = soFilePath.substring(0, index); - File hapFile = new File(hapPath); - long oldSize = getOldSize(soList, hapFile); - long newSize = getNewSize(fileInfoList, hapFile); - soFile.setCompress(oldSize < newSize ? TRUE : FALSE); - soFiles.add(soFile); - sum += param.getSize(); - } - deleteFile(outPath); - paramModelSuffix.setTotalSize(sum); - soFiles.sort(Comparator.comparing(SoFile::getSize).reversed()); - paramModelSuffix.setFiles(soFiles); - } else { - List fileInfoList = sameSuffixMap.get(next); - for (FileInfo fileInfo : fileInfoList) { - String path = fileInfo.getFile(); - fileInfo.setFile(splitPath(path, SUFFIX_FOLDER_NAME)); - } - fileInfoList.sort(Comparator.comparing(FileInfo::getSize).reversed()); - paramModelSuffix.setFiles(fileInfoList); - long sum = fileInfoList.stream().mapToLong(FileInfo::getSize).sum(); - paramModelSuffix.setTotalSize(sum); - } - resulList.add(paramModelSuffix); - } - return resulList; - } - - private String setHtmlData(SuffixResult suffixResult) { - String pathHtml = EMPTY_STRING; - if (suffixResult.getPathList() != null && !suffixResult.getPathList().isEmpty()) { - pathHtml = getPathListHtml(suffixResult.getPathList()); - } - String pathListHtml = EMPTY_STRING; - if (!pathHtml.isEmpty() || !EMPTY_STRING.equals(pathHtml)) { - pathListHtml = getHtmlRow(PATH_LIST, pathHtml); - } - String taskTypeHtml = getHtmlRow(TASK_TYPE, suffixResult.getTaskType()); - String taskDescHtml = getHtmlRow(TASK_DESC, suffixResult.getTaskDesc()); - String paramHtml = getHtmlRow(PARAM, suffixResult.getParam()); - String startTimeHtml = getHtmlRow(START_TIME, suffixResult.getStartTime()); - String stopTimeHtml = getHtmlRow(STOP_TIME, suffixResult.getStopTime()); - String resultValue = getResultHtmlOfSuffix(suffixResult.getResult()); - String resultHtml = getHtmlRowResultClass(RESULT, resultValue); - return "" + taskTypeHtml + taskDescHtml + paramHtml - + startTimeHtml + stopTimeHtml + pathListHtml + resultHtml + "
    "; - } - - private String splitPath(String path, String packageName) { - String res = EMPTY_STRING; - String[] split = path.split(packageName); - if (split.length > 0) { - res = split[1]; - } - return res; - } - - private int getIndex(String osName, String path) { - if (osName.startsWith(WIN)) { - return path.indexOf(WIN_FILE_SEPARATOR + LIBS_NAME); - } else { - return path.indexOf(LINUX_FILE_SEPARATOR + LIBS_NAME); - } - } - - private long getOldSize(List soList, File hapFile) { - long oldSize = ZERO; - for (String file : soList) { - File tmp = new File(file); - if (tmp.getName().equals(hapFile.getName())) { - oldSize = tmp.length(); - } - } - return oldSize; - } - - private long getNewSize(List fileInfoList, File hapFile) { - long newSize = ZERO; - for (FileInfo fileInfo : fileInfoList) { - if (fileInfo.getFile().contains(hapFile.getPath())) { - File tmp = new File(fileInfo.getFile()); - newSize += tmp.length(); - } - } - return newSize; - } - - private static HashMap> accountFileType(HashMap> map, String path) { - File file = new File(path); - String[] split = file.getName().split("\\."); - if (split.length == SCAN_LEVEL) { - if (map.containsKey(split[1])) { - accountType(map, file, split[1]); - } else { - accountTypeUnknown(map, file, split[1]); - } - } else if (split.length == 1) { // no suffix - if (map.containsKey(SUFFIX_TYPE_UNKNOWN)) { - accountType(map, file, SUFFIX_TYPE_UNKNOWN); - } else { - accountTypeUnknown(map, file, SUFFIX_TYPE_UNKNOWN); - } - } - return map; - } - - private static void accountType(HashMap> hashMap, File file, String key) { - FileInfo fileInfo = getFileInfo(file); - List paramModel2s = hashMap.get(key); - paramModel2s.add(fileInfo); - hashMap.put(key, paramModel2s); - } - - private static void accountTypeUnknown(HashMap> hashMap, - File file, String key) { - FileInfo fileInfo = getFileInfo(file); - List fileNew = new ArrayList<>(); - fileNew.add(fileInfo); - hashMap.put(key, fileNew); - } - - private static FileInfo getFileInfo(File file) { - FileInfo fileInfo = new FileInfo(); - fileInfo.setFile(file.getPath()); - long size = FileUtils.getFileSize(file.getPath()); - fileInfo.setSize(size); - return fileInfo; - } - - private static String getPathListHtml(List models) { - StringBuilder pathListHtml = new StringBuilder(EMPTY_STRING); - for (String strHtml : models) { - pathListHtml.append(strHtml).append("
    "); - } - return pathListHtml.toString(); - } - - private static String getResultHtmlOfSuffix(List models) { - StringBuilder resultHtml = new StringBuilder(EMPTY_STRING); - resultHtml.append("
      "); - for (int i = 0; i < models.size(); i++) { - ParamModelSuffix model = models.get(i); - String filesHtml; - if (model.getSuffix().equalsIgnoreCase(FILE_TYPE_SO)) { - filesHtml = getHtmlRowSonSo(RESULT_FILES, model.getFiles()); - } else { - filesHtml = getHtmlRowSon(RESULT_FILES, model.getFiles()); - } - String liHtml; - if (SHOW_SIZE > i) { - liHtml = "
    • "; - } else { - liHtml = "
    • "; - } - String suffixHtml = getHtmlRowResult(SUFFIX_FOLDER_NAME, model.getSuffix(), - " class=\"suffixLayout\"", " class=\"suffixKey\"", " class=\"suffixValue\""); - String totalSizeHtml = getHtmlRowResult(RESULT_TOTAL_SIZE, model.getTotalSize(), - " class=\"suffixLayout\"", " class=\"suffixKey\"", " class=\"suffixValue\""); - String modelHtml = liHtml + "" - + suffixHtml + totalSizeHtml + filesHtml + "
    • "; - resultHtml.append(modelHtml); - } - resultHtml.append("
    "); - if (models.size() > SHOW_SIZE) { - resultHtml.append(String.format(HTML_BUTTON_SHOW, SUFFIX_FOLDER_NAME, SUFFIX_FOLDER_NAME)); - resultHtml.append(String.format(HTML_BUTTON_HIDE, SUFFIX_FOLDER_NAME, SUFFIX_FOLDER_NAME)); - } - return resultHtml.toString(); - } - - private static String getHtmlRow(String key, String valve) { - return "" + key + "" + valve + ""; - } - - private static String getHtmlRow(String key, long valve) { - return "" + key + "" + valve + ""; - } - - private static String getHtmlRowResultClass(String key, String valve) { - return "" + key + "" + valve + ""; - } - - private static String getHtmlRowResult(String key, String valve, - String trClass, String tdClassKey, String tdClassValue) { - return String.format("" + key + "" + valve + "", - trClass, tdClassKey, tdClassValue); - } - - private static String getHtmlRowResult(String key, long valve, - String trClass, String tdClassKey, String tdClassValue) { - return String.format("" + key + "" + valve + "", - trClass, tdClassKey, tdClassValue); - } - - private static String getHtmlRowSon(String key, List files) { - StringBuilder resValve = new StringBuilder(EMPTY_STRING); - for (FileInfo son : files) { - resValve.insert(0, RESULT_FILE + ":" + son.getFile() + "
    "); - resValve.insert(0, RESULT_SIZE + ":" + son.getSize() + "
    "); - } - return "" + key - + "" + resValve + ""; - } - - private static String getHtmlRowSonSo(String key, List files) { - StringBuilder resValve = new StringBuilder(EMPTY_STRING); - for (SoFile so : files) { - resValve.insert(0, RESULT_FILE + ":" + so.getFile() + "
    "); - resValve.insert(0, RESULT_SIZE + ":" + so.getSize() + "
    "); - resValve.insert(0, RESULT_COMPRESS + ":" + so.getCompress() + "
    "); - } - return "" + key - + "" + resValve + ""; - } - - private static String getCurrentTime() { - long currentTimeMillis = System.currentTimeMillis(); - return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS").format(currentTimeMillis); - } - - private static void unpackHap(String srcPath, String outPath) throws BundleException { - try (FileInputStream fis = new FileInputStream(srcPath); - ZipInputStream zipInputStream = new ZipInputStream(new BufferedInputStream(fis))) { - File destDir = new File(outPath); - if (!destDir.exists()) { - destDir.mkdirs(); - } - ZipEntry entry; - while ((entry = zipInputStream.getNextEntry()) != null) { - String entryName = entry.getName(); - File entryFile = new File(outPath, entryName); - - if (entry.isDirectory()) { - entryFile.mkdirs(); - zipInputStream.closeEntry(); - continue; - } - File parent = entryFile.getParentFile(); - if (!parent.exists()) { - parent.mkdirs(); - } - - FileOutputStream fos = new FileOutputStream(entryFile); - byte[] buffer = new byte[BUFFER_SIZE]; - int bytesRead; - while ((bytesRead = zipInputStream.read(buffer)) != -1) { - fos.write(buffer, 0, bytesRead); - } - fos.close(); - zipInputStream.closeEntry(); - } - } catch (IOException e) { - LOG.error("unpack hap failed IOException " + e.getMessage()); - throw new BundleException("unpack hap failed IOException " + e.getMessage()); - } - } - - private static void deleteFile(final String path) { - File file = new File(path); - if (file.exists()) { - if (file.isDirectory()) { - File[] files = file.listFiles(); - for (int i = 0; i < files.length; i++) { - deleteFile(files[i].toString()); - } - } - file.delete(); - } - } -} -- Gitee From c84506b6082f5f2039875dd251015a2bb6115f89 Mon Sep 17 00:00:00 2001 From: hupeixi Date: Wed, 29 Nov 2023 15:52:17 +0800 Subject: [PATCH 5/6] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=89=AB=E6=8F=8F?= =?UTF-8?q?=E5=86=85=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: hupeixi --- adapter/ohos/ScanStatDuplicate.java | 115 +++------------------------- adapter/ohos/ScanStatFileSize.java | 115 ++-------------------------- 2 files changed, 18 insertions(+), 212 deletions(-) diff --git a/adapter/ohos/ScanStatDuplicate.java b/adapter/ohos/ScanStatDuplicate.java index fb53b97f..f0a8f134 100644 --- a/adapter/ohos/ScanStatDuplicate.java +++ b/adapter/ohos/ScanStatDuplicate.java @@ -19,10 +19,8 @@ package ohos; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.serializer.SerializerFeature; -import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; -import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.math.BigInteger; @@ -31,8 +29,6 @@ import java.security.NoSuchAlgorithmException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.List; -import java.util.zip.ZipEntry; -import java.util.zip.ZipInputStream; /** * scanDuplicate info @@ -41,9 +37,8 @@ import java.util.zip.ZipInputStream; */ public class ScanStatDuplicate { - private static final String LINUX_FILE_SEPARATOR = "/"; + private static final String UNPACK_NAME = "unpack"; private static final String DUPLICATE_FOLDER_NAME = "duplicate"; - private static final String BACKUPS = "backups"; private static final String EMPTY_STRING = ""; private static final String SHA_256 = "SHA-256"; private static final String RESULT_MD5 = "md5"; @@ -57,9 +52,6 @@ public class ScanStatDuplicate { private static final String START_TIME = "startTime"; private static final String STOP_TIME = "stopTime"; private static final String RESULT = "result"; - private static final String HAP = ".hap"; - private static final String HSP = ".hsp"; - private static final int BUFFER_SIZE = 10 * 1024; private static final int MD5_BUFFER_SIZE = 1024; private static final long ZERO = 0L; private static final long DUPLICATE_TYPE = 1L; @@ -150,13 +142,10 @@ public class ScanStatDuplicate { * @param jsonList List data * @return duplicate html */ - public String statDuplicate(Utility utility, List jsonList) - throws BundleException, IOException, NoSuchAlgorithmException { + public String statDuplicate(Utility utility, List jsonList, List fileList) + throws IOException, NoSuchAlgorithmException { DuplicateResult duplicateResult = new DuplicateResult(); duplicateResult.setStartTime(getCurrentTime()); - String currentDir = System.getProperty("user.dir"); - String targetPath = currentDir + LINUX_FILE_SEPARATOR + DUPLICATE_FOLDER_NAME; - List fileList = getAllInputFileList(utility, targetPath); List resList = getDuplicateResList(fileList); File parentFile = new File(utility.getOutPath()); if (!parentFile.exists() && !parentFile.mkdirs()) { @@ -180,7 +169,6 @@ public class ScanStatDuplicate { String resultHtml = getHtmlRowResultClass(RESULT, resultValue); String htmlStr = "" + taskTypeHtml + taskDescHtml + paramHtml + startTimeHtml + stopTimeHtml + resultHtml + "
    "; - deleteFile(targetPath); String jsonStr = JSON.toJSONString(duplicateResult, new SerializerFeature[]{ SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteDateUseDateFormat}); @@ -193,79 +181,6 @@ public class ScanStatDuplicate { return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS").format(currentTimeMillis); } - private List getAllInputFileList(Utility utility, String path) throws BundleException, IOException { - ArrayList fileList = new ArrayList<>(); - unpackHap(utility.getInput(), path); - File file = new File(path); - File[] files = file.listFiles(); - if (files == null) { - LOG.error("getAllInputFileList: no file in this file path."); - return fileList; - } - String copyPath = path + LINUX_FILE_SEPARATOR + BACKUPS; - for (File f : files) { - String fName = f.getName(); - if (fName.endsWith(HSP) || fName.endsWith(HAP)) { - String absolutePath = f.getCanonicalPath(); - File destDir = new File(copyPath); - if (!destDir.exists()) { - destDir.mkdirs(); - } - String targetPath = copyPath + LINUX_FILE_SEPARATOR + fName; - File targetFile = new File(targetPath); - File sourceFile = new File(absolutePath); - FileUtils.copyFile(sourceFile, targetFile); - deleteFile(absolutePath); - String outPath = path + LINUX_FILE_SEPARATOR + fName; - File outDir = new File(outPath); - if (!outDir.exists()) { - outDir.mkdirs(); - } - unpackHap(targetPath, outPath); - } - } - deleteFile(copyPath); - FileUtils.getFileList(path, fileList); - return fileList; - } - - private static void unpackHap(String srcPath, String outPath) throws BundleException { - try (FileInputStream fis = new FileInputStream(srcPath); - ZipInputStream zipInputStream = new ZipInputStream(new BufferedInputStream(fis))) { - File destDir = new File(outPath); - if (!destDir.exists()) { - destDir.mkdirs(); - } - ZipEntry entry; - while ((entry = zipInputStream.getNextEntry()) != null) { - String entryName = entry.getName(); - File entryFile = new File(outPath, entryName); - - if (entry.isDirectory()) { - entryFile.mkdirs(); - zipInputStream.closeEntry(); - continue; - } - File parent = entryFile.getParentFile(); - if (!parent.exists()) { - parent.mkdirs(); - } - - FileOutputStream fos = new FileOutputStream(entryFile); - byte[] buffer = new byte[BUFFER_SIZE]; - int bytesRead; - while ((bytesRead = zipInputStream.read(buffer)) != MINUS_ONE) { - fos.write(buffer, 0, bytesRead); - } - fos.close(); - zipInputStream.closeEntry(); - } - } catch (IOException e) { - LOG.error("unpack hap failed IOException " + e.getMessage()); - throw new BundleException("unpack hap failed IOException " + e.getMessage()); - } - } - public List getDuplicateResList(List fileList) throws IOException, NoSuchAlgorithmException { List resList = new ArrayList<>(); for (String filePath : fileList) { @@ -275,7 +190,7 @@ public class ScanStatDuplicate { String eleMd5 = element.getMd5(); if (eleMd5.equals(md5)) { List eleFiles = element.getFiles(); - eleFiles.add(cutPath(filePath, DUPLICATE_FOLDER_NAME)); + eleFiles.add(cutPath(filePath, UNPACK_NAME)); element.setFiles(eleFiles); addFlag = false; } @@ -286,7 +201,7 @@ public class ScanStatDuplicate { model.setMd5(md5); model.setSize(size); List files = model.getFiles(); - files.add(cutPath(filePath, DUPLICATE_FOLDER_NAME)); + files.add(cutPath(filePath, UNPACK_NAME)); resList.add(model); } } @@ -354,19 +269,6 @@ public class ScanStatDuplicate { return resultHtml.toString(); } - private static void deleteFile(final String path) { - File file = new File(path); - if (file.exists()) { - if (file.isDirectory()) { - File[] files = file.listFiles(); - for (int i = 0; i < files.length; i++) { - deleteFile(files[i].toString()); - } - } - file.delete(); - } - } - private String md5HashCode(String filePath) throws IOException, NoSuchAlgorithmException { MessageDigest md = MessageDigest.getInstance(SHA_256); InputStream fis = new FileInputStream(filePath); @@ -382,8 +284,11 @@ public class ScanStatDuplicate { } private String cutPath(String path, String packageName) { + String res = EMPTY_STRING; String[] split = path.split(packageName); - return split[1]; + if (split.length > 0) { + res = split[1]; + } + return res; } - } diff --git a/adapter/ohos/ScanStatFileSize.java b/adapter/ohos/ScanStatFileSize.java index 57b115b5..a042bf20 100644 --- a/adapter/ohos/ScanStatFileSize.java +++ b/adapter/ohos/ScanStatFileSize.java @@ -19,17 +19,11 @@ package ohos; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.serializer.SerializerFeature; -import java.io.BufferedInputStream; import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Comparator; import java.util.List; -import java.util.zip.ZipEntry; -import java.util.zip.ZipInputStream; /** * scanFileSize info @@ -38,8 +32,6 @@ import java.util.zip.ZipInputStream; */ public class ScanStatFileSize { - private static final String LINUX_FILE_SEPARATOR = "/"; - private static final String BACKUPS = "backups"; private static final String FILE_SIZE_FOLDER_NAME = "fileSize"; private static final String EMPTY_STRING = ""; private static final String FILE_SIZE_DESC = "find files whose size exceed the limit size"; @@ -54,18 +46,14 @@ public class ScanStatFileSize { private static final String START_TIME = "startTime"; private static final String STOP_TIME = "stopTime"; private static final String RESULT = "result"; - private static final String HAP = ".hap"; - private static final String HSP = ".hsp"; - private static final int BUFFER_SIZE = 10 * 1024; private static final long ZERO = 0L; private static final long FILE_SIZE_TYPE = 2L; - private static final int MINUS_ONE = -1; private static final long SHOW_SIZE = 10L; private static final String HTML_BUTTON_SHOW = ""; private static final String HTML_BUTTON_HIDE = ""; - + private static final String UNPACK_NAME = "unpack"; private static final Log LOG = new Log(ScanStatFileSize.class.toString()); private static class ParamModelFileSize { @@ -139,20 +127,16 @@ public class ScanStatFileSize { * @param jsonList List data * @return fileSize html */ - public String statFileSize(Utility utility, List jsonList) - throws BundleException, IOException { + public String statFileSize(Utility utility, List jsonList, List fileList) { FileSizeResult fileSizeResult = new FileSizeResult(); fileSizeResult.setStartTime(getCurrentTime()); - String currentDir = System.getProperty("user.dir"); - String targetPath = currentDir + LINUX_FILE_SEPARATOR + FILE_SIZE_FOLDER_NAME; - List fileList = getAllInputFileList(utility, targetPath); List resList = new ArrayList<>(); for (String filePath : fileList) { long statFileSize = Long.parseLong(utility.getStatFileSize()); long size = FileUtils.getFileSize(filePath); if (size > statFileSize) { ParamModelFileSize model = new ParamModelFileSize(); - model.setFile(cutPath(filePath, FILE_SIZE_FOLDER_NAME)); + model.setFile(cutPath(filePath, UNPACK_NAME)); model.setSize(size); resList.add(model); } @@ -177,50 +161,17 @@ public class ScanStatFileSize { String resultHtml = getHtmlRowResultClass(RESULT, resultValue); String htmlStr = "" + taskTypeHtml + taskDescHtml + paramHtml + startTimeHtml + stopTimeHtml + resultHtml + "
    "; - deleteFile(targetPath); jsonList.add(jsonStr); return htmlStr; } - private List getAllInputFileList(Utility utility, String path) throws BundleException, IOException { - ArrayList fileList = new ArrayList<>(); - unpackHap(utility.getInput(), path); - File file = new File(path); - File[] files = file.listFiles(); - if (files == null) { - LOG.error("getAllInputFileList: no file in this file path."); - return fileList; - } - String copyPath = path + LINUX_FILE_SEPARATOR + BACKUPS; - for (File f : files) { - String fName = f.getName(); - if (fName.endsWith(HSP) || fName.endsWith(HAP)) { - String absolutePath = f.getCanonicalPath(); - File destDir = new File(copyPath); - if (!destDir.exists()) { - destDir.mkdirs(); - } - String targetPath = copyPath + LINUX_FILE_SEPARATOR + fName; - File targetFile = new File(targetPath); - File sourceFile = new File(absolutePath); - FileUtils.copyFile(sourceFile, targetFile); - deleteFile(absolutePath); - String outPath = path + LINUX_FILE_SEPARATOR + fName; - File outDir = new File(outPath); - if (!outDir.exists()) { - outDir.mkdirs(); - } - unpackHap(targetPath, outPath); - } - } - deleteFile(copyPath); - FileUtils.getFileList(path, fileList); - return fileList; - } - private String cutPath(String path, String packageName) { + String res = EMPTY_STRING; String[] split = path.split(packageName); - return split[1]; + if (split.length > 0) { + res = split[1]; + } + return res; } private static String getResultHtmlFileSize(List models) { @@ -277,54 +228,4 @@ public class ScanStatFileSize { long currentTimeMillis = System.currentTimeMillis(); return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS").format(currentTimeMillis); } - - private static void unpackHap(String srcPath, String outPath) throws BundleException { - try (FileInputStream fis = new FileInputStream(srcPath); - ZipInputStream zipInputStream = new ZipInputStream(new BufferedInputStream(fis))) { - File destDir = new File(outPath); - if (!destDir.exists()) { - destDir.mkdirs(); - } - ZipEntry entry; - while ((entry = zipInputStream.getNextEntry()) != null) { - String entryName = entry.getName(); - File entryFile = new File(outPath, entryName); - - if (entry.isDirectory()) { - entryFile.mkdirs(); - zipInputStream.closeEntry(); - continue; - } - File parent = entryFile.getParentFile(); - if (!parent.exists()) { - parent.mkdirs(); - } - - FileOutputStream fos = new FileOutputStream(entryFile); - byte[] buffer = new byte[BUFFER_SIZE]; - int bytesRead; - while ((bytesRead = zipInputStream.read(buffer)) != MINUS_ONE) { - fos.write(buffer, 0, bytesRead); - } - fos.close(); - zipInputStream.closeEntry(); - } - } catch (IOException e) { - LOG.error("unpack hap failed IOException " + e.getMessage()); - throw new BundleException("unpack hap failed IOException " + e.getMessage()); - } - } - - private static void deleteFile(final String path) { - File file = new File(path); - if (file.exists()) { - if (file.isDirectory()) { - File[] files = file.listFiles(); - for (int i = 0; i < files.length; i++) { - deleteFile(files[i].toString()); - } - } - file.delete(); - } - } } -- Gitee From 2821a199cff6215528917d7a937cb0c1def93586 Mon Sep 17 00:00:00 2001 From: hupeixi Date: Thu, 30 Nov 2023 21:21:48 +0800 Subject: [PATCH 6/6] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: hupeixi --- adapter/ohos/ScanEntrance.java | 61 +++++++++++++++++++++++++++ adapter/ohos/ScanStatDuplicate.java | 64 ++++++++++++++++++++--------- adapter/ohos/ScanStatFileSize.java | 46 +++++++++++++++------ 3 files changed, 138 insertions(+), 33 deletions(-) create mode 100644 adapter/ohos/ScanEntrance.java diff --git a/adapter/ohos/ScanEntrance.java b/adapter/ohos/ScanEntrance.java new file mode 100644 index 00000000..299c2ac5 --- /dev/null +++ b/adapter/ohos/ScanEntrance.java @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ohos; + +/** + * scanEntrance info + * + * @since 2023/11/23 + */ + +public class ScanEntrance { + private static final int EXIT_STATUS_NORMAL = 0; + private static final int EXIT_STATUS_EXCEPTION = 1; + private static final Log LOG = new Log(ScanEntrance.class.toString()); + + + /** + * scan tool main function. + * + * @param args command line + */ + + public static void main(String[] args) { + Utility utility = new Utility(); + + if (!CommandParser.commandParser(utility, args)) { + LOG.error("ScanEntrance::main exit, parser failed"); + ShowHelp.scanHelp(); + System.exit(EXIT_STATUS_EXCEPTION); + } + + if (!ScanVerify.commandVerify(utility)) { + LOG.error("ScanEntrance::main exit, verify failed"); + ShowHelp.scanHelp(); + System.exit(EXIT_STATUS_EXCEPTION); + } + + Scan scan = new Scan(); + if (!scan.scanProcess(utility)) { + LOG.error("ScanEntrance::main exit, compress failed"); + ShowHelp.scanHelp(); + System.exit(EXIT_STATUS_EXCEPTION); + } + + System.exit(EXIT_STATUS_NORMAL); + } +} \ No newline at end of file diff --git a/adapter/ohos/ScanStatDuplicate.java b/adapter/ohos/ScanStatDuplicate.java index f0a8f134..0f2637b5 100644 --- a/adapter/ohos/ScanStatDuplicate.java +++ b/adapter/ohos/ScanStatDuplicate.java @@ -52,6 +52,7 @@ public class ScanStatDuplicate { private static final String START_TIME = "startTime"; private static final String STOP_TIME = "stopTime"; private static final String RESULT = "result"; + private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss:SSS"; private static final int MD5_BUFFER_SIZE = 1024; private static final long ZERO = 0L; private static final long DUPLICATE_TYPE = 1L; @@ -62,6 +63,30 @@ public class ScanStatDuplicate { + "onclick=\"show_%s()\" style=\"display: block\">more"; private static final String HTML_BUTTON_HIDE = ""; + private static final String MSG_CREATE_FAILED = + "Compressor::compressHapAddition create target file parent directory failed."; + private static final String HTML_TABLE_BOX = ""; + private static final String HTML_TABLE_END = "
    "; + private static final String HTML_TRTD_LAYOUT = ""; + private static final String HTML_TRTD_VALUE = ""; + private static final String HTML_TRTD_END = ""; + private static final String HTML_TRTD_RESULT = ""; + private static final String HTML_TRTD_S = ""; + private static final String HTML_TRTD_END_S = ""; + private static final String HTML_LI_CIRCLE = "
  • "; + private static final String HTML_LI_END = "
  • "; + private static final String HTML_TRTD_DUPLICATEKEY = ""; + private static final String HTML_TD_DUPLICATEVALUE = "
      "; + private static final String HTML_ULTDTR_END = "
    "; + private static final String HTML_UL_HEAD = "
      "; + private static final String CLASS_DUPLICATE_LAYOUT = " class=\"duplicateLayout\""; + private static final String CLASS_DUPLICATE_KEY = " class=\"duplicateKey\""; + private static final String CLASS_DUPLICATE_VALUE = " class=\"duplicateValue\""; + private static final String HTML_LI_HEAD = "
    • "; + private static final String HTML_LI_DUPLICATE = "
    • "; + private static final String HTML_TABLE_DUPLICATE = ""; + private static final String HTML_TABLELI_END = "
    • "; + private static final String HTML_UL_END = "
    "; private static final Log LOG = new Log(ScanStatDuplicate.class.toString()); private static class ParamModel { @@ -149,7 +174,7 @@ public class ScanStatDuplicate { List resList = getDuplicateResList(fileList); File parentFile = new File(utility.getOutPath()); if (!parentFile.exists() && !parentFile.mkdirs()) { - LOG.error("Compressor::compressHapAddition create target file parent directory failed."); + LOG.error(MSG_CREATE_FAILED); } List filterList = new ArrayList<>(); for (ParamModel m : resList) { @@ -167,8 +192,8 @@ public class ScanStatDuplicate { String stopTimeHtml = getHtmlRow(STOP_TIME, duplicateResult.getStopTime()); String resultValue = getResultHtml(duplicateResult.getResult()); String resultHtml = getHtmlRowResultClass(RESULT, resultValue); - String htmlStr = "" + taskTypeHtml + taskDescHtml + paramHtml - + startTimeHtml + stopTimeHtml + resultHtml + "
    "; + String htmlStr = HTML_TABLE_BOX + taskTypeHtml + taskDescHtml + paramHtml + + startTimeHtml + stopTimeHtml + resultHtml + HTML_TABLE_END; String jsonStr = JSON.toJSONString(duplicateResult, new SerializerFeature[]{ SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteDateUseDateFormat}); @@ -178,7 +203,7 @@ public class ScanStatDuplicate { private static String getCurrentTime() { long currentTimeMillis = System.currentTimeMillis(); - return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS").format(currentTimeMillis); + return new SimpleDateFormat(DATE_FORMAT).format(currentTimeMillis); } public List getDuplicateResList(List fileList) throws IOException, NoSuchAlgorithmException { @@ -209,59 +234,58 @@ public class ScanStatDuplicate { } private static String getHtmlRow(String key, long valve) { - return "" + key + "" + valve + ""; + return HTML_TRTD_LAYOUT + key + HTML_TRTD_VALUE + valve + HTML_TRTD_END; } private static String getHtmlRow(String key, String valve) { - return "" + key + "" + valve + ""; + return HTML_TRTD_LAYOUT + key + HTML_TRTD_VALUE + valve + HTML_TRTD_END; } private static String getHtmlRowResultClass(String key, String valve) { - return "" + key + "" + valve + ""; + return HTML_TRTD_RESULT + key + HTML_TRTD_VALUE + valve + HTML_TRTD_END; } private static String getHtmlRowResult(String key, long valve, String trClass, String tdClassKey, String tdClassValue) { - return String.format("" + key + "" + valve + "", + return String.format(HTML_TRTD_S + key + HTML_TRTD_END_S + valve + HTML_TRTD_END, trClass, tdClassKey, tdClassValue); } private static String getHtmlRowResult(String key, String valve, String trClass, String tdClassKey, String tdClassValue) { - return String.format("" + key + "" + valve + "", + return String.format(HTML_TRTD_S + key + HTML_TRTD_END_S + valve + HTML_TRTD_END, trClass, tdClassKey, tdClassValue); } private static String getHtmlRowResult(String key, List valve) { StringBuilder resValve = new StringBuilder(EMPTY_STRING); for (String ele : valve) { - resValve.insert(0, "
  • " + ele + "
  • "); + resValve.insert(0, HTML_LI_CIRCLE + ele + HTML_LI_END); } - return "" + key - + "
      " + resValve + "
    "; + return HTML_TRTD_DUPLICATEKEY + key + HTML_TD_DUPLICATEVALUE + resValve + HTML_ULTDTR_END; } private static String getResultHtml(List models) { StringBuilder resultHtml = new StringBuilder(EMPTY_STRING); - resultHtml.append("
      "); + resultHtml.append(HTML_UL_HEAD); for (int i = 0; i < models.size(); i++) { ParamModel model = models.get(i); String md5Html = getHtmlRowResult(RESULT_MD5, model.getMd5(), - " class=\"duplicateLayout\"", " class=\"duplicateKey\"", " class=\"duplicateValue\""); + CLASS_DUPLICATE_LAYOUT, CLASS_DUPLICATE_KEY, CLASS_DUPLICATE_VALUE); String sizeHtml = getHtmlRowResult(RESULT_SIZE, model.getSize(), - " class=\"duplicateLayout\"", " class=\"duplicateKey\"", " class=\"duplicateValue\""); + CLASS_DUPLICATE_LAYOUT, CLASS_DUPLICATE_KEY, CLASS_DUPLICATE_VALUE); String filesHtml = getHtmlRowResult(RESULT_FILES, model.getFiles()); String liHtml; if (SHOW_SIZE > i) { - liHtml = "
    • "; + liHtml = HTML_LI_HEAD; } else { - liHtml = "
    • "; + liHtml = HTML_LI_DUPLICATE; } - String modelHtml = liHtml + "" - + md5Html + sizeHtml + filesHtml + "
    • "; + String modelHtml = liHtml + HTML_TABLE_DUPLICATE + + md5Html + sizeHtml + filesHtml + HTML_TABLELI_END; resultHtml.append(modelHtml); } - resultHtml.append("
    "); + resultHtml.append(HTML_UL_END); if (models.size() > SHOW_SIZE) { resultHtml.append(String.format(HTML_BUTTON_SHOW, DUPLICATE_FOLDER_NAME, DUPLICATE_FOLDER_NAME)); resultHtml.append(String.format(HTML_BUTTON_HIDE, DUPLICATE_FOLDER_NAME, DUPLICATE_FOLDER_NAME)); diff --git a/adapter/ohos/ScanStatFileSize.java b/adapter/ohos/ScanStatFileSize.java index a042bf20..8be8d7b9 100644 --- a/adapter/ohos/ScanStatFileSize.java +++ b/adapter/ohos/ScanStatFileSize.java @@ -54,6 +54,22 @@ public class ScanStatFileSize { private static final String HTML_BUTTON_HIDE = ""; private static final String UNPACK_NAME = "unpack"; + private static final String HTML_TABLE = ""; + private static final String HTML_TABLE_END = ""; + private static final String CLASS_BOX_TABLE = " class=\"boxTable\""; + private static final String CLASS_FILE_SIZE_TABLE = " class=\"fileSizeTable\""; + private static final String CLASS_FILE_SIZE_LAYOUT = " class=\"fileSizeLayout\""; + private static final String CLASS_FILE_SIZE_KEY = " class=\"fileSizeKey\""; + private static final String CLASS_FILE_SIZE_VALUE = " class=\"fileSizeValue\""; + private static final String CLASS_FILE_SIZE = " class=\"fileSize\""; + private static final String HTML_TR = ""; + private static final String HTML_TR_END = ""; + private static final String HTML_TD = ""; + private static final String HTML_TD_END = ""; + private static final String CLASS_LAYOUT = " class=\"layout\""; + private static final String CLASS_KEY = " class=\"key\""; + private static final String CLASS_VALUE = " class=\"value\""; + private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss:SSS"; private static final Log LOG = new Log(ScanStatFileSize.class.toString()); private static class ParamModelFileSize { @@ -125,6 +141,7 @@ public class ScanStatFileSize { * * @param utility utility data * @param jsonList List data + * @param fileList List data * @return fileSize html */ public String statFileSize(Utility utility, List jsonList, List fileList) { @@ -159,8 +176,8 @@ public class ScanStatFileSize { String stopTimeHtml = getHtmlRow(STOP_TIME, fileSizeResult.getStopTime()); String resultValue = getResultHtmlFileSize(fileSizeResult.getResult()); String resultHtml = getHtmlRowResultClass(RESULT, resultValue); - String htmlStr = "" + taskTypeHtml + taskDescHtml + paramHtml - + startTimeHtml + stopTimeHtml + resultHtml + "
    "; + String htmlStr = String.format(HTML_TABLE, CLASS_BOX_TABLE) + taskTypeHtml + taskDescHtml + paramHtml + + startTimeHtml + stopTimeHtml + resultHtml + HTML_TABLE_END; jsonList.add(jsonStr); return htmlStr; } @@ -176,23 +193,23 @@ public class ScanStatFileSize { private static String getResultHtmlFileSize(List models) { StringBuilder resultHtml = new StringBuilder(EMPTY_STRING); - resultHtml.append(""); + resultHtml.append(String.format(HTML_TABLE, CLASS_FILE_SIZE_TABLE)); String resultFieldHtml = getHtmlRowResult(FILE_SIZE_RESULT_FILE, FILE_SIZE_RESULT_SIZE, - " class=\"fileSizeLayout\"", " class=\"fileSizeKey\"", " class=\"fileSizeValue\""); + CLASS_FILE_SIZE_LAYOUT, CLASS_FILE_SIZE_KEY, CLASS_FILE_SIZE_VALUE); resultHtml.append(resultFieldHtml); for (int i = 0; i < models.size(); i++) { ParamModelFileSize model = models.get(i); String resultRowHtml; if (SHOW_SIZE > i) { resultRowHtml = getHtmlRowResult(model.getFile(), model.getSize(), - " class=\"fileSizeLayout\"", " class=\"fileSizeKey\"", " class=\"fileSizeValue\""); + CLASS_FILE_SIZE_LAYOUT, CLASS_FILE_SIZE_KEY, CLASS_FILE_SIZE_VALUE); } else { resultRowHtml = getHtmlRowResult(model.getFile(), model.getSize(), - " class=\"fileSize\"", " class=\"fileSizeKey\"", " class=\"fileSizeValue\""); + CLASS_FILE_SIZE, CLASS_FILE_SIZE_KEY, CLASS_FILE_SIZE_VALUE); } resultHtml.append(resultRowHtml); } - resultHtml.append("
    "); + resultHtml.append(HTML_TABLE_END); if (models.size() > SHOW_SIZE) { resultHtml.append(String.format(HTML_BUTTON_SHOW, FILE_SIZE_FOLDER_NAME, FILE_SIZE_FOLDER_NAME)); resultHtml.append(String.format(HTML_BUTTON_HIDE, FILE_SIZE_FOLDER_NAME, FILE_SIZE_FOLDER_NAME)); @@ -201,31 +218,34 @@ public class ScanStatFileSize { } private static String getHtmlRow(String key, String valve) { - return "" + key + "" + valve + ""; + return String.format(HTML_TR, CLASS_LAYOUT) + String.format(HTML_TD, CLASS_KEY) + key + HTML_TD_END + + String.format(HTML_TD, CLASS_VALUE) + valve + HTML_TD_END + HTML_TR_END; } private static String getHtmlRow(String key, long valve) { - return "" + key + "" + valve + ""; + return String.format(HTML_TR, CLASS_LAYOUT) + String.format(HTML_TD, CLASS_KEY) + key + HTML_TD_END + + String.format(HTML_TD, CLASS_VALUE) + valve + HTML_TD_END + HTML_TR_END; } private static String getHtmlRowResultClass(String key, String valve) { - return "" + key + "" + valve + ""; + return String.format(HTML_TR, CLASS_LAYOUT) + String.format(HTML_TD, CLASS_KEY) + key + HTML_TD_END + + String.format(HTML_TD, CLASS_VALUE) + valve + HTML_TD_END + HTML_TR_END; } private static String getHtmlRowResult(String key, String valve, String trClass, String tdClassKey, String tdClassValue) { - return String.format("" + key + "" + valve + "", + return String.format(HTML_TR + HTML_TD + key + HTML_TD_END + HTML_TD + valve + HTML_TD_END + HTML_TR_END, trClass, tdClassKey, tdClassValue); } private static String getHtmlRowResult(String key, long valve, String trClass, String tdClassKey, String tdClassValue) { - return String.format("" + key + "" + valve + "", + return String.format(HTML_TR + HTML_TD + key + HTML_TD_END + HTML_TD + valve + HTML_TD_END + HTML_TR_END, trClass, tdClassKey, tdClassValue); } private static String getCurrentTime() { long currentTimeMillis = System.currentTimeMillis(); - return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS").format(currentTimeMillis); + return new SimpleDateFormat(DATE_FORMAT).format(currentTimeMillis); } } -- Gitee