From 97c4dbf47c4f7b0eab0e50f93d32c9b23674817c Mon Sep 17 00:00:00 2001 From: kunge-hub Date: Fri, 17 Nov 2023 11:31:43 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E5=85=BC=E5=AE=B9=E4=B8=8D=E5=90=88?= =?UTF-8?q?=E6=B3=95=E6=96=87=E4=BB=B6=E6=83=85=E5=86=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: kunge-hub --- adapter/ohos/Compressor.java | 111 +++++++++++++++++++++++++++++++++-- 1 file changed, 105 insertions(+), 6 deletions(-) diff --git a/adapter/ohos/Compressor.java b/adapter/ohos/Compressor.java index d1aaaa6c..d690bdb3 100644 --- a/adapter/ohos/Compressor.java +++ b/adapter/ohos/Compressor.java @@ -33,6 +33,7 @@ import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.nio.file.StandardCopyOption; import java.nio.file.attribute.FileTime; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; @@ -1135,16 +1136,16 @@ public class Compressor { LOG.error("Compressor::hapAddition create out file parent directory failed."); } } + boolean exceptionFlag = false; FileOutputStream fileOut = null; CheckedOutputStream checkedOut = null; + String backName = BACKUP_PREFIX + hapFileName; try { - String backName = BACKUP_PREFIX + hapFileName; copyHapFile(utility, backName); fileOut = new FileOutputStream(destFile); checkedOut = new CheckedOutputStream(fileOut, new CRC32()); zipOut = new ZipOutputStream(checkedOut); - compressHapAddition(utility); - deleteFile(backName); + exceptionFlag = compressHapAddition(utility); } catch (FileNotFoundException exception) { LOG.error("Compressor::HapAddition hapFile not found exception" + exception.getMessage()); } catch (BundleException ignored) { @@ -1156,6 +1157,29 @@ public class Compressor { Utility.closeStream(zipOut); Utility.closeStream(checkedOut); Utility.closeStream(fileOut); + if (exceptionFlag) { + ArrayList files = new ArrayList<>(); + FileUtils.getFileList(utility.getOutPath(), files); + if (files.size() > 1) { + String delPath = utility.getOutPath() + LINUX_FILE_SEPARATOR + hapFileName; + deleteFile(delPath); + String currentDir = System.getProperty("user.dir"); + String backupPath = currentDir + LINUX_FILE_SEPARATOR + backName; + File oldHapParentFile = hapPath.getParentFile(); + String oldHapPath = oldHapParentFile.getAbsolutePath() + LINUX_FILE_SEPARATOR + hapFileName; + Path oldFilePath = Paths.get(backupPath); + Path newFilePath = Paths.get(oldHapPath); + try { + Files.move(oldFilePath, newFilePath, StandardCopyOption.REPLACE_EXISTING); + } catch (IOException e) { + LOG.error("Compressor::HapAddition FilesMove IO exception."); + } + } else { + String delPath = utility.getOutPath(); + deleteFile(delPath); + } + } + deleteFile(backName); } } @@ -1239,7 +1263,8 @@ public class Compressor { }); } - private void compressHapAddition(Utility utility) throws BundleException, IOException { + private boolean compressHapAddition(Utility utility) throws BundleException, IOException { + boolean exceptionFlag = false; // decompression hap file to hapAddition String currentDir = System.getProperty("user.dir"); String hapAdditionPath = currentDir + LINUX_FILE_SEPARATOR + HAPADDITION_FOLDER_NAME; @@ -1273,9 +1298,9 @@ public class Compressor { throw new BundleException(errMsg); } if (utility.getHapPath().endsWith(HAP_SUFFIX)) { - compressHap(utility); + exceptionFlag = compressHapAdditionHap(utility, hapAdditionPath); } else if (utility.getHapPath().endsWith(HSP_SUFFIX)) { - compressHsp(utility); + exceptionFlag = compressHapAdditionHsp(utility, hapAdditionPath); } else { String errMsg = "Compressor::compressHapAddition compressFile failed."; LOG.error(errMsg); @@ -1283,6 +1308,80 @@ public class Compressor { } // delete packaging and unpacking process files deleteFile(hapAdditionPath); + return exceptionFlag; + } + + private boolean compressHapAdditionHap(Utility utility, String hapAdditionPath) throws BundleException { + boolean exceptionFlag = false; + if (utility.getJsonPath().isEmpty() && !utility.getBinPath().isEmpty()) { + // only for slim device + compressHapMode(utility); + return exceptionFlag; + } + setGenerateBuildHash(utility); + if (isModuleJSON(utility.getJsonPath())) { + if (!checkStageHap(utility)) { + LOG.error("checkStageHap failed."); + deleteFile(hapAdditionPath); + exceptionFlag = true; + return exceptionFlag; + } + Optional optional = FileUtils.getFileContent(utility.getJsonPath()); + String jsonString = optional.get(); + String moduleType = ModuleJsonUtil.parseModuleType(jsonString); + if (TYPE_SHARED.equals(moduleType)) { + LOG.warning("Compress mode is hap, but module type is shared."); + } + String bundleType = ModuleJsonUtil.parseStageBundleType(jsonString); + if (TYPE_SHARED.equals(bundleType)) { + LOG.warning("Compress mode is hap, but app type is shared."); + } + compressHapModeForModule(utility); + buildHash(utility); + } else { + if (!checkFAHap(utility)) { + LOG.error("checkFAHap failed."); + deleteFile(hapAdditionPath); + exceptionFlag = true; + return exceptionFlag; + } + compressHapMode(utility); + buildHash(utility); + } + return exceptionFlag; + } + + private boolean compressHapAdditionHsp(Utility utility, String hapAdditionPath) throws BundleException { + boolean exceptionFlag = false; + setGenerateBuildHash(utility); + if (isModuleJSON(utility.getJsonPath())) { + Optional optional = FileUtils.getFileContent(utility.getJsonPath()); + String jsonString = optional.get(); + if (!checkStageAtomicService(jsonString)) { + LOG.error("checkStageAtomicService failed."); + deleteFile(hapAdditionPath); + exceptionFlag = true; + return exceptionFlag; + } + // check whether is an overlay hsp or not + if (!checkStageOverlayCfg(jsonString)) { + LOG.error("checkStageOverlayCfg failed."); + deleteFile(hapAdditionPath); + exceptionFlag = true; + return exceptionFlag; + } + String moduleType = ModuleJsonUtil.parseModuleType(jsonString); + if (!TYPE_SHARED.equals(moduleType)) { + LOG.error("module type must be shared."); + LOG.error("compressHsp failed."); + deleteFile(hapAdditionPath); + exceptionFlag = true; + return exceptionFlag; + } + } + compressHSPMode(utility); + buildHash(utility); + return exceptionFlag; } /** -- Gitee From d541d2ec83a4cd372611e66f74414739a75f8b61 Mon Sep 17 00:00:00 2001 From: kunge-hub Date: Fri, 17 Nov 2023 15:55:07 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E6=8F=90=E5=8F=96=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: kunge-hub --- adapter/ohos/Compressor.java | 47 ++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/adapter/ohos/Compressor.java b/adapter/ohos/Compressor.java index d690bdb3..688a11d7 100644 --- a/adapter/ohos/Compressor.java +++ b/adapter/ohos/Compressor.java @@ -1157,29 +1157,34 @@ public class Compressor { Utility.closeStream(zipOut); Utility.closeStream(checkedOut); Utility.closeStream(fileOut); - if (exceptionFlag) { - ArrayList files = new ArrayList<>(); - FileUtils.getFileList(utility.getOutPath(), files); - if (files.size() > 1) { - String delPath = utility.getOutPath() + LINUX_FILE_SEPARATOR + hapFileName; - deleteFile(delPath); - String currentDir = System.getProperty("user.dir"); - String backupPath = currentDir + LINUX_FILE_SEPARATOR + backName; - File oldHapParentFile = hapPath.getParentFile(); - String oldHapPath = oldHapParentFile.getAbsolutePath() + LINUX_FILE_SEPARATOR + hapFileName; - Path oldFilePath = Paths.get(backupPath); - Path newFilePath = Paths.get(oldHapPath); - try { - Files.move(oldFilePath, newFilePath, StandardCopyOption.REPLACE_EXISTING); - } catch (IOException e) { - LOG.error("Compressor::HapAddition FilesMove IO exception."); - } - } else { - String delPath = utility.getOutPath(); - deleteFile(delPath); + handleException(exceptionFlag, utility, hapFileName, backName, hapPath); + deleteFile(backName); + } + } + + private void handleException(boolean exceptionFlag, Utility utility, String hapFileName, + String backName, File hapPath) { + if (exceptionFlag) { + ArrayList files = new ArrayList<>(); + FileUtils.getFileList(utility.getOutPath(), files); + if (files.size() > 1) { + String delPath = utility.getOutPath() + LINUX_FILE_SEPARATOR + hapFileName; + deleteFile(delPath); + String currentDir = System.getProperty("user.dir"); + String backupPath = currentDir + LINUX_FILE_SEPARATOR + backName; + File oldHapParentFile = hapPath.getParentFile(); + String oldHapPath = oldHapParentFile.getAbsolutePath() + LINUX_FILE_SEPARATOR + hapFileName; + Path oldFilePath = Paths.get(backupPath); + Path newFilePath = Paths.get(oldHapPath); + try { + Files.move(oldFilePath, newFilePath, StandardCopyOption.REPLACE_EXISTING); + } catch (IOException e) { + LOG.error("Compressor::HapAddition FilesMove IO exception."); } + } else { + String delPath = utility.getOutPath(); + deleteFile(delPath); } - deleteFile(backName); } } -- Gitee From b8fb2a714a596bbc46834d76d59f06d50538d12f Mon Sep 17 00:00:00 2001 From: kunge-hub Date: Fri, 17 Nov 2023 16:12:52 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: kunge-hub --- adapter/ohos/Compressor.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/adapter/ohos/Compressor.java b/adapter/ohos/Compressor.java index 688a11d7..79a5ad1c 100644 --- a/adapter/ohos/Compressor.java +++ b/adapter/ohos/Compressor.java @@ -1136,10 +1136,10 @@ public class Compressor { LOG.error("Compressor::hapAddition create out file parent directory failed."); } } - boolean exceptionFlag = false; FileOutputStream fileOut = null; CheckedOutputStream checkedOut = null; String backName = BACKUP_PREFIX + hapFileName; + boolean exceptionFlag = false; try { copyHapFile(utility, backName); fileOut = new FileOutputStream(destFile); @@ -1173,13 +1173,13 @@ public class Compressor { String currentDir = System.getProperty("user.dir"); String backupPath = currentDir + LINUX_FILE_SEPARATOR + backName; File oldHapParentFile = hapPath.getParentFile(); - String oldHapPath = oldHapParentFile.getAbsolutePath() + LINUX_FILE_SEPARATOR + hapFileName; - Path oldFilePath = Paths.get(backupPath); - Path newFilePath = Paths.get(oldHapPath); try { + String oldHapPath = oldHapParentFile.getCanonicalPath() + LINUX_FILE_SEPARATOR + hapFileName; + Path oldFilePath = Paths.get(backupPath); + Path newFilePath = Paths.get(oldHapPath); Files.move(oldFilePath, newFilePath, StandardCopyOption.REPLACE_EXISTING); } catch (IOException e) { - LOG.error("Compressor::HapAddition FilesMove IO exception."); + LOG.error("Compressor::handleException IO exception."); } } else { String delPath = utility.getOutPath(); -- Gitee From e8c02fbe76af6e2ea966a792b47da6741666d1f9 Mon Sep 17 00:00:00 2001 From: kunge-hub Date: Fri, 17 Nov 2023 16:28:24 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=8F=82=E6=95=B0?= =?UTF-8?q?=E4=BD=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: kunge-hub --- adapter/ohos/Compressor.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/adapter/ohos/Compressor.java b/adapter/ohos/Compressor.java index 79a5ad1c..1871abab 100644 --- a/adapter/ohos/Compressor.java +++ b/adapter/ohos/Compressor.java @@ -1136,10 +1136,10 @@ public class Compressor { LOG.error("Compressor::hapAddition create out file parent directory failed."); } } + boolean exceptionFlag = false; FileOutputStream fileOut = null; CheckedOutputStream checkedOut = null; String backName = BACKUP_PREFIX + hapFileName; - boolean exceptionFlag = false; try { copyHapFile(utility, backName); fileOut = new FileOutputStream(destFile); @@ -1269,7 +1269,6 @@ public class Compressor { } private boolean compressHapAddition(Utility utility) throws BundleException, IOException { - boolean exceptionFlag = false; // decompression hap file to hapAddition String currentDir = System.getProperty("user.dir"); String hapAdditionPath = currentDir + LINUX_FILE_SEPARATOR + HAPADDITION_FOLDER_NAME; @@ -1293,8 +1292,8 @@ public class Compressor { LOG.error(errMsg); throw new BundleException(errMsg); } - // package a new hap file + boolean exceptionFlag = false; try { setUtilityParameter(hapAdditionPath, utility); } catch (IOException e) { -- Gitee