diff --git a/adapter/ohos/Compressor.java b/adapter/ohos/Compressor.java index c4223934c6adb778c43da2a11dbff9247609cde1..3346181239db2cea508c5d383ca75fc0eb4f9a5b 100644 --- a/adapter/ohos/Compressor.java +++ b/adapter/ohos/Compressor.java @@ -2931,6 +2931,13 @@ public class Compressor { hapVerifyInfo.setStageModule(true); ModuleJsonUtil.parseStageHapVerifyInfo(hapVerifyInfo); hapVerifyInfo.setFileLength(FileUtils.getFileSize(filePath)); + File srcFile = new File(filePath); + String fileStr = srcFile.getName(); + if (fileStr.toLowerCase(Locale.ENGLISH).endsWith(HAP_SUFFIX)) { + hapVerifyInfo.setFileType(HAP_SUFFIX); + } else if (fileStr.toLowerCase(Locale.ENGLISH).endsWith(HSP_SUFFIX)) { + hapVerifyInfo.setFileType(HSP_SUFFIX); + } return hapVerifyInfo; } diff --git a/adapter/ohos/HapVerify.java b/adapter/ohos/HapVerify.java index 35bc7950151b24e1a93777ddb85f15f27e47a4a6..c07e22371de0b25d7422810143572b2e55e307f9 100644 --- a/adapter/ohos/HapVerify.java +++ b/adapter/ohos/HapVerify.java @@ -46,6 +46,8 @@ class HapVerify { private static final long FILE_LENGTH_1M = 1024 * 1024L; private static final double FILE_SIZE_OFFSET_DOUBLE = 0.01d; private static final int FILE_SIZE_DECIMAL_PRECISION = 2; + private static final String HAP_SUFFIX = ".hap"; + private static final String HSP_SUFFIX = ".hsp"; /** * check hap is verify. @@ -231,15 +233,16 @@ class HapVerify { if (optional.isPresent()) { verifyInfo = optional.get(); } + List hapList = new ArrayList<>(); + int hspMinCompatibleVersionCodeMax = -1; + int hspTargetApiVersionMax = -1; VerifyCollection verifyCollection = new VerifyCollection(); verifyCollection.bundleName = verifyInfo.getBundleName(); verifyCollection.setBundleType(verifyInfo.getBundleType()); verifyCollection.vendor = verifyInfo.getVendor(); verifyCollection.versionCode = verifyInfo.getVersion().versionCode; verifyCollection.versionName = verifyInfo.getVersion().versionName; - verifyCollection.minCompatibleVersionCode = verifyInfo.getVersion().minCompatibleVersionCode; verifyCollection.compatibleApiVersion = verifyInfo.getApiVersion().getCompatibleApiVersion(); - verifyCollection.targetApiVersion = verifyInfo.getApiVersion().getTargetApiVersion(); verifyCollection.releaseType = verifyInfo.getApiVersion().getReleaseType(); verifyCollection.targetBundleName = verifyInfo.getTargetBundleName(); verifyCollection.targetPriority = verifyInfo.getTargetPriority(); @@ -253,6 +256,41 @@ class HapVerify { hapVerifyInfo.getModuleName() + ") has different values."); return false; } + if (hapVerifyInfo.getFileType() == HAP_SUFFIX) { + hapList.add(hapVerifyInfo); + } else if (hapVerifyInfo.getFileType() == HSP_SUFFIX) { + if (hapVerifyInfo.getVersion().minCompatibleVersionCode > hspMinCompatibleVersionCodeMax) { + hspMinCompatibleVersionCodeMax = hapVerifyInfo.getVersion().minCompatibleVersionCode; + } + if (hapVerifyInfo.getApiVersion().getTargetApiVersion() > hspTargetApiVersionMax) { + hspTargetApiVersionMax = hapVerifyInfo.getApiVersion().getTargetApiVersion(); + } + } + } + if (!appFieldsIsValid(hapList, hspMinCompatibleVersionCodeMax, hspTargetApiVersionMax)) { + return false; + } + return true; + } + + private static boolean appFieldsIsValid(List hapVerifyInfos, int minCompatibleVersionCode, + int targetApiVersion) { + if (hapVerifyInfos.isEmpty()) { + LOG.warning("hapList empty"); + return true; + } + HapVerifyInfo hap = hapVerifyInfos.get(0); + for (HapVerifyInfo hapInfo : hapVerifyInfos) { + if (hap.getVersion().minCompatibleVersionCode != hapInfo.getVersion().minCompatibleVersionCode || + hap.getApiVersion().getTargetApiVersion() != hapInfo.getApiVersion().getTargetApiVersion()) { + LOG.error("hap minCompatibleVersionCode or targetApiVersion different"); + return false; + } + } + if (hap.getVersion().minCompatibleVersionCode < minCompatibleVersionCode || + hap.getApiVersion().getTargetApiVersion() < targetApiVersion) { + LOG.error("minCompatibleVersionCode or targetApiVersion property hap less than hsp"); + return false; } return true; } @@ -271,18 +309,10 @@ class HapVerify { LOG.error("input module versionCode is different."); return false; } - if (verifyCollection.minCompatibleVersionCode != hapVerifyInfo.getVersion().minCompatibleVersionCode) { - LOG.error("input module minCompatibleVersionCode is different."); - return false; - } if (verifyCollection.compatibleApiVersion != hapVerifyInfo.getApiVersion().getCompatibleApiVersion()) { LOG.error("input module minApiVersion is different."); return false; } - if (verifyCollection.targetApiVersion != hapVerifyInfo.getApiVersion().getTargetApiVersion()) { - LOG.error("input module targetApiVersion is different."); - return false; - } if (!verifyCollection.releaseType.equals(hapVerifyInfo.getApiVersion().getReleaseType())) { if (verifyCollection.getModuleType().equals(TYPE_SHARED) || hapVerifyInfo.getModuleType().equals(TYPE_SHARED)) { diff --git a/adapter/ohos/HapVerifyInfo.java b/adapter/ohos/HapVerifyInfo.java index 2be4ff24fbf954b9dd6b4c74b07e14e21b2f3df2..a962f7da43f546da1824d307064e98afdcce13e5 100644 --- a/adapter/ohos/HapVerifyInfo.java +++ b/adapter/ohos/HapVerifyInfo.java @@ -141,6 +141,8 @@ class HapVerifyInfo { private MultiAppMode multiAppMode = new MultiAppMode(); + private String fileType = ""; + /** * get bundle name form HapVerifyInfo. */ @@ -522,4 +524,12 @@ class HapVerifyInfo { public void setMultiAppMode(MultiAppMode multiAppMode) { this.multiAppMode = multiAppMode; } + + public String getFileType() { + return fileType; + } + + public void setFileType(String fileType) { + this.fileType = fileType; + } } diff --git a/packing_tool/frameworks/include/json/hap_verify_info.h b/packing_tool/frameworks/include/json/hap_verify_info.h index c687db8ea795b16a8fac8a4f5316b7cca236aa5d..f197167a433688575db53adf500ee9ac90d43b8b 100644 --- a/packing_tool/frameworks/include/json/hap_verify_info.h +++ b/packing_tool/frameworks/include/json/hap_verify_info.h @@ -101,6 +101,8 @@ public: void SetContinueTypeMap(const std::map>& continueTypeMap); const MultiAppMode& GetMultiAppMode() const; void SetMultiAppMode(const MultiAppMode& multiAppMode); + const std::string& GetFileType() const; + void SetFileType(const std::string& fileType); private: void ConvertToDependency(); @@ -141,6 +143,7 @@ private: std::list proxyDataUris_; std::map> continueTypeMap_; MultiAppMode multiAppMode_; + std::string fileType_ = ""; }; } // namespace AppPackingTool } // namespace OHOS diff --git a/packing_tool/frameworks/include/json/hap_verify_utils.h b/packing_tool/frameworks/include/json/hap_verify_utils.h index fba3cdfd52215b60315d3c08b0803268bf1c3dc4..1b3cf8a580b38f43403d2ec3ca500814b5beccb2 100644 --- a/packing_tool/frameworks/include/json/hap_verify_utils.h +++ b/packing_tool/frameworks/include/json/hap_verify_utils.h @@ -134,6 +134,8 @@ private: static bool CheckContinueTypeIsValid(const std::list& hapVerifyInfos); static bool CheckContinueTypeIsValid(const HapVerifyInfo& hapVerifyInfo); static bool CheckContinueTypeIsValid(const HapVerifyInfo& hapVerifyInfo1, const HapVerifyInfo& hapVerifyInfo2); + static bool AppFieldsIsValid(const std::list& hapVerifyInfos, + int32_t minCompatibleVersionCode, int32_t targetApiVersion); }; } // namespace AppPackingTool } // namespace OHOS diff --git a/packing_tool/frameworks/src/json/hap_verify_info.cpp b/packing_tool/frameworks/src/json/hap_verify_info.cpp index 94f0ebc4d9b5698c8bf1d306afbb9dcc1b868d9e..19cb2f13e89561d8217a575532eb2d59d507b0e3 100644 --- a/packing_tool/frameworks/src/json/hap_verify_info.cpp +++ b/packing_tool/frameworks/src/json/hap_verify_info.cpp @@ -363,5 +363,15 @@ void HapVerifyInfo::ConvertToDependency() } } } + +const std::string& HapVerifyInfo::GetFileType() const +{ + return fileType_; +} + +void HapVerifyInfo::SetFileType(const std::string& fileType) +{ + fileType_ = fileType; +} } // namespace AppPackingTool } // namespace OHOS diff --git a/packing_tool/frameworks/src/json/hap_verify_utils.cpp b/packing_tool/frameworks/src/json/hap_verify_utils.cpp index d339f622275cda94c7c020b7dda4c805ff674c75..a4732eb5d3837dd7dd54d56e83720039219c7d6b 100644 --- a/packing_tool/frameworks/src/json/hap_verify_utils.cpp +++ b/packing_tool/frameworks/src/json/hap_verify_utils.cpp @@ -38,6 +38,8 @@ const std::string ENTRY_NAME = "entryName"; const std::string ATOMIC_SERVICE = "atomicService"; const std::string TYPE_SHARED = "shared"; const std::string HAR = "har"; +const std::string HAP_SUFFIX = ".hap"; +const std::string HSP_SUFFIX = ".hsp"; const int32_t TWO = 2; const long FILE_LENGTH_1M = 1024 * 1024L; } @@ -100,6 +102,9 @@ bool HapVerifyUtils::CheckAppFieldsIsSame(const std::list& hapVer LOGE("hapVerifyInfos is empty!"); return false; } + std::list hapList; + int32_t hspMinCompatibleVersionCodeMax = -1; + int32_t hspTargetApiVersionMax = -1; HapVerifyInfo hapVerifyInfo = *hapVerifyInfos.begin(); VerifyCollection verifyCollection; verifyCollection.bundleName = hapVerifyInfo.GetBundleName(); @@ -107,9 +112,7 @@ bool HapVerifyUtils::CheckAppFieldsIsSame(const std::list& hapVer verifyCollection.vendor = hapVerifyInfo.GetVendor(); verifyCollection.versionCode = hapVerifyInfo.GetVersion().versionCode; verifyCollection.versionName = hapVerifyInfo.GetVersion().versionName; - verifyCollection.minCompatibleVersionCode = hapVerifyInfo.GetVersion().minCompatibleVersionCode; verifyCollection.compatibleApiVersion = hapVerifyInfo.GetApiVersion().compatibleApiVersion; - verifyCollection.targetApiVersion = hapVerifyInfo.GetApiVersion().targetApiVersion; verifyCollection.releaseType = hapVerifyInfo.GetApiVersion().releaseType; verifyCollection.targetBundleName = hapVerifyInfo.GetTargetBundleName(); verifyCollection.targetPriority = hapVerifyInfo.GetTargetPriority(); @@ -123,6 +126,43 @@ bool HapVerifyUtils::CheckAppFieldsIsSame(const std::list& hapVer verifyCollection.moduleName.c_str(), hapVerifyInfo.GetModuleName().c_str()); return false; } + + if (hapVerifyInfo.GetFileType() == HAP_SUFFIX) { + hapList.emplace_back(hapVerifyInfo); + } else if (hapVerifyInfo.GetFileType() == HSP_SUFFIX) { + if (hapVerifyInfo.GetVersion().minCompatibleVersionCode > hspMinCompatibleVersionCodeMax) { + hspMinCompatibleVersionCodeMax = hapVerifyInfo.GetVersion().minCompatibleVersionCode; + } + if (hapVerifyInfo.GetApiVersion().targetApiVersion > hspTargetApiVersionMax) { + hspTargetApiVersionMax = hapVerifyInfo.GetApiVersion().targetApiVersion; + } + } + } + if (!AppFieldsIsValid(hapList, hspMinCompatibleVersionCodeMax, hspTargetApiVersionMax)) { + return false; + } + return true; +} + +bool HapVerifyUtils::AppFieldsIsValid(const std::list& hapVerifyInfos, + int32_t minCompatibleVersionCode, int32_t targetApiVersion) +{ + if (hapVerifyInfos.empty()) { + LOGW("hapList empty"); + return true; + } + HapVerifyInfo hap = *hapVerifyInfos.begin(); + for (HapVerifyInfo hapInfo : hapVerifyInfos) { + if (hap.GetVersion().minCompatibleVersionCode != hapInfo.GetVersion().minCompatibleVersionCode || + hap.GetApiVersion().targetApiVersion != hapInfo.GetApiVersion().targetApiVersion) { + LOGE("hap minCompatibleVersionCode or targetApiVersion different"); + return false; + } + } + if (hap.GetVersion().minCompatibleVersionCode < minCompatibleVersionCode || + hap.GetApiVersion().targetApiVersion < targetApiVersion) { + LOGE("minCompatibleVersionCode or targetApiVersion property hap less than hsp"); + return false; } return true; } @@ -141,16 +181,14 @@ bool HapVerifyUtils::AppFieldsIsSame(const VerifyCollection& verifyCollection, c LOGE("input module bundleType is different."); return false; } - if (verifyCollection.versionCode != hapVerifyInfo.GetVersion().versionCode || - verifyCollection.minCompatibleVersionCode != hapVerifyInfo.GetVersion().minCompatibleVersionCode) { - LOGE("input module versionCode or minCompatibleVersionCode is different."); + if (verifyCollection.versionCode != hapVerifyInfo.GetVersion().versionCode) { + LOGE("input module versionCode is different."); return false; } if (verifyCollection.compatibleApiVersion != hapVerifyInfo.GetApiVersion().compatibleApiVersion || - verifyCollection.targetApiVersion != hapVerifyInfo.GetApiVersion().targetApiVersion || verifyCollection.releaseType != hapVerifyInfo.GetApiVersion().releaseType) { - LOGE("input module compatibleApiVersion or targetApiVersion or releaseType is different."); - LOGE("Solutions: > Check if the compatibleApiVersion / targetApiVersion / releaseType" \ + LOGE("input module compatibleApiVersion or releaseType is different."); + LOGE("Solutions: > Check if the compatibleApiVersion / releaseType" \ " is the same in different modules."); return false; } diff --git a/packing_tool/frameworks/src/json/module_json_utils.cpp b/packing_tool/frameworks/src/json/module_json_utils.cpp index ca5cdf8c20f65cbac5a312aaf3fdf4070c9c2774..7ede4143c5bd2c9e8940c0bf6e19774b80ae1852 100644 --- a/packing_tool/frameworks/src/json/module_json_utils.cpp +++ b/packing_tool/frameworks/src/json/module_json_utils.cpp @@ -62,10 +62,20 @@ bool ModuleJsonUtils::GetStageHapVerifyInfo(const std::string& hapFilePath, HapV LOGE("Get resouce map from hap file failed! hapFile=%s", hapFilePath.c_str()); return false; } + std::string fileType = ""; + fs::path fsHapPath(hapFilePath); + std::string fileName = fsHapPath.filename().string(); + std::transform(fileName.begin(), fileName.end(), fileName.begin(), ::tolower); + if (Utils::EndsWith(fileName, HAP_SUFFIX)) { + fileType = HAP_SUFFIX; + } else if (Utils::EndsWith(fileName, HSP_SUFFIX)) { + fileType = HSP_SUFFIX; + } hapVerifyInfo.SetResourceMap(resourceMap); hapVerifyInfo.SetProfileStr(fileContent); hapVerifyInfo.SetStageModule(true); hapVerifyInfo.SetFileLength(fileLength); + hapVerifyInfo.SetFileType(fileType); ModuleJson moduleJson; if (!moduleJson.ParseFromString(fileContent)) { LOGE("Parse json string failed!");