diff --git a/adapter/ohos/Compressor.java b/adapter/ohos/Compressor.java index d1aaaa6c8d20147d3303bc4a18485264e67feb3d..1871abab7a07540a9ed443ae43aa39b9568d65de 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,34 @@ public class Compressor { Utility.closeStream(zipOut); Utility.closeStream(checkedOut); Utility.closeStream(fileOut); + 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(); + 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::handleException IO exception."); + } + } else { + String delPath = utility.getOutPath(); + deleteFile(delPath); + } } } @@ -1239,7 +1268,7 @@ public class Compressor { }); } - private void compressHapAddition(Utility utility) throws BundleException, IOException { + private boolean compressHapAddition(Utility utility) throws BundleException, IOException { // decompression hap file to hapAddition String currentDir = System.getProperty("user.dir"); String hapAdditionPath = currentDir + LINUX_FILE_SEPARATOR + HAPADDITION_FOLDER_NAME; @@ -1263,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) { @@ -1273,9 +1302,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 +1312,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; } /**