diff --git a/bundle.json b/bundle.json index 570c8e61203824d4efadcf57fa763e7dbbacb73e..52d2f8027d7fdc91cbd506041055d0e0c3924f17 100755 --- a/bundle.json +++ b/bundle.json @@ -18,7 +18,10 @@ "deps": { "components": [ "json", - "zlib" + "hilog", + "zlib", + "cJSON", + "openssl" ] }, "build": { diff --git a/ohos_packing_tool/frameworks/BUILD.gn b/ohos_packing_tool/frameworks/BUILD.gn index 78bb699a6b08dfcd807e0dede7937cfd9b81a347..0eb6403122fad9441f88f8b74b91dca860dae794 100644 --- a/ohos_packing_tool/frameworks/BUILD.gn +++ b/ohos_packing_tool/frameworks/BUILD.gn @@ -14,19 +14,42 @@ import("//build/ohos.gni") ohos_executable("ohos_packing_tool") { - include_dirs = [ "include" ] + include_dirs = [ + "include", + "include/json" + ] sources = [ "src/hap_packager.cpp", "src/hsp_packager.cpp", + "src/packageNormalize.cpp", "src/main.cpp", "src/packager.cpp", "src/shell_command.cpp", + "src/zip_wrapper.cpp", + "src/unzip_wrapper.cpp", + "src/zip_utils.cpp", + "src/log.cpp", + "src/utils.cpp", + "src/json/distro_filter.cpp", + "src/json/hap_verify_info.cpp", + "src/json/json_utils.cpp", + "src/json/module_json.cpp", + "src/json/module_json_utils.cpp", + "src/json/normalize_version_utils.cpp", + "src/json/pack_info.cpp", + "src/json/pack_info_utils.cpp", + "src/json/patch_json.cpp", + "src/json/patch_json_utils.cpp", + "src/json/pt_json.cpp", ] configs = [] cflags = [] external_deps = [ "json:nlohmann_json_static", + "cJSON:cjson", + "hilog:libhilog", "zlib:libz", + "openssl:libcrypto_shared", ] install_enable = false subsystem_name = "developtools" diff --git a/ohos_packing_tool/frameworks/include/app_log_wrapper.h b/ohos_packing_tool/frameworks/include/app_log_wrapper.h new file mode 100644 index 0000000000000000000000000000000000000000..360631d772963eef62c3abf037ebdb5a02d7e8b0 --- /dev/null +++ b/ohos_packing_tool/frameworks/include/app_log_wrapper.h @@ -0,0 +1,81 @@ +/* + * Copyright (c) 2021 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. + */ + +#ifndef FOUNDATION_APPEXECFWK_STANDARD_COMMON_LOG_INCLUDE_APP_LOG_WRAPPER_H +#define FOUNDATION_APPEXECFWK_STANDARD_COMMON_LOG_INCLUDE_APP_LOG_WRAPPER_H + +#include "hilog/log.h" +#include + +namespace OHOS { +namespace AppExecFwk { +#ifndef LOG_DOMAIN +#define LOG_DOMAIN 0xD001100 +#endif + +#ifndef APP_LOG_TAG +#define APP_LOG_TAG "BundleMgrService" +#endif + +enum class AppLogLevel { DEBUG = 0, INFO, WARN, ERROR, FATAL }; + +static constexpr OHOS::HiviewDFX::HiLogLabel APP_LABEL = {LOG_CORE, LOG_DOMAIN, APP_LOG_TAG}; + +class AppLogWrapper { +public: + static bool JudgeLevel(const AppLogLevel &level); + + static void SetLogLevel(const AppLogLevel &level) + { + level_ = level; + } + + static const AppLogLevel &GetLogLevel() + { + return level_; + } + + static std::string GetBriefFileName(const char *str); + +private: + static AppLogLevel level_; +}; + +#define FILENAME (__builtin_strrchr(__FILE__, '/') ? __builtin_strrchr(__FILE__, '/') + 1 : __FILE__) +#define APP_LOGD(fmt, ...) \ + ((void)HILOG_IMPL(LOG_CORE, LOG_DEBUG, LOG_DOMAIN, APP_LOG_TAG, \ + "[%{public}s(%{public}s:%{public}d)]" fmt, FILENAME, __FUNCTION__, __LINE__, ##__VA_ARGS__)) +#define APP_LOGI(fmt, ...) \ + ((void)HILOG_IMPL(LOG_CORE, LOG_INFO, LOG_DOMAIN, APP_LOG_TAG, \ + "[%{public}s(%{public}s:%{public}d)]" fmt, FILENAME, __FUNCTION__, __LINE__, ##__VA_ARGS__)) +#define APP_LOGW(fmt, ...) \ + ((void)HILOG_IMPL(LOG_CORE, LOG_WARN, LOG_DOMAIN, APP_LOG_TAG, \ + "[%{public}s(%{public}s:%{public}d)]" fmt, FILENAME, __FUNCTION__, __LINE__, ##__VA_ARGS__)) +#define APP_LOGE(fmt, ...) \ + ((void)HILOG_IMPL(LOG_CORE, LOG_ERROR, LOG_DOMAIN, APP_LOG_TAG, \ + "[%{public}s(%{public}s:%{public}d)]" fmt, FILENAME, __FUNCTION__, __LINE__, ##__VA_ARGS__)) +#define APP_LOGF(fmt, ...) \ + ((void)HILOG_IMPL(LOG_CORE, LOG_FATAL, LOG_DOMAIN, APP_LOG_TAG, \ + "[%{public}s(%{public}s:%{public}d)]" fmt, FILENAME, __FUNCTION__, __LINE__, ##__VA_ARGS__)) + +#define APP_LOGI_NOFUNC(fmt, ...) \ + ((void)HILOG_IMPL(LOG_CORE, LOG_INFO, LOG_DOMAIN, APP_LOG_TAG, fmt, ##__VA_ARGS__)) +#define APP_LOGW_NOFUNC(fmt, ...) \ + ((void)HILOG_IMPL(LOG_CORE, LOG_WARN, LOG_DOMAIN, APP_LOG_TAG, fmt, ##__VA_ARGS__)) +#define APP_LOGE_NOFUNC(fmt, ...) \ + ((void)HILOG_IMPL(LOG_CORE, LOG_ERROR, LOG_DOMAIN, APP_LOG_TAG, fmt, ##__VA_ARGS__)) +} // namespace AppExecFwk +} // namespace OHOS +#endif // FOUNDATION_APPEXECFWK_STANDARD_COMMON_LOG_INCLUDE_APP_LOG_WRAPPER_H diff --git a/ohos_packing_tool/frameworks/include/constants.h b/ohos_packing_tool/frameworks/include/constants.h index b6daeb26455718c9a3390759960714bf59329d01..d53fcf64464bdf99d5007114465921af4bfe6430 100644 --- a/ohos_packing_tool/frameworks/include/constants.h +++ b/ohos_packing_tool/frameworks/include/constants.h @@ -73,6 +73,8 @@ const std::string PARAM_RPCID = "rpcid"; const std::string PARAM_APPQF_PATH = "appqf-path"; const std::string PARAM_MAIN_MODULE_LIMIT = "main-module-limit"; const std::string PARAM_NORMAL_MODULE_LIMIT = "normal-module-limit"; +const std::string PARAM_BUNDLE_NAME = "bundle-name"; +const std::string LINUX_FILE_SEPARATOR = "/"; const std::string MODULE_JSON = "module.json"; const std::string CONFIG_JSON = "config.json"; @@ -88,6 +90,8 @@ const std::string JSON_SUFFIX = ".json"; const std::string HAP_SUFFIX = ".hap"; const std::string HSP_SUFFIX = ".hsp"; const std::string APP_SUFFIX = ".app"; +const std::string VERSION_NAME_PATTERN = "^[0-9.]+|(?=.*[{])(?=.*[}])[0-9a-zA-Z_.{}]+$"; +const std::string BUNDLE_NAME_PATTERN = "([a-zA-Z]|[a-zA-Z]+(_*[0-9a-zA-Z])+)(\\.[0-9a-zA-Z]|\\.[0-9a-zA-Z]+(_*[0-9a-zA-Z])+){2,}"; constexpr const char* SHORT_OPTIONS = ""; const struct option LONG_OPTIONS[] = { @@ -126,6 +130,7 @@ const struct option LONG_OPTIONS[] = { {PARAM_APPQF_PATH.c_str(), required_argument, nullptr, 34}, {PARAM_MAIN_MODULE_LIMIT.c_str(), required_argument, nullptr, 35}, {PARAM_NORMAL_MODULE_LIMIT.c_str(), required_argument, nullptr, 36}, + {PARAM_BUNDLE_NAME.c_str(), required_argument, nullptr, 37}, {nullptr, 0, nullptr, 0}, }; constexpr const int OPTIONS_SIZE = sizeof(LONG_OPTIONS) / sizeof(LONG_OPTIONS[0]); diff --git a/ohos_packing_tool/frameworks/include/hap_packager.h b/ohos_packing_tool/frameworks/include/hap_packager.h index a6e142875bef82d8238204d24a8dc0144c18bc4a..4054ee4515638698375f4627acc690507798f2aa 100644 --- a/ohos_packing_tool/frameworks/include/hap_packager.h +++ b/ohos_packing_tool/frameworks/include/hap_packager.h @@ -21,7 +21,7 @@ #include #include "packager.h" - +#include "zip_wrapper.h" namespace OHOS { namespace AppPackingTool { @@ -35,6 +35,9 @@ protected: int PreProcess() override; int Process() override; int PostProcess() override; + +private: + ZipWrapper zipWrapper_; }; } // namespace AppExecFwk diff --git a/ohos_packing_tool/frameworks/include/json/dependency_item.h b/ohos_packing_tool/frameworks/include/json/dependency_item.h new file mode 100644 index 0000000000000000000000000000000000000000..b10b98c59457495d1e98eac29256645d1a67b38a --- /dev/null +++ b/ohos_packing_tool/frameworks/include/json/dependency_item.h @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2024 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. + */ + +#ifndef DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_DEPENDENCY_ITEM_H +#define DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_DEPENDENCY_ITEM_H + +#include + +namespace OHOS { +namespace AppPackingTool { +struct DependencyItem { + std::string bundleName = ""; + std::string moduleName = ""; + int versionCode = 0; +}; +} // namespace AppPackingTool +} // namespace OHOS + +#endif // DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_DEPENDENCY_ITEM_H diff --git a/ohos_packing_tool/frameworks/include/json/distro_filter.h b/ohos_packing_tool/frameworks/include/json/distro_filter.h new file mode 100644 index 0000000000000000000000000000000000000000..0fe94505d1638f89c36238035326203023ccae38 --- /dev/null +++ b/ohos_packing_tool/frameworks/include/json/distro_filter.h @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2024 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. + */ + +#ifndef DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_DISTRO_FILTER_H +#define DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_DISTRO_FILTER_H + +#include +#include +#include + +namespace OHOS { +namespace AppPackingTool { + +struct PolicyValue { + std::string policy = ""; + std::list value; + + bool ParseFromString(const std::string& jsonString); + + bool IsEmpty() const; +}; + +typedef PolicyValue ApiVersion; +typedef PolicyValue ScreenShape; +typedef PolicyValue ScreenDensity; +typedef PolicyValue ScreenWindow; +typedef PolicyValue CountryCode; + +struct DistroFilter { + ApiVersion apiVersion; + ScreenShape screenShape; + ScreenDensity screenDensity; + ScreenWindow screenWindow; + CountryCode countryCode; + + bool ParseFromString(const std::string& jsonString); + + bool IsEmpty() const; + + std::string Dump(); +}; +} // namespace AppPackingTool +} // namespace OHOS + +#endif // DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_DISTRO_FILTER_H diff --git a/ohos_packing_tool/frameworks/include/json/hap_verify_info.h b/ohos_packing_tool/frameworks/include/json/hap_verify_info.h new file mode 100644 index 0000000000000000000000000000000000000000..615411591c9d654f7d10b1bbf23deef314eede30 --- /dev/null +++ b/ohos_packing_tool/frameworks/include/json/hap_verify_info.h @@ -0,0 +1,358 @@ +/* + * Copyright (c) 2024 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. + */ + +#ifndef DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_HAP_VERIFY_INFO_H +#define DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_HAP_VERIFY_INFO_H + +#include +#include +#include +#include "version.h" +#include "module_api_version.h" +#include "distro_filter.h" +#include "dependency_item.h" +#include "preload_item.h" +#include "multi_app_mode.h" + +namespace OHOS { +namespace AppPackingTool { + + +class HapVerifyInfo { +public: + HapVerifyInfo(); + virtual ~HapVerifyInfo(); + + // HapVerifyInfo(const HapVerifyInfo &) = delete; + // HapVerifyInfo &operator=(const HapVerifyInfo &) = delete; + + const std::string& GetBundleName() const { + return bundleName_; + } + + void SetBundleName(const std::string& bundleName) { + bundleName_ = bundleName; + } + + const std::string& GetVendor() const { + return vendor_; + } + + void SetVendor(const std::string& vendor) { + vendor_ = vendor; + } + + const Version& GetVersion() const { + return version_; + } + + void SetVersion(const Version& version) { + version_ = version; + } + + const ModuleApiVersion& GetApiVersion() const { + return apiVersion_; + } + + void SetApiVersion(const ModuleApiVersion& apiVersion) { + apiVersion_ = apiVersion; + } + + const std::string& GetModuleName() const { + return moduleName_; + } + + void SetModuleName(const std::string& moduleName) { + moduleName_ = moduleName; + } + + const std::string& GetPackageName() const { + return packageName_; + } + + void SetPackageName(const std::string& packageName) { + packageName_ = packageName; + } + + const std::list& GetAbilityNames() const { + return abilityNames_; + } + + void SetAbilityNames(const std::list& abilityNames) { + abilityNames_ = abilityNames; + } + + void AddAbilityNames(const std::list& nameList) { + abilityNames_.insert(abilityNames_.end(), nameList.begin(), nameList.end()); + } + + const DistroFilter& GetDistroFilter() const { + return distroFilter_; + } + + void SetDistroFilter(const DistroFilter& distroFilter) { + distroFilter_ = distroFilter; + } + + const std::list& GetDeviceTypes() const { + return deviceTypes_; + } + + void SetDeviceTypes(const std::list& deviceTypes) { + deviceTypes_ = deviceTypes; + } + + const bool& IsStageModule() const { + return isStageModule_; + } + + void SetStageModule(const bool& isStageModule) { + isStageModule_ = isStageModule; + } + + const std::string& GetModuleType() const { + return moduleType_; + } + + void SetModuleType(const std::string& moduleType) { + moduleType_ = moduleType; + } + + const bool& IsInstallationFree() const { + return isInstallationFree_; + } + + void SetInstallationFree(const bool& isInstallationFree) { + isInstallationFree_ = isInstallationFree; + } + + const std::list& GetDependencies() const { + return dependencies_; + } + + void SetDependencies(const std::list& dependencies) { + dependencies_ = dependencies; + } + + const std::list& getDependencyItemList() const { + return dependencyItemList_; + } + + void SetDependencyItemList(const std::list& dependencyItemList) { + dependencyItemList_ = dependencyItemList; + ConvertToDependency(); + } + + // SUNJIAN: profile str is json string + const std::string& GetProfileStr() const { + return profileStr_; + } + + void SetProfileStr(const std::string& profileStr) { + profileStr_ = profileStr; + } + + const std::map& GetResourceMap() const { + return resourceMap_; + } + + void SetResourceMap(const std::map& resourceMap) { + resourceMap_ = resourceMap; + } + + const std::string& GetBundleType() const { + return bundleType_; + } + + void SetBundleType(const std::string& bundleType) { + bundleType_ = bundleType; + } + + const std::string& getAtomicServiceType() const { + return atomicServiceType_; + } + + void SetAtomicServiceType(const std::string& atomicServiceType) { + atomicServiceType_ = atomicServiceType; + } + + const std::list& GetPreloadItems() const { + return preloadItems_; + } + + void SetPreloadItems(const std::list& preloadItems) { + preloadItems_ = preloadItems; + } + + const std::string& GetTargetBundleName() const { + return targetBundleName_; + } + + void SetTargetBundleName(const std::string& targetBundleName) { + targetBundleName_ = targetBundleName; + } + + const int& GetTargetPriority() const { + return targetPriority_; + } + + void SetTargetPriority(const int& priority) { + targetPriority_ = priority; + } + + const std::string& GetTargetModuleName() const { + return targetModuleName_; + } + + void SetTargetModuleName(const std::string& targetModuleName) { + targetModuleName_ = targetModuleName; + } + + const int& GetTargetModulePriority() const { + return targetModulePriority_; + } + + void SetTargetModulePriority(const int& priority) { + targetModulePriority_ = priority; + } + + // SUNJIAN: the json file size + const int64_t& GetFileLength() const { + return fileLength_; + } + + void SetFileLength(const int64_t& fileLength) { + fileLength_ = fileLength; + } + + // SUNJIAN: What's this? + const int& GetEntrySizeLimit() const { + return entrySizeLimit_; + } + + void SetEntrySizeLimit(const int& limit) { + entrySizeLimit_ = limit; + } + + // SUNJIAN: What's this? + const int& GetNotEntrySizeLimit() const { + return notEntrySizeLimit_; + } + + void SetNotEntrySizeLimit(const int& notEntrySizeLimit) { + notEntrySizeLimit_ = notEntrySizeLimit; + } + + // SUNJIAN: What's this? + const int& GetSumSizeLimit() const { + return sumSizeLimit_; + } + + void SetSumSizeLimit(const int& sumSizeLimit) { + sumSizeLimit_ = sumSizeLimit; + } + + const bool& IsDebug() const { + return debug_; + } + + void SetDebug(const bool& debug) { + debug_ = debug; + } + + const std::string& GetCompileSdkVersion() const { + return compileSdkVersion_; + } + + void SetCompileSdkVersion(const std::string& compileSdkVersion) { + compileSdkVersion_ = compileSdkVersion; + } + + const std::string& GetCompileSdkType() const { + return compileSdkType_; + } + + void SetCompileSdkType(const std::string& compileSdkType) { + compileSdkType_ = compileSdkType; + } + + const std::list& GetProxyDataUris() const { + return proxyDataUris_; + } + + void SetProxyDataUris(const std::list& proxyDataUris) { + proxyDataUris_ = proxyDataUris; + } + + const std::map>& GetContinueTypeMap() const { + return continueTypeMap_; + } + + void SetContinueTypeMap(const std::map>& continueTypeMap) { + continueTypeMap_ = continueTypeMap; + } + + const MultiAppMode& GetMultiAppMode() const { + return multiAppMode_; + } + + void SetMultiAppMode(const MultiAppMode& multiAppMode) { + multiAppMode_ = multiAppMode; + } + +private: + void ConvertToDependency(); + +private: + std::string bundleName_ = ""; + std::string vendor_ = ""; + Version version_; + ModuleApiVersion apiVersion_; + std::string moduleName_ = ""; + std::string packageName_ = ""; + std::list abilityNames_; + DistroFilter distroFilter_; + std::list deviceTypes_; + bool isStageModule_ = true; + std::string moduleType_ = ""; + std::string atomicServiceType_ = ""; + bool isInstallationFree_ = false; + std::list dependencies_; + std::list dependencyItemList_; + std::string profileStr_ = ""; + std::map resourceMap_; + std::string bundleType_ = "app"; + std::string targetBundleName_ = ""; + int targetPriority_ = 0; + std::string targetModuleName_ = ""; + int targetModulePriority_ = 0; + std::list preloadItems_; + int64_t fileLength_ = 0L; + int entrySizeLimit_ = 2; + int notEntrySizeLimit_ = 2; + int sumSizeLimit_ = 10; + bool debug_ = false; + std::string compileSdkVersion_ = ""; + std::string compileSdkType_ = ""; + std::list proxyDataUris_; + std::map> continueTypeMap_; + MultiAppMode multiAppMode_; + +}; + +} // namespace AppPackingTool +} // namespace OHOS + +#endif // DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_HAP_VERIFY_INFO_H diff --git a/ohos_packing_tool/frameworks/include/json/hqf_info.h b/ohos_packing_tool/frameworks/include/json/hqf_info.h new file mode 100644 index 0000000000000000000000000000000000000000..eead0257d9cd7d3798a56712824131b363acccef --- /dev/null +++ b/ohos_packing_tool/frameworks/include/json/hqf_info.h @@ -0,0 +1,122 @@ +/* + * Copyright (c) 2024 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. + */ + +#ifndef DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_HQF_INFO_H +#define DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_HQF_INFO_H + +#include +#include +#include + +namespace OHOS { +namespace AppPackingTool { + +class HqfInfo { +public: + HqfInfo() {}; + virtual ~HqfInfo() {}; + + // HqfInfo(const HqfInfo &) = delete; + // HqfInfo &operator=(const HqfInfo &) = delete; + + const std::string& GetBundleName() const { + return bundleName_; + } + + void SetBundleName(const std::string& bundleName) { + bundleName_ = bundleName; + } + + const int32_t& GetVersionCode() const { + return versionCode_; + } + + void SetVersionCode(const int32_t& versionCode) { + versionCode_ = versionCode; + } + + const std::string& GetVersionName() const { + return versionName_; + } + + void SetVersionName(const std::string& versionName) { + versionName_ = versionName; + } + + const int32_t& GetPatchVersionCode() const { + return patchVersionCode_; + } + + void SetPatchVersionCode(const int32_t& patchVersionCode) { + patchVersionCode_ = patchVersionCode; + } + + const std::string& GetPatchVersionName() const { + return patchVersionName_; + } + + void SetPatchVersionName(const std::string& patchVersionName) { + patchVersionName_ = patchVersionName; + } + + const std::string& GetModuleName() const { + return moduleName_; + } + + void SetModuleName(const std::string& moduleName) { + moduleName_ = moduleName; + } + + const std::string& GetType() const { + return type_; + } + + void SetType(const std::string& type) { + type_ = type; + } + + const std::list& GetDeviceTypes() const { + return deviceTypes_; + } + + void SetDeviceTypes(const std::list& deviceTypes) { + deviceTypes_ = deviceTypes; + } + + const std::string& GetOriginalModuleHash() const { + return originalModuleHash_; + } + + void SetOriginalModuleHash(const std::string& originalModuleHash) { + originalModuleHash_ = originalModuleHash; + } + +private: + std::string bundleName_ = ""; + int32_t versionCode_ = -1; + std::string versionName_ = ""; + int32_t patchVersionCode_ = -1; + std::string patchVersionName_ = ""; + + std::string moduleName_ = ""; + std::string type_ = ""; + std::list deviceTypes_; + std::string originalModuleHash_ = ""; +}; + +} // namespace AppPackingTool +} // namespace OHOS + +#endif // DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_HQF_INFO_H diff --git a/ohos_packing_tool/frameworks/include/json/json_utils.h b/ohos_packing_tool/frameworks/include/json/json_utils.h new file mode 100644 index 0000000000000000000000000000000000000000..f1808b68bcc95668889e6aab67d9c181b03df793 --- /dev/null +++ b/ohos_packing_tool/frameworks/include/json/json_utils.h @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2024 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. + */ + +#ifndef DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_JSON_UTILS_H +#define DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_JSON_UTILS_H + +#include +#include +#include +#include "pt_json.h" + +namespace fs = std::filesystem; + +namespace OHOS { +namespace AppPackingTool { + +const std::string MODULE_JSON = "module.json"; +const std::string CONFIG_JSON = "config.json"; +const std::string PATCH_JSON = "patch.json"; + +class JsonUtils { +public: + JsonUtils(); + virtual ~JsonUtils(); + + JsonUtils(const JsonUtils &) = delete; + JsonUtils &operator=(const JsonUtils &) = delete; + + static bool CheckFileName(const std::string& filePath, const std::string& fileName); + static bool IsModuleJson(const std::string& filePath); + static bool IsConfigJson(const std::string& filePath); + static bool IsPatchJson(const std::string& filePath); + + static std::unique_ptr JsonFromFile(const std::string& filePath); + static bool JsonToFile(const std::unique_ptr& json, const std::string& filePath); + static bool StrToFile(const std::string& str, const std::string& filePath); +}; + +} // namespace AppPackingTool +} // namespace OHOS + +#endif // DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_JSON_UTILS_H diff --git a/ohos_packing_tool/frameworks/include/json/module_api_version.h b/ohos_packing_tool/frameworks/include/json/module_api_version.h new file mode 100644 index 0000000000000000000000000000000000000000..503514cebb6058d493d79818ccc77e5b9b004cad --- /dev/null +++ b/ohos_packing_tool/frameworks/include/json/module_api_version.h @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2024 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. + */ + +#ifndef DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_MODULE_API_VERSION_H +#define DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_MODULE_API_VERSION_H + +#include + +namespace OHOS { +namespace AppPackingTool { +struct ModuleApiVersion { + int compatibleApiVersion = -1; + int targetApiVersion = -1; + std::string releaseType = ""; +}; +} // namespace AppPackingTool +} // namespace OHOS + +#endif // DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_MODULE_API_VERSION_H diff --git a/ohos_packing_tool/frameworks/include/json/module_json.h b/ohos_packing_tool/frameworks/include/json/module_json.h new file mode 100644 index 0000000000000000000000000000000000000000..85c83d45898508696dc5c642cc0e5274323bc30d --- /dev/null +++ b/ohos_packing_tool/frameworks/include/json/module_json.h @@ -0,0 +1,193 @@ +/* + * Copyright (c) 2024 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. + */ + +#ifndef DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_MODULE_JSON_H +#define DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_MODULE_JSON_H + +#include +#include +#include +#include +#include "pt_json.h" +#include "version.h" +#include "module_api_version.h" +#include "hap_verify_info.h" +#include "module_metadata_info.h" +#include "normalize_version.h" + +namespace OHOS { +namespace AppPackingTool { + +class ModuleJson { +public: + ModuleJson(); + virtual ~ModuleJson(); + + // ModuleJson(const ModuleJson &) = delete; + // ModuleJson &operator=(const ModuleJson &) = delete; + + bool ParseFromString(const std::string& jsonString); + bool ParseFromFile(const std::string& jsonFile); + std::string ToString(); + void Release(); + + bool IsValid(); + + // object funcs + bool GetAppObject(std::unique_ptr& appObj); + bool GetVersionObject(std::unique_ptr& versionObj); + bool GetModuleObject(std::unique_ptr& moduleObj); + bool GetDistroObject(std::unique_ptr& distroObj); + bool GetDistroObjectByModuleObj(std::unique_ptr& moduleObj, std::unique_ptr& distroObj); + bool GetApiVersionObject(std::unique_ptr& apiVersion); + + // stage funcs, module.json + bool GetStageVersion(Version& version); + bool GetStageVersionByAppObj(std::unique_ptr& obj, Version& version); + bool SetStageVersionCode(const int& versionCode); + bool SetStageVersionName(const std::string& versionName); + bool GetStageModuleApiVersion(ModuleApiVersion& moduleApiVersion); + bool GetStageModuleApiVersionByAppObj(std::unique_ptr& obj, ModuleApiVersion& moduleApiVersion); + bool GetStageModuleName(std::string& moduleName); + bool GetStageModuleNameByModuleObj(std::unique_ptr& obj, std::string& moduleName); + bool GetStageEntry(std::list& deviceTypes); + bool GetStageDeviceTypes(std::list& deviceTypes); + bool GetStageDeviceTypesByModuleObj(std::unique_ptr& obj, std::list& deviceTypes); + bool GetStageHapVerifyInfo(HapVerifyInfo& hapVerifyInfo); + bool GetStageDistroFilter(DistroFilter& distroFilter); + bool GetStageDistroFilterByModuleObj(std::unique_ptr& obj, DistroFilter& distroFilter); + bool GetStageInstallationFree(bool& installationFree); + bool GetStageInstallationFreeByModuleObj(std::unique_ptr& obj, bool& installationFree); + bool GetStageBundleType(std::string& bundleType); + bool GetStageCompileSdkType(std::string& compileSdkType); + bool GetStageCompileSdkTypeByAppObj(std::unique_ptr& obj, std::string& compileSdkType); + bool GetStageCompileSdkVersion(std::string& compileSdkVersion); + bool GetStageCompileSdkVersionByAppObj(std::unique_ptr& obj, std::string& compileSdkVersion); + bool GetStageModuleType(std::string& moduleType); + bool GetStageModuleTypeByModuleObj(std::unique_ptr& obj, std::string& moduleType); + bool GetStageDebug(bool& debug); + bool GetStageDebugByAppObj(std::unique_ptr& obj, bool& debug); + bool GetStageAsanEnabled(bool& asanEnabled); + bool GetStageAsanEnabledByAppObj(std::unique_ptr& obj, bool& asanEnabled); + bool GetStageTsanEnabled(bool& tsanEnabled); + bool GetStageTsanEnabledByAppObj(std::unique_ptr& obj, bool& tsanEnabled); + bool GetStageApiReleaseType(std::string& apiReleaseType); + bool GetStageApiReleaseTypeByAppObj(std::unique_ptr& obj, std::string& apiReleaseType); + bool GetStageCompressNativeLibs(bool& compressNativeLibs); + bool GetStageCompressNativeLibsByAppObj(std::unique_ptr& obj, bool& compressNativeLibs); + bool GetAbilityContinueTypeMap(std::map>& abilityContinueTypeMap); + bool GetAbilityContinueTypeMapByModuleObj(std::unique_ptr& obj, std::map>& abilityContinueTypeMap); + bool GetExtensionAbilityNames(std::list& extensionAbilityNames); + bool GetExtensionAbilityNamesByModuleObj(std::unique_ptr& obj, std::list& extensionAbilityNames); + bool GetModuleMetadatas(std::list& moduleMetadataInfos); + bool GetModuleMetadatasByModuleObj(std::unique_ptr& obj, std::list& moduleMetadataInfos); + bool ParseModuleMetadatasToDistroFilter(const std::list& moduleMetadataInfos, DistroFilter& distroFilter); + + bool IsExistedStageRequestPermissions(); + bool IsExistedStageModuleTargetPriority(); + bool IsExistedStageAppTargetPriority(); + + // fa funcs config.json + bool GetFaVersion(Version& version); + bool GetFaVersionByAppObj(std::unique_ptr& obj, Version& version); + bool GetFaVersionByVersionObj(std::unique_ptr& obj, Version& version); + bool SetFaVersionCode(const int& versionCode); + bool SetFaVersionName(const std::string& versionName); + bool GetFaInstallationFree(bool& installationFree); + bool GetFaInstallationFreeByModuleObj(std::unique_ptr& obj, bool& installationFree); + bool GetFaInstallationFreeByDistroObj(std::unique_ptr& obj, bool& installationFree); + bool GetFaBundleType(std::string& bundleType); + bool GetFaModuleApiVersion(ModuleApiVersion& moduleApiVersion); + bool GetFaModuleApiVersionByAppObj(std::unique_ptr& obj, ModuleApiVersion& moduleApiVersion); + bool GetFaModuleApiVersionByApiVersionObj(std::unique_ptr& obj, ModuleApiVersion& moduleApiVersion); + bool GetFaModuleName(std::string& faModuleName); + bool GetFaModuleNameByModuleObj(std::unique_ptr& obj, std::string& faModuleName); + bool GetFaModuleNameByDistroObj(std::unique_ptr& obj, std::string& faModuleName); + bool GetFaPackageStr(std::string& packageStr); + bool GetFaPackageStrByModuleObj(std::unique_ptr& obj, std::string& packageStr); + bool GetFaEntry(std::list& deviceTypes); + bool GetFaDeviceTypes(std::list& deviceTypes); + bool GetFaDeviceTypesByModuleObj(std::unique_ptr& obj, std::list& deviceTypes); + bool GetFaHapVerifyInfo(HapVerifyInfo& hapVerifyInfo); + bool GetFaDistroFilter(DistroFilter& distroFilter); + bool GetFaDistroFilterByModuleObj(std::unique_ptr& obj, DistroFilter& distroFilter); + bool GetFaCompileSdkType(std::string& compileSdkType); + bool GetFaCompileSdkTypeByAppObj(std::unique_ptr& obj, std::string& compileSdkType); + bool GetFaCompileSdkVersion(std::string& compileSdkVersion); + bool GetFaCompileSdkVersionByAppObj(std::unique_ptr& obj, std::string& compileSdkVersion); + bool GetFaModuleType(std::string& moduleType); + bool GetFaModuleTypeByModuleObj(std::unique_ptr& obj, std::string& moduleType); + bool GetFaDebug(bool& debug); + bool GetFaDebugByAppObj(std::unique_ptr& obj, bool& debug); + bool GetFaAsanEnabled(bool& asanEnabled); + bool GetFaAsanEnabledByAppObj(std::unique_ptr& obj, bool& asanEnabled); + bool GetFaReleaseType(std::string& releaseType); + bool GetFaReleaseTypeByAppObj(std::unique_ptr& obj, std::string& releaseType); + + // common funcs + bool GetBundleName(std::string& bundleName); + bool GetBundleNameByAppObj(std::unique_ptr& obj, std::string& bundleName); + bool SetBundleName(const std::string& bundleName); + bool GetModuleName(std::string& moduleName); + bool GetModuleNameByModuleObj(std::unique_ptr& obj, std::string& moduleName); + bool GetVendor(std::string& vendor); + bool GetVendorByAppObj(std::unique_ptr& obj, std::string& vendor); + bool GetTargetBundleName(std::string& targetBundleName); + bool GetTargetBundleNameByAppObj(std::unique_ptr& obj, std::string& targetBundleName); + bool GetTargetModuleName(std::string& targetModuleName); + bool GetTargetModuleNameByModuleObj(std::unique_ptr& obj, std::string& targetModuleName); + bool GetTargetPriority(int32_t& targetPriority); + bool GetTargetPriorityByAppObj(std::unique_ptr& obj, int32_t& targetPriority); + bool GetTargetModulePriority(int32_t& targetModulePriority); + bool GetTargetModulePriorityByModuleObj(std::unique_ptr& obj, int32_t& targetModulePriority); + // config.json or module.json + bool GetAbilityNames(std::list& abilityNames); + bool GetAbilityNamesByModuleObj(std::unique_ptr& obj, std::list& abilityNames); + // unknown + bool GetDependencyItems(std::list& dependencyItems, const std::string& defaultBundleName); + bool GetDependencyItemsByModuleObj(std::unique_ptr& obj, std::list& dependencyItems, const std::string& defaultBundleName); + bool GetAtomicServicePreloads(std::list& preloadItems); + bool GetAtomicServicePreloadsByModuleObj(std::unique_ptr& obj, std::list& preloadItems); + bool GetProxyDataUris(std::list& proxyDataUris); + bool GetProxyDataUrisByModuleObj(std::unique_ptr& obj, std::list& proxyDataUris); + bool GetProxyDataUrisByProxyDatasObj(std::unique_ptr& obj, std::list& proxyDataUris); + bool GetMultiAppMode(MultiAppMode& multiAppMode); + bool GetMultiAppModeByAppObj(std::unique_ptr& obj, MultiAppMode& multiAppMode); + + bool IsModuleAtomicServiceValid(); + bool CheckEntryInAtomicService(); + bool CheckAtomicServiceInstallationFree(); + + bool GetGenerateBuildHash(bool& generateBuildHash); + bool RemoveGenerateBuildHash(); + bool RemoveGenerateBuildHashFromAppObj(); + bool RemoveGenerateBuildHashFromModuleObj(); + + bool GetNormalizeVersion(NormalizeVersion& normalizeVersion, const bool& isStage = true); + bool SetVersionCodeAndName(const int& versionCode, const std::string& versionName, const bool& isStage = true); + + bool SetBuildHash(const std::string& buildHash); + + // json function for hqf + bool GetPatchModuleName(std::string& patchModuleName); + +private: + std::unique_ptr root_ = nullptr; +}; + +} // namespace AppPackingTool +} // namespace OHOS + +#endif // DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_MODULE_JSON_H diff --git a/ohos_packing_tool/frameworks/include/json/module_json_utils.h b/ohos_packing_tool/frameworks/include/json/module_json_utils.h new file mode 100644 index 0000000000000000000000000000000000000000..f3d4b4f2b00494de3841bc6d58c06782e40e2a7f --- /dev/null +++ b/ohos_packing_tool/frameworks/include/json/module_json_utils.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2024 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. + */ + +#ifndef DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_MODULE_JSON_UTILS_H +#define DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_MODULE_JSON_UTILS_H + +#include +#include "hap_verify_info.h" + +namespace OHOS { +namespace AppPackingTool { + +class ModuleJsonUtils { +public: + ModuleJsonUtils(); + virtual ~ModuleJsonUtils(); + + ModuleJsonUtils(const ModuleJsonUtils &) = delete; + ModuleJsonUtils &operator=(const ModuleJsonUtils &) = delete; + + static bool GetStageHapVerifyInfo(const std::string& hapFilePath, HapVerifyInfo& hapVerifyInfo); + static bool GetFaHapVerifyInfo(const std::string& hapFilePath, HapVerifyInfo& hapVerifyInfo); + static bool CheckHapsIsValid(const std::list& fileList, const bool& isSharedApp); + static bool IsModuleHap(const std::string hapFilePath); + +private: + static bool CheckSharedAppIsValid(const std::list& hapVerifyInfos, bool& isOverlay); + static bool CheckDuplicatedIsValid(const HapVerifyInfo& hapVerifyInfo1, const HapVerifyInfo& hapVerifyInfo2); + static bool CheckPolicyValueDisjoint(const PolicyValue& policyValue1, const PolicyValue& policyValue2); +}; +} // namespace AppPackingTool +} // namespace OHOS + +#endif // DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_MODULE_JSON_UTILS_H diff --git a/ohos_packing_tool/frameworks/include/json/module_metadata_info.h b/ohos_packing_tool/frameworks/include/json/module_metadata_info.h new file mode 100644 index 0000000000000000000000000000000000000000..04ca65f9ce4c78f168897836b662c85cfb41679e --- /dev/null +++ b/ohos_packing_tool/frameworks/include/json/module_metadata_info.h @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2024 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. + */ + +#ifndef DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_MODULE_METADATA_INFO_H +#define DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_MODULE_METADATA_INFO_H + +#include + +namespace OHOS { +namespace AppPackingTool { +struct ModuleMetadataInfo { + std::string name = ""; + std::string value = ""; + std::string resource = ""; +}; +} // namespace AppPackingTool +} // namespace OHOS + +#endif // DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_MODULE_METADATA_INFO_H diff --git a/ohos_packing_tool/frameworks/include/json/multi_app_mode.h b/ohos_packing_tool/frameworks/include/json/multi_app_mode.h new file mode 100644 index 0000000000000000000000000000000000000000..973488a161113520d849a5132d6cf53fcc911323 --- /dev/null +++ b/ohos_packing_tool/frameworks/include/json/multi_app_mode.h @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2024 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. + */ + +#ifndef DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_MULTI_APP_MODE_H +#define DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_MULTI_APP_MODE_H + +#include + +namespace OHOS { +namespace AppPackingTool { +// java : java has two override functions: equals() and hashCode() +struct MultiAppMode { + std::string multiAppModeType = ""; + int maxCount = 0; +}; +} // namespace AppPackingTool +} // namespace OHOS + +#endif // DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_MULTI_APP_MODE_H diff --git a/ohos_packing_tool/frameworks/include/json/normalize_version.h b/ohos_packing_tool/frameworks/include/json/normalize_version.h new file mode 100644 index 0000000000000000000000000000000000000000..1c149ed2c423c489f66ab9b8584a08790642fc0d --- /dev/null +++ b/ohos_packing_tool/frameworks/include/json/normalize_version.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2024 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. + */ + +#ifndef DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_NORMALIZE_VERSION_H +#define DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_NORMALIZE_VERSION_H + +#include +#include "pt_json.h" +namespace { + const std::string ORIGIN_VERSION_CODE = "originVersionCode"; + const std::string ORIGIN_VERSION_NAME = "originVersionName"; + const std::string MODULE_NAME = "moduleName"; +} + +namespace OHOS { +namespace AppPackingTool { +struct NormalizeVersion { + int originVersionCode = -1; + std::string originVersionName = ""; + std::string moduleName = ""; +}; +} // namespace AppPackingTool +} // namespace OHOS + +#endif // DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_NORMALIZE_VERSION_H diff --git a/ohos_packing_tool/frameworks/include/json/normalize_version_utils.h b/ohos_packing_tool/frameworks/include/json/normalize_version_utils.h new file mode 100644 index 0000000000000000000000000000000000000000..342a0cfebe8714825668760e642e9e9b042d5279 --- /dev/null +++ b/ohos_packing_tool/frameworks/include/json/normalize_version_utils.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2024 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. + */ + +#ifndef DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_NORMALIZE_VERSION_UTILS_H +#define DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_NORMALIZE_VERSION_UTILS_H + +#include +#include +#include "normalize_version.h" + +namespace OHOS { +namespace AppPackingTool { +class NormalizeVersionUtils { +public: + NormalizeVersionUtils(); + virtual ~NormalizeVersionUtils(); + + NormalizeVersionUtils(const NormalizeVersionUtils &) = delete; + NormalizeVersionUtils &operator=(const NormalizeVersionUtils &) = delete; + + static std::string ToString(const NormalizeVersion& normalizeVersion); + static std::string ArrayToString(const std::list& normalizeVersions); +}; +} // namespace AppPackingTool +} // namespace OHOS + +#endif // DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_NORMALIZE_VERSION_UTILS_H diff --git a/ohos_packing_tool/frameworks/include/json/pack_info.h b/ohos_packing_tool/frameworks/include/json/pack_info.h new file mode 100644 index 0000000000000000000000000000000000000000..ada57a1dfa251f94b74246fa0d6cdc2a90de7551 --- /dev/null +++ b/ohos_packing_tool/frameworks/include/json/pack_info.h @@ -0,0 +1,95 @@ +/* + * Copyright (c) 2024 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. + */ + +#ifndef DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_PACK_INFO_H +#define DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_PACK_INFO_H + +#include +#include +#include "pt_json.h" + +namespace OHOS { +namespace AppPackingTool { +struct PackInfoVersion { + int code = -1; + std::string name = ""; +}; + +class PackInfo { +public: + PackInfo(); + virtual ~PackInfo(); + + // PackInfo(const PackInfo &) = delete; + // PackInfo &operator=(const PackInfo &) = delete; + + bool ParseFromString(const std::string& jsonString); + bool ParseFromFile(const std::string& jsonFile); + std::string ToString(); + void Release(); + + bool IsValid(); + + // object funcs + bool GetSummaryObject(std::unique_ptr& summaryObj); + bool GetPackagesObject(std::unique_ptr& packagesObj); + bool GetAppObject(std::unique_ptr& appObj); + bool GetModulesObject(std::unique_ptr& modulesObj); + bool GetVersionObject(std::unique_ptr& versionObj); + bool GetDistroObject(int32_t moduleIndex, std::unique_ptr& distroObj); + bool GetDistroObjectByModulesObj(const std::unique_ptr& modulesObj, + int32_t moduleIndex, std::unique_ptr& distroObj); + bool GetDistroObjectByModuleObj(const std::unique_ptr& moduleObj, + std::unique_ptr& distroObj); + bool GetPackageObject(int32_t packageIndex, std::unique_ptr& packageObj); + bool GetExtensionAbilitiesObj(int32_t moduleIndex, std::unique_ptr& extensionAbilitiesObj); + bool GetExtensionAbilitiesObjByModulesObj(const std::unique_ptr& modulesObj, + int32_t moduleIndex, std::unique_ptr& extensionAbilitiesObj); + bool GetExtensionAbilitiesObjByModuleObj(const std::unique_ptr& moduleObj, + std::unique_ptr& extensionAbilitiesObj); + bool GetFormsObjByExtensionAbilityObj(const std::unique_ptr& extensionAbilityObj, + std::unique_ptr& formsObj); + + // funcs + bool GetBundleName(std::string& bundleName); + bool GetBundleNameByAppObj(const std::unique_ptr& appObj, std::string& bundleName); + bool SetBundleName(const std::string& bundleName); + bool GetBundleType(std::string& bundleType, const std::string& defaultBundleType); + bool GetBundleTypeByAppObj(const std::unique_ptr& appObj, std::string& bundleType, + const std::string& defaultBundleType); + bool GetVersion(PackInfoVersion& version); + bool GetVersionByVersionObj(const std::unique_ptr& appObj, PackInfoVersion& version); + bool SetVersionCode(const int& versionCode); + bool SetVersionName(const std::string& versionName); + bool GetModuleNameByDistroObj(const std::unique_ptr& distroObj, std::string& moduleName); + bool GetNameByPackageObj(const std::unique_ptr& packageObj, std::string& name); + bool GetNameByFormObj(const std::unique_ptr& formObj, std::string& name); + bool GetDefaultDimensionByFormObj(const std::unique_ptr& formObj, std::string& defaultDimension); + bool GetSupportDimensionsByFormObj(const std::unique_ptr& formObj, std::list& supportDimensions); + bool GetFormNames(std::list& formNames, std::list& formFullNames); + bool GetFormNamesByExtensionAbilitiesObj(const std::unique_ptr& extensionAbilitiesObj, + std::string moduleName, std::list& formNames, std::list& formFullNames); + bool GetFormNamesByFormsObj(const std::unique_ptr& formsObj, + std::string moduleName, std::list& formNames, std::list& formFullNames); + + +private: + std::unique_ptr root_ = nullptr; +}; + +} // namespace AppPackingTool +} // namespace OHOS + +#endif // DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_PACK_INFO_H diff --git a/ohos_packing_tool/frameworks/include/json/pack_info_utils.h b/ohos_packing_tool/frameworks/include/json/pack_info_utils.h new file mode 100644 index 0000000000000000000000000000000000000000..ff83b1d5053e9ab5f8179ab3392fc1929362356d --- /dev/null +++ b/ohos_packing_tool/frameworks/include/json/pack_info_utils.h @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2024 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. + */ + +#ifndef DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_PACK_INFO_UTILS_H +#define DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_PACK_INFO_UTILS_H + +#include +#include +#include "pt_json.h" +#include "pack_info.h" + +namespace OHOS { +namespace AppPackingTool { + +class PackInfoUtils { +public: + PackInfoUtils(); + virtual ~PackInfoUtils(); + + PackInfoUtils(const PackInfoUtils &) = delete; + PackInfoUtils &operator=(const PackInfoUtils &) = delete; + + static bool MergeTwoPackInfos(std::string& srcPackInfoJsonStr1, std::string& srcPackInfoJsonStr2, + std::string& dstPackInfoJsonStr); + static bool MergeTwoPackInfosByPackagePair(std::string& srcPackInfoJsonStr1, std::string& srcPackInfoJsonStr2, + std::map, std::string& dstPackInfoJsonStr); + +private: + static bool MergeTwoPackInfos(PackInfo& srcPackInfo1, PackInfo& srcPackInfo2); + static bool MergeTwoPackInfosByPackagePair(PackInfo& srcPackInfo1, PackInfo& srcPackInfo2, + std::string& packageName, std::string& moduleName); + static bool VerifyPackInfos(PackInfo& finalPackInfo, PackInfo& srcPackInfo); + static bool CheckBundleNameInPackInfo(PackInfo& packInfo1, PackInfo& packInfo2); + static bool CheckBundleTypeInPackInfo(PackInfo& packInfo1, PackInfo& packInfo2); + static bool CheckVersionCodeInPackInfo(PackInfo& packInfo1, PackInfo& packInfo2); +}; + +} // namespace AppPackingTool +} // namespace OHOS + +#endif // DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_MODULE_JSON_UTILS_H diff --git a/ohos_packing_tool/frameworks/include/json/patch_json.h b/ohos_packing_tool/frameworks/include/json/patch_json.h new file mode 100644 index 0000000000000000000000000000000000000000..0e1a5a70984830fca528db8cfa3b98b3a1e50e2d --- /dev/null +++ b/ohos_packing_tool/frameworks/include/json/patch_json.h @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2024 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. + */ + +#ifndef DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_PATCH_JSON_H +#define DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_PATCH_JSON_H + +#include +#include +#include "pt_json.h" +#include "hqf_info.h" + +namespace OHOS { +namespace AppPackingTool { + +class PatchJson { +public: + PatchJson(); + virtual ~PatchJson(); + + // PatchJson(const PatchJson &) = delete; + // PatchJson &operator=(const PatchJson &) = delete; + + bool ParseFromString(const std::string& jsonString); + bool ParseFromFile(const std::string& jsonFile); + std::string ToString(); + void Release(); + + bool IsValid(); + + // // object funcs + bool GetAppObject(std::unique_ptr& appObj); + bool GetModuleObject(std::unique_ptr& moduleObj); + + // app funcs + bool GetBundleName(std::string& bundleName); + bool GetBundleNameByAppObj(const std::unique_ptr& appObj, std::string& bundleName); + bool GetVersionCode(int32_t& versionCode); + bool GetVersionCodeByAppObj(const std::unique_ptr& appObj, int32_t& versionCode); + bool GetVersionName(std::string& versionName); + bool GetVersionNameByAppObj(const std::unique_ptr& appObj, std::string& versionName); + bool GetPatchVersionCode(int32_t& patchVersionCode); + bool GetPatchVersionCodeByAppObj(const std::unique_ptr& appObj, int32_t& patchVersionCode); + bool GetPatchVersionName(std::string& patchVersionName); + bool GetPatchVersionNameByAppObj(const std::unique_ptr& appObj, std::string& patchVersionName); + + // module funcs + bool GetName(std::string& name); + bool GetNameByModuleObj(const std::unique_ptr& moduleObj, std::string& name); + bool GetType(std::string& type); + bool GetTypeByModuleObj(const std::unique_ptr& moduleObj, std::string& type); + bool GetDeviceTypes(std::list& deviceTypes); + bool GetDeviceTypesByModuleObj(const std::unique_ptr& moduleObj, std::list& deviceTypes); + bool GetOriginalModuleHash(std::string& originalModuleHash); + bool GetOriginalModuleHashByModuleObj(const std::unique_ptr& moduleObj, std::string& originalModuleHash); + + bool GetHqfInfo(HqfInfo& hqfInfo); + +private: + std::unique_ptr root_ = nullptr; +}; + +} // namespace AppPackingTool +} // namespace OHOS + +#endif // DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_PATCH_JSON_H diff --git a/ohos_packing_tool/frameworks/include/json/patch_json_utils.h b/ohos_packing_tool/frameworks/include/json/patch_json_utils.h new file mode 100644 index 0000000000000000000000000000000000000000..2e08b4f7a1cba71cd5c442b2959ffdbbae5b03db --- /dev/null +++ b/ohos_packing_tool/frameworks/include/json/patch_json_utils.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2024 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. + */ + +#ifndef DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_PATCH_JSON_UTILS_H +#define DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_PATCH_JSON_UTILS_H + +#include +#include +#include "patch_json.h" +#include "hqf_info.h" + +namespace OHOS { +namespace AppPackingTool { + +class PatchJsonUtils { +public: + PatchJsonUtils(); + virtual ~PatchJsonUtils(); + + PatchJsonUtils(const PatchJsonUtils &) = delete; + PatchJsonUtils &operator=(const PatchJsonUtils &) = delete; + + static bool ParsePatchByJsonFilePath(const std::string& patchJsonFilePath, HqfInfo& hqfInfo); + static bool ParsePatchByJsonStr(const std::string& patchJsonStr, HqfInfo& hqfInfo); +}; + +} // namespace AppPackingTool +} // namespace OHOS + +#endif // DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_PATCH_JSON_UTILS_H diff --git a/ohos_packing_tool/frameworks/include/json/preload_item.h b/ohos_packing_tool/frameworks/include/json/preload_item.h new file mode 100644 index 0000000000000000000000000000000000000000..fc7487890facde6f892ed8e7128c627dec08f935 --- /dev/null +++ b/ohos_packing_tool/frameworks/include/json/preload_item.h @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2024 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. + */ + +#ifndef DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_PRELOAD_ITEM_H +#define DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_PRELOAD_ITEM_H + +#include + +namespace OHOS { +namespace AppPackingTool { +struct PreloadItem { + std::string moduleName = ""; +}; +} // namespace AppPackingTool +} // namespace OHOS + +#endif // DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_PRELOAD_ITEM_H diff --git a/ohos_packing_tool/frameworks/include/json/pt_json.h b/ohos_packing_tool/frameworks/include/json/pt_json.h new file mode 100644 index 0000000000000000000000000000000000000000..6a993e7a6321716a91e92e03ef7e9a84bb477107 --- /dev/null +++ b/ohos_packing_tool/frameworks/include/json/pt_json.h @@ -0,0 +1,126 @@ +/* + * Copyright (c) 2022 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. + */ + +#ifndef ECMASCRIPT_TOOLING_BASE_PT_JSON_H +#define ECMASCRIPT_TOOLING_BASE_PT_JSON_H + +#include +#include + +#include "cJSON.h" + +namespace OHOS { +namespace AppPackingTool { + +enum class Result : uint8_t { + SUCCESS, + NOT_EXIST, + TYPE_ERROR, +}; + +class PtJson { +public: + PtJson() = default; + explicit PtJson(cJSON *object) : object_(object) {} + ~PtJson() = default; + + // Create empty json object + static std::unique_ptr CreateObject(); + static std::unique_ptr CreateArray(); + + // Release cJSON object memory + void ReleaseRoot(); + + // String parse to json + static std::unique_ptr Parse(const std::string &data); + + // To string + std::string Stringify() const; + + // Add Json child + bool Add(const char *key, bool value) const; + bool Add(const char *key, int32_t value) const; + bool Add(const char *key, int64_t value) const; + bool Add(const char *key, uint32_t value) const; + bool Add(const char *key, double value) const; + bool Add(const char *key, const char *value) const; + bool Add(const char *key, const std::unique_ptr &value) const; + + // Push back to array + bool Push(bool value) const; + bool Push(int32_t value) const; + bool Push(int64_t value) const; + bool Push(uint32_t value) const; + bool Push(double value) const; + bool Push(const char *value) const; + bool Push(const std::unique_ptr &value) const; + + // Remove Json child + bool Remove(const char *key) const; + + bool Contains(const char *key) const; + + std::string GetKey() const; + + cJSON *GetJson() const; + + // Type check + bool IsBool() const; + bool IsNumber() const; + bool IsString() const; + bool IsObject() const; + bool IsArray() const; + bool IsNull() const; + + // Object accessor + bool GetBool(bool defaultValue = false) const; + int32_t GetInt(int32_t defaultValue = 0) const; + int64_t GetInt64(int64_t defaultValue = 0) const; + uint32_t GetUInt(uint32_t defaultValue = 0) const; + uint64_t GetUInt64(uint64_t defaultValue = 0) const; + double GetDouble(double defaultValue = 0.0) const; + std::string GetString() const; + + // Array accessor + int32_t GetSize() const; + std::unique_ptr Get(int32_t index) const; + + // Child item accessor + Result GetBool(const char *key, bool *value) const; + Result GetInt(const char *key, int32_t *value) const; + Result GetInt64(const char *key, int64_t *value) const; + Result GetUInt(const char *key, uint32_t *value) const; + Result GetUInt64(const char *key, uint64_t *value) const; + Result GetDouble(const char *key, double *value) const; + Result GetString(const char *key, std::string *value) const; + Result GetObject(const char *key, std::unique_ptr *value) const; + Result GetArray(const char *key, std::unique_ptr *value) const; + Result GetAny(const char *key, std::unique_ptr *value) const; + + Result SetBool(const char *key, const bool& value); + Result SetInt(const char *key, const int32_t& value); + Result SetInt64(const char *key, const int64_t& value); + Result SetUInt(const char *key, const uint32_t& value); + Result SetUInt64(const char *key, const uint64_t& value); + Result SetDouble(const char *key, const double& value); + Result SetString(const char *key, const std::string& value); + +private: + cJSON *object_ = nullptr; +}; +} // namespace AppPackingTool +} // namespace OHOS + +#endif // ECMASCRIPT_TOOLING_BASE_PT_JSON_H diff --git a/ohos_packing_tool/frameworks/include/json/version.h b/ohos_packing_tool/frameworks/include/json/version.h new file mode 100644 index 0000000000000000000000000000000000000000..1051ca11838e208c23193fe5ae5795cd1ccb39b3 --- /dev/null +++ b/ohos_packing_tool/frameworks/include/json/version.h @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2024 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. + */ + +#ifndef DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_VERSION_H +#define DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_VERSION_H + +#include + +namespace OHOS { +namespace AppPackingTool { +struct Version { + int versionCode = -1; + std::string versionName = ""; + int minCompatibleVersionCode = -1; +}; +} // namespace AppPackingTool +} // namespace OHOS + +#endif // DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_VERSION_H diff --git a/ohos_packing_tool/frameworks/include/log.h b/ohos_packing_tool/frameworks/include/log.h new file mode 100644 index 0000000000000000000000000000000000000000..c8b9e0094467337066e39cd4740382806efdb413 --- /dev/null +++ b/ohos_packing_tool/frameworks/include/log.h @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2024 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. + */ + +#ifndef DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_LOG_H +#define DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_LOG_H + +namespace OHOS { +namespace AppPackingTool { + +enum LOG_LEVEL { + LOG_LEVEL_DEBUG = 0, + LOG_LEVEL_INFO, + LOG_LEVEL_WARN, + LOG_LEVEL_ERROR, + LOG_LEVEL_FATAL +}; + +void log(char *file, char *func, int line, int level, char *format, ...); + +#define LOGD(format, ...) log((char *)__FILE__, (char *)__func__, __LINE__, LOG_LEVEL_DEBUG, (char *)format, ##__VA_ARGS__) +#define LOGI(format, ...) log((char *)__FILE__, (char *)__func__, __LINE__, LOG_LEVEL_INFO, (char *)format, ##__VA_ARGS__) +#define LOGW(format, ...) log((char *)__FILE__, (char *)__func__, __LINE__, LOG_LEVEL_WARN, (char *)format, ##__VA_ARGS__) +#define LOGE(format, ...) log((char *)__FILE__, (char *)__func__, __LINE__, LOG_LEVEL_ERROR, (char *)format, ##__VA_ARGS__) +#define LOGF(format, ...) log((char *)__FILE__, (char *)__func__, __LINE__, LOG_LEVEL_FATAL, (char *)format, ##__VA_ARGS__) + +} // namespace AppPackingTool +} // namespace OHOS + +#endif // DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_ZIP_UTILS_H diff --git a/ohos_packing_tool/frameworks/include/packageNormalize.h b/ohos_packing_tool/frameworks/include/packageNormalize.h new file mode 100644 index 0000000000000000000000000000000000000000..14dcca8b9a47b4f6510d91db0670648b7062520d --- /dev/null +++ b/ohos_packing_tool/frameworks/include/packageNormalize.h @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2024 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. + */ + +#ifndef DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_PACKAGE_NORMALIZE_H +#define DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_PACKAGE_NORMALIZE_H +#include +#include +#include + +#include "packager.h" +#include "unzip_wrapper.h" +#include "zip_wrapper.h" +#include "json/module_json.h" +#include "json/normalize_version.h" +namespace OHOS { +namespace AppPackingTool { +class PackageNormalize : public Packager { +public: + PackageNormalize(const std::map ¶meterMap, std::string &resultReceiver); + ~PackageNormalize() override {} +protected: + int InitAllowedParam() override; + int PreProcess() override; + int Process() override; + int PostProcess() override; +private: + void GerFormattedHspList(std::vector &inputList, std::string filePath); + bool ModifyModuleJson(const std::string &moduleJsonPath, + const int newVersionCode, const std::string newBundleName); + bool ModifyPackInfo(const std::string &moduleJsonPath, + const int newVersionCode, const std::string newBundleName); + ZipWrapper zipWrapper_; + UnzipWrapper unzipWrapper_; + std::vector hspList_; +}; + +} // namespace AppExecFwk +} // namespace OHOS + +#endif // DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_VERSION_NORMALIZE_H \ No newline at end of file diff --git a/ohos_packing_tool/frameworks/include/unzip_wrapper.h b/ohos_packing_tool/frameworks/include/unzip_wrapper.h new file mode 100644 index 0000000000000000000000000000000000000000..80eb008102359b4ba829b3f1af115c3fd0395989 --- /dev/null +++ b/ohos_packing_tool/frameworks/include/unzip_wrapper.h @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2024 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. + */ + +#ifndef DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_UNZIP_WRAPPER_H +#define DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_UNZIP_WRAPPER_H + +#include +#include "zip_constants.h" + +namespace OHOS { +namespace AppPackingTool { + +#define ZIP_FILE_ATTR_DIRECTORY 16 + +class UnzipWrapper { +public: + UnzipWrapper(); + explicit UnzipWrapper(std::string path); + virtual ~UnzipWrapper(); + + UnzipWrapper(const UnzipWrapper &) = delete; + UnzipWrapper &operator=(const UnzipWrapper &) = delete; + + int Open(std::string& unzPath); + int Open(); + void Close(); + int UnzipFile(std::string filePath); + + bool IsOpen() const { + return (unzFile_ != nullptr); + } + +protected: + std::string ExtractFile(const std::string filePath); + +private: + unzFile unzFile_ = nullptr; + unz_global_info64 unzGlobalInfo_; + std::string unzFilePath_; +}; + +} // namespace AppPackingTool +} // namespace OHOS + +#endif // DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_UNZIP_WRAPPER_H diff --git a/ohos_packing_tool/frameworks/include/utils.h b/ohos_packing_tool/frameworks/include/utils.h new file mode 100644 index 0000000000000000000000000000000000000000..6773536560de43c79ffb52866c1a377d404877ca --- /dev/null +++ b/ohos_packing_tool/frameworks/include/utils.h @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2024 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. + */ + +#ifndef DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_UTILS_H +#define DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_UTILS_H + +#include +#include +#include + +namespace OHOS { +namespace AppPackingTool { + +class Utils +{ +public: + Utils(); + virtual ~Utils(); + + Utils(const Utils &) = delete; + Utils &operator=(const Utils &) = delete; + + static std::string GetFileContent(const std::string filePath); + static std::string ListToString(const std::list& lst); + static std::string ReplaceAll(std::string str, const std::string& from, const std::string& to); + static int64_t GetFileLength(const std::string filePath); + static bool EndsWith(const std::string& str, const std::string& suffix); + static bool CheckDisjoint(const std::list& list1, const std::list& list2); + static bool CheckContainsAll(const std::list& list1, const std::list& list2); + static std::string GetSha256Str(const std::string &str); + static std::string GetSha256File(const std::string &filePath); + + static bool IsFileExists(const std::string& file); + static bool IsFile(const std::string& file); + static bool IsDirectory(const std::string& dir); + static bool RemoveFile(const std::string& file); + static bool RemoveDirectory(const std::string& dir); + static std::string GetFilePathByDir(const std::string& dir, const std::string& fileName); + static bool ForceCreateDirectory(const std::string& dir); + static bool ForceRemoveDirectory(const std::string& dir, bool isDeleteSelf = true); +}; + +} // namespace AppPackingTool +} // namespace OHOS + +#endif // DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_JSON_UTILS_H diff --git a/ohos_packing_tool/frameworks/include/zip_constants.h b/ohos_packing_tool/frameworks/include/zip_constants.h new file mode 100644 index 0000000000000000000000000000000000000000..91512b6bebef8801540f55a70bc93af2b0ca4a86 --- /dev/null +++ b/ohos_packing_tool/frameworks/include/zip_constants.h @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2024 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. + */ + +#ifndef DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_ZIP_CONSTANTS_H +#define DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_ZIP_CONSTANTS_H + +#include +#include +#include + +#include "contrib/minizip/zip.h" +#include "contrib/minizip/unzip.h" + +namespace fs = std::filesystem; + +namespace OHOS { +namespace AppPackingTool { + +enum ZipErrCode { + ZIP_ERR_SUCCESS = 0, + ZIP_ERR_FAILURE +}; + +enum class ZipLevel : int32_t { + ZIP_LEVEL_DEFAULT = Z_DEFAULT_COMPRESSION, + ZIP_LEVEL_0 = Z_NO_COMPRESSION, + ZIP_LEVEL_1 = Z_BEST_SPEED, + ZIP_LEVEL_2, + ZIP_LEVEL_3, + ZIP_LEVEL_4, + ZIP_LEVEL_5, + ZIP_LEVEL_6, + ZIP_LEVEL_7, + ZIP_LEVEL_8, + ZIP_LEVEL_9 = Z_BEST_COMPRESSION +}; +#define ZIP_LEVEL_NO_COMPRESS ZipLevel::ZIP_LEVEL_0 +#define ZIP_LEVEL_BEST_SPEED ZipLevel::ZIP_LEVEL_1 +#define ZIP_LEVEL_BEST_COMPRESS ZipLevel::ZIP_LEVEL_9 + +#define MAX_ZIP_BUFFER_SIZE 4096 + +} // namespace AppPackingTool +} // namespace OHOS + +#endif // DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_ZIP_CONSTANTS_H diff --git a/ohos_packing_tool/frameworks/include/zip_utils.h b/ohos_packing_tool/frameworks/include/zip_utils.h new file mode 100644 index 0000000000000000000000000000000000000000..3883e34e6322e73f4b237c457b8556c09b174591 --- /dev/null +++ b/ohos_packing_tool/frameworks/include/zip_utils.h @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2024 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. + */ + +#ifndef DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_ZIP_UTILS_H +#define DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_ZIP_UTILS_H + +#include "zip_wrapper.h" +#include "unzip_wrapper.h" + +namespace OHOS { +namespace AppPackingTool { + +class ZipUtils { +public: + ZipUtils(); + virtual ~ZipUtils(); + + ZipUtils(const ZipUtils &) = delete; + ZipUtils &operator=(const ZipUtils &) = delete; + + static int Zip(const std::string& filePath, const std::string& zipFilePath, + const std::string& zipPath = "", const ZipLevel& zipLevel = ZipLevel::ZIP_LEVEL_DEFAULT, + const int& append = APPEND_STATUS_CREATE); + + static int Unzip(const std::string& zipPath, const std::string& filePath); + + static bool IsFileExistsInZip(const std::string& zipFilePath, const std::string& filename); + static bool IsFileNameExistsInZip(const std::string& zipFilePath, const std::string& filename); + + static bool GetFileContentFromZip(const std::string& zipFilePath, const std::string& filename, + std::string& fileContent); + + static bool GetResourceMapFromZip(const std::string& zipFilePath, std::map& resourceMap); + +private: + static bool GetUnzipCurrentFileContent(unzFile& unzipFile, std::string& fileContent); +}; + +} // namespace AppPackingTool +} // namespace OHOS + +#endif // DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_ZIP_UTILS_H diff --git a/ohos_packing_tool/frameworks/include/zip_wrapper.h b/ohos_packing_tool/frameworks/include/zip_wrapper.h new file mode 100644 index 0000000000000000000000000000000000000000..33ab0db32785aa2da1754c45caf4f02ac3b2fa68 --- /dev/null +++ b/ohos_packing_tool/frameworks/include/zip_wrapper.h @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2024 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. + */ + +#ifndef DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_ZIP_WRAPPER_H +#define DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_ZIP_WRAPPER_H + +#include +#include +#include "zip_constants.h" + +namespace OHOS { +namespace AppPackingTool { + +class ZipWrapper { +public: + ZipWrapper(); + explicit ZipWrapper(std::string zipPath); + virtual ~ZipWrapper(); + + ZipWrapper(const ZipWrapper &) = delete; + ZipWrapper &operator=(const ZipWrapper &) = delete; + + int Open(std::string& zipPath, int append = APPEND_STATUS_CREATE); + int Open(int append = APPEND_STATUS_CREATE); + void Close(); + + void SetZipFilePath(std::string path) { + zipFilePath_ = path; + } + + void SetZipLevel(ZipLevel zipLevel) { + zipLevel_ = zipLevel; + } + + const zip_fileinfo& ZipFileInfo() const { + return zipFileInfo_; + } + + bool IsOpen() const { + return (zipFile_ != nullptr); + } + + int AddFileOrDirectoryToZip(const char *filePath, const char *zipPath); + int AddFileOrDirectoryToZip(const std::string &filePath, const std::string &zipPath); + int AddFileOrDirectoryToZip(const fs::path &fsFilePath, const fs::path &fsZipPath); + + int WriteStringToZip(const std::string &content, const std::string& zipPath); + +protected: + int AddFileToZip(const std::string &filePath, const std::string &zipPath); + int AddFileToZip(const fs::path &filePath, const fs::path &zipPath); + +private: + zipFile zipFile_ = nullptr; + zip_fileinfo zipFileInfo_ = {}; + std::string zipFilePath_; + ZipLevel zipLevel_ = ZipLevel::ZIP_LEVEL_DEFAULT; +}; + +} // namespace AppPackingTool +} // namespace OHOS + +#endif // DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_ZIP_WRAPPER_H diff --git a/ohos_packing_tool/frameworks/src/hap_packager.cpp b/ohos_packing_tool/frameworks/src/hap_packager.cpp index fda8730b78a34631cbb4b22bcfdfb85f3f48cd5a..fff4e1da32a5ea46f64eacf311f225cf4d4ccdf0 100644 --- a/ohos_packing_tool/frameworks/src/hap_packager.cpp +++ b/ohos_packing_tool/frameworks/src/hap_packager.cpp @@ -51,47 +51,49 @@ int HapPackager::Process() { std::cout << "Hap DoPackage" << std::endl; std::string outPath = parameterMap_.at(Constants::PARAM_OUT_PATH); - zipFile zf = zipOpen64(outPath.c_str(), APPEND_STATUS_CREATE); - if (zf == nullptr) { - std::cout << "err zipOpen64 null" << std::endl; + zipWrapper_.Open(outPath); + if (!zipWrapper_.IsOpen()) { + std::cout << "HapPackager::Process: zipWrapper Open failed!" << std::endl; return ERR_INVALID_VALUE; } - zip_fileinfo fi = {}; std::map::const_iterator it = parameterMap_.find(Constants::PARAM_JSON_PATH); if (it != parameterMap_.end()) { if (ParseJsonFile(moduleJson, it->second)) { - WriteStringToZip(zf, moduleJson.dump(), fs::path(Constants::MODULE_JSON), fi); + if (zipWrapper_.WriteStringToZip(moduleJson.dump(), Constants::MODULE_JSON) != ZipErrCode::ZIP_ERR_SUCCESS) { + std::cout << "HapPackager::Process: zipWrapper WriteStringToZip failed!" << std::endl; + return ERR_INVALID_VALUE; + } } } it = parameterMap_.find(Constants::PARAM_LIB_PATH); if (it != parameterMap_.end()) { - AddFileToZip(zf, fs::path(it->second), fs::path(Constants::LIB_PATH), fi); + zipWrapper_.AddFileOrDirectoryToZip(it->second, Constants::LIB_PATH); } it = parameterMap_.find(Constants::PARAM_RESOURCES_PATH); if (it != parameterMap_.end()) { - AddFileToZip(zf, fs::path(it->second), fs::path(Constants::RESOURCES_PATH), fi); + zipWrapper_.AddFileOrDirectoryToZip(it->second, Constants::RESOURCES_PATH); } it = parameterMap_.find(Constants::PARAM_INDEX_PATH); if (it != parameterMap_.end()) { - AddFileToZip(zf, fs::path(it->second), fs::path(Constants::RESOURCES_INDEX), fi); + zipWrapper_.AddFileOrDirectoryToZip(it->second, Constants::RESOURCES_INDEX); } it = parameterMap_.find(Constants::PARAM_PACK_INFO_PATH); if (it != parameterMap_.end()) { - AddFileToZip(zf, fs::path(it->second), fs::path(Constants::PACK_INFO), fi); + zipWrapper_.AddFileOrDirectoryToZip(it->second, Constants::PACK_INFO); } it = parameterMap_.find(Constants::PARAM_ETS_PATH); if (it != parameterMap_.end()) { - AddFileToZip(zf, fs::path(it->second), fs::path(Constants::ETS_PATH), fi); + zipWrapper_.AddFileOrDirectoryToZip(it->second, Constants::ETS_PATH); } it = parameterMap_.find(Constants::PARAM_RPCID_PATH); if (it != parameterMap_.end()) { - AddFileToZip(zf, fs::path(it->second), fs::path(Constants::RPCID_SC), fi); + zipWrapper_.AddFileOrDirectoryToZip(it->second, Constants::RPCID_SC); } it = parameterMap_.find(Constants::PARAM_PKG_CONTEXT_PATH); if (it != parameterMap_.end()) { - AddFileToZip(zf, fs::path(it->second), fs::path(Constants::PKG_CONTEXT_JSON), fi); + zipWrapper_.AddFileOrDirectoryToZip(it->second, Constants::PKG_CONTEXT_JSON); } - zipClose(zf, nullptr); + zipWrapper_.Close(); return ERR_OK; } diff --git a/ohos_packing_tool/frameworks/src/json/distro_filter.cpp b/ohos_packing_tool/frameworks/src/json/distro_filter.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2df1c5bc5d8674fb71b5d6d964b4bd115d8316ba --- /dev/null +++ b/ohos_packing_tool/frameworks/src/json/distro_filter.cpp @@ -0,0 +1,168 @@ +/* + * Copyright (c) 2024 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. + */ + +#include +#include +#include +#include "distro_filter.h" +#include "pt_json.h" +#include "utils.h" + +namespace OHOS { +namespace AppPackingTool { +namespace { + const std::string API_VERSION = "apiVersion"; + const std::string SCREEN_SHAPE = "screenShape"; + const std::string SCREEN_DENSITY = "screenDensity"; + const std::string SCREEN_WINDOW = "screenWindow"; + const std::string COUNTRY_CODE = "countryCode"; + const std::string POLICY = "policy"; + const std::string VALUE = "value"; +} + +bool PolicyValue::ParseFromString(const std::string& jsonString) +{ + if (jsonString.length() == 0) { + return false; + } + std::unique_ptr root = PtJson::Parse(jsonString); + if (!root) { + return false; + } + if (root->Contains(POLICY.c_str())) { + if (root->GetString(POLICY.c_str(), &policy) != Result::SUCCESS) { + return false; + } + } + if (root->Contains(VALUE.c_str())) { + std::unique_ptr valueObj; + if (root->GetArray(VALUE.c_str(), &valueObj) != Result::SUCCESS) { + return false; + } + for (int i = 0; i< valueObj->GetSize(); i++) { + value.push_back(valueObj->Get(i)->GetString()); + } + } + return true; +} + +bool PolicyValue::IsEmpty() const +{ + return policy.empty(); +} + +bool DistroFilter::ParseFromString(const std::string& jsonString) +{ + if (jsonString.length() == 0) { + return false; + } + std::unique_ptr root = PtJson::Parse(jsonString); + if (!root) { + return false; + } + if (root->Contains(API_VERSION.c_str())) { + std::string apiVersionJsonStr; + if (root->GetString(API_VERSION.c_str(), &apiVersionJsonStr) != Result::SUCCESS) { + return false; + } + if (!apiVersion.ParseFromString(apiVersionJsonStr)) { + return false; + } + } + if (root->Contains(SCREEN_SHAPE.c_str())) { + std::string screenShapeJsonStr; + if (root->GetString(SCREEN_SHAPE.c_str(), &screenShapeJsonStr) != Result::SUCCESS) { + return false; + } + if (!screenShape.ParseFromString(screenShapeJsonStr)) { + return false; + } + } + if (root->Contains(SCREEN_DENSITY.c_str())) { + std::string screenDensityJsonStr; + if (root->GetString(SCREEN_DENSITY.c_str(), &screenDensityJsonStr) != Result::SUCCESS) { + return false; + } + if (!screenDensity.ParseFromString(screenDensityJsonStr)) { + return false; + } + } + if (root->Contains(SCREEN_WINDOW.c_str())) { + std::string screenWindowJsonStr; + if (root->GetString(SCREEN_WINDOW.c_str(), &screenWindowJsonStr) != Result::SUCCESS) { + return false; + } + if (!screenWindow.ParseFromString(screenWindowJsonStr)) { + return false; + } + } + if (root->Contains(COUNTRY_CODE.c_str())) { + std::string countryCodeJsonStr; + if (root->GetString(COUNTRY_CODE.c_str(), &countryCodeJsonStr) != Result::SUCCESS) { + return false; + } + if (!countryCode.ParseFromString(countryCodeJsonStr)) { + return false; + } + } + return true; +} + +bool DistroFilter::IsEmpty() const +{ + if (apiVersion.IsEmpty() && screenShape.IsEmpty() && screenDensity.IsEmpty() && + screenWindow.IsEmpty() && countryCode.IsEmpty()) { + return true; + } + return false; +} + +std::string DistroFilter::Dump() +{ + std::string dumpStr = ""; + if (apiVersion.policy.empty() && screenShape.policy.empty() && screenDensity.policy.empty() + && screenWindow.policy.empty() && countryCode.policy.empty()) { + return dumpStr; + } + dumpStr = "distroFilter:"; + if (!apiVersion.policy.empty()) { + std::string apiVersionStr = "apiVersion: policy is " + apiVersion.policy + ", value is " + + Utils::ListToString(apiVersion.value); + dumpStr += " " + apiVersionStr; + } + if (!screenShape.policy.empty()) { + std::string screenShapeStr = "screenShape: policy is " + screenShape.policy + ", value is " + + Utils::ListToString(screenShape.value); + dumpStr += " " + screenShapeStr; + } + if (!screenDensity.policy.empty()) { + std::string screenDensityStr = "screenDensity: policy is " + screenDensity.policy + ", value is " + + Utils::ListToString(screenDensity.value); + dumpStr += " " + screenDensityStr; + } + if (!screenWindow.policy.empty()) { + std::string screenWindowStr = "screenWindow: policy is " + screenWindow.policy + ", value is " + + Utils::ListToString(screenWindow.value); + dumpStr += " " + screenWindowStr; + } + if (!countryCode.policy.empty()) { + std::string countryCodeStr = "countryCode: policy is " + countryCode.policy + ", value is " + + Utils::ListToString(countryCode.value); + dumpStr += " " + countryCodeStr; + } + return dumpStr; +} +} // namespace AppPackingTool +} // namespace OHOS diff --git a/ohos_packing_tool/frameworks/src/json/hap_verify_info.cpp b/ohos_packing_tool/frameworks/src/json/hap_verify_info.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0a7c7a07f0ce6cb74b3a84941504e224a1166086 --- /dev/null +++ b/ohos_packing_tool/frameworks/src/json/hap_verify_info.cpp @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2024 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. + */ + +#include +#include "hap_verify_info.h" + +namespace OHOS { +namespace AppPackingTool { +namespace {} + +HapVerifyInfo::HapVerifyInfo() +{ +} + +HapVerifyInfo::~HapVerifyInfo() +{ +} + +void HapVerifyInfo::ConvertToDependency() { + for (DependencyItem item : dependencyItemList_) { + if (!item.bundleName.empty() && item.bundleName.compare(bundleName_) == 0) { + dependencies_.emplace_back(item.bundleName); + } + } +} + +} // namespace AppPackingTool +} // namespace OHOS diff --git a/ohos_packing_tool/frameworks/src/json/json_utils.cpp b/ohos_packing_tool/frameworks/src/json/json_utils.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2553235b11d58d2261ab63afb269e96bf31a3402 --- /dev/null +++ b/ohos_packing_tool/frameworks/src/json/json_utils.cpp @@ -0,0 +1,84 @@ +/* + * Copyright (c) 2024 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. + */ + +#include +#include +#include "json_utils.h" + +namespace OHOS { +namespace AppPackingTool { +namespace {} + +JsonUtils::JsonUtils() +{} + +JsonUtils::~JsonUtils() +{} + +bool JsonUtils::CheckFileName(const std::string& filePath, const std::string& fileName) +{ + fs::path fsFilePath(filePath); + if (fs::is_regular_file(fsFilePath) && fsFilePath.filename().compare(fileName) == 0) { + return true; + } + return false; +} + +bool JsonUtils::IsModuleJson(const std::string& filePath) +{ + return CheckFileName(filePath, MODULE_JSON); +} + +bool JsonUtils::IsConfigJson(const std::string& filePath) +{ + return CheckFileName(filePath, CONFIG_JSON); +} + +bool JsonUtils::IsPatchJson(const std::string& filePath) +{ + return CheckFileName(filePath, PATCH_JSON); +} + +std::unique_ptr JsonUtils::JsonFromFile(const std::string& filePath) +{ + std::ifstream inFile(filePath, std::ios::in); + if (!inFile.is_open()) { + return nullptr; + } + std::string fileContent((std::istreambuf_iterator(inFile)), std::istreambuf_iterator()); + inFile.close(); + return PtJson::Parse(fileContent); +} + +bool JsonUtils::JsonToFile(const std::unique_ptr& json, const std::string& filePath) +{ + if (json.get() == nullptr) { + return false; + } + return StrToFile(json->Stringify(), filePath); +} + +bool JsonUtils::StrToFile(const std::string& str, const std::string& filePath) +{ + std::ofstream outFile(filePath, std::ios::out); + if (!outFile.is_open()) { + return false; + } + outFile << str; + outFile.close(); + return true; +} +} // namespace AppPackingTool +} // namespace OHOS diff --git a/ohos_packing_tool/frameworks/src/json/module_json.cpp b/ohos_packing_tool/frameworks/src/json/module_json.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f47455edb80a376de15178a1026fa97e1d442c38 --- /dev/null +++ b/ohos_packing_tool/frameworks/src/json/module_json.cpp @@ -0,0 +1,2190 @@ +/* + * Copyright (c) 2024 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. + */ + +#include +#include "module_json.h" +#include "utils.h" + +namespace OHOS { +namespace AppPackingTool { +namespace { + const std::string APP = "app"; + const std::string BUNDLE_TYPE = "bundleType"; + const std::string ABILITIES = "abilities"; + const std::string VERSIONCODE = "versionCode"; + const std::string VERSIONNAME = "versionName"; + const std::string MIN_COMPATIBLE_VERSION_CODE = "minCompatibleVersionCode"; + const std::string API_VERSION = "apiVersion"; + const std::string MIN_API_VERSION = "minAPIVersion"; + const std::string TARGET_API_VERSION = "targetAPIVersion"; + const std::string API_RELEASE_TYPE = "apiReleaseType"; + const std::string DEBUG = "debug"; + const std::string COMPATIBLE = "compatible"; + const std::string RELEASE_TYPE = "releaseType"; + const std::string TARGET = "target"; + const std::string VERSION = "version"; + const std::string CODE = "code"; + const std::string NAME = "name"; + const std::string MODULE = "module"; + const std::string MODULE_NAME = "moduleName"; + const std::string MODULE_TYPE = "moduleType"; + const std::string DISTRO = "distro"; + const std::string PACKAGE = "package"; + const std::string BUNDLE_NAME = "bundleName"; + const std::string ENTRY = "entry"; + const std::string DEVICE_TYPE = "deviceType"; + const std::string DEVICE_TYPES = "deviceTypes"; + const std::string TYPE= "type"; + const std::string VENDOR = "vendor"; + const std::string METADATA = "metadata"; + const std::string RESOURCE = "resource"; + const std::string PROFILE = "$profile:"; + const std::string VALUE = "value"; + const std::string JSON_PREFIX = ".json"; + const std::string DISTRO_FILTER = "distroFilter"; + const std::string DISTRIBUTION_FILTER = "distributionFilter"; + const std::string DEPENDENCIES = "dependencies"; + const std::string EXTENSION_ABILITIES = "extensionAbilities"; + const std::string INSTALLATION_FREE = "installationFree"; + const std::string COMPRESS_NATIVE_LIBS = "compressNativeLibs"; + const std::string ASAN_ENABLED = "asanEnabled"; + const std::string TSAN_ENABLED = "tsanEnabled"; + const std::string ATOMIC_SERVICE = "atomicService"; + const std::string PRELOADS = "preloads"; + const std::string SHARED = "shared"; + const std::string APP_SERVICE = "appService"; + const std::string REQUEST_PERMISSIONS = "requestPermissions"; + const std::string TARGET_MODULE_NAME = "targetModuleName"; + const std::string TARGET_PRIORITY = "targetPriority"; + const std::string TARGET_BUNDLE_NAME = "targetBundleName"; + const std::string DEVICE_CONFIG = "deviceConfig"; + const std::string DEFAULT = "default"; + const std::string COMPILE_SDK_VERSION = "compileSdkVersion"; + const std::string COMPILE_SDK_TYPE = "compileSdkType"; + const std::string PROXY_DATAS = "proxyDatas"; + const std::string PROXY_DATA = "proxyData"; + const std::string PROXY_URI = "uri"; + const std::string CONTINUE_TYPE = "continueType"; + const std::string MULTI_APP_MODE = "multiAppMode"; + const std::string MULTI_APP_MODE_TYPE = "multiAppModeType"; + const std::string MULTI_APP_MODE_NUMBER = "maxCount"; + const std::string GENERATE_BUILD_HASH = "generateBuildHash"; + const std::string BUILD_HASH = "buildHash"; +} + +ModuleJson::ModuleJson() +{ +} + +ModuleJson::~ModuleJson() +{ +} + +bool ModuleJson::ParseFromString(const std::string& jsonString) +{ + Release(); + if (jsonString.length() == 0) { + return false; + } + root_ = PtJson::Parse(jsonString); + return IsValid(); +} + +bool ModuleJson::ParseFromFile(const std::string& jsonFile) +{ + Release(); + std::ifstream inFile(jsonFile, std::ios::in); + if (!inFile.is_open()) { + return false; + } + std::string fileContent((std::istreambuf_iterator(inFile)), std::istreambuf_iterator()); + inFile.close(); + root_ = PtJson::Parse(fileContent); + return IsValid(); +} + +std::string ModuleJson::ToString() +{ + return root_->Stringify(); +} + +void ModuleJson::Release() +{ + if (root_.get() != nullptr) { + root_->ReleaseRoot(); + root_ = nullptr; + } +} + +bool ModuleJson::IsValid() +{ + return (root_.get() != nullptr); +} + +bool ModuleJson::GetAppObject(std::unique_ptr& appObj) +{ + if (root_.get() == nullptr) { + return false; + } + if (!root_->Contains(APP.c_str())) { + return false; + } + if (root_->GetObject(APP.c_str(), &appObj) != Result::SUCCESS) { + return false; + } + return true; +} + +bool ModuleJson::GetVersionObject(std::unique_ptr& versionObj) +{ + std::unique_ptr appObj; + if (!GetAppObject(appObj)) { + return false; + } + if (!appObj->Contains(VERSION.c_str())) { + return false; + } + if (appObj->GetObject(VERSION.c_str(), &versionObj) != Result::SUCCESS) { + return false; + } + return true; +} + +bool ModuleJson::GetModuleObject(std::unique_ptr& moduleObj) +{ + if (root_.get() == nullptr) { + return false; + } + if (!root_->Contains(MODULE.c_str())) { + return false; + } + if (root_->GetObject(MODULE.c_str(), &moduleObj) != Result::SUCCESS) { + return false; + } + return true; +} + + +bool ModuleJson::GetDistroObject(std::unique_ptr& distroObj) +{ + std::unique_ptr moduleObj; + if (!GetModuleObject(moduleObj)) { + return false; + } + return GetDistroObjectByModuleObj(moduleObj, distroObj); +} + +bool ModuleJson::GetDistroObjectByModuleObj(std::unique_ptr& moduleObj, std::unique_ptr& distroObj) +{ + if (!moduleObj) { + return false; + } + if (!moduleObj->Contains(DISTRO.c_str())) { + return false; + } + if (moduleObj->GetObject(DISTRO.c_str(), &distroObj) != Result::SUCCESS) { + return false; + } + return true; +} + +bool ModuleJson::GetApiVersionObject(std::unique_ptr& apiVersion) +{ + std::unique_ptr appObj; + if (!GetAppObject(appObj)) { + return false; + } + if (!appObj->Contains(API_VERSION.c_str())) { + return false; + } + if (appObj->GetObject(API_VERSION.c_str(), &apiVersion) != Result::SUCCESS) { + return false; + } + return true; +} + +bool ModuleJson::GetStageVersion(Version& version) +{ + std::unique_ptr appObj; + if (!GetAppObject(appObj)) { + return false; + } + return GetStageVersionByAppObj(appObj, version); +} + +bool ModuleJson::GetStageVersionByAppObj(std::unique_ptr& obj, Version& version) +{ + if (!obj) { + return false; + } + if (!obj->Contains(VERSIONCODE.c_str()) || !obj->Contains(VERSIONNAME.c_str())) { + return false; + } + if (obj->GetInt(VERSIONCODE.c_str(), &version.versionCode) != Result::SUCCESS) { + return false; + } + if (obj->GetString(VERSIONNAME.c_str(), &version.versionName) != Result::SUCCESS) { + return false; + } + if (obj->Contains(MIN_COMPATIBLE_VERSION_CODE.c_str())) { + if (obj->GetInt(MIN_COMPATIBLE_VERSION_CODE.c_str(), &version.minCompatibleVersionCode) != Result::SUCCESS) { + return false; + } + } else { + version.minCompatibleVersionCode = version.versionCode; + } + return true; +} + +bool ModuleJson::SetStageVersionCode(const int& versionCode) +{ + std::unique_ptr appObj; + if (!GetAppObject(appObj)) { + return false; + } + if (!appObj->Contains(VERSIONCODE.c_str())) { + return false; + } + if (appObj->SetInt(VERSIONCODE.c_str(), versionCode) != Result::SUCCESS) { + return false; + } + return true; +} + +bool ModuleJson::SetStageVersionName(const std::string& versionName) +{ + std::unique_ptr appObj; + if (!GetAppObject(appObj)) { + return false; + } + if (!appObj->Contains(VERSIONNAME.c_str())) { + return false; + } + if (appObj->SetString(VERSIONNAME.c_str(), versionName) != Result::SUCCESS) { + return false; + } + return true; +} + +bool ModuleJson::GetFaVersion(Version& version) +{ + std::unique_ptr appObj; + if (!GetAppObject(appObj)) { + return false; + } + return GetFaVersionByAppObj(appObj, version); +} + +bool ModuleJson::GetFaVersionByAppObj(std::unique_ptr& obj, Version& version) +{ + if (!obj) { + return false; + } + if (!obj->Contains(VERSION.c_str())) { + return false; + } + std::unique_ptr versionObj; + if (obj->GetObject(VERSION.c_str(), &versionObj) != Result::SUCCESS) { + return false; + } + return GetFaVersionByVersionObj(versionObj, version); +} + +bool ModuleJson::GetFaVersionByVersionObj(std::unique_ptr& obj, Version& version) +{ + if (!obj) { + return false; + } + if (!obj->Contains(CODE.c_str()) || !obj->Contains(NAME.c_str())) { + return false; + } + if (obj->GetInt(CODE.c_str(), &version.versionCode) != Result::SUCCESS) { + return false; + } + if (obj->GetString(NAME.c_str(), &version.versionName) != Result::SUCCESS) { + return false; + } + if (obj->Contains(MIN_COMPATIBLE_VERSION_CODE.c_str())) { + if (obj->GetInt(MIN_COMPATIBLE_VERSION_CODE.c_str(), &version.minCompatibleVersionCode) != Result::SUCCESS) { + return false; + } + } else { + version.minCompatibleVersionCode = version.versionCode; + } + return true; +} + +bool ModuleJson::SetFaVersionCode(const int& versionCode) +{ + std::unique_ptr versionObj; + if (!GetVersionObject(versionObj)) { + return false; + } + if (!versionObj->Contains(CODE.c_str())) { + return false; + } + if (versionObj->SetInt(CODE.c_str(), versionCode) != Result::SUCCESS) { + return false; + } + return true; +} + +bool ModuleJson::SetFaVersionName(const std::string& versionName) +{ + std::unique_ptr versionObj; + if (!GetVersionObject(versionObj)) { + return false; + } + if (!versionObj->Contains(NAME.c_str())) { + return false; + } + if (versionObj->SetString(NAME.c_str(), versionName) != Result::SUCCESS) { + return false; + } + return true; +} + +bool ModuleJson::GetFaInstallationFree(bool& installationFree) +{ + std::unique_ptr distroObj; + if (!GetDistroObject(distroObj)) { + return false; + } + return GetFaInstallationFreeByDistroObj(distroObj, installationFree); +} + +bool ModuleJson::GetFaInstallationFreeByModuleObj(std::unique_ptr& obj, bool& installationFree) +{ + if (!obj) { + return false; + } + std::unique_ptr distroObj; + if (!GetDistroObjectByModuleObj(obj, distroObj)) { + return false; + } + return GetFaInstallationFreeByDistroObj(distroObj, installationFree); +} + +bool ModuleJson::GetFaInstallationFreeByDistroObj(std::unique_ptr& obj, bool& installationFree) +{ + if (!obj) { + return false; + } + if (!obj->Contains(INSTALLATION_FREE.c_str())) { + return false; + } + if (obj->GetBool(INSTALLATION_FREE.c_str(), &installationFree) != Result::SUCCESS) { + return false; + } + return true; +} + +bool ModuleJson::GetStageInstallationFree(bool& installationFree) +{ + std::unique_ptr moduleObj; + if (!GetModuleObject(moduleObj)) { + return false; + } + return GetStageInstallationFreeByModuleObj(moduleObj, installationFree); +} + +bool ModuleJson::GetStageInstallationFreeByModuleObj(std::unique_ptr& obj, bool& installationFree) +{ + if (!obj) { + return false; + } + if (!obj->Contains(INSTALLATION_FREE.c_str())) { + return false; + } + if (obj->GetBool(INSTALLATION_FREE.c_str(), &installationFree) != Result::SUCCESS) { + return false; + } + return true; +} + +bool ModuleJson::GetFaBundleType(std::string& bundleType) +{ + bool installationFree = false; + if (!GetFaInstallationFree(installationFree)) { + return false; + } + if (installationFree) { + bundleType = ATOMIC_SERVICE; + } else { + bundleType = APP; + } + return true; +} + +bool ModuleJson::GetStageModuleApiVersion(ModuleApiVersion& moduleApiVersion) +{ + std::unique_ptr appObj; + if (!GetAppObject(appObj)) { + return false; + } + return GetStageModuleApiVersionByAppObj(appObj, moduleApiVersion); +} + +bool ModuleJson::GetStageModuleApiVersionByAppObj(std::unique_ptr& obj, ModuleApiVersion& moduleApiVersion) +{ + if (!obj) { + return false; + } + if (obj->Contains(MIN_API_VERSION.c_str())) { + if (obj->GetInt(MIN_API_VERSION.c_str(), &moduleApiVersion.compatibleApiVersion) != Result::SUCCESS) { + return false; + } + } + if (obj->Contains(TARGET_API_VERSION.c_str())) { + if (obj->GetInt(TARGET_API_VERSION.c_str(), &moduleApiVersion.targetApiVersion) != Result::SUCCESS) { + return false; + } + } + if (obj->Contains(API_RELEASE_TYPE.c_str())) { + if (obj->GetString(API_RELEASE_TYPE.c_str(), &moduleApiVersion.releaseType) != Result::SUCCESS) { + return false; + } + } + return true; +} + +bool ModuleJson::GetFaModuleApiVersion(ModuleApiVersion& moduleApiVersion) +{ + std::unique_ptr appObj; + if (!GetAppObject(appObj)) { + return false; + } + return GetFaModuleApiVersionByAppObj(appObj, moduleApiVersion); +} + +bool ModuleJson::GetFaModuleApiVersionByAppObj(std::unique_ptr& obj, ModuleApiVersion& moduleApiVersion) +{ + if (!obj) { + return false; + } + if (!obj->Contains(API_VERSION.c_str())) { + return false; + } + std::unique_ptr apiVersionObj; + if (obj->GetObject(API_VERSION.c_str(), &apiVersionObj) != Result::SUCCESS) { + return false; + } + return GetFaModuleApiVersionByApiVersionObj(apiVersionObj, moduleApiVersion); +} + +bool ModuleJson::GetFaModuleApiVersionByApiVersionObj(std::unique_ptr& obj, ModuleApiVersion& moduleApiVersion) +{ + if (!obj) { + return false; + } + if (obj->Contains(COMPATIBLE.c_str())) { + if (obj->GetInt(COMPATIBLE.c_str(), &moduleApiVersion.compatibleApiVersion) != Result::SUCCESS) { + return false; + } + } + if (obj->Contains(TARGET.c_str())) { + if (obj->GetInt(TARGET.c_str(), &moduleApiVersion.targetApiVersion) != Result::SUCCESS) { + return false; + } + } + if (obj->Contains(RELEASE_TYPE.c_str())) { + if (obj->GetString(RELEASE_TYPE.c_str(), &moduleApiVersion.releaseType) != Result::SUCCESS) { + return false; + } + } + return true; +} + +bool ModuleJson::GetStageModuleName(std::string& stageModuleName) +{ + std::unique_ptr moduleObj; + if (!GetModuleObject(moduleObj)) { + return false; + } + return GetStageModuleNameByModuleObj(moduleObj, stageModuleName); +} + +bool ModuleJson::GetStageModuleNameByModuleObj(std::unique_ptr& obj, std::string& stageModuleName) +{ + if (!obj) { + return false; + } + if (!obj->Contains(NAME.c_str())) { + return false; + } + if (obj->GetString(NAME.c_str(), &stageModuleName) != Result::SUCCESS) { + return false; + } + return true; +} + +bool ModuleJson::GetModuleName(std::string& moduleName) +{ + std::unique_ptr moduleObj; + if (!GetModuleObject(moduleObj)) { + return false; + } + return GetModuleNameByModuleObj(moduleObj, moduleName); +} + +bool ModuleJson::GetModuleNameByModuleObj(std::unique_ptr& obj, std::string& moduleName) +{ + if (!obj) { + return false; + } + if (!obj->Contains(NAME.c_str())) { + return false; + } + if (obj->GetString(NAME.c_str(), &moduleName) != Result::SUCCESS) { + return false; + } + return true; +} + +bool ModuleJson::GetFaModuleName(std::string& faModuleName) +{ + std::unique_ptr moduleObj; + if (!GetModuleObject(moduleObj)) { + return false; + } + std::unique_ptr distroObj; + return GetDistroObjectByModuleObj(moduleObj, distroObj); + if (!distroObj->Contains(MODULE_NAME.c_str())) { + return false; + } + if (distroObj->GetString(MODULE_NAME.c_str(), &faModuleName) != Result::SUCCESS) { + return false; + } + return true; +} + +bool ModuleJson::GetFaModuleNameByModuleObj(std::unique_ptr& obj, std::string& faModuleName) +{ + if (!obj) { + return false; + } + std::unique_ptr distroObj; + if (!GetDistroObjectByModuleObj(obj, distroObj)) { + return false; + } + return GetFaModuleNameByDistroObj(distroObj, faModuleName); +} + +bool ModuleJson::GetFaModuleNameByDistroObj(std::unique_ptr& obj, std::string& faModuleName) +{ + if (!obj) { + return false; + } + if (!obj->Contains(MODULE_NAME.c_str())) { + return false; + } + if (obj->GetString(MODULE_NAME.c_str(), &faModuleName) != Result::SUCCESS) { + return false; + } + return true; +} + +bool ModuleJson::GetPatchModuleName(std::string& patchModuleName) +{ + std::unique_ptr moduleObj; + if (!GetModuleObject(moduleObj)) { + return false; + } + if (!moduleObj->Contains(NAME.c_str())) { + return false; + } + if (moduleObj->GetString(NAME.c_str(), &patchModuleName) != Result::SUCCESS) { + return false; + } + return true; +} + +bool ModuleJson::GetFaPackageStr(std::string& packageStr) +{ + std::unique_ptr moduleObj; + if (!GetModuleObject(moduleObj)) { + return false; + } + return GetFaPackageStrByModuleObj(moduleObj, packageStr); +} + +bool ModuleJson::GetFaPackageStrByModuleObj(std::unique_ptr& obj, std::string& packageStr) +{ + if (!obj) { + return false; + } + if (!obj->Contains(PACKAGE.c_str())) { + return false; + } + if (obj->GetString(PACKAGE.c_str(), &packageStr) != Result::SUCCESS) { + return false; + } + return true; +} + +bool ModuleJson::GetBundleName(std::string& bundleName) +{ + std::unique_ptr appObj; + if (!GetAppObject(appObj)) { + return false; + } + return GetBundleNameByAppObj(appObj, bundleName); +} + +bool ModuleJson::GetBundleNameByAppObj(std::unique_ptr& obj, std::string& bundleName) +{ + if (!obj) { + return false; + } + if (!obj->Contains(BUNDLE_NAME.c_str())) { + return false; + } + if (obj->GetString(BUNDLE_NAME.c_str(), &bundleName) != Result::SUCCESS) { + return false; + } + return true; +} + +bool ModuleJson::SetBundleName(const std::string& bundleName) +{ + std::unique_ptr appObj; + if (!GetAppObject(appObj)) { + return false; + } + if (!appObj->Contains(BUNDLE_NAME.c_str())) { + return false; + } + if (appObj->SetString(BUNDLE_NAME.c_str(), bundleName) != Result::SUCCESS) { + return false; + } + return true; +} + +bool ModuleJson::GetVendor(std::string& vendor) +{ + std::unique_ptr appObj; + if (!GetAppObject(appObj)) { + return false; + } + return GetVendorByAppObj(appObj, vendor); +} + +bool ModuleJson::GetVendorByAppObj(std::unique_ptr& obj, std::string& vendor) +{ + if (!obj) { + return false; + } + if (obj->Contains(VENDOR.c_str())) { + if (obj->GetString(VENDOR.c_str(), &vendor) != Result::SUCCESS) { + return false; + } + } else { + vendor = ""; + } + return true; +} + +bool ModuleJson::GetStageCompileSdkType(std::string& compileSdkType) +{ + std::unique_ptr appObj; + if (!GetAppObject(appObj)) { + return false; + } + return GetStageCompileSdkTypeByAppObj(appObj, compileSdkType); +} + +bool ModuleJson::GetStageCompileSdkTypeByAppObj(std::unique_ptr& obj, std::string& compileSdkType) +{ + if (!obj) { + return false; + } + if (obj->Contains(COMPILE_SDK_TYPE.c_str())) { + if (obj->GetString(COMPILE_SDK_TYPE.c_str(), &compileSdkType) != Result::SUCCESS) { + return false; + } + } else { + compileSdkType = ""; + } + return true; +} + +bool ModuleJson::GetStageCompileSdkVersion(std::string& compileSdkVersion) +{ + std::unique_ptr appObj; + if (!GetAppObject(appObj)) { + return false; + } + return GetStageCompileSdkVersionByAppObj(appObj, compileSdkVersion); +} + +bool ModuleJson::GetStageCompileSdkVersionByAppObj(std::unique_ptr& obj, std::string& compileSdkVersion) +{ + if (!obj) { + return false; + } + if (obj->Contains(COMPILE_SDK_VERSION.c_str())) { + if (obj->GetString(COMPILE_SDK_VERSION.c_str(), &compileSdkVersion) != Result::SUCCESS) { + return false; + } + } else { + compileSdkVersion = ""; + } + return true; +} + +bool ModuleJson::GetFaCompileSdkType(std::string& compileSdkType) +{ + std::unique_ptr appObj; + if (!GetAppObject(appObj)) { + return false; + } + return GetFaCompileSdkTypeByAppObj(appObj, compileSdkType); +} + +bool ModuleJson::GetFaCompileSdkTypeByAppObj(std::unique_ptr& obj, std::string& compileSdkType) +{ + if (!obj) { + return false; + } + if (!obj->Contains(API_VERSION.c_str())) { + return false; + } + std::unique_ptr apiVersionObj; + if (obj->GetObject(API_VERSION.c_str(), &apiVersionObj) != Result::SUCCESS) { + return false; + } + if (apiVersionObj->Contains(COMPILE_SDK_TYPE.c_str())) { + if (apiVersionObj->GetString(COMPILE_SDK_TYPE.c_str(), &compileSdkType) != Result::SUCCESS) { + return false; + } + } else { + compileSdkType = ""; + } + return true; +} + +bool ModuleJson::GetFaCompileSdkVersion(std::string& compileSdkVersion) +{ + std::unique_ptr appObj; + if (!GetAppObject(appObj)) { + return false; + } + return GetFaCompileSdkVersionByAppObj(appObj, compileSdkVersion); +} + +bool ModuleJson::GetFaCompileSdkVersionByAppObj(std::unique_ptr& obj, std::string& compileSdkVersion) +{ + if (!obj) { + return false; + } + if (!obj->Contains(API_VERSION.c_str())) { + return false; + } + std::unique_ptr apiVersionObj; + if (obj->GetObject(API_VERSION.c_str(), &apiVersionObj) != Result::SUCCESS) { + return false; + } + if (apiVersionObj->Contains(COMPILE_SDK_VERSION.c_str())) { + if (apiVersionObj->GetString(COMPILE_SDK_VERSION.c_str(), &compileSdkVersion) != Result::SUCCESS) { + return false; + } + } else { + compileSdkVersion = ""; + } + return true; +} + +bool ModuleJson::GetStageDebug(bool& debug) +{ + std::unique_ptr appObj; + if (!GetAppObject(appObj)) { + return false; + } + return GetStageDebugByAppObj(appObj, debug); +} + +bool ModuleJson::GetStageDebugByAppObj(std::unique_ptr& obj, bool& debug) +{ + if (!obj) { + return false; + } + if (obj->Contains(DEBUG.c_str())) { + if (obj->GetBool(DEBUG.c_str(), &debug) != Result::SUCCESS) { + return false; + } + } else { + debug = false; + } + return true; +} + +bool ModuleJson::GetFaDebug(bool& debug) +{ + std::unique_ptr appObj; + if (!GetAppObject(appObj)) { + return false; + } + return GetFaDebugByAppObj(appObj, debug); +} + +bool ModuleJson::GetFaDebugByAppObj(std::unique_ptr& obj, bool& debug) +{ + if (!obj) { + return false; + } + if (!obj->Contains(DEVICE_CONFIG.c_str())) { + return false; + } + std::unique_ptr deviceConfigObj; + if (obj->GetObject(DEVICE_CONFIG.c_str(), &deviceConfigObj) != Result::SUCCESS) { + return false; + } + if (!deviceConfigObj->Contains(DEFAULT.c_str())) { + return false; + } + if (deviceConfigObj->GetBool(DEFAULT.c_str(), &debug) != Result::SUCCESS) { + return false; + } + return true; +} + +bool ModuleJson::GetTargetBundleName(std::string& targetBundleName) +{ + std::unique_ptr appObj; + if (!GetAppObject(appObj)) { + return false; + } + return GetTargetBundleNameByAppObj(appObj, targetBundleName); +} + +bool ModuleJson::GetTargetBundleNameByAppObj(std::unique_ptr& obj, std::string& targetBundleName) +{ + if (!obj) { + return false; + } + if (obj->Contains(TARGET_BUNDLE_NAME.c_str())) { + if (obj->GetString(TARGET_BUNDLE_NAME.c_str(), &targetBundleName) != Result::SUCCESS) { + return false; + } + } else { + targetBundleName = ""; + } + return true; +} + +bool ModuleJson::GetTargetPriority(int32_t& targetPriority) +{ + std::unique_ptr appObj; + if (!GetAppObject(appObj)) { + return false; + } + return GetTargetPriorityByAppObj(appObj, targetPriority); +} + +bool ModuleJson::GetTargetPriorityByAppObj(std::unique_ptr& obj, int32_t& targetPriority) +{ + if (!obj) { + return false; + } + if (obj->Contains(TARGET_PRIORITY.c_str())) { + if (obj->GetInt(TARGET_PRIORITY.c_str(), &targetPriority) != Result::SUCCESS) { + return false; + } + } else { + targetPriority = 0; + } + return true; +} + +bool ModuleJson::GetTargetModuleName(std::string& targetModuleName) +{ + std::unique_ptr moduleObj; + if (!GetModuleObject(moduleObj)) { + return false; + } + return GetTargetModuleNameByModuleObj(moduleObj, targetModuleName); +} + +bool ModuleJson::GetTargetModuleNameByModuleObj(std::unique_ptr& obj, std::string& targetModuleName) +{ + if (!obj) { + return false; + } + if (obj->Contains(TARGET_MODULE_NAME.c_str())) { + if (obj->GetString(TARGET_MODULE_NAME.c_str(), &targetModuleName) != Result::SUCCESS) { + return false; + } + } else { + targetModuleName = ""; + } + return true; +} + +bool ModuleJson::GetTargetModulePriority(int32_t& targetModulePriority) +{ + std::unique_ptr moduleObj; + if (!GetModuleObject(moduleObj)) { + return false; + } + return GetTargetModulePriorityByModuleObj(moduleObj, targetModulePriority); +} + +bool ModuleJson::GetTargetModulePriorityByModuleObj(std::unique_ptr& obj, int32_t& targetModulePriority) +{ + if (!obj) { + return false; + } + if (obj->Contains(TARGET_PRIORITY.c_str())) { + if (obj->GetInt(TARGET_PRIORITY.c_str(), &targetModulePriority) != Result::SUCCESS) { + return false; + } + } else { + targetModulePriority = 0; + } + return true; +} + +// java: parseFaEntry / getDeviceTypeFromFAModule +// parseFaEntry not called anywhere +bool ModuleJson::GetFaEntry(std::list& deviceTypes) +{ + std::unique_ptr moduleObj; + if (!GetModuleObject(moduleObj)) { + return false; + } + std::unique_ptr distroObj; + if (!moduleObj->Contains(DISTRO.c_str())) { + return false; + } + if (moduleObj->GetObject(DISTRO.c_str(), &distroObj) != Result::SUCCESS) { + return false; + } + std::string moduleType; + if (!distroObj->Contains(MODULE_TYPE.c_str())) { + return false; + } + if (distroObj->GetString(MODULE_TYPE.c_str(), &moduleType) != Result::SUCCESS) { + return false; + } + if (moduleType.compare(ENTRY) == 0) { + if (!GetFaDeviceTypesByModuleObj(moduleObj, deviceTypes)) { + return false; + } + } + return true; +} + +bool ModuleJson::GetFaDeviceTypes(std::list& deviceTypes) +{ + std::unique_ptr moduleObj; + if (!GetModuleObject(moduleObj)) { + return false; + } + return GetFaDeviceTypesByModuleObj(moduleObj, deviceTypes); +} + +bool ModuleJson::GetFaDeviceTypesByModuleObj(std::unique_ptr& obj, std::list& deviceTypes) +{ + if (!obj) { + return false; + } + if (!obj->Contains(DEVICE_TYPE.c_str())) { + return false; + } + std::unique_ptr deviceTypeObj; + if (obj->GetArray(DEVICE_TYPE.c_str(), &deviceTypeObj) != Result::SUCCESS) { + return false; + } + for (int i = 0; i < deviceTypeObj->GetSize(); i++) { + deviceTypes.push_back(deviceTypeObj->Get(i)->GetString()); + } + return true; +} + +// java: parseStageEntry / getDeviceTypesFromStageModule +bool ModuleJson::GetStageEntry(std::list& deviceTypes) +{ + std::unique_ptr moduleObj; + if (!GetModuleObject(moduleObj)) { + return false; + } + std::string moduleType; + if (!moduleObj->Contains(TYPE.c_str())) { + return false; + } + if (moduleObj->GetString(TYPE.c_str(), &moduleType) != Result::SUCCESS) { + return false; + } + if (moduleType.compare(ENTRY) == 0) { + if (!GetStageDeviceTypesByModuleObj(moduleObj, deviceTypes)) { + return false; + } + } + return true; +} + +bool ModuleJson::GetStageDeviceTypes(std::list& deviceTypes) +{ + std::unique_ptr moduleObj; + if (!GetModuleObject(moduleObj)) { + return false; + } + return GetStageDeviceTypesByModuleObj(moduleObj, deviceTypes); +} + +bool ModuleJson::GetStageDeviceTypesByModuleObj(std::unique_ptr& obj, std::list& deviceTypes) +{ + if (!obj) { + return false; + } + if (!obj->Contains(DEVICE_TYPES.c_str())) { + return false; + } + std::unique_ptr deviceTypeObj; + if (obj->GetArray(DEVICE_TYPES.c_str(), &deviceTypeObj) != Result::SUCCESS) { + return false; + } + for (int i = 0; i < deviceTypeObj->GetSize(); i++) { + deviceTypes.push_back(deviceTypeObj->Get(i)->GetString()); + } + return true; +} + + +bool ModuleJson::GetModuleMetadatas(std::list& moduleMetadataInfos) +{ + std::unique_ptr moduleObj; + if (!GetModuleObject(moduleObj)) { + return false; + } + return GetModuleMetadatasByModuleObj(moduleObj, moduleMetadataInfos); +} + +bool ModuleJson::GetModuleMetadatasByModuleObj(std::unique_ptr& obj, std::list& moduleMetadataInfos) +{ + if (!obj) { + return false; + } + std::unique_ptr moduleMetadataInfosObj; + if (obj->Contains(METADATA.c_str())) { + if (obj->GetArray(METADATA.c_str(), &moduleMetadataInfosObj) != Result::SUCCESS) { + return false; + } + for (int i = 0; i < moduleMetadataInfosObj->GetSize(); i++) { + ModuleMetadataInfo moduleMetadataInfo; + std::unique_ptr moduleMetadataInfoObj = moduleMetadataInfosObj->Get(i); + if (moduleMetadataInfoObj->Contains(NAME.c_str())) { + if (moduleMetadataInfoObj->GetString(NAME.c_str(), &moduleMetadataInfo.name) != Result::SUCCESS) { + return false; + } + } + if (moduleMetadataInfoObj->Contains(VALUE.c_str())) { + if (moduleMetadataInfoObj->GetString(VALUE.c_str(), &moduleMetadataInfo.value) != Result::SUCCESS) { + return false; + } + } + if (moduleMetadataInfoObj->Contains(RESOURCE.c_str())) { + std::string resource; + if (moduleMetadataInfoObj->GetString(RESOURCE.c_str(), &resource) != Result::SUCCESS) { + return false; + } + moduleMetadataInfo.resource = Utils::ReplaceAll(resource, PROFILE, "") + JSON_PREFIX; + + } + moduleMetadataInfos.push_back(moduleMetadataInfo); + } + } + return true; +} + +bool ModuleJson::GetStageDistroFilter(DistroFilter& distroFilter) +{ + std::list moduleMetadataInfos; + if (!GetModuleMetadatas(moduleMetadataInfos)) { + return false; + } + return ParseModuleMetadatasToDistroFilter(moduleMetadataInfos, distroFilter); +} + +bool ModuleJson::GetStageDistroFilterByModuleObj(std::unique_ptr& obj, DistroFilter& distroFilter) +{ + std::list moduleMetadataInfos; + if (!GetModuleMetadatasByModuleObj(obj, moduleMetadataInfos)) { + return false; + } + return ParseModuleMetadatasToDistroFilter(moduleMetadataInfos, distroFilter); +} + +bool ModuleJson::ParseModuleMetadatasToDistroFilter(const std::list& moduleMetadataInfos, DistroFilter& distroFilter) +{ + for (auto& moduleMetadataInfo : moduleMetadataInfos) { + if (moduleMetadataInfo.resource.empty()) { + continue; + } + std::unique_ptr distroFilterObj = PtJson::Parse(moduleMetadataInfo.resource); + if (!distroFilterObj) { + return false; + } + std::string distroFilterJsonStr; + if (distroFilterObj->Contains(DISTRIBUTION_FILTER.c_str())) { + if (distroFilterObj->GetString(DISTRIBUTION_FILTER.c_str(), &distroFilterJsonStr) != Result::SUCCESS) { + return false; + } + } else if (distroFilterObj->Contains(DISTRO_FILTER.c_str())) { + if (distroFilterObj->GetString(DISTRO_FILTER.c_str(), &distroFilterJsonStr) != Result::SUCCESS) { + return false; + } + } + if (!distroFilter.ParseFromString(distroFilterJsonStr)) { + return false; + } + } + return true; +} + +bool ModuleJson::GetAbilityNames(std::list& abilityNames) +{ + std::unique_ptr moduleObj; + if (!GetModuleObject(moduleObj)) { + return false; + } + return GetAbilityNamesByModuleObj(moduleObj, abilityNames); +} + +bool ModuleJson::GetAbilityNamesByModuleObj(std::unique_ptr& obj, std::list& abilityNames) +{ + if (!obj) { + return false; + } + if (!obj->Contains(ABILITIES.c_str())) { + return false; + } + std::unique_ptr abilitiesObj; + if (obj->GetArray(ABILITIES.c_str(), &abilitiesObj) != Result::SUCCESS) { + return false; + } + for (int i = 0; i < abilitiesObj->GetSize(); i++) { + std::unique_ptr abilityObj = abilitiesObj->Get(i); + if (abilityObj->Contains(NAME.c_str())) { + std::string name; + if (abilityObj->GetString(NAME.c_str(), &name) != Result::SUCCESS) { + return false; + } + abilityNames.push_back(name); + } + } + return true; +} + +bool ModuleJson::GetProxyDataUris(std::list& proxyDataUris) +{ + std::unique_ptr moduleObj; + if (!GetModuleObject(moduleObj)) { + return false; + } + return GetProxyDataUrisByModuleObj(moduleObj, proxyDataUris); +} + +bool ModuleJson::GetProxyDataUrisByModuleObj(std::unique_ptr& obj, std::list& proxyDataUris) +{ + if (!obj) { + return false; + } + if (obj->Contains(PROXY_DATAS.c_str())) { + std::unique_ptr proxyDatasObj; + if (obj->GetArray(PROXY_DATAS.c_str(), &proxyDatasObj) != Result::SUCCESS) { + return false; + } + if (!GetProxyDataUrisByProxyDatasObj(proxyDatasObj, proxyDataUris)) { + return false; + } + } else if (obj->Contains(PROXY_DATA.c_str())) { + std::unique_ptr proxyDatasObj; + if (obj->GetArray(PROXY_DATA.c_str(), &proxyDatasObj) != Result::SUCCESS) { + return false; + } + if (!GetProxyDataUrisByProxyDatasObj(proxyDatasObj, proxyDataUris)) { + return false; + } + } + return true; +} + +bool ModuleJson::GetProxyDataUrisByProxyDatasObj(std::unique_ptr& obj, std::list& proxyDataUris) +{ + if (!obj || !obj->IsArray()) { + return false; + } + for (int i = 0; i < obj->GetSize(); i++) { + std::unique_ptr proxyDataObj = obj->Get(i); + if (!proxyDataObj->Contains(PROXY_URI.c_str())) { + return false; + } + std::string proxyUri; + if (proxyDataObj->GetString(PROXY_URI.c_str(), &proxyUri) != Result::SUCCESS) { + return false; + } + proxyDataUris.push_back(proxyUri); + } + return true; +} + +bool ModuleJson::GetExtensionAbilityNames(std::list& extensionAbilityNames) +{ + std::unique_ptr moduleObj; + if (!GetModuleObject(moduleObj)) { + return false; + } + return GetAbilityNamesByModuleObj(moduleObj, extensionAbilityNames); +} + +bool ModuleJson::GetExtensionAbilityNamesByModuleObj(std::unique_ptr& obj, std::list& extensionAbilityNames) +{ + if (!obj) { + return false; + } + if (!obj->Contains(EXTENSION_ABILITIES.c_str())) { + return false; + } + std::unique_ptr abilitiesObj; + if (obj->GetArray(EXTENSION_ABILITIES.c_str(), &abilitiesObj) != Result::SUCCESS) { + return false; + } + for (int i = 0; i < abilitiesObj->GetSize(); i++) { + std::unique_ptr abilityObj = abilitiesObj->Get(i); + if (abilityObj->Contains(NAME.c_str())) { + std::string name; + if (abilityObj->GetString(NAME.c_str(), &name) != Result::SUCCESS) { + return false; + } + extensionAbilityNames.push_back(name); + } + } + return true; +} + +bool ModuleJson::GetStageModuleType(std::string& moduleType) +{ + std::unique_ptr moduleObj; + if (!GetModuleObject(moduleObj)) { + return false; + } + return GetStageModuleTypeByModuleObj(moduleObj, moduleType); +} + +bool ModuleJson::GetStageModuleTypeByModuleObj(std::unique_ptr& obj, std::string& moduleType) +{ + if (!obj) { + return false; + } + if (obj->Contains(TYPE.c_str())) { + if (obj->GetString(TYPE.c_str(), &moduleType) != Result::SUCCESS) { + return false; + } + } else { + moduleType = ""; + } + return true; +} + +bool ModuleJson::GetFaModuleType(std::string& moduleType) +{ + std::unique_ptr moduleObj; + if (!GetModuleObject(moduleObj)) { + return false; + } + return GetFaModuleTypeByModuleObj(moduleObj, moduleType); +} + +// java : parseFAIsEntry +bool ModuleJson::GetFaModuleTypeByModuleObj(std::unique_ptr& obj, std::string& moduleType) +{ + if (!obj) { + return false; + } + moduleType = ""; + if (obj->Contains(DISTRO.c_str())) { + std::unique_ptr distroObj; + if (obj->GetObject(DISTRO.c_str(), &distroObj) != Result::SUCCESS) { + return false; + } + if (distroObj->Contains(MODULE_TYPE.c_str())) { + if (obj->GetString(MODULE_TYPE.c_str(), &moduleType) != Result::SUCCESS) { + return false; + } + } + } + return true; +} + +bool ModuleJson::GetFaReleaseType(std::string& releaseType) +{ + std::unique_ptr appObj; + if (!GetAppObject(appObj)) { + return false; + } + return GetFaReleaseTypeByAppObj(appObj, releaseType); +} + +bool ModuleJson::GetFaReleaseTypeByAppObj(std::unique_ptr& obj, std::string& releaseType) +{ + if (!obj) { + return false; + } + std::unique_ptr apiVersionObj; + releaseType = ""; + if (obj->Contains(API_VERSION.c_str())) { + if (obj->GetObject(API_VERSION.c_str(), &apiVersionObj) != Result::SUCCESS) { + return false; + } + if (apiVersionObj->Contains(RELEASE_TYPE.c_str())) { + if (obj->GetString(RELEASE_TYPE.c_str(), &releaseType) != Result::SUCCESS) { + return false; + } + } + } + return true; +} + +bool ModuleJson::GetDependencyItems(std::list& dependencyItems, const std::string& defaultBundleName) +{ + std::unique_ptr moduleObj; + if (!GetModuleObject(moduleObj)) { + return false; + } + return GetDependencyItemsByModuleObj(moduleObj, dependencyItems, defaultBundleName); +} + +bool ModuleJson::GetDependencyItemsByModuleObj(std::unique_ptr& obj, std::list& dependencyItems, const std::string& defaultBundleName) +{ + if (!obj) { + return false; + } + if (obj->Contains(DEPENDENCIES.c_str())) { + std::unique_ptr dependencyItemsObj; + if (obj->GetArray(DEPENDENCIES.c_str(), &dependencyItemsObj) != Result::SUCCESS) { + return false; + } + for (int i = 0; i < dependencyItemsObj->GetSize(); i++) { + std::unique_ptr dependencyItemObj = dependencyItemsObj->Get(i); + DependencyItem dependencyItem; + if (dependencyItemObj->Contains(BUNDLE_NAME.c_str())) { + if (dependencyItemObj->GetString(BUNDLE_NAME.c_str(), &dependencyItem.bundleName) != Result::SUCCESS) { + return false; + } + } else { + dependencyItem.bundleName = defaultBundleName; + } + if (dependencyItemObj->Contains(MODULE_NAME.c_str())) { + if (dependencyItemObj->GetString(MODULE_NAME.c_str(), &dependencyItem.moduleName) != Result::SUCCESS) { + return false; + } + } + dependencyItems.push_back(dependencyItem); + } + } + + return true; +} + +bool ModuleJson::GetStageBundleType(std::string& bundleType) +{ + std::unique_ptr appObj; + std::unique_ptr moduleObj; + if (!GetAppObject(appObj) || !GetModuleObject(moduleObj)) { + return false; + } + if (!moduleObj->Contains(TYPE.c_str())) { + return false; + } + std::string moduleName; + std::string moduleType; + bool installationFree = false; + if (!GetStageModuleNameByModuleObj(moduleObj, moduleName)) { + return false; + } + if (!GetStageModuleTypeByModuleObj(moduleObj, moduleType)) { + return false; + } + GetStageInstallationFreeByModuleObj(moduleObj, installationFree); + if (!appObj->Contains(BUNDLE_TYPE.c_str())) { + if (installationFree) { + return false; + } + bundleType = APP; + return true; + } else if (appObj->GetString(BUNDLE_TYPE.c_str(), &bundleType) == Result::SUCCESS) { + if (bundleType.compare(APP) == 0) { + if (installationFree) { + return false; + } + return true; + } else if (bundleType.compare(ATOMIC_SERVICE) == 0) { + if (!installationFree) { + return false; + } + return true; + } else if (bundleType.compare(SHARED) == 0) { + if (moduleType.compare(SHARED) != 0) { + return false; + } + return true; + } else if (bundleType.compare(APP_SERVICE) == 0) { + return true; + } + } + return false; +} + +bool ModuleJson::GetAtomicServicePreloads(std::list& preloadItems) +{ + std::unique_ptr moduleObj; + if (!GetModuleObject(moduleObj)) { + return false; + } + return GetAtomicServicePreloadsByModuleObj(moduleObj, preloadItems); +} + +bool ModuleJson::GetAtomicServicePreloadsByModuleObj(std::unique_ptr& obj, std::list& preloadItems) +{ + if (!obj) { + return false; + } + + if (!obj->Contains(ATOMIC_SERVICE.c_str())) { + return true; + } + std::unique_ptr atomicServiceObj; + if (obj->GetObject(ATOMIC_SERVICE.c_str(), &atomicServiceObj) != Result::SUCCESS) { + return false; + } + + if (!atomicServiceObj->Contains(PRELOADS.c_str())) { + return true; + } + std::unique_ptr preloadsObj; + if (obj->GetArray(PRELOADS.c_str(), &preloadsObj) != Result::SUCCESS) { + return false; + } + for (int i = 0; i < preloadsObj->GetSize(); i++) { + std::unique_ptr preloadObj = preloadsObj->Get(i); + PreloadItem preloadItem; + std::string moduleName; + if (preloadObj->Contains(MODULE_NAME.c_str())) { + if (preloadObj->GetString(MODULE_NAME.c_str(), &moduleName) == Result::SUCCESS) { + preloadItem.moduleName = moduleName; + } + } + preloadItems.push_back(preloadItem); + } + + return true; +} + +bool ModuleJson::GetAbilityContinueTypeMap(std::map>& abilityContinueTypeMap) +{ + std::unique_ptr moduleObj; + if (!GetModuleObject(moduleObj)) { + return false; + } + return GetAbilityContinueTypeMapByModuleObj(moduleObj, abilityContinueTypeMap); +} + +bool ModuleJson::GetAbilityContinueTypeMapByModuleObj(std::unique_ptr& obj, std::map>& abilityContinueTypeMap) +{ + if (!obj) { + return false; + } + if (obj->Contains(ABILITIES.c_str())) { + std::unique_ptr abilitysObj; + if (obj->GetArray(PRELOADS.c_str(), &abilitysObj) != Result::SUCCESS) { + return false; + } + for (int i = 0; i < abilitysObj->GetSize(); i++) { + std::unique_ptr abilityObj = abilitysObj->Get(i); + if (!abilityObj->Contains(NAME.c_str())) { + return false; + } + std::string abilityName; + if (abilityObj->GetString(MODULE_NAME.c_str(), &abilityName) != Result::SUCCESS) { + return false; + } + std::list continueTypes; + if (abilityObj->Contains(CONTINUE_TYPE.c_str())) { + std::unique_ptr continueTypeObj; + if (obj->GetArray(CONTINUE_TYPE.c_str(), &continueTypeObj) != Result::SUCCESS) { + return false; + } + + for (int j = 0; j < continueTypeObj->GetSize(); i++) { + continueTypes.push_back(continueTypeObj->Get(i)->GetString()); + } + } + abilityContinueTypeMap.emplace(abilityName, continueTypes); + } + } + return true; +} + +bool ModuleJson::GetMultiAppMode(MultiAppMode& multiAppMode) +{ + std::unique_ptr appObj; + if (!GetAppObject(appObj)) { + return false; + } + return GetMultiAppModeByAppObj(appObj, multiAppMode); +} +bool ModuleJson::GetMultiAppModeByAppObj(std::unique_ptr& obj, MultiAppMode& multiAppMode) +{ + if (!obj) { + return false; + } + if (obj->Contains(MULTI_APP_MODE.c_str())) { + std::unique_ptr multiAppModeObj; + if (obj->GetObject(MULTI_APP_MODE.c_str(), &multiAppModeObj) != Result::SUCCESS) { + return false; + } + multiAppMode.multiAppModeType = ""; + multiAppMode.maxCount = 0; + if (multiAppModeObj->Contains(MULTI_APP_MODE_TYPE.c_str())) { + if (obj->GetString(MULTI_APP_MODE_TYPE.c_str(), &multiAppMode.multiAppModeType) != Result::SUCCESS) { + return false; + } + } + if (multiAppModeObj->Contains(MULTI_APP_MODE_NUMBER.c_str())) { + if (obj->GetInt(MULTI_APP_MODE_NUMBER.c_str(), &multiAppMode.maxCount) != Result::SUCCESS) { + return false; + } + } + } + return true; +} + +bool ModuleJson::GetStageAsanEnabled(bool& asanEnabled) +{ + std::unique_ptr appObj; + if (!GetAppObject(appObj)) { + return false; + } + return GetStageAsanEnabledByAppObj(appObj, asanEnabled); +} + +bool ModuleJson::GetStageAsanEnabledByAppObj(std::unique_ptr& obj, bool& asanEnabled) +{ + if (!obj) { + return false; + } + if (obj->Contains(ASAN_ENABLED.c_str())) { + if (obj->GetBool(ASAN_ENABLED.c_str(), &asanEnabled) != Result::SUCCESS) { + return false; + } + } else { + asanEnabled = false; + } + return true; +} + +bool ModuleJson::GetFaAsanEnabled(bool& asanEnabled) +{ + std::unique_ptr appObj; + if (!GetAppObject(appObj)) { + return false; + } + return GetFaAsanEnabledByAppObj(appObj, asanEnabled); +} + +bool ModuleJson::GetFaAsanEnabledByAppObj(std::unique_ptr& obj, bool& asanEnabled) +{ + if (!obj) { + return false; + } + if (obj->Contains(ASAN_ENABLED.c_str())) { + if (obj->GetBool(ASAN_ENABLED.c_str(), &asanEnabled) != Result::SUCCESS) { + return false; + } + } else { + asanEnabled = false; + } + return true; +} + +bool ModuleJson::GetStageTsanEnabled(bool& tsanEnabled) +{ + std::unique_ptr appObj; + if (!GetAppObject(appObj)) { + return false; + } + return GetStageTsanEnabledByAppObj(appObj, tsanEnabled); +} + +bool ModuleJson::GetStageTsanEnabledByAppObj(std::unique_ptr& obj, bool& tsanEnabled) +{ + if (!obj) { + return false; + } + if (obj->Contains(TSAN_ENABLED.c_str())) { + if (obj->GetBool(TSAN_ENABLED.c_str(), &tsanEnabled) != Result::SUCCESS) { + return false; + } + } else { + tsanEnabled = false; + } + return true; +} + +bool ModuleJson::GetStageCompressNativeLibs(bool& compressNativeLibs) +{ + std::unique_ptr appObj; + if (!GetAppObject(appObj)) { + return false; + } + return GetStageCompressNativeLibsByAppObj(appObj, compressNativeLibs); +} + +bool ModuleJson::GetStageCompressNativeLibsByAppObj(std::unique_ptr& obj, bool& compressNativeLibs) +{ + if (!obj) { + return false; + } + if (obj->Contains(COMPRESS_NATIVE_LIBS.c_str())) { + if (obj->GetBool(COMPRESS_NATIVE_LIBS.c_str(), &compressNativeLibs) != Result::SUCCESS) { + return false; + } + } else { + compressNativeLibs = false; + } + return true; +} + +// apiReleaseType was included in ModuleApiVersion. +bool ModuleJson::GetStageApiReleaseType(std::string& apiReleaseType) +{ + std::unique_ptr appObj; + if (!GetAppObject(appObj)) { + return false; + } + return GetStageApiReleaseTypeByAppObj(appObj, apiReleaseType); +} + +bool ModuleJson::GetStageApiReleaseTypeByAppObj(std::unique_ptr& obj, std::string& apiReleaseType) +{ + if (!obj) { + return false; + } + if (obj->Contains(API_RELEASE_TYPE.c_str())) { + if (obj->GetString(API_RELEASE_TYPE.c_str(), &apiReleaseType) != Result::SUCCESS) { + return false; + } + } else { + apiReleaseType = ""; + } + return true; +} + +bool ModuleJson::GetFaDistroFilter(DistroFilter& distroFilter) +{ + std::unique_ptr moduleObj; + if (!GetModuleObject(moduleObj)) { + return false; + } + return GetFaDistroFilterByModuleObj(moduleObj, distroFilter); +} + +bool ModuleJson::GetFaDistroFilterByModuleObj(std::unique_ptr& obj, DistroFilter& distroFilter) +{ + if (!obj) { + return false; + } + if (obj->Contains(DISTRO_FILTER.c_str())) { + std::string distroFilterStr; + if (obj->GetString(DISTRO_FILTER.c_str(), &distroFilterStr) != Result::SUCCESS) { + return false; + } + std::unique_ptr distroFilterObj = PtJson::Parse(distroFilterStr); + if (!distroFilterObj) { + return false; + } + std::string distroFilterJsonStr; + if (distroFilterObj->Contains(DISTRO_FILTER.c_str())) { + if (distroFilterObj->GetString(DISTRO_FILTER.c_str(), &distroFilterJsonStr) != Result::SUCCESS) { + return false; + } + } + if (!distroFilter.ParseFromString(distroFilterJsonStr)) { + return false; + } + } + return true; +} + +// java Compressor.java : parseStageHapVerifyInfo / ModuleJsonUtil.java : parseStageHapVerifyInfo +// The following parameters require additional settings +// SetStageModule: stage is true, fa is false +// SetFileLength +// SetResourceMap +// SetProfileStr: this param can be delete, because it is also parsed by MODULE_JSON file +bool ModuleJson::GetStageHapVerifyInfo(HapVerifyInfo& hapVerifyInfo) +{ + std::unique_ptr appObj; + std::unique_ptr moduleObj; + if (!GetAppObject(appObj)) { + return false; + } + if (!GetModuleObject(moduleObj)) { + return false; + } + std::string bundleName; + std::string vendor; + Version version; + ModuleApiVersion moduleApiVersion; + std::string moduleName; + // std::list moduleMetadataInfos; + DistroFilter distroFilter; + std::list deviceTypes; + std::list abilityNames; + std::list extensionAbilityNames; + std::string moduleType; + std::list dependencyItems; + bool installationFree = false; + std::string bundleType; + std::list preloadItems; + std::string targetBundleName; + int32_t targetPriority = 0; + std::string targetModuleName; + int32_t targetModulePriority = 0; + bool debug = false; + std::string compileSdkType; + std::string compileSdkVersion; + std::list proxyDataUris; + std::map> abilityContinueTypeMap; + MultiAppMode multiAppMode; + if (!GetBundleNameByAppObj(appObj, bundleName)) { + return false; + } + if (!GetVendorByAppObj(appObj, vendor)) { + return false; + } + if (!GetStageVersionByAppObj(appObj, version)) { + return false; + } + if (!GetStageModuleApiVersionByAppObj(appObj, moduleApiVersion)) { + return false; + } + if (!GetStageModuleNameByModuleObj(moduleObj, moduleName)) { + return false; + } + if (!GetStageModuleNameByModuleObj(moduleObj, moduleName)) { + return false; + } + if (!GetStageDistroFilterByModuleObj(moduleObj, distroFilter)) { + return false; + } + if (!GetStageDeviceTypesByModuleObj(moduleObj, deviceTypes)) { + return false; + } + if (!GetAbilityNamesByModuleObj(moduleObj, abilityNames)) { + return false; + } + if (!GetExtensionAbilityNamesByModuleObj(moduleObj, extensionAbilityNames)) { + return false; + } + if (!GetStageModuleTypeByModuleObj(moduleObj, moduleType)) { + return false; + } + if (!GetDependencyItemsByModuleObj(moduleObj, dependencyItems, bundleName)) { + return false; + } + if (!GetStageInstallationFreeByModuleObj(moduleObj, installationFree)) { + return false; + } + if (!GetStageBundleType(bundleType)) { + return false; + } + if (!GetAtomicServicePreloadsByModuleObj(moduleObj, preloadItems)) { + return false; + } + if (!GetTargetBundleNameByAppObj(appObj, targetBundleName)) { + return false; + } + if (!GetTargetPriorityByAppObj(appObj, targetPriority)) { + return false; + } + if (!GetTargetModuleNameByModuleObj(moduleObj, targetModuleName)) { + return false; + } + if (!GetTargetModulePriorityByModuleObj(moduleObj, targetModulePriority)) { + return false; + } + if (!GetStageDebugByAppObj(appObj, debug)) { + return false; + } + if (!GetStageCompileSdkTypeByAppObj(moduleObj, compileSdkType)) { + return false; + } + if (!GetStageCompileSdkVersionByAppObj(moduleObj, compileSdkVersion)) { + return false; + } + if (!GetProxyDataUrisByModuleObj(moduleObj, proxyDataUris)) { + return false; + } + if (!GetAbilityContinueTypeMapByModuleObj(moduleObj, abilityContinueTypeMap)) { + return false; + } + if (!GetMultiAppModeByAppObj(moduleObj, multiAppMode)) { + return false; + } + hapVerifyInfo.SetBundleName(bundleName); + hapVerifyInfo.SetVendor(vendor); + hapVerifyInfo.SetVersion(version); + hapVerifyInfo.SetApiVersion(moduleApiVersion); + hapVerifyInfo.SetModuleName(moduleName); + hapVerifyInfo.SetDistroFilter(distroFilter); + hapVerifyInfo.SetDeviceTypes(deviceTypes); + hapVerifyInfo.SetAbilityNames(abilityNames); + hapVerifyInfo.AddAbilityNames(extensionAbilityNames); + hapVerifyInfo.SetModuleType(moduleType); + hapVerifyInfo.SetDependencyItemList(dependencyItems); + hapVerifyInfo.SetInstallationFree(installationFree); + hapVerifyInfo.SetBundleType(bundleType); + hapVerifyInfo.SetPreloadItems(preloadItems); + hapVerifyInfo.SetTargetBundleName(targetBundleName); + hapVerifyInfo.SetTargetPriority(targetPriority); + hapVerifyInfo.SetTargetModuleName(targetModuleName); + hapVerifyInfo.SetTargetModulePriority(targetModulePriority); + hapVerifyInfo.SetDebug(debug); + hapVerifyInfo.SetCompileSdkType(compileSdkType); + hapVerifyInfo.SetCompileSdkVersion(compileSdkVersion); + hapVerifyInfo.SetProxyDataUris(proxyDataUris); + hapVerifyInfo.SetContinueTypeMap(abilityContinueTypeMap); + hapVerifyInfo.SetMultiAppMode(multiAppMode); + + return true; +} + +bool ModuleJson::GetFaHapVerifyInfo(HapVerifyInfo& hapVerifyInfo) +{ + std::unique_ptr appObj; + std::unique_ptr moduleObj; + if (!GetAppObject(appObj)) { + return false; + } + if (!GetModuleObject(moduleObj)) { + return false; + } + std::string bundleName; + std::string bundleType; + std::string vendor; + Version version; + ModuleApiVersion moduleApiVersion; + std::string moduleName; + DistroFilter distroFilter; + std::list deviceTypes; + std::list abilityNames; + std::string moduleType; + std::string packageStr; + std::list dependencyItems; + bool installationFree = false; + bool debug = false; + std::string compileSdkType; + std::string compileSdkVersion; + + if (!GetBundleNameByAppObj(appObj, bundleName)) { + return false; + } + if (!GetFaBundleType(bundleType)) { + return false; + } + if (!GetVendorByAppObj(appObj, vendor)) { + return false; + } + if (!GetFaVersionByAppObj(appObj, version)) { + return false; + } + if (!GetFaModuleApiVersionByAppObj(appObj, moduleApiVersion)) { + return false; + } + if (!GetFaModuleNameByModuleObj(moduleObj, moduleName)) { + return false; + } + if (!GetFaDistroFilterByModuleObj(moduleObj, distroFilter)) { + return false; + } + if (!GetStageDeviceTypesByModuleObj(moduleObj, deviceTypes)) { + return false; + } + if (!GetAbilityNamesByModuleObj(moduleObj, abilityNames)) { + return false; + } + if (!GetFaModuleTypeByModuleObj(moduleObj, moduleType)) { + return false; + } + if (!GetFaPackageStrByModuleObj(moduleObj, packageStr)) { + return false; + } + if (!GetDependencyItemsByModuleObj(moduleObj, dependencyItems, bundleName)) { + return false; + } + if (!GetFaInstallationFreeByModuleObj(moduleObj, installationFree)) { + return false; + } + if (!GetFaDebugByAppObj(appObj, debug)) { + return false; + } + if (!GetFaCompileSdkTypeByAppObj(moduleObj, compileSdkType)) { + return false; + } + if (!GetFaCompileSdkVersionByAppObj(moduleObj, compileSdkVersion)) { + return false; + } + + hapVerifyInfo.SetBundleName(bundleName); + hapVerifyInfo.SetBundleType(bundleType); + hapVerifyInfo.SetVendor(vendor); + hapVerifyInfo.SetVersion(version); + hapVerifyInfo.SetApiVersion(moduleApiVersion); + hapVerifyInfo.SetModuleName(moduleName); + hapVerifyInfo.SetDistroFilter(distroFilter); + hapVerifyInfo.SetDeviceTypes(deviceTypes); + hapVerifyInfo.SetAbilityNames(abilityNames); + hapVerifyInfo.SetModuleType(moduleType); + hapVerifyInfo.SetPackageName(packageStr); + hapVerifyInfo.SetDependencyItemList(dependencyItems); + hapVerifyInfo.SetInstallationFree(installationFree); + hapVerifyInfo.SetDebug(debug); + hapVerifyInfo.SetCompileSdkType(compileSdkType); + hapVerifyInfo.SetCompileSdkVersion(compileSdkVersion); + + return true; +} + +bool ModuleJson::IsExistedStageRequestPermissions() +{ + std::unique_ptr moduleObj; + if (!GetModuleObject(moduleObj)) { + return false; + } + return moduleObj->Contains(REQUEST_PERMISSIONS.c_str()); +} + +bool ModuleJson::IsExistedStageModuleTargetPriority() +{ + std::unique_ptr moduleObj; + if (!GetModuleObject(moduleObj)) { + return false; + } + return moduleObj->Contains(TARGET_PRIORITY.c_str()); +} + +bool ModuleJson::IsExistedStageAppTargetPriority() +{ + std::unique_ptr appObj; + if (!GetModuleObject(appObj)) { + return false; + } + return appObj->Contains(TARGET_PRIORITY.c_str()); +} + +bool ModuleJson::IsModuleAtomicServiceValid() +{ + std::unique_ptr moduleObj; + if (!GetModuleObject(moduleObj)) { + return false; + } + if (!moduleObj->Contains(ATOMIC_SERVICE.c_str())) { + return true; + } + std::unique_ptr appObj; + if (!GetAppObject(appObj)) { + return false; + } + if (moduleObj->Contains(ATOMIC_SERVICE.c_str()) && !appObj->Contains(BUNDLE_TYPE.c_str())) { + return false; + } else if (appObj->Contains(BUNDLE_TYPE.c_str())) { + std::string bundleType; + if (appObj->GetString(BUNDLE_TYPE.c_str(), &bundleType) != Result::SUCCESS) { + return false; + } + if (bundleType.compare(ATOMIC_SERVICE) != 0) { + return false; + } + } + return true; +} + +bool ModuleJson::CheckEntryInAtomicService() +{ + std::string bundleType; + if (!GetStageBundleType(bundleType)) { + return false; + } + if (bundleType.compare(ATOMIC_SERVICE) != 0) { + return true; + } + std::string moduleType; + std::list abilityNames; + if (!GetStageModuleType(moduleType) || !GetAbilityNames(abilityNames)) { + return false; + } + if (moduleType.compare(ENTRY) == 0 && abilityNames.empty()) { + return false; + } + return true; +} + +bool ModuleJson::CheckAtomicServiceInstallationFree() +{ + std::unique_ptr moduleObj; + std::unique_ptr appObj; + if (!GetModuleObject(moduleObj) || !GetAppObject(appObj)) { + return false; + } + bool installationFree = false; + if (!GetStageInstallationFreeByModuleObj(moduleObj, installationFree)) { + return false; + } + if (!appObj->Contains(BUNDLE_TYPE.c_str())) { + if (installationFree) { + return false; + } + return true; + } + std::string bundleType; + if (!GetStageBundleType(bundleType)) { + return false; + } + if ((bundleType.compare(APP) == 0) || + (bundleType.compare(SHARED) == 0) || + (bundleType.compare(APP_SERVICE) == 0)) { + if (installationFree) { + return false; + } + } else if (bundleType.compare(ATOMIC_SERVICE) == 0) { + if (!installationFree) { + return false; + } + } else { + return false; + } + return true; +} + +bool ModuleJson::GetGenerateBuildHash(bool& generateBuildHash) +{ + std::unique_ptr moduleObj; + std::unique_ptr appObj; + if (!GetModuleObject(moduleObj) || !GetAppObject(appObj)) { + return false; + } + if (appObj->Contains(GENERATE_BUILD_HASH.c_str())) { + if (appObj->GetBool(GENERATE_BUILD_HASH.c_str(), &generateBuildHash) != Result::SUCCESS) { + return false; + } + } else if (moduleObj->Contains(GENERATE_BUILD_HASH.c_str())) { + if (moduleObj->GetBool(GENERATE_BUILD_HASH.c_str(), &generateBuildHash) != Result::SUCCESS) { + return false; + } + } else { + return false; + } + return true; +} + +bool ModuleJson::RemoveGenerateBuildHash() +{ + if (!RemoveGenerateBuildHashFromAppObj() || !RemoveGenerateBuildHashFromModuleObj()) { + return false; + } + return true; +} + +bool ModuleJson::RemoveGenerateBuildHashFromAppObj() +{ + std::unique_ptr appObj; + if (!GetAppObject(appObj)) { + return false; + } + if (appObj->Contains(GENERATE_BUILD_HASH.c_str())) { + appObj->Remove(GENERATE_BUILD_HASH.c_str()); + } + return true; +} + +bool ModuleJson::RemoveGenerateBuildHashFromModuleObj() +{ + std::unique_ptr moduleObj; + if (!GetModuleObject(moduleObj)) { + return false; + } + if (moduleObj->Contains(GENERATE_BUILD_HASH.c_str())) { + moduleObj->Remove(GENERATE_BUILD_HASH.c_str()); + } + return true; +} + +bool ModuleJson::GetNormalizeVersion(NormalizeVersion& normalizeVersion, const bool& isStage) +{ + Version version; + if (isStage) { + if (!GetStageVersion(version)) { + return false; + } + } else { + if (!GetFaVersion(version)) { + return false; + } + } + std::string moduleName; + if (!GetModuleName(moduleName)) { + return false; + } + normalizeVersion.originVersionCode = version.versionCode; + normalizeVersion.originVersionName = version.versionName; + normalizeVersion.moduleName = moduleName; + return true; +} + +bool ModuleJson::SetVersionCodeAndName(const int& versionCode, const std::string& versionName, const bool& isStage) +{ + if (isStage) { + if (!SetStageVersionCode(versionCode) || !SetStageVersionName(versionName)) { + return false; + } + } else { + if (!SetFaVersionCode(versionCode) || !SetFaVersionName(versionName)) { + return false; + } + } + return true; +} + +bool ModuleJson::SetBuildHash(const std::string& buildHash) +{ + std::unique_ptr moduleObj; + if (!GetModuleObject(moduleObj)) { + return false; + } + if (moduleObj->Contains(BUILD_HASH.c_str())) { + if (moduleObj->SetString(BUILD_HASH.c_str(), buildHash) != Result::SUCCESS) { + return false; + } + } else { + if (!moduleObj->Add(BUILD_HASH.c_str(), buildHash.c_str())) { + return false; + } + } + return true; +} +} // namespace AppPackingTool +} // namespace OHOS diff --git a/ohos_packing_tool/frameworks/src/json/module_json_utils.cpp b/ohos_packing_tool/frameworks/src/json/module_json_utils.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6afd274b93f25179a7e77fe5ddc56c30711fd3db --- /dev/null +++ b/ohos_packing_tool/frameworks/src/json/module_json_utils.cpp @@ -0,0 +1,252 @@ +/* + * Copyright (c) 2024 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. + */ + +// #include +// #include +// #include +#include +#include +#include +#include "module_json.h" +#include "module_json_utils.h" +#include "zip_utils.h" +#include "log.h" +#include "utils.h" + +namespace fs = std::filesystem; + +namespace OHOS { +namespace AppPackingTool { +namespace { + const std::string MODULE_JSON = "module.json"; + const std::string CONFIG_JSON = "config.json"; + const std::string HAP_SUFFIX = ".hap"; + const std::string HSP_SUFFIX = ".hsp"; + const int SHARED_APP_HSP_LIMIT = 1; + const std::string TYPE_SHARED = "shared"; + const std::string INCLUDE = "include"; + const std::string EXCLUDE = "exclude"; +} + +ModuleJsonUtils::ModuleJsonUtils() +{} + +ModuleJsonUtils::~ModuleJsonUtils() +{} + +// java : parseStageHapVerifyInfo +bool ModuleJsonUtils::GetStageHapVerifyInfo(const std::string& hapFilePath, HapVerifyInfo& hapVerifyInfo) +{ + std::string fileContent; + std::map resourceMap; + int64_t fileLength = Utils::GetFileLength(hapFilePath); + if (fileLength < 0) { + LOGE("Get hap file length failed! hapFile=%s", hapFilePath.c_str()); + return false; + } + if (!ZipUtils::GetFileContentFromZip(hapFilePath, MODULE_JSON, fileContent)) { + LOGE("Get module.json content from hap file failed! hapFile=%s", hapFilePath.c_str()); + return false; + } + if (!ZipUtils::GetResourceMapFromZip(hapFilePath, resourceMap)) { + LOGE("Get resouce map from hap file failed! hapFile=%s", hapFilePath.c_str()); + return false; + } + ModuleJson moduleJson; + if (!moduleJson.ParseFromString(fileContent)) { + LOGE("Parse json string failed!"); + return false; + } + if (!moduleJson.GetStageHapVerifyInfo(hapVerifyInfo)) { + LOGE("Get stage hap verify info failed!"); + return false; + } + hapVerifyInfo.SetResourceMap(resourceMap); + hapVerifyInfo.SetProfileStr(fileContent); + hapVerifyInfo.SetStageModule(true); + hapVerifyInfo.SetFileLength(fileLength); + return true; +} + +bool ModuleJsonUtils::GetFaHapVerifyInfo(const std::string& hapFilePath, HapVerifyInfo& hapVerifyInfo) +{ + std::string fileContent; + int64_t fileLength = Utils::GetFileLength(hapFilePath); + if (fileLength < 0) { + LOGE("Get hap file length failed! hapFile=%s", hapFilePath.c_str()); + return false; + } + if (!ZipUtils::GetFileContentFromZip(hapFilePath, CONFIG_JSON, fileContent)) { + LOGE("Get module.json content from hap file failed! hapFile=%s", hapFilePath.c_str()); + return false; + } + ModuleJson moduleJson; + if (!moduleJson.ParseFromString(fileContent)) { + LOGE("Parse json string failed!"); + return false; + } + if (!moduleJson.GetFaHapVerifyInfo(hapVerifyInfo)) { + LOGE("Get stage hap verify info failed!"); + return false; + } + hapVerifyInfo.SetProfileStr(fileContent); + hapVerifyInfo.SetStageModule(false); + hapVerifyInfo.SetFileLength(fileLength); + return true; +} + +// java : HapVerify::checkPolicyValueDisjoint +bool ModuleJsonUtils::CheckPolicyValueDisjoint(const PolicyValue& policyValue1, const PolicyValue& policyValue2) +{ + if (policyValue1.policy.compare(EXCLUDE) == 0 && policyValue2.policy.compare(INCLUDE) == 0) { + if (policyValue2.value.empty() || Utils::CheckContainsAll(policyValue1.value, policyValue2.value)) { + return true; + } + } else if (policyValue1.policy.compare(INCLUDE) == 0 && policyValue2.policy.compare(INCLUDE) == 0) { + if (Utils::CheckDisjoint(policyValue1.value, policyValue2.value)) { + return true; + } + } else if (policyValue1.policy.compare(INCLUDE) == 0 && policyValue2.policy.compare(EXCLUDE) == 0) { + if (policyValue1.value.empty() || Utils::CheckContainsAll(policyValue2.value, policyValue1.value)) { + return true; + } + } else if (policyValue1.policy.compare(EXCLUDE) == 0 && policyValue2.policy.compare(EXCLUDE) == 0) { + return false; + } + return false; +} + +// java : HapVerify::checkDuplicatedIsValid / HapVerify::checkDistroFilterDisjoint +bool ModuleJsonUtils::CheckDuplicatedIsValid(const HapVerifyInfo& hapVerifyInfo1, const HapVerifyInfo& hapVerifyInfo2) +{ + if (Utils::CheckDisjoint(hapVerifyInfo1.GetDeviceTypes(), hapVerifyInfo2.GetDeviceTypes())) { + return true; + } + const DistroFilter& distroFilter1 = hapVerifyInfo1.GetDistroFilter(); + const DistroFilter& distroFilter2 = hapVerifyInfo2.GetDistroFilter(); + if (distroFilter1.IsEmpty() || distroFilter2.IsEmpty()) { + return false; + } + if (!distroFilter1.apiVersion.IsEmpty() && !distroFilter2.apiVersion.IsEmpty()) { + if (CheckPolicyValueDisjoint(distroFilter1.apiVersion, distroFilter2.apiVersion)) { + return true; + } + } + if (!distroFilter1.screenShape.IsEmpty() && !distroFilter2.screenShape.IsEmpty()) { + if (CheckPolicyValueDisjoint(distroFilter1.screenShape, distroFilter2.screenShape)) { + return true; + } + } + if (!distroFilter1.screenDensity.IsEmpty() && !distroFilter2.screenDensity.IsEmpty()) { + if (CheckPolicyValueDisjoint(distroFilter1.screenDensity, distroFilter2.screenDensity)) { + return true; + } + } + if (!distroFilter1.screenWindow.IsEmpty() && !distroFilter2.screenWindow.IsEmpty()) { + if (CheckPolicyValueDisjoint(distroFilter1.screenWindow, distroFilter2.screenWindow)) { + return true; + } + } + if (!distroFilter1.countryCode.IsEmpty() && !distroFilter2.countryCode.IsEmpty()) { + if (CheckPolicyValueDisjoint(distroFilter1.countryCode, distroFilter2.countryCode)) { + return true; + } + } + return false; +} + +// java : Compressor::checkSharedAppIsValid / HapVerify::checkSharedApppIsValid +bool ModuleJsonUtils::CheckSharedAppIsValid(const std::list& hapVerifyInfos, bool& isOverlay) +{ + if (hapVerifyInfos.empty()) { + return false; + } + if (hapVerifyInfos.size() > SHARED_APP_HSP_LIMIT) { + return false; + } + for (HapVerifyInfo hapVerifyInfo : hapVerifyInfos) { + if (!hapVerifyInfo.GetTargetBundleName().empty()) { + isOverlay = true; + return true; + } + } + std::string moduleName = hapVerifyInfos.front().GetModuleName(); + for (HapVerifyInfo hapVerifyInfo : hapVerifyInfos) { + if (hapVerifyInfo.GetModuleName().compare(moduleName) != 0) { + return false; + } + if (!hapVerifyInfo.getDependencyItemList().empty()) { + return false; + } + if (!hapVerifyInfo.GetModuleType().compare(TYPE_SHARED)) { + return false; + } + } + for (auto iter1 = hapVerifyInfos.begin(); iter1 != hapVerifyInfos.end(); iter1++) { + for (auto iter2 = std::next(iter1); iter2 != hapVerifyInfos.end(); iter2++) { + if (!CheckDuplicatedIsValid(*iter1, *iter2)) { + return false; + } + } + } + return true; +} + +bool ModuleJsonUtils::CheckHapsIsValid(const std::list& fileList, const bool& isSharedApp) +{ + std::list hapVerifyInfos; + for (auto& hapPath : fileList) { + if (hapPath.empty()) { + LOGE("Hap file path is empty!"); + return false; + } + fs::path fsHapPath(hapPath); + std::string fileName = fsHapPath.filename().string(); + if (fileName.empty()) { + LOGE("Hap file name is empty!"); + return false; + } + std::transform(fileName.begin(), fileName.end(), fileName.begin(), ::tolower); + if (!Utils::EndsWith(fileName, HAP_SUFFIX) && !Utils::EndsWith(fileName, HSP_SUFFIX)) { + LOGE("Hap file is not a hap or hsp file!"); + return false; + } + HapVerifyInfo hapVerifyInfo; + if (IsModuleHap(hapPath)) { + if (!GetStageHapVerifyInfo(hapPath, hapVerifyInfo)) { + return false; + } + } else { + if (!GetFaHapVerifyInfo(hapPath, hapVerifyInfo)) { + return false; + } + } + hapVerifyInfos.push_back(hapVerifyInfo); + } + + if (isSharedApp) { + + } else { + + } + return true; +} + +bool ModuleJsonUtils::IsModuleHap(const std::string hapFilePath) +{ + return ZipUtils::IsFileExistsInZip(hapFilePath, MODULE_JSON); +} +} // namespace AppPackingTool +} // namespace OHOS diff --git a/ohos_packing_tool/frameworks/src/json/normalize_version_utils.cpp b/ohos_packing_tool/frameworks/src/json/normalize_version_utils.cpp new file mode 100644 index 0000000000000000000000000000000000000000..55505c5e7f3f7d9609d11a14d57eef9f5c8a64b7 --- /dev/null +++ b/ohos_packing_tool/frameworks/src/json/normalize_version_utils.cpp @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2024 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. + */ + +#include "pt_json.h" +#include "normalize_version_utils.h" +#include "log.h" + +namespace OHOS { +namespace AppPackingTool { +namespace {} + +NormalizeVersionUtils::NormalizeVersionUtils() +{ +} + +NormalizeVersionUtils::~NormalizeVersionUtils() +{ +} + +std::string NormalizeVersionUtils::ToString(const NormalizeVersion& normalizeVersion) +{ + std::unique_ptr ptJson = PtJson::CreateObject(); + if (!ptJson->Add(MODULE_NAME.c_str(), normalizeVersion.moduleName.c_str())) { + return ""; + } + if (!ptJson->Add(ORIGIN_VERSION_CODE.c_str(), normalizeVersion.originVersionCode)) { + return ""; + } + if (!ptJson->Add(ORIGIN_VERSION_NAME.c_str(), normalizeVersion.originVersionName.c_str())) { + return ""; + } + return ptJson->Stringify(); +} + +std::string NormalizeVersionUtils::ArrayToString(const std::list& normalizeVersions) +{ + std::unique_ptr versionsJson = PtJson::CreateArray(); + for (auto& normalizeVersion : normalizeVersions) { + std::unique_ptr versionJson = PtJson::CreateObject(); + if (!versionJson->Add(MODULE_NAME.c_str(), normalizeVersion.moduleName.c_str())) { + return ""; + } + if (!versionJson->Add(ORIGIN_VERSION_CODE.c_str(), normalizeVersion.originVersionCode)) { + return ""; + } + if (!versionJson->Add(ORIGIN_VERSION_NAME.c_str(), normalizeVersion.originVersionName.c_str())) { + return ""; + } + versionsJson->Push(versionJson); + } + return versionsJson->Stringify(); +} +} // namespace AppPackingTool +} // namespace OHOS diff --git a/ohos_packing_tool/frameworks/src/json/pack_info.cpp b/ohos_packing_tool/frameworks/src/json/pack_info.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f069b5e000e6e6e5b634bd7654e24bc218a83cbd --- /dev/null +++ b/ohos_packing_tool/frameworks/src/json/pack_info.cpp @@ -0,0 +1,548 @@ +/* + * Copyright (c) 2024 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. + */ + +#include +#include +#include "pack_info.h" +#include "log.h" +#include "utils.h" + +namespace OHOS { +namespace AppPackingTool { +namespace { + const std::string SUMMARY = "summary"; + const std::string PACKAGES = "packages"; + const std::string APP = "app"; + const std::string MODULES = "modules"; + // const std::string MODULE = "module"; + const std::string BUNDLE_NAME = "bundleName"; + const std::string BUNDLE_TYPE = "bundleType"; + const std::string VERSION = "version"; + const std::string CODE = "code"; + const std::string NAME = "name"; + const std::string DISTRO = "distro"; + const std::string MODULE_NAME = "moduleName"; + const std::string EXTENSION_ABILITIES = "extensionAbilities"; + const std::string FORMS = "forms"; + const std::string DEFAULT_DIMENSION = "defaultDimension"; + const std::string SUPPORT_DIMENSIONS = "supportDimensions"; + const char ASTERISK = '*'; +} + +PackInfo::PackInfo() +{ +} + +PackInfo::~PackInfo() +{ +} + +bool PackInfo::ParseFromString(const std::string& jsonString) +{ + Release(); + if (jsonString.length() == 0) { + return false; + } + root_ = PtJson::Parse(jsonString); + return IsValid(); +} + +bool PackInfo::ParseFromFile(const std::string& jsonFile) +{ + Release(); + std::ifstream inFile(jsonFile, std::ios::in); + if (!inFile.is_open()) { + return false; + } + std::string fileContent((std::istreambuf_iterator(inFile)), std::istreambuf_iterator()); + inFile.close(); + root_ = PtJson::Parse(fileContent); + return IsValid(); +} + +std::string PackInfo::ToString() +{ + return root_->Stringify(); +} + +void PackInfo::Release() +{ + if (root_) { + root_->ReleaseRoot(); + root_ = nullptr; + } +} + +bool PackInfo::IsValid() +{ + return (root_.get() != nullptr); +} + +bool PackInfo::GetSummaryObject(std::unique_ptr& summaryObj) +{ + if (root_.get() == nullptr) { + return false; + } + if (!root_->Contains(SUMMARY.c_str())) { + return false; + } + if (root_->GetObject(SUMMARY.c_str(), &summaryObj) != Result::SUCCESS) { + return false; + } + return true; +} + +bool PackInfo::GetPackagesObject(std::unique_ptr& packagesObj) +{ + if (root_.get() == nullptr) { + return false; + } + if (!root_->Contains(PACKAGES.c_str())) { + return false; + } + if (root_->GetArray(PACKAGES.c_str(), &packagesObj) != Result::SUCCESS) { + return false; + } + return true; +} + +bool PackInfo::GetAppObject(std::unique_ptr& appObj) +{ + std::unique_ptr summaryObj; + if (!GetSummaryObject(summaryObj)) { + return false; + } + if (!summaryObj->Contains(APP.c_str())) { + return false; + } + if (summaryObj->GetObject(APP.c_str(), &appObj) != Result::SUCCESS) { + return false; + } + return true; +} + +bool PackInfo::GetModulesObject(std::unique_ptr& modulesObj) +{ + std::unique_ptr summaryObj; + if (!GetSummaryObject(summaryObj)) { + return false; + } + if (!summaryObj->Contains(MODULES.c_str())) { + return false; + } + if (summaryObj->GetArray(MODULES.c_str(), &modulesObj) != Result::SUCCESS) { + return false; + } + return true; +} + +bool PackInfo::GetBundleName(std::string& bundleName) +{ + std::unique_ptr appObj; + if (!GetAppObject(appObj)) { + return false; + } + return GetBundleNameByAppObj(appObj, bundleName); +} + +bool PackInfo::GetBundleNameByAppObj(const std::unique_ptr& appObj, std::string& bundleName) +{ + if (!appObj) { + return false; + } + if (!appObj->Contains(BUNDLE_NAME.c_str())) { + return false; + } + if (appObj->GetString(BUNDLE_NAME.c_str(), &bundleName) != Result::SUCCESS) { + return false; + } + return true; +} + +bool PackInfo::SetBundleName(const std::string& bundleName) +{ + std::unique_ptr appObj; + if (!GetAppObject(appObj)) { + return false; + } + if (!appObj->Contains(BUNDLE_NAME.c_str())) { + return false; + } + if (appObj->SetString(BUNDLE_NAME.c_str(), bundleName) != Result::SUCCESS) { + return false; + } + return true; +} + +bool PackInfo::GetBundleType(std::string& bundleType, const std::string& defaultBundleType) +{ + std::unique_ptr appObj; + if (!GetAppObject(appObj)) { + return false; + } + return GetBundleTypeByAppObj(appObj, bundleType, defaultBundleType); +} + +bool PackInfo::GetBundleTypeByAppObj(const std::unique_ptr& appObj, std::string& bundleType, const std::string& defaultBundleType) +{ + if (!appObj) { + return false; + } + if (appObj->Contains(BUNDLE_TYPE.c_str())) { + if (appObj->GetString(BUNDLE_TYPE.c_str(), &bundleType) != Result::SUCCESS) { + return false; + } + } else { + bundleType = defaultBundleType; + } + return true; +} + +bool PackInfo::GetVersionObject(std::unique_ptr& versionObj) +{ + std::unique_ptr appObj; + if (!GetAppObject(appObj)) { + return false; + } + if (!appObj->Contains(VERSION.c_str())) { + return false; + } + if (appObj->GetObject(VERSION.c_str(), &versionObj) != Result::SUCCESS) { + return false; + } + return true; +} + +bool PackInfo::GetDistroObject(int32_t moduleIndex, std::unique_ptr& distroObj) +{ + std::unique_ptr modulesObj; + if (!GetModulesObject(modulesObj)) { + return false; + } + return GetDistroObjectByModulesObj(modulesObj, moduleIndex, distroObj); +} + +bool PackInfo::GetDistroObjectByModulesObj(const std::unique_ptr& modulesObj, + int32_t moduleIndex, std::unique_ptr& distroObj) +{ + if (!modulesObj || !modulesObj->IsArray()) { + return false; + } + if (moduleIndex >= modulesObj->GetSize()) { + return false; + } + std::unique_ptr moduleObj = modulesObj->Get(moduleIndex); + return GetDistroObjectByModuleObj(moduleObj, distroObj); +} + +bool PackInfo::GetDistroObjectByModuleObj(const std::unique_ptr& moduleObj, + std::unique_ptr& distroObj) +{ + if (!moduleObj) { + return false; + } + if (!moduleObj->Contains(DISTRO.c_str())) { + return false; + } + if (moduleObj->GetObject(DISTRO.c_str(), &distroObj) != Result::SUCCESS) { + return false; + } + return true; +} + +bool PackInfo::GetExtensionAbilitiesObj(int32_t moduleIndex, std::unique_ptr& extensionAbilitiesObj) +{ + std::unique_ptr modulesObj; + if (!GetModulesObject(modulesObj)) { + return false; + } + return GetDistroObjectByModulesObj(modulesObj, moduleIndex, extensionAbilitiesObj); +} + +bool PackInfo::GetExtensionAbilitiesObjByModulesObj(const std::unique_ptr& modulesObj, + int32_t moduleIndex, std::unique_ptr& extensionAbilitiesObj) +{ + if (!modulesObj || !modulesObj->IsArray()) { + return false; + } + if (moduleIndex >= modulesObj->GetSize()) { + return false; + } + std::unique_ptr moduleObj = modulesObj->Get(moduleIndex); + return GetExtensionAbilitiesObjByModuleObj(moduleObj, extensionAbilitiesObj); +} + +bool PackInfo::GetExtensionAbilitiesObjByModuleObj(const std::unique_ptr& moduleObj, + std::unique_ptr& extensionAbilitiesObj) +{ + if (!moduleObj) { + return false; + } + if (!moduleObj->Contains(EXTENSION_ABILITIES.c_str())) { + return false; + } + if (moduleObj->GetArray(EXTENSION_ABILITIES.c_str(), &extensionAbilitiesObj) != Result::SUCCESS) { + return false; + } + return true; +} + +bool PackInfo::GetFormsObjByExtensionAbilityObj(const std::unique_ptr& extensionAbilityObj, + std::unique_ptr& formsObj) +{ + if (!extensionAbilityObj) { + return false; + } + if (!extensionAbilityObj->Contains(FORMS.c_str())) { + return false; + } + if (extensionAbilityObj->GetArray(FORMS.c_str(), &formsObj) != Result::SUCCESS) { + return false; + } + return true; +} + +bool PackInfo::GetPackageObject(int32_t packageIndex, std::unique_ptr& packageObj) +{ + std::unique_ptr packagesObj; + if (!GetPackagesObject(packagesObj)) { + return false; + } + if (!packagesObj->IsArray()) { + return false; + } + if (packageIndex >= packagesObj->GetSize()) { + return false; + } + packageObj = packagesObj->Get(packageIndex); + return true; +} + +bool PackInfo::GetVersion(PackInfoVersion& version) +{ + std::unique_ptr versionObj; + if (!GetVersionObject(versionObj)) { + return false; + } + return GetVersionByVersionObj(versionObj, version); +} + +bool PackInfo::GetVersionByVersionObj(const std::unique_ptr& versionObj, PackInfoVersion& version) +{ + if (!versionObj) { + return false; + } + if (!versionObj->Contains(CODE.c_str()) || !versionObj->Contains(NAME.c_str())) { + return false; + } + if (versionObj->GetInt(CODE.c_str(), &version.code) != Result::SUCCESS) { + return false; + } + if (versionObj->GetString(NAME.c_str(), &version.name) != Result::SUCCESS) { + return false; + } + return true; +} + +bool PackInfo::SetVersionCode(const int& versionCode) +{ + std::unique_ptr versionObj; + if (!GetVersionObject(versionObj)) { + return false; + } + if (!versionObj->Contains(CODE.c_str())) { + return false; + } + if (versionObj->SetInt(CODE.c_str(), versionCode) != Result::SUCCESS) { + return false; + } + return true; +} + +bool PackInfo::SetVersionName(const std::string& versionName) +{ + std::unique_ptr versionObj; + if (!GetVersionObject(versionObj)) { + return false; + } + if (!versionObj->Contains(NAME.c_str())) { + return false; + } + if (versionObj->SetString(NAME.c_str(), versionName) != Result::SUCCESS) { + return false; + } + return true; +} + +bool PackInfo::GetModuleNameByDistroObj(const std::unique_ptr& distroObj, std::string& moduleName) +{ + if (!distroObj) { + return false; + } + if (distroObj->Contains(MODULE_NAME.c_str())) { + if (distroObj->GetString(MODULE_NAME.c_str(), &moduleName) != Result::SUCCESS) { + return false; + } + } else { + moduleName = ""; + } + return true; +} + +bool PackInfo::GetNameByPackageObj(const std::unique_ptr& packageObj, std::string& name) +{ + if (!packageObj) { + return false; + } + if (packageObj->Contains(NAME.c_str())) { + if (packageObj->GetString(NAME.c_str(), &name) != Result::SUCCESS) { + return false; + } + } else { + name = ""; + } + return true; +} + +bool PackInfo::GetNameByFormObj(const std::unique_ptr& formObj, std::string& name) +{ + if (!formObj) { + return false; + } + if (formObj->Contains(NAME.c_str())) { + if (formObj->GetString(NAME.c_str(), &name) != Result::SUCCESS) { + return false; + } + } else { + name = ""; + } + return true; +} + +bool PackInfo::GetDefaultDimensionByFormObj(const std::unique_ptr& formObj, std::string& defaultDimension) +{ + if (!formObj) { + return false; + } + if (!formObj->Contains(DEFAULT_DIMENSION.c_str())) { + return false; + } + if (formObj->GetString(DEFAULT_DIMENSION.c_str(), &defaultDimension) != Result::SUCCESS) { + return false; + } + if (std::count(defaultDimension.begin(), defaultDimension.end(), ASTERISK) != 1) { + return false; + } + return true; +} + +bool PackInfo::GetSupportDimensionsByFormObj(const std::unique_ptr& formObj, std::list& supportDimensions) +{ + if (!formObj) { + return false; + } + if (!formObj->Contains(SUPPORT_DIMENSIONS.c_str())) { + return false; + } + std::unique_ptr supportDimensionsObj; + if (formObj->GetArray(SUPPORT_DIMENSIONS.c_str(), &supportDimensionsObj) != Result::SUCCESS) { + return false; + } + for (int i = 0; i < supportDimensionsObj->GetSize(); i++) { + supportDimensions.push_back(supportDimensionsObj->Get(i)->GetString()); + } + return true; +} + +// java : parsePackInfoFormsName +bool PackInfo::GetFormNames(std::list& formNames, std::list& formFullNames) +{ + std::unique_ptr modulesObj; + if (!GetModulesObject(modulesObj)) { + return false; + } + if (!modulesObj->IsArray()) { + return false; + } + for (int i = 0; i < modulesObj->GetSize(); i++) { + std::unique_ptr distroObj; + if (!GetDistroObjectByModuleObj(modulesObj->Get(i), distroObj)) { + return false; + } + std::string moduleName; + if (!GetModuleNameByDistroObj(distroObj, moduleName) || moduleName.empty()) { + continue; + } + std::unique_ptr extensionAbilitiesObj; + if (!GetExtensionAbilitiesObjByModuleObj(modulesObj->Get(i), extensionAbilitiesObj)) { + return false; + } + if (!GetFormNamesByExtensionAbilitiesObj(extensionAbilitiesObj, moduleName, formNames, formFullNames)) { + return false; + } + } + return true; +} + +bool PackInfo::GetFormNamesByExtensionAbilitiesObj(const std::unique_ptr& extensionAbilitiesObj, + std::string moduleName, std::list& formNames, std::list& formFullNames) +{ + if (!extensionAbilitiesObj || !extensionAbilitiesObj->IsArray()) { + return false; + } + for (int j = 0; j < extensionAbilitiesObj->GetSize(); j++) { + std::unique_ptr formsObj; + if (!GetFormsObjByExtensionAbilityObj(extensionAbilitiesObj->Get(j), formsObj)) { + return false; + } + if (!GetFormNamesByFormsObj(formsObj, moduleName, formNames, formFullNames)) { + return false; + } + } + return true; +} + + +bool PackInfo::GetFormNamesByFormsObj(const std::unique_ptr& formsObj, + std::string moduleName, std::list& formNames, std::list& formFullNames) +{ + if (!formsObj || !formsObj->IsArray()) { + return false; + } + for (int k = 0; k < formsObj->GetSize(); k++) { + std::string formName; + std::string defaultDimension; + std::list supportDimensions; + std::string formFullName; + if (!GetNameByFormObj(formsObj->Get(k), formName) || formName.empty()) { + continue; + } + formNames.push_back(formName); + if (!GetDefaultDimensionByFormObj(formsObj->Get(k), defaultDimension)) { + return false; + } + if (!GetSupportDimensionsByFormObj(formsObj->Get(k), supportDimensions)) { + return false; + } + for (std::string supportDimension : supportDimensions) { + formFullName = moduleName + "/" + formName + "-" + Utils::ReplaceAll(supportDimension, "*", "x"); + formFullNames.push_back(formFullName); + } + } + return true; +} + +} // namespace AppPackingTool +} // namespace OHOS diff --git a/ohos_packing_tool/frameworks/src/json/pack_info_utils.cpp b/ohos_packing_tool/frameworks/src/json/pack_info_utils.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8db2c26e7ad6ca8c52003d202fc31307602f760b --- /dev/null +++ b/ohos_packing_tool/frameworks/src/json/pack_info_utils.cpp @@ -0,0 +1,252 @@ +/* + * Copyright (c) 2024 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. + */ + +#include +#include "pack_info_utils.h" +#include "log.h" + +namespace OHOS { +namespace AppPackingTool { +namespace { + const std::string DEFAULT_BUNDLE_TYPE = "APP"; + const std::string MODULE = "module"; + const std::string PACKAGES = "packages"; + const char DOT = '.'; +} + +PackInfoUtils::PackInfoUtils() +{ +} + +PackInfoUtils::~PackInfoUtils() +{ +} + +// java : mergeTwoPackInfo +bool PackInfoUtils::MergeTwoPackInfos(std::string& srcPackInfoJsonStr1, std::string& srcPackInfoJsonStr2, std::string& dstPackInfoJsonStr) +{ + PackInfo srcPackInfo1; + PackInfo srcPackInfo2; + if (!srcPackInfo1.ParseFromString(srcPackInfoJsonStr1)) { + return false; + } + if (!srcPackInfo2.ParseFromString(srcPackInfoJsonStr2)) { + return false; + } + if (!VerifyPackInfos(srcPackInfo1, srcPackInfo2)) { + return false; + } + if (!MergeTwoPackInfos(srcPackInfo1, srcPackInfo2)) { + return false; + } + dstPackInfoJsonStr = srcPackInfo1.ToString(); + return true; +} + +// java : mergePackInfoObj +bool PackInfoUtils::MergeTwoPackInfos(PackInfo& srcPackInfo1, PackInfo& srcPackInfo2) +{ + std::unique_ptr modulesObj1; + std::unique_ptr modulesObj2; + if (!srcPackInfo1.GetModulesObject(modulesObj1) || !srcPackInfo2.GetModulesObject(modulesObj2)) { + return false; + } + if (!modulesObj1->IsArray() || !modulesObj2->IsArray()) { + return false; + } + for (int i = 0; i < modulesObj2->GetSize(); i++) { + if (!modulesObj1->Push(modulesObj2->Get(i))) { + return false; + } + } + + std::unique_ptr packagesObj1; + std::unique_ptr packagesObj2; + if (!srcPackInfo1.GetPackagesObject(packagesObj1) || !srcPackInfo2.GetPackagesObject(packagesObj2)) { + LOGE("323"); + return false; + } + if (!packagesObj1->IsArray() || !packagesObj2->IsArray()) { + return false; + } + for (int i = 0; i < packagesObj2->GetSize(); i++) { + if (!packagesObj1->Push(packagesObj2->Get(i))) { + return false; + } + } + return true; +} + +// java : mergeTwoPackInfoByPackagePair +bool PackInfoUtils::MergeTwoPackInfosByPackagePair(std::string& srcPackInfoJsonStr1, std::string& srcPackInfoJsonStr2, + std::map packagesMap, std::string& dstPackInfoJsonStr) +{ + PackInfo srcPackInfo1; + PackInfo srcPackInfo2; + if (!srcPackInfo1.ParseFromString(srcPackInfoJsonStr1)) { + return false; + } + if (!srcPackInfo2.ParseFromString(srcPackInfoJsonStr2)) { + return false; + } + if (!VerifyPackInfos(srcPackInfo1, srcPackInfo2)) { + return false; + } + std::map::iterator iter = packagesMap.begin(); + while (iter != packagesMap.end()) + { + std::string packageName = iter->first; + std::string moduleName = iter->second; + std::string tmpStr = packageName.substr(0, packageName.find_last_of(DOT)); + if (!MergeTwoPackInfos(srcPackInfo1, srcPackInfo2)) { + return false; + } + ++iter; + } + dstPackInfoJsonStr = srcPackInfo1.ToString(); + return true; +} + +// java : mergeTwoPackInfoObjByPackagePair +bool PackInfoUtils::MergeTwoPackInfosByPackagePair(PackInfo& srcPackInfo1, PackInfo& srcPackInfo2, + std::string& packageName, std::string& moduleName) +{ + std::unique_ptr modulesObj1; + std::unique_ptr modulesObj2; + if (!srcPackInfo1.GetModulesObject(modulesObj1) || !srcPackInfo2.GetModulesObject(modulesObj2)) { + return false; + } + if (!modulesObj1->IsArray() || !modulesObj2->IsArray()) { + return false; + } + bool isFind = false; + for (int i = 0; i < modulesObj2->GetSize(); i++) { + std::unique_ptr distroObj; + if (!srcPackInfo2.GetDistroObject(i, distroObj)) { + return false; + } + std::string moduleNameInDistroObj; + if (!srcPackInfo2.GetModuleNameByDistroObj(distroObj, moduleNameInDistroObj)) { + return false; + } + if (moduleNameInDistroObj.compare(moduleName) == 0) { + if (!modulesObj1->Push(modulesObj2->Get(i))) { + return false; + } + isFind = true; + break; + } + } + if (!isFind) { + return false; + } + + std::unique_ptr packagesObj1; + std::unique_ptr packagesObj2; + if (!srcPackInfo1.GetPackagesObject(packagesObj1) || !srcPackInfo2.GetPackagesObject(packagesObj2)) { + return false; + } + if (!packagesObj1->IsArray() || !packagesObj2->IsArray()) { + return false; + } + isFind = false; + for (int i = 0; i < packagesObj2->GetSize(); i++) { + std::string packageNameInPackageObj; + if (!srcPackInfo2.GetNameByPackageObj(packagesObj2->Get(i), packageNameInPackageObj)) { + return false; + } + if (packageNameInPackageObj.compare(packageName) == 0) { + if (!packagesObj1->Push(packagesObj2->Get(i))) { + return false; + } + isFind = true; + break; + } + } + if (!isFind) { + return false; + } + return true; +} + +// java : verifyPackInfo +bool PackInfoUtils::VerifyPackInfos(PackInfo& packInfo1, PackInfo& packInfo2) +{ + std::unique_ptr appObj1; + std::unique_ptr appObj2; + if (!packInfo1.GetAppObject(appObj1) || !packInfo2.GetAppObject(appObj2)) { + return false; + } + if (!CheckBundleNameInPackInfo(packInfo1, packInfo2)) { + return false; + } + if (!CheckBundleTypeInPackInfo(packInfo1, packInfo2)) { + return false; + } + if (!CheckVersionCodeInPackInfo(packInfo1, packInfo2)) { + return false; + } + return true; +} + +bool PackInfoUtils::CheckBundleNameInPackInfo(PackInfo& packInfo1, PackInfo& packInfo2) +{ + std::string bundleName1; + std::string bundleName2; + if (!packInfo1.GetBundleName(bundleName1)) { + return false; + } + if (!packInfo2.GetBundleName(bundleName2)) { + return false; + } + if (bundleName1.compare(bundleName2) != 0) { + return false; + } + return true; +} + +bool PackInfoUtils::CheckBundleTypeInPackInfo(PackInfo& packInfo1, PackInfo& packInfo2) +{ + std::string bundleType1; + std::string bundleType2; + if (!packInfo1.GetBundleType(bundleType1, DEFAULT_BUNDLE_TYPE)) { + return false; + } + if (!packInfo2.GetBundleType(bundleType2, DEFAULT_BUNDLE_TYPE)) { + return false; + } + if (bundleType1.compare(bundleType2) != 0) { + return false; + } + return true; +} + +bool PackInfoUtils::CheckVersionCodeInPackInfo(PackInfo& packInfo1, PackInfo& packInfo2) +{ + PackInfoVersion version1; + PackInfoVersion version2; + if (!packInfo1.GetVersion(version1)) { + return false; + } + if (!packInfo2.GetVersion(version2)) { + return false; + } + if (version1.code != version2.code) { + return false; + } + return true; +} +} // namespace AppPackingTool +} // namespace OHOS diff --git a/ohos_packing_tool/frameworks/src/json/patch_json.cpp b/ohos_packing_tool/frameworks/src/json/patch_json.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b474702076e2b8aded9b58d35195dc842b37696b --- /dev/null +++ b/ohos_packing_tool/frameworks/src/json/patch_json.cpp @@ -0,0 +1,364 @@ +/* + * Copyright (c) 2024 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. + */ + +#include +#include "patch_json.h" +#include "log.h" + +namespace OHOS { +namespace AppPackingTool { +namespace { + const std::string APP = "app"; + const std::string MODULE = "module"; + const std::string BUNDLE_NAME = "bundleName"; + const std::string VERSION_CODE = "versionCode"; + const std::string VERSION_NAME = "versionName"; + const std::string PATCH_VERSION_CODE = "patchVersionCode"; + const std::string PATCH_VERSION_NAME = "patchVersionName"; + const std::string NAME = "name"; + const std::string TYPE= "type"; + const std::string DEVICE_TYPES = "deviceTypes"; + const std::string ORIGINAL_MODULE_HASH = "originalModuleHash"; +} + +PatchJson::PatchJson() +{ +} + +PatchJson::~PatchJson() +{ +} + +bool PatchJson::ParseFromString(const std::string& jsonString) +{ + Release(); + if (jsonString.length() == 0) { + return false; + } + root_ = PtJson::Parse(jsonString); + return IsValid(); +} + +bool PatchJson::ParseFromFile(const std::string& jsonFile) +{ + Release(); + std::ifstream inFile(jsonFile, std::ios::in); + if (!inFile.is_open()) { + return false; + } + std::string fileContent((std::istreambuf_iterator(inFile)), std::istreambuf_iterator()); + inFile.close(); + root_ = PtJson::Parse(fileContent); + return IsValid(); +} + +std::string PatchJson::ToString() +{ + return root_->Stringify(); +} + +void PatchJson::Release() +{ + // if (root_.get() != nullptr) { + if (root_) { + root_->ReleaseRoot(); + root_ = nullptr; + } +} + +bool PatchJson::IsValid() +{ + return (root_.get() != nullptr); +} + +bool PatchJson::GetAppObject(std::unique_ptr& appObj) +{ + if (root_.get() == nullptr) { + return false; + } + if (!root_->Contains(APP.c_str())) { + return false; + } + if (root_->GetObject(APP.c_str(), &appObj) != Result::SUCCESS) { + return false; + } + return true; +} + +bool PatchJson::GetModuleObject(std::unique_ptr& moduleObj) +{ + if (root_.get() == nullptr) { + return false; + } + if (!root_->Contains(MODULE.c_str())) { + return false; + } + if (root_->GetObject(MODULE.c_str(), &moduleObj) != Result::SUCCESS) { + return false; + } + return true; +} + +bool PatchJson::GetBundleName(std::string& bundleName) +{ + std::unique_ptr appObj; + if (!GetAppObject(appObj)) { + return false; + } + return GetBundleNameByAppObj(appObj, bundleName); +} + +bool PatchJson::GetBundleNameByAppObj(const std::unique_ptr& appObj, std::string& bundleName) +{ + if (!appObj) { + return false; + } + if (!appObj->Contains(BUNDLE_NAME.c_str())) { + return false; + } + if (appObj->GetString(BUNDLE_NAME.c_str(), &bundleName) != Result::SUCCESS) { + return false; + } + return true; +} + +bool PatchJson::GetVersionCode(int32_t& versionCode) +{ + std::unique_ptr appObj; + if (!GetAppObject(appObj)) { + return false; + } + return GetVersionCodeByAppObj(appObj, versionCode); +} + +bool PatchJson::GetVersionCodeByAppObj(const std::unique_ptr& appObj, int32_t& versionCode) +{ + if (!appObj) { + return false; + } + if (!appObj->Contains(VERSION_CODE.c_str())) { + return false; + } + if (appObj->GetInt(VERSION_CODE.c_str(), &versionCode) != Result::SUCCESS) { + return false; + } + return true; +} + +bool PatchJson::GetVersionName(std::string& versionName) +{ + std::unique_ptr appObj; + if (!GetAppObject(appObj)) { + return false; + } + return GetVersionNameByAppObj(appObj, versionName); +} + +bool PatchJson::GetVersionNameByAppObj(const std::unique_ptr& appObj, std::string& versionName) +{ + if (!appObj) { + return false; + } + if (!appObj->Contains(VERSION_NAME.c_str())) { + return false; + } + if (appObj->GetString(VERSION_NAME.c_str(), &versionName) != Result::SUCCESS) { + return false; + } + return true; +} + +bool PatchJson::GetPatchVersionCode(int32_t& patchVersionCode) +{ + std::unique_ptr appObj; + if (!GetAppObject(appObj)) { + return false; + } + return GetPatchVersionCodeByAppObj(appObj, patchVersionCode); +} + +bool PatchJson::GetPatchVersionCodeByAppObj(const std::unique_ptr& appObj, int32_t& patchVersionCode) +{ + if (!appObj) { + return false; + } + if (!appObj->Contains(PATCH_VERSION_CODE.c_str())) { + return false; + } + if (appObj->GetInt(PATCH_VERSION_CODE.c_str(), &patchVersionCode) != Result::SUCCESS) { + return false; + } + return true; +} + +bool PatchJson::GetPatchVersionName(std::string& patchVersionName) +{ + std::unique_ptr appObj; + if (!GetAppObject(appObj)) { + return false; + } + return GetPatchVersionNameByAppObj(appObj, patchVersionName); +} + +bool PatchJson::GetPatchVersionNameByAppObj(const std::unique_ptr& appObj, std::string& patchVersionName) +{ + if (!appObj) { + return false; + } + if (!appObj->Contains(PATCH_VERSION_NAME.c_str())) { + return false; + } + if (appObj->GetString(PATCH_VERSION_NAME.c_str(), &patchVersionName) != Result::SUCCESS) { + return false; + } + return true; +} + +bool PatchJson::GetName(std::string& name) +{ + std::unique_ptr moduleObj; + if (!GetModuleObject(moduleObj)) { + return false; + } + return GetNameByModuleObj(moduleObj, name); +} + +bool PatchJson::GetNameByModuleObj(const std::unique_ptr& moduleObj, std::string& name) +{ + if (!moduleObj) { + return false; + } + if (!moduleObj->Contains(NAME.c_str())) { + return false; + } + if (moduleObj->GetString(NAME.c_str(), &name) != Result::SUCCESS) { + return false; + } + return true; +} + +bool PatchJson::GetType(std::string& type) +{ + std::unique_ptr moduleObj; + if (!GetModuleObject(moduleObj)) { + return false; + } + return GetTypeByModuleObj(moduleObj, type); +} + +bool PatchJson::GetTypeByModuleObj(const std::unique_ptr& moduleObj, std::string& type) +{ + if (!moduleObj) { + return false; + } + if (!moduleObj->Contains(TYPE.c_str())) { + return false; + } + if (moduleObj->GetString(TYPE.c_str(), &type) != Result::SUCCESS) { + return false; + } + return true; +} + +bool PatchJson::GetDeviceTypes(std::list& deviceTypes) +{ + std::unique_ptr moduleObj; + if (!GetModuleObject(moduleObj)) { + return false; + } + return GetDeviceTypesByModuleObj(moduleObj, deviceTypes); +} + +bool PatchJson::GetDeviceTypesByModuleObj(const std::unique_ptr& moduleObj, std::list& deviceTypes) +{ + if (!moduleObj) { + return false; + } + if (!moduleObj->Contains(DEVICE_TYPES.c_str())) { + return false; + } + std::unique_ptr deviceTypesObj; + if (moduleObj->GetArray(DEVICE_TYPES.c_str(), &deviceTypesObj) != Result::SUCCESS) { + return false; + } + for (int i = 0; i < deviceTypesObj->GetSize(); i++) { + deviceTypes.push_back(deviceTypesObj->Get(i)->GetString()); + } + return true; +} + +bool PatchJson::GetOriginalModuleHash(std::string& originalModuleHash) +{ + std::unique_ptr moduleObj; + if (!GetModuleObject(moduleObj)) { + return false; + } + return GetOriginalModuleHashByModuleObj(moduleObj, originalModuleHash); +} + +bool PatchJson::GetOriginalModuleHashByModuleObj(const std::unique_ptr& moduleObj, std::string& originalModuleHash) +{ + if (!moduleObj) { + return false; + } + if (!moduleObj->Contains(ORIGINAL_MODULE_HASH.c_str())) { + return false; + } + if (moduleObj->GetString(ORIGINAL_MODULE_HASH.c_str(), &originalModuleHash) != Result::SUCCESS) { + return false; + } + return true; +} + +bool PatchJson::GetHqfInfo(HqfInfo& hqfInfo) +{ + std::unique_ptr appObj; + std::unique_ptr moduleObj; + if (!GetAppObject(appObj) || !GetModuleObject(moduleObj)) { + return false; + } + std::string bundleName = ""; + int32_t versionCode = -1; + std::string versionName = ""; + int32_t patchVersionCode = -1; + std::string patchVersionName = ""; + std::string moduleName = ""; + std::string type = ""; + std::list deviceTypes; + std::string originalModuleHash = ""; + if (!GetBundleNameByAppObj(appObj, bundleName) || + !GetVersionCodeByAppObj(appObj, versionCode) || + !GetVersionNameByAppObj(appObj, versionName) || + !GetPatchVersionCodeByAppObj(appObj, patchVersionCode) || + !GetPatchVersionNameByAppObj(appObj, patchVersionName) || + !GetNameByModuleObj(moduleObj, moduleName) || + !GetTypeByModuleObj(moduleObj, type) || + !GetDeviceTypesByModuleObj(moduleObj, deviceTypes) || + !GetOriginalModuleHashByModuleObj(moduleObj, originalModuleHash)) { + return false; + } + hqfInfo.SetBundleName(bundleName); + hqfInfo.SetVersionCode(versionCode); + hqfInfo.SetVersionName(versionName); + hqfInfo.SetPatchVersionCode(patchVersionCode); + hqfInfo.SetPatchVersionName(patchVersionName); + hqfInfo.SetModuleName(moduleName); + hqfInfo.SetType(type); + hqfInfo.SetDeviceTypes(deviceTypes); + hqfInfo.SetOriginalModuleHash(originalModuleHash); + return true; +} +} // namespace AppPackingTool +} // namespace OHOS diff --git a/ohos_packing_tool/frameworks/src/json/patch_json_utils.cpp b/ohos_packing_tool/frameworks/src/json/patch_json_utils.cpp new file mode 100644 index 0000000000000000000000000000000000000000..75d6217963a4793f41330013ce7e8f2240650d4c --- /dev/null +++ b/ohos_packing_tool/frameworks/src/json/patch_json_utils.cpp @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2024 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. + */ + +#include +#include "patch_json_utils.h" +#include "log.h" + +namespace OHOS { +namespace AppPackingTool { +namespace {} + +PatchJsonUtils::PatchJsonUtils() +{ +} + +PatchJsonUtils::~PatchJsonUtils() +{ +} + +bool PatchJsonUtils::ParsePatchByJsonFilePath(const std::string& patchJsonFilePath, HqfInfo& hqfInfo) +{ + PatchJson patchJson; + if (!patchJson.ParseFromFile(patchJsonFilePath)) { + return false; + } + if (!patchJson.GetHqfInfo(hqfInfo)) { + return false; + } + return true; +} + +bool PatchJsonUtils::ParsePatchByJsonStr(const std::string& patchJsonStr, HqfInfo& hqfInfo) +{ + PatchJson patchJson; + if (!patchJson.ParseFromString(patchJsonStr)) { + return false; + } + if (!patchJson.GetHqfInfo(hqfInfo)) { + return false; + } + return true; +} + +} // namespace AppPackingTool +} // namespace OHOS diff --git a/ohos_packing_tool/frameworks/src/json/pt_json.cpp b/ohos_packing_tool/frameworks/src/json/pt_json.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0a33262d6f3d1c426dc15039cf94b3880c1c80a7 --- /dev/null +++ b/ohos_packing_tool/frameworks/src/json/pt_json.cpp @@ -0,0 +1,559 @@ +/* + * Copyright (c) 2022 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. + */ + +#include "pt_json.h" +#include +#include "../log.h" + +// #include "libpandabase/macros.h" + +namespace OHOS { +namespace AppPackingTool { +std::unique_ptr PtJson::CreateObject() +{ + return std::make_unique(cJSON_CreateObject()); +} + +std::unique_ptr PtJson::CreateArray() +{ + return std::make_unique(cJSON_CreateArray()); +} + +void PtJson::ReleaseRoot() +{ + if (object_ != nullptr) { + LOGI("ReleaseRoot 11111"); + cJSON_Delete(object_); + LOGI("ReleaseRoot 22222"); + object_ = nullptr; + } +} + +std::unique_ptr PtJson::Parse(const std::string &data) +{ + cJSON *value = cJSON_ParseWithOpts(data.c_str(), nullptr, true); + return std::make_unique(value); +} + +std::string PtJson::Stringify() const +{ + if (object_ == nullptr) { + return ""; + } + + char *str = cJSON_PrintUnformatted(object_); + if (str == nullptr) { + return ""; + } + + std::string result(str); + cJSON_free(str); + return result; +} + +bool PtJson::Add(const char *key, bool value) const +{ + assert(key != nullptr && !Contains(key)); + + cJSON *node = cJSON_CreateBool(value); + if (node == nullptr) { + return false; + } + + cJSON_bool ret = cJSON_AddItemToObject(object_, key, node); + if (ret == 0) { + cJSON_Delete(node); + return false; + } + + return true; +} + +bool PtJson::Add(const char *key, int32_t value) const +{ + return Add(key, static_cast(value)); +} + +bool PtJson::Add(const char *key, int64_t value) const +{ + return Add(key, static_cast(value)); +} + +bool PtJson::Add(const char *key, uint32_t value) const +{ + return Add(key, static_cast(value)); +} + +bool PtJson::Add(const char *key, double value) const +{ + assert(key != nullptr && !Contains(key)); + + cJSON *node = cJSON_CreateNumber(value); + if (node == nullptr) { + return false; + } + + cJSON_bool ret = cJSON_AddItemToObject(object_, key, node); + if (ret == 0) { + cJSON_Delete(node); + return false; + } + + return true; +} + +bool PtJson::Add(const char *key, const char *value) const +{ + assert(key != nullptr && !Contains(key)); + + cJSON *node = cJSON_CreateString(value); + if (node == nullptr) { + return false; + } + + cJSON_bool ret = cJSON_AddItemToObject(object_, key, node); + if (ret == 0) { + cJSON_Delete(node); + return false; + } + + return true; +} + +bool PtJson::Add(const char *key, const std::unique_ptr &value) const +{ + assert(key != nullptr && !Contains(key)); + + cJSON *node = value->GetJson(); + if (node == nullptr) { + return false; + } + + cJSON_bool ret = cJSON_AddItemToObject(object_, key, node); + if (ret == 0) { + return false; + } + + return true; +} + +bool PtJson::Push(bool value) const +{ + cJSON *node = cJSON_CreateBool(value); + if (node == nullptr) { + return false; + } + + cJSON_bool ret = cJSON_AddItemToArray(object_, node); + if (ret == 0) { + cJSON_Delete(node); + return false; + } + + return true; +} + +bool PtJson::Push(int32_t value) const +{ + return Push(static_cast(value)); +} + +bool PtJson::Push(int64_t value) const +{ + return Push(static_cast(value)); +} + +bool PtJson::Push(uint32_t value) const +{ + return Push(static_cast(value)); +} + +bool PtJson::Push(double value) const +{ + cJSON *node = cJSON_CreateNumber(value); + if (node == nullptr) { + return false; + } + + cJSON_bool ret = cJSON_AddItemToArray(object_, node); + if (ret == 0) { + cJSON_Delete(node); + return false; + } + + return true; +} + +bool PtJson::Push(const char *value) const +{ + cJSON *node = cJSON_CreateString(value); + if (node == nullptr) { + return false; + } + + cJSON_bool ret = cJSON_AddItemToArray(object_, node); + if (ret == 0) { + cJSON_Delete(node); + return false; + } + + return true; +} + +bool PtJson::Push(const std::unique_ptr &value) const +{ + if (value == nullptr) { + return false; + } + + cJSON *node = value->GetJson(); + if (node == nullptr) { + return false; + } + + cJSON_bool ret = cJSON_AddItemToArray(object_, node); + if (ret == 0) { + return false; + } + + return true; +} + +bool PtJson::Remove(const char *key) const +{ + assert(key != nullptr && Contains(key)); + + cJSON_DeleteItemFromObject(object_, key); + return true; +} + +bool PtJson::Contains(const char *key) const +{ + cJSON *node = cJSON_GetObjectItemCaseSensitive(object_, key); + return node != nullptr; +} + +std::string PtJson::GetKey() const +{ + if (object_ == nullptr || object_->string == nullptr) { + return ""; + } + + return std::string(object_->string); +} + +cJSON *PtJson::GetJson() const +{ + return object_; +} + +bool PtJson::IsBool() const +{ + return cJSON_IsBool(object_) != 0; +} + +bool PtJson::IsNumber() const +{ + return cJSON_IsNumber(object_) != 0; +} + +bool PtJson::IsString() const +{ + return cJSON_IsString(object_) != 0; +} + +bool PtJson::IsObject() const +{ + return cJSON_IsObject(object_) != 0; +} + +bool PtJson::IsArray() const +{ + return cJSON_IsArray(object_) != 0; +} + +bool PtJson::IsNull() const +{ + return cJSON_IsNull(object_) != 0; +} + +bool PtJson::GetBool(bool defaultValue) const +{ + if (!IsBool()) { + return defaultValue; + } + + return cJSON_IsTrue(object_) != 0; +} + +int32_t PtJson::GetInt(int32_t defaultValue) const +{ + if (!IsNumber()) { + return defaultValue; + } + + return static_cast(object_->valuedouble); +} + +int64_t PtJson::GetInt64(int64_t defaultValue) const +{ + if (!IsNumber()) { + return defaultValue; + } + + return static_cast(object_->valuedouble); +} + +uint32_t PtJson::GetUInt(uint32_t defaultValue) const +{ + if (!IsNumber()) { + return defaultValue; + } + + return static_cast(object_->valuedouble); +} + +uint64_t PtJson::GetUInt64(uint64_t defaultValue) const +{ + if (!IsNumber()) { + return defaultValue; + } + + return static_cast(object_->valuedouble); +} + +double PtJson::GetDouble(double defaultValue) const +{ + if (!IsNumber()) { + return defaultValue; + } + + return object_->valuedouble; +} + +std::string PtJson::GetString() const +{ + if (!IsString()) { + return ""; + } + + return std::string(object_->valuestring); +} + +int32_t PtJson::GetSize() const +{ + return cJSON_GetArraySize(object_); +} + +std::unique_ptr PtJson::Get(int32_t index) const +{ + return std::make_unique(cJSON_GetArrayItem(object_, index)); +} + +Result PtJson::GetBool(const char *key, bool *value) const +{ + cJSON *item = cJSON_GetObjectItem(object_, key); + if (item == nullptr) { + return Result::NOT_EXIST; + } + if (cJSON_IsBool(item) == 0) { + return Result::TYPE_ERROR; + } + + *value = cJSON_IsTrue(item) != 0; + return Result::SUCCESS; +} + +Result PtJson::SetBool(const char *key, const bool& value) +{ + cJSON *item = cJSON_GetObjectItem(object_, key); + if (item == nullptr) { + return Result::NOT_EXIST; + } + cJSON_ReplaceItemInObject(object_, key, cJSON_CreateBool(value)); + return Result::SUCCESS; +} + +Result PtJson::GetInt(const char *key, int32_t *value) const +{ + double result; + Result ret = GetDouble(key, &result); + if (ret == Result::SUCCESS) { + *value = static_cast(result); + } + return ret; +} + +Result PtJson::SetInt(const char *key, const int32_t& value) +{ + cJSON *item = cJSON_GetObjectItem(object_, key); + if (item == nullptr) { + return Result::NOT_EXIST; + } + cJSON_ReplaceItemInObject(object_, key, cJSON_CreateNumber(value)); + return Result::SUCCESS; +} + +Result PtJson::GetInt64(const char *key, int64_t *value) const +{ + double result; + Result ret = GetDouble(key, &result); + if (ret == Result::SUCCESS) { + *value = static_cast(result); + } + return ret; +} + +Result PtJson::SetInt64(const char *key, const int64_t& value) +{ + cJSON *item = cJSON_GetObjectItem(object_, key); + if (item == nullptr) { + return Result::NOT_EXIST; + } + cJSON_ReplaceItemInObject(object_, key, cJSON_CreateNumber(value)); + return Result::SUCCESS; +} + +Result PtJson::GetUInt(const char *key, uint32_t *value) const +{ + double result; + Result ret = GetDouble(key, &result); + if (ret == Result::SUCCESS) { + *value = static_cast(result); + } + return ret; +} + +Result PtJson::SetUInt(const char *key, const uint32_t& value) +{ + cJSON *item = cJSON_GetObjectItem(object_, key); + if (item == nullptr) { + return Result::NOT_EXIST; + } + cJSON_ReplaceItemInObject(object_, key, cJSON_CreateNumber(value)); + return Result::SUCCESS; +} + +Result PtJson::GetUInt64(const char *key, uint64_t *value) const +{ + double result; + Result ret = GetDouble(key, &result); + if (ret == Result::SUCCESS) { + *value = static_cast(result); + } + return ret; +} + +Result PtJson::SetUInt64(const char *key, const uint64_t& value) +{ + cJSON *item = cJSON_GetObjectItem(object_, key); + if (item == nullptr) { + return Result::NOT_EXIST; + } + cJSON_ReplaceItemInObject(object_, key, cJSON_CreateNumber(value)); + return Result::SUCCESS; +} + +Result PtJson::GetDouble(const char *key, double *value) const +{ + cJSON *item = cJSON_GetObjectItem(object_, key); + if (item == nullptr) { + return Result::NOT_EXIST; + } + if (cJSON_IsNumber(item) == 0) { + return Result::TYPE_ERROR; + } + + *value = item->valuedouble; + return Result::SUCCESS; +} + +Result PtJson::SetDouble(const char *key, const double& value) +{ + cJSON *item = cJSON_GetObjectItem(object_, key); + if (item == nullptr) { + return Result::NOT_EXIST; + } + cJSON_ReplaceItemInObject(object_, key, cJSON_CreateNumber(value)); + return Result::SUCCESS; +} + +Result PtJson::GetString(const char *key, std::string *value) const +{ + cJSON *item = cJSON_GetObjectItem(object_, key); + if (item == nullptr) { + return Result::NOT_EXIST; + } + if (cJSON_IsString(item) == 0) { + return Result::TYPE_ERROR; + } + + *value = item->valuestring; + return Result::SUCCESS; +} + +Result PtJson::SetString(const char *key, const std::string& value) +{ + cJSON *item = cJSON_GetObjectItem(object_, key); + if (item == nullptr) { + return Result::NOT_EXIST; + } + if (cJSON_IsString(item) == 0) { + return Result::TYPE_ERROR; + } + cJSON_SetValuestring(item, value.c_str()); + return Result::SUCCESS; +} + +Result PtJson::GetObject(const char *key, std::unique_ptr *value) const +{ + cJSON *item = cJSON_GetObjectItem(object_, key); + if (item == nullptr) { + return Result::NOT_EXIST; + } + if (cJSON_IsObject(item) == 0) { + return Result::TYPE_ERROR; + } + + *value = std::make_unique(item); + return Result::SUCCESS; +} + +Result PtJson::GetArray(const char *key, std::unique_ptr *value) const +{ + cJSON *item = cJSON_GetObjectItem(object_, key); + if (item == nullptr) { + return Result::NOT_EXIST; + } + if (cJSON_IsArray(item) == 0) { + return Result::TYPE_ERROR; + } + + *value = std::make_unique(item); + return Result::SUCCESS; +} + +Result PtJson::GetAny(const char *key, std::unique_ptr *value) const +{ + cJSON *item = cJSON_GetObjectItem(object_, key); + if (item == nullptr) { + return Result::NOT_EXIST; + } + + *value = std::make_unique(item); + return Result::SUCCESS; +} +} // namespace AppPackingTool +} // namespace OHOS diff --git a/ohos_packing_tool/frameworks/src/log.cpp b/ohos_packing_tool/frameworks/src/log.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a5c0110a9173e07467ec1114e572af4fc626f587 --- /dev/null +++ b/ohos_packing_tool/frameworks/src/log.cpp @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2024 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. + */ + +#include +#include +#include +#include +#include +#include "log.h" + +namespace OHOS { +namespace AppPackingTool { +#define MAX_LOG_SIZE 10240 +#define DEFAULT_LOG_LEVEL LOG_LEVEL_DEBUG +const char LogLevelFlag[5] = {'D', 'I', 'W', 'E', 'F'}; + +void log(char *file, char *func, int line, int level, char *format, ...) +{ + if (level < DEFAULT_LOG_LEVEL) { + return; + } + auto now = std::chrono::system_clock::now(); + std::time_t now_t = std::chrono::system_clock::to_time_t(now); + std::tm* now_tm = std::localtime(&now_t); + std::chrono::milliseconds ms = std::chrono::duration_cast(now.time_since_epoch()) % 1000; + + va_list args; + va_start(args, format); + char buffer[MAX_LOG_SIZE]; + std::vsnprintf(buffer, MAX_LOG_SIZE, format, args); + va_end(args); + + std::cout << "[" << std::put_time(now_tm, "%Y-%m-%d %H:%M:%S") + << '.' << std::setfill('0') << std::setw(3) << ms.count() + << std::setw(0) << "]" + << "[" << file << ":" << line << "]" + << "[" << func << "]" + << "[" << LogLevelFlag[level] << "] " + << buffer + << std::endl; +} + +} // namespace AppPackingTool +} // namespace OHOS diff --git a/ohos_packing_tool/frameworks/src/packageNormalize.cpp b/ohos_packing_tool/frameworks/src/packageNormalize.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ad79a84189f264bd4168ee29b25236de9f722bbf --- /dev/null +++ b/ohos_packing_tool/frameworks/src/packageNormalize.cpp @@ -0,0 +1,248 @@ +/* + * Copyright (c) 2024 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. + */ + +#include +#include +#include +#include +#include +#include +#include + +#include "packageNormalize.h" +#include "constants.h" +#include "zip_utils.h" +#include "utils.h" +#include "json/json_utils.h" +#include "json/pack_info.h" +#include "log.h" +namespace OHOS { +namespace AppPackingTool { +namespace {} + +PackageNormalize::PackageNormalize(const std::map ¶meterMap, std::string &resultReceiver) + : Packager(parameterMap, resultReceiver) +{} + +int PackageNormalize::InitAllowedParam() +{ + allowedParameters_ = { + {} + }; + return ERR_OK; +} + +bool isPositiveInteger(const std::string &str) { + if (str.empty()) { + return false; + } + for (char c : str) { + if (!std::isdigit(c)) { + return false; + } + } + try { + int value = std::stoi(str); + return value > 0; + } catch (const std::out_of_range&) { + return false; + } +} + +int PackageNormalize::PreProcess() +{ + LOGD("PackageNormalize PreProcess"); + auto it = parameterMap_.find(Constants::PARAM_OUT_PATH); + if (it == parameterMap_.end()) { + LOGE("CompressVerify::validatePackageNormalizeMode out-path is empty"); + return ERR_INVALID_VALUE; + } else if (!fs::exists(it->second) || !fs::is_directory(it->second)) { + LOGE("CompressVerify::validatePackageNormalizeMode out-path is not a directory."); + return ERR_INVALID_VALUE; + } + + //校验hsp列表 + it = parameterMap_.find(Constants::PARAM_INPUT_LIST); + if (it == parameterMap_.end()) { + LOGE("CompressVerify::validatePackageNormalizeMode hsp-list is empty."); + return ERR_INVALID_VALUE; + } else { + GerFormattedHspList(hspList_, it->second); + if (hspList_.size() == 0) { + LOGE("CompressVerify::validatePackageNormalizeMode hsp-list is invalid."); + return ERR_INVALID_VALUE; + } + for (std::string path : hspList_) { + fs::path hspPath(path); + if (!fs::exists(hspPath) || !fs::is_regular_file(hspPath)) { + LOGE("CompressVerify::validatePackageNormalizeMode hsp-list is invalid."); + return ERR_INVALID_VALUE; + } + } + } + + it = parameterMap_.find(Constants::PARAM_BUNDLE_NAME); + std::regex pattern(Constants::BUNDLE_NAME_PATTERN); + if (it == parameterMap_.end() || !std::regex_match(it->second, pattern) ) { + LOGE("CompressVerify::validatePackageNormalizeMode bundle-name is invalid."); + return ERR_INVALID_VALUE; + } + + it = parameterMap_.find(Constants::PARAM_VERSION_CODE); + if (it == parameterMap_.end() || !isPositiveInteger(it->second)) { + LOGE("CompressVerify::validatePackageNormalizeMode version-code is invalid."); + return ERR_INVALID_VALUE; + } + + return ERR_OK; +} + +void PackageNormalize::GerFormattedHspList(std::vector &inputList,const std::string filePath) +{ + if (fs::exists(filePath) && fs::is_directory(filePath)) { + for (const auto& entry : std::filesystem::directory_iterator(filePath)) { + if (entry.is_regular_file()) { + auto path = entry.path(); + if (path.extension() == ".hsp") { + inputList.push_back(path.string()); + } + } + } + return; + } + + std::size_t start = 0, end = 0; + while ((end = filePath.find(',', start)) != std::string::npos) { + std::string part = filePath.substr(start, end - start); + if (fs::path(part).extension() == ".hsp") { + inputList.push_back(part); + } + inputList.push_back(part); + start = end + 1; + } + + std::string part = filePath.substr(start); + if (fs::path(part).extension() == ".hsp") { + inputList.push_back(part); + } +} + +std::string GetFileName(std::string &path) +{ + std::string tmpString = path; + size_t tmpNum = tmpString.rfind('/'); + if (tmpNum == std::string::npos) { + return tmpString; + } + tmpString = tmpString.substr(tmpNum + 1, tmpString.size() - tmpNum); + return tmpString; +} + + +std::string GetFileNameWithoutSuffix(std::string &path) +{ + std::string tmpString = path; + size_t tmpNum = tmpString.rfind('/'); + if (tmpNum == std::string::npos) { + return tmpString; + } + tmpString = tmpString.substr(tmpNum + 1, tmpString.size() - tmpNum -5); + return tmpString; +} + +bool PackageNormalize::ModifyModuleJson(const std::string &moduleJsonPath, + const int newVersionCode, const std::string newBundleName) +{ + ModuleJson moduleJson; + if (!moduleJson.ParseFromFile(moduleJsonPath)) { + LOGE("updateModuleJson failed, parse json is null."); + return false; + } + if(!moduleJson.SetBundleName(newBundleName)) { + LOGE("parseAndModifyModuleJson failed, json file not valid."); + return false; + } + if(!moduleJson.SetStageVersionCode(newVersionCode)) { + LOGE("parseAndModifyModuleJson failed, json file not valid."); + return false; + } + if (!JsonUtils::StrToFile(moduleJson.ToString(), moduleJsonPath)) { + LOGE("parseAndModifyModuleJson failed, writeJson failed."); + return false; + } + return true; +} + +bool PackageNormalize::ModifyPackInfo(const std::string &packInfoPath, + const int newVersionCode, const std::string newBundleName) +{ + PackInfo packInfo; + if (!packInfo.ParseFromFile(packInfoPath)) { + LOGE("updatePackInfo failed, parse json is null."); + return false; + } + if(!packInfo.SetBundleName(newBundleName)) { + LOGE("parseAndModifypackInfo failed, json file not valid."); + return false; + } + if(!packInfo.SetVersionCode(newVersionCode)) { + LOGE("parseAndModifypackInfo failed, json file not valid."); + return false; + } + if (!JsonUtils::StrToFile(packInfo.ToString(), packInfoPath)) { + LOGE("parseAndModifyModuleJson failed, writeJson failed."); + return false; + } + return true; +} + +int PackageNormalize::Process() +{ + //TODO::支持目录操作 + LOGD("Start Do Hap Hsp VersionNormalize"); + std::string outPath = parameterMap_.at(Constants::PARAM_OUT_PATH); + std::string tempPath = outPath + "/temp"; + int versionCode = std::stoi(parameterMap_.at(Constants::PARAM_VERSION_CODE)); + std::string bundleName = parameterMap_.at(Constants::PARAM_BUNDLE_NAME); + for (std::string path : hspList_) { + ZipUtils::Unzip(path, tempPath); + std::string moduleJsonPath = tempPath + Constants::LINUX_FILE_SEPARATOR + Constants::MODULE_JSON; + std::string packInfoPath = tempPath + Constants::LINUX_FILE_SEPARATOR + Constants::PACK_INFO; + + if (!fs::exists(moduleJsonPath) || !fs::is_regular_file(moduleJsonPath) || !fs::exists(packInfoPath) || + !fs::is_regular_file(packInfoPath)) { + LOGE("PackageNormalize failed: hsp structure is invalid. %s, \n %s", moduleJsonPath.c_str(), packInfoPath.c_str()); + return ERR_INVALID_VALUE; + } + + if (!ModifyModuleJson(moduleJsonPath, versionCode, bundleName)) { + return ERR_INVALID_VALUE; + } + if (!ModifyPackInfo(packInfoPath, versionCode, bundleName)) { + return ERR_INVALID_VALUE; + } + ZipUtils::Zip(tempPath, outPath + "/" + GetFileName(path)); + Utils::ForceRemoveDirectory(tempPath); + } + return ERR_OK; +} + +int PackageNormalize::PostProcess() +{ + return ERR_OK; +} + +} // namespace AppPackingTool +} // namespace OHOS \ No newline at end of file diff --git a/ohos_packing_tool/frameworks/src/packager.cpp b/ohos_packing_tool/frameworks/src/packager.cpp index 77c2a7ed8d8fb9080f87e40ab23e962cce70a7e5..f634f023c9e477d21eb62c6ac634885cc49e6360 100644 --- a/ohos_packing_tool/frameworks/src/packager.cpp +++ b/ohos_packing_tool/frameworks/src/packager.cpp @@ -20,7 +20,7 @@ #include #include "packager.h" - +#include "log.h" namespace OHOS { namespace AppPackingTool { namespace {} @@ -39,6 +39,8 @@ std::string Packager::MakePackage() if (PreProcess() != ERR_OK) { std::cout << "PreCheck err" << std::endl; + LOGE("CompressEntrance::main exit, verify failed"); + return "ERROR"; } if (Process() != ERR_OK) { diff --git a/ohos_packing_tool/frameworks/src/shell_command.cpp b/ohos_packing_tool/frameworks/src/shell_command.cpp index f2ddea80790a53c3592c9f3bbae7eb21e5887238..2f4e3b7f74b5fb119131ba8d617d193aa1b05143 100644 --- a/ohos_packing_tool/frameworks/src/shell_command.cpp +++ b/ohos_packing_tool/frameworks/src/shell_command.cpp @@ -24,7 +24,7 @@ #include "packager.h" #include "hap_packager.h" #include "hsp_packager.h" - +#include "packageNormalize.h" namespace OHOS { namespace AppPackingTool { namespace { @@ -50,9 +50,9 @@ ShellCommand::~ShellCommand() {} int ShellCommand::CreateCommandMap() { commandMap_ = { - {"help", [this] { return this->RunAsHelpCommand(); } }, - {"pack", [this] { return this->RunAsPackCommand(); } }, - {"unpack", [this] { return this->RunAsUnpackCommand(); } } + {"help", std::bind(&ShellCommand::RunAsHelpCommand, this)}, + {"pack", std::bind(&ShellCommand::RunAsPackCommand, this)}, + {"unpack", std::bind(&ShellCommand::RunAsUnpackCommand, this)}, }; return ERR_OK; } @@ -148,6 +148,10 @@ std::unique_ptr ShellCommand::getPackager() std::unique_ptr packager = std::make_unique(parameterMap_, resultReceiver_); return packager; + } else if (mode == Constants::MODE_PACKAGE_NORMALIZE) { + std::unique_ptr packager = + std::make_unique(parameterMap_, resultReceiver_); + return packager; } resultReceiver_.append("not support --mode: ").append(mode).append("\n"); return nullptr; diff --git a/ohos_packing_tool/frameworks/src/unzip_wrapper.cpp b/ohos_packing_tool/frameworks/src/unzip_wrapper.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9ec67b554af9678a42aa93052daf8c23d7dff977 --- /dev/null +++ b/ohos_packing_tool/frameworks/src/unzip_wrapper.cpp @@ -0,0 +1,143 @@ +/* + * Copyright (c) 2024 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. + */ + +#include +#include +#include "unzip_wrapper.h" +#include "log.h" + +namespace OHOS { +namespace AppPackingTool { +namespace {} + +UnzipWrapper::UnzipWrapper() +{} + +UnzipWrapper::UnzipWrapper(std::string path) : unzFilePath_(path) +{} + +UnzipWrapper::~UnzipWrapper() +{ + Close(); +} + +int UnzipWrapper::Open(std::string& unzPath) +{ + unzFilePath_ = unzPath; + return Open(); +} + +int UnzipWrapper::Open() +{ + if (unzFile_ != nullptr) { + LOGE("unzip file handle has open"); + return ZIP_ERR_SUCCESS; + } + unzFile_ = unzOpen64(unzFilePath_.c_str()); + if (unzFile_ == nullptr) { + LOGE("unzip file handle open failed"); + return ZIP_ERR_FAILURE; + } + return ZIP_ERR_SUCCESS; +} + +void UnzipWrapper::Close() +{ + if (unzFile_ != nullptr) { + unzClose(unzFile_); + unzFile_ = nullptr; + } +} + +std::string UnzipWrapper::ExtractFile(const std::string filePath) +{ + char filename[MAX_ZIP_BUFFER_SIZE] = {0}; + unz_file_info64 fileInfo; + if (unzGetCurrentFileInfo64(unzFile_, &fileInfo, filename, MAX_ZIP_BUFFER_SIZE, NULL, 0, NULL, 0) != UNZ_OK) { + LOGE("get current file info in zip failed!"); + return ""; + } + fs::path fsUnzipPath(filename); + fs::path fsFilePath(filePath); + fs::path fsFullFilePath = fsFilePath / filename; + if (fileInfo.external_fa == ZIP_FILE_ATTR_DIRECTORY || fsUnzipPath.string().rfind('/') == fsUnzipPath.string().length()) { + if (!fs::exists(fsFullFilePath)) { + LOGI("fsFullFilePath not exist, create: %s", fsFullFilePath.string().c_str()); + fs::create_directories(fsFullFilePath.string()); + } + return fsFullFilePath.string(); + } + if (!fs::exists(fsFullFilePath.parent_path())) { + LOGI("fsFullFilePath not exists, create : %s", fsFullFilePath.parent_path().string().c_str()); + fs::create_directories(fsFullFilePath.parent_path().string()); + } + if (unzOpenCurrentFile(unzFile_) != UNZ_OK) { + LOGE("open current file in zip failed![filename=%s]", filename); + return ""; + } + std::fstream file; + file.open(fsFullFilePath.string(), std::ios_base::out | std::ios_base::binary); + if (!file.is_open()) { + LOGE("open file failed![fsFullFilePath=%s]", fsFullFilePath.string().c_str()); + return ""; + } + char fileData[MAX_ZIP_BUFFER_SIZE] = {0}; + int32_t bytesRead; + do { + bytesRead = unzReadCurrentFile(unzFile_, (voidp)fileData, MAX_ZIP_BUFFER_SIZE); + if (bytesRead < 0) { + LOGE("Read current file in zip failed!!"); + file.close(); + return ""; + } + file.write(fileData, bytesRead); + + } while (bytesRead > 0); + file.close(); + unzCloseCurrentFile(unzFile_); + return fsFullFilePath.string(); +} + +int UnzipWrapper::UnzipFile(std::string filePath) +{ + LOGI("Unzip file[%s] to [%s]", unzFilePath_.c_str(), filePath.c_str()); + if (unzFile_ == nullptr) { + LOGE("zip file not open"); + return ZIP_ERR_FAILURE; + } + if (unzGetGlobalInfo64(unzFile_, &unzGlobalInfo_) != UNZ_OK ) { + LOGE("Get global info failed!"); + return ZIP_ERR_FAILURE; + } + int ret = UNZ_OK; + for (size_t i = 0; i < unzGlobalInfo_.number_entry; ++i) { + std::string f = ExtractFile(filePath); + if (f.empty()) { + LOGE("Extract file failed!"); + return ZIP_ERR_FAILURE; + } + ret = unzGoToNextFile(unzFile_); + if (ret == UNZ_END_OF_LIST_OF_FILE) { + break; + } else if (ret != UNZ_OK) { + LOGE("Go to next file in zip failed!"); + return ZIP_ERR_FAILURE; + } + } + return ZIP_ERR_SUCCESS; +} + +} // namespace AppPackingTool +} // namespace OHOS diff --git a/ohos_packing_tool/frameworks/src/utils.cpp b/ohos_packing_tool/frameworks/src/utils.cpp new file mode 100644 index 0000000000000000000000000000000000000000..93d9f2d3021eba08f55a2a104398263a2ac3f6e4 --- /dev/null +++ b/ohos_packing_tool/frameworks/src/utils.cpp @@ -0,0 +1,251 @@ +/* + * Copyright (c) 2024 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. + */ + +// #include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "utils.h" + +namespace OHOS { +namespace AppPackingTool { +namespace { + const int BUFFER_SIZE = 1024; + const int HEX_WIDTH = 2; + const char PATH_DELIMITER = '/'; +} + +Utils::Utils() +{} + +Utils::~Utils() +{} + +std::string Utils::GetFileContent(const std::string filePath) +{ + std::ifstream inFile(filePath, std::ios::in); + if (!inFile.is_open()) { + return nullptr; + } + std::string fileContent((std::istreambuf_iterator(inFile)), std::istreambuf_iterator()); + inFile.close(); + return fileContent; +} + +std::string Utils::ListToString(const std::list& lst) { + std::stringstream ss; + for (auto str : lst) { + ss << str; + } + return ss.str(); +} + +std::string Utils::ReplaceAll(std::string str, const std::string& from, const std::string& to) +{ + size_t startPos = 0; + while ((startPos = str.find(from, startPos)) != std::string::npos) { + str.replace(startPos, from.length(), to); + startPos += to.length(); + } + return str; +} + +int64_t Utils::GetFileLength(const std::string filePath) +{ + struct stat statbuf = { 0 }; + if (stat(filePath.c_str(), &statbuf) != 0) { + return -1; + } + return statbuf.st_size; +} + +bool Utils::EndsWith(const std::string& str, const std::string& suffix) +{ + if (str.size() >= suffix.size() && std::equal(suffix.rbegin(), suffix.rend(), str.rbegin())) { + return true; + } + return false; +} + +// If there is no same element then returns true, else then returns false +bool Utils::CheckDisjoint(const std::list& list1, const std::list& list2) +{ + for (const std::string& str1 : list1) { + for (const std::string& str2 : list2) { + if (str1.compare(str2) == 0) { + return false; + } + } + } + return true; +} + +// If list1 contains all elements in list2, returns true, else returns false +bool Utils::CheckContainsAll(const std::list& list1, const std::list& list2) +{ + bool isFind = false; + for (const std::string& str2 : list2) { + isFind = false; + for (const std::string& str1 : list1) { + if (str2.compare(str1) == 0) { + isFind = true; + break; + } + } + if (!isFind) { + return false; + } + } + return true; +} + +std::string Utils::GetSha256Str(const std::string &str) +{ + unsigned char hash[SHA256_DIGEST_LENGTH]; + SHA256_CTX ctx; + SHA256_Init(&ctx); + SHA256_Update(&ctx, str.c_str(), str.length()); + SHA256_Final(hash, &ctx); + + // Convert hash to hex string + std::stringstream ss; + for (int i = 0; i < SHA256_DIGEST_LENGTH; ++i) { + ss << std::hex << std::setw(HEX_WIDTH) << std::setfill('0') << static_cast(hash[i]); + } + return ss.str(); +} + +std::string Utils::GetSha256File(const std::string &filePath) +{ + std::ifstream file(filePath, std::ios::binary); + if (!file) { + return ""; + } + SHA256_CTX ctx; + SHA256_Init(&ctx); + std::vector buffer(BUFFER_SIZE); + while (!file.eof()) { + file.read(buffer.data(), BUFFER_SIZE); + SHA256_Update(&ctx, buffer.data(), file.gcount()); + } + unsigned char hash[SHA256_DIGEST_LENGTH]; + SHA256_Final(hash, &ctx); + + // Convert hash to hex string + std::stringstream ss; + for (int i = 0; i < SHA256_DIGEST_LENGTH; ++i) { + ss << std::hex << std::setw(HEX_WIDTH) << std::setfill('0') << static_cast(hash[i]); + } + return ss.str(); +} + +bool Utils::IsFileExists(const std::string& file) +{ + return access(file.c_str(), F_OK) == 0; +} + +bool Utils::IsFile(const std::string& file) +{ + struct stat statBuf {}; + return lstat(file.c_str(), &statBuf) == 0 ? S_ISREG(statBuf.st_mode) : false; +} + +bool Utils::IsDirectory(const std::string& dir) +{ + struct stat statBuf {}; + return lstat(dir.c_str(), &statBuf) == 0 ? S_ISDIR(statBuf.st_mode) : false; +} + +bool Utils::RemoveFile(const std::string& file) +{ + return !IsFileExists(file) || (remove(file.c_str()) == 0); +} + +bool Utils::RemoveDirectory(const std::string& dir) +{ + return !IsFileExists(dir) || (rmdir(dir.c_str()) == 0); +} + +std::string Utils::GetFilePathByDir(const std::string& dir, const std::string& fileName) +{ + if (dir.empty()) { + return fileName; + } + std::string filePath = dir; + if (filePath.back() != '/') { + filePath.push_back(PATH_DELIMITER); + } + filePath.append(fileName); + return filePath; +} + +bool Utils::ForceCreateDirectory(const std::string& dir) +{ + std::string::size_type index = 0; + do { + std::string subPath; + index = dir.find('/', index + 1); // (index + 1) means the next char traversed + if (index == std::string::npos) { + subPath = dir; + } else { + subPath = dir.substr(0, index); + } + + if (!IsFileExists(subPath) && mkdir(subPath.c_str(), S_IRWXU) != 0) { + return false; + } + } while (index != std::string::npos); + return IsFileExists(dir); +} + +bool Utils::ForceRemoveDirectory(const std::string& dir, bool isDeleteSelf) +{ + if (IsFile(dir)) { + return RemoveFile(dir); + } else if (IsDirectory(dir)) { + DIR* dirPtr = opendir(dir.c_str()); + if (dirPtr == nullptr) { + return false; + } + struct dirent* dirInfo = nullptr; + while ((dirInfo = readdir(dirPtr)) != nullptr) { + // do not process the special dir + if (strcmp(dirInfo->d_name, ".") == 0 || strcmp(dirInfo->d_name, "..") == 0) { + continue; + } + std::string filePath = GetFilePathByDir(dir, dirInfo->d_name); + if (!ForceRemoveDirectory(filePath)) { + closedir(dirPtr); + return false; + } + } + closedir(dirPtr); + if (isDeleteSelf && !RemoveDirectory(dir)) { + return false; + } + } else { + return false; + } + return true; +} +} // namespace AppPackingTool +} // namespace OHOS diff --git a/ohos_packing_tool/frameworks/src/zip_utils.cpp b/ohos_packing_tool/frameworks/src/zip_utils.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9675e3916626f86ea1a4198b5325c6bb6844dec0 --- /dev/null +++ b/ohos_packing_tool/frameworks/src/zip_utils.cpp @@ -0,0 +1,291 @@ +/* + * Copyright (c) 2024 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. + */ + +#include +#include +#include +#include +#include "zip_utils.h" +#include "log.h" +#include "utils.h" + +namespace OHOS { +namespace AppPackingTool { +namespace { + const std::string RESOURCE_PATH = "resources/base/profile/"; +} + +ZipUtils::ZipUtils() +{} + +ZipUtils::~ZipUtils() +{} + +int ZipUtils::Zip(const std::string& filePath, const std::string& zipFilePath, + const std::string& zipPath, const ZipLevel& zipLevel, const int& append) +{ + ZipWrapper zipWrapper(zipFilePath); + if (zipWrapper.Open(append) != ZIP_ERR_SUCCESS) { + LOGE("ZipWrapper Open failed!"); + return ZIP_ERR_FAILURE; + } + if (zipLevel != ZipLevel::ZIP_LEVEL_DEFAULT) { + zipWrapper.SetZipLevel(zipLevel); + } + int ret = zipWrapper.AddFileOrDirectoryToZip(filePath, zipPath); + if (ret != ZIP_ERR_SUCCESS) { + LOGE("ZipWrapper AddFileOrDirectoryToZip failed!"); + } + zipWrapper.Close(); + return ret; +} + +int ZipUtils::Unzip(const std::string& zipPath, const std::string& filePath) +{ + UnzipWrapper unzipWrapper(zipPath); + if (unzipWrapper.Open() != ZIP_ERR_SUCCESS) { + LOGE("UnzipWrapper Open failed!"); + return ZIP_ERR_FAILURE; + } + int ret = unzipWrapper.UnzipFile(filePath); + if (ret != ZIP_ERR_SUCCESS) { + LOGE("UnzipWrapper UnzipFile failed!"); + } + unzipWrapper.Close(); + return ret; +} + +bool ZipUtils::IsFileExistsInZip(const std::string& zipFilePath, const std::string& filename) +{ + fs::path fsZipFilePath(zipFilePath); + if (!fs::is_regular_file(fsZipFilePath)) { + LOGE("Zip file is not a regular file!"); + return false; + } + unzFile unzipFile = unzOpen64(zipFilePath.c_str()); + if (unzipFile == nullptr) { + LOGE("Open zip file failed! zipFilePath=%s", zipFilePath.c_str()); + return false; + } + unz_global_info64 unzGlobalInfo; + if (unzGetGlobalInfo64(unzipFile, &unzGlobalInfo) != UNZ_OK ) { + LOGE("Get zip global info! zipFilePath=%s", zipFilePath.c_str()); + unzClose(unzipFile); + return false; + } + char filePathInZip[MAX_ZIP_BUFFER_SIZE] = {0}; + unz_file_info64 fileInfo; + int ret = 0; + bool isExist = false; + for (size_t i = 0; i < unzGlobalInfo.number_entry; ++i) { + if (unzGetCurrentFileInfo64(unzipFile, &fileInfo, filePathInZip, MAX_ZIP_BUFFER_SIZE, NULL, 0, NULL, 0) != UNZ_OK) { + LOGE("Get current file info in zip failed!"); + break; + } + std::string strFilePathInZip(filePathInZip); + if (fileInfo.external_fa != ZIP_FILE_ATTR_DIRECTORY && strFilePathInZip.length() >= filename.length()) { + std::transform(strFilePathInZip.begin(), strFilePathInZip.end(), strFilePathInZip.begin(), ::tolower); + if (strFilePathInZip.compare(filename) == 0) { + isExist = true; + break; + } + } + ret = unzGoToNextFile(unzipFile); + if (ret == UNZ_END_OF_LIST_OF_FILE) { + break; + } else if (ret != UNZ_OK) { + LOGE("Go to next file in zip failed!"); + break; + } + } + unzClose(unzipFile); + return isExist; +} + +bool ZipUtils::IsFileNameExistsInZip(const std::string& zipFilePath, const std::string& filename) +{ + fs::path fsZipFilePath(zipFilePath); + if (!fs::is_regular_file(fsZipFilePath)) { + LOGE("Zip file is not a regular file!"); + return false; + } + unzFile unzipFile = unzOpen64(zipFilePath.c_str()); + if (unzipFile == nullptr) { + LOGE("Open zip file failed! zipFilePath=%s", zipFilePath.c_str()); + return false; + } + unz_global_info64 unzGlobalInfo; + if (unzGetGlobalInfo64(unzipFile, &unzGlobalInfo) != UNZ_OK ) { + LOGE("Get zip global info! zipFilePath=%s", zipFilePath.c_str()); + unzClose(unzipFile); + return false; + } + char filePathInZip[MAX_ZIP_BUFFER_SIZE] = {0}; + unz_file_info64 fileInfo; + int ret = 0; + bool isExist = false; + for (size_t i = 0; i < unzGlobalInfo.number_entry; ++i) { + if (unzGetCurrentFileInfo64(unzipFile, &fileInfo, filePathInZip, MAX_ZIP_BUFFER_SIZE, NULL, 0, NULL, 0) != UNZ_OK) { + LOGE("Get current file info in zip failed!"); + break; + } + std::string strFilePathInZip(filePathInZip); + if (fileInfo.external_fa != ZIP_FILE_ATTR_DIRECTORY && strFilePathInZip.length() >= filename.length()) { + if (strFilePathInZip.substr(strFilePathInZip.length() - filename.length()).compare(filename) == 0) { + isExist = true; + break; + } + } + ret = unzGoToNextFile(unzipFile); + if (ret == UNZ_END_OF_LIST_OF_FILE) { + break; + } else if (ret != UNZ_OK) { + LOGE("Go to next file in zip failed!"); + break; + } + } + unzClose(unzipFile); + return isExist; +} + +bool ZipUtils::GetFileContentFromZip(const std::string& zipFilePath, const std::string& filename, std::string& fileContent) +{ + fs::path fsZipFilePath(zipFilePath); + if (!fs::is_regular_file(fsZipFilePath)) { + LOGE("Zip file is not a regular file!"); + return false; + } + unzFile unzipFile = unzOpen64(zipFilePath.c_str()); + if (unzipFile == nullptr) { + LOGE("Open zip file failed! zipFilePath=%s", zipFilePath.c_str()); + return false; + } + if (unzLocateFile(unzipFile, filename.c_str(), 0) != UNZ_OK) { + LOGE("Locate file failed! filename=%s", filename.c_str()); + unzClose(unzipFile); + return false; + } + unz_file_info64 fileInfo; + char filePathInZip[MAX_ZIP_BUFFER_SIZE] = {0}; + if (unzGetCurrentFileInfo64(unzipFile, &fileInfo, filePathInZip, MAX_ZIP_BUFFER_SIZE, NULL, 0, NULL, 0) != UNZ_OK) { + LOGE("Get current file info in zip failed! filename=%s", filename.c_str()); + unzClose(unzipFile); + return false; + } + if (unzOpenCurrentFile(unzipFile) != UNZ_OK) { + LOGE("Open current file in zip failed! filename=%s", filename.c_str()); + unzClose(unzipFile); + return false; + } + char buffer[MAX_ZIP_BUFFER_SIZE]; + int readLen = 0; + fileContent = ""; + while(true) { + std::memset(buffer, 0, MAX_ZIP_BUFFER_SIZE); + readLen = unzReadCurrentFile(unzipFile, buffer, MAX_ZIP_BUFFER_SIZE); + if (readLen < 0) { + LOGE("Read current file in zip failed! filename=%s", filename.c_str()); + unzCloseCurrentFile(unzipFile); + unzClose(unzipFile); + return false; + } else if (readLen == 0) { + break; + } + fileContent += std::string(buffer); + } + unzCloseCurrentFile(unzipFile); + unzClose(unzipFile); + return true; +} + +bool ZipUtils::GetUnzipCurrentFileContent(unzFile& unzipFile, std::string& fileContent) +{ + if (unzipFile == nullptr) { + return false; + } + if (unzOpenCurrentFile(unzipFile) != UNZ_OK) { + LOGE("Open current file in zip failed!"); + return false; + } + char buffer[MAX_ZIP_BUFFER_SIZE]; + int readLen = 0; + fileContent = ""; + while(true) { + std::memset(buffer, 0, MAX_ZIP_BUFFER_SIZE); + readLen = unzReadCurrentFile(unzipFile, buffer, MAX_ZIP_BUFFER_SIZE); + if (readLen < 0) { + LOGE("Read current file in zip failed!"); + unzCloseCurrentFile(unzipFile); + return false; + } else if (readLen == 0) { + break; + } + fileContent += std::string(buffer); + } + unzCloseCurrentFile(unzipFile); + return true; +} + +bool ZipUtils::GetResourceMapFromZip(const std::string& zipFilePath, std::map& resourceMap) +{ + fs::path fsZipFilePath(zipFilePath); + if (!fs::is_regular_file(fsZipFilePath)) { + LOGE("Zip file is not a regular file!"); + return false; + } + unzFile unzipFile = unzOpen64(zipFilePath.c_str()); + if (unzipFile == nullptr) { + LOGE("Open zip file failed! zipFilePath=%s", zipFilePath.c_str()); + return false; + } + unz_global_info64 unzGlobalInfo; + if (unzGetGlobalInfo64(unzipFile, &unzGlobalInfo) != UNZ_OK ) { + LOGE("Get zip global info! zipFilePath=%s", zipFilePath.c_str()); + unzClose(unzipFile); + return false; + } + char filePathInZip[MAX_ZIP_BUFFER_SIZE] = {0}; + unz_file_info64 fileInfo; + int ret = 0; + for (size_t i = 0; i < unzGlobalInfo.number_entry; ++i) { + if (unzGetCurrentFileInfo64(unzipFile, &fileInfo, filePathInZip, MAX_ZIP_BUFFER_SIZE, NULL, 0, NULL, 0) != UNZ_OK) { + LOGE("Get current file info in zip failed!"); + break; + } + std::string strFilePathInZip(filePathInZip); + if (fileInfo.external_fa != ZIP_FILE_ATTR_DIRECTORY && strFilePathInZip.length() > RESOURCE_PATH.length()) { + if (strFilePathInZip.find(RESOURCE_PATH) != std::string::npos) { + std::string fileName = Utils::ReplaceAll(strFilePathInZip, RESOURCE_PATH, ""); + std::string fileContent; + if (!GetUnzipCurrentFileContent(unzipFile, fileContent)) { + LOGE("Get current file content failed! filename=%s", filePathInZip); + continue; + } + resourceMap.emplace(fileName, fileContent); + } + } + ret = unzGoToNextFile(unzipFile); + if (ret == UNZ_END_OF_LIST_OF_FILE) { + break; + } else if (ret != UNZ_OK) { + LOGE("Go to next file in zip failed!"); + break; + } + } + unzClose(unzipFile); + return true; +} +} // namespace AppPackingTool +} // namespace OHOS diff --git a/ohos_packing_tool/frameworks/src/zip_wrapper.cpp b/ohos_packing_tool/frameworks/src/zip_wrapper.cpp new file mode 100644 index 0000000000000000000000000000000000000000..507d37edf1a0bf5cc80529797b00276eea42db7b --- /dev/null +++ b/ohos_packing_tool/frameworks/src/zip_wrapper.cpp @@ -0,0 +1,166 @@ +/* + * Copyright (c) 2024 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. + */ + +#include +#include +#include "zip_wrapper.h" +#include "log.h" +#include "app_log_wrapper.h" + +namespace OHOS { +namespace AppPackingTool { +namespace {} + +ZipWrapper::ZipWrapper() : zipFile_(nullptr) +{} + +ZipWrapper::ZipWrapper(std::string zipPath) : zipFile_(nullptr), zipFilePath_(zipPath) +{} + +ZipWrapper::~ZipWrapper() +{ + Close(); +} + +int ZipWrapper::Open(std::string& zipPath, int append) +{ + zipFilePath_ = zipPath; + return Open(append); +} + +int ZipWrapper::Open(int append) +{ + if (zipFile_ != nullptr) { + LOGE("zip file handle has open"); + return ZIP_ERR_SUCCESS; + } + zipFile_ = zipOpen64(zipFilePath_.c_str(), append); + if (zipFile_ == nullptr) { + LOGE("zip file handle open failed"); + return ZIP_ERR_FAILURE; + } + return ZIP_ERR_SUCCESS; +} + +void ZipWrapper::Close() +{ + if (zipFile_ != nullptr) { + zipClose(zipFile_, nullptr); + zipFile_ = nullptr; + } +} + +int ZipWrapper::AddFileOrDirectoryToZip(const char *filePath, const char *zipPath) +{ + return AddFileOrDirectoryToZip(fs::path(filePath), fs::path(zipPath)); +} + +int ZipWrapper::AddFileOrDirectoryToZip(const std::string &filePath, const std::string &zipPath) +{ + return AddFileOrDirectoryToZip(fs::path(filePath), fs::path(zipPath)); +} + +int ZipWrapper::AddFileOrDirectoryToZip(const fs::path &fsFilePath, const fs::path &fsZipPath) +{ + APP_LOGD("Add [%{public}s] into zip[%{public}s]", fsFilePath.string().c_str(), fsZipPath.string().c_str()); + int ret = ZIP_ERR_SUCCESS; + if (fs::is_directory(fsFilePath)) { + for (const auto &entry : fs::directory_iterator(fsFilePath)) { + fs::path fsZipFullPath = fsZipPath / entry.path().filename(); + ret = AddFileOrDirectoryToZip(entry.path(), fsZipFullPath); + if (ret != ZIP_ERR_SUCCESS) { + return ret; + } + } + } else if (fs::is_regular_file(fsFilePath)) { + ret = AddFileToZip(fsFilePath, fsZipPath); + if (ret != ZIP_ERR_SUCCESS) { + return ret; + } + } + return ZIP_ERR_SUCCESS; +} + +int ZipWrapper::WriteStringToZip(const std::string &content, const std::string& zipPath) +{ + if (!IsOpen()) { + LOGE("zip file is not open"); + return ZIP_ERR_FAILURE; + } + int ret = zipOpenNewFileInZip64(zipFile_, zipPath.c_str(), &zipFileInfo_, nullptr, 0, + nullptr, 0, nullptr, 0, static_cast(zipLevel_), 1); + if (ret != ZIP_OK) { + LOGE("open file in zip failed![ret=%d][zipPath=%s]", ret, zipPath.c_str()); + return ZIP_ERR_FAILURE; + } + ret = zipWriteInFileInZip(zipFile_, content.data(), content.length()); + if (ret < 0) { + LOGE("write file in zip failed![errno=%d][ret=%d]", errno, ret); + ret = ZIP_ERR_FAILURE; + } + zipCloseFileInZip(zipFile_); + return ret; +} + +int ZipWrapper::AddFileToZip(const std::string &filePath, const std::string &zipPath) +{ + return AddFileToZip(fs::path(filePath), fs::path(zipPath)); +} + +int ZipWrapper::AddFileToZip(const fs::path &filePath, const fs::path &zipPath) +{ + if (!IsOpen()) { + LOGE("zip file is not open"); + return ZIP_ERR_FAILURE; + } + if (!fs::is_regular_file(filePath)) { + LOGE("filePath is not regular file![filePath=%s]", filePath.string().c_str()); + return ZIP_ERR_FAILURE; + } + std::ifstream file(filePath.string(), std::ios::binary); + if (!file.is_open()) { + LOGE("open file failed![filePath=%s]", filePath.string().c_str()); + return ZIP_ERR_FAILURE; + } + int ret = zipOpenNewFileInZip64(zipFile_, zipPath.string().c_str(), &zipFileInfo_, nullptr, 0, + nullptr, 0, nullptr, 0, static_cast(zipLevel_), 1); + if (ret != ZIP_OK) { + LOGE("open file in zip failed![ret=%d][zipPath=%s]", ret, zipPath.string().c_str()); + file.close(); + return ZIP_ERR_FAILURE; + } + ret = ZIP_ERR_SUCCESS; + char buffer[MAX_ZIP_BUFFER_SIZE]; + while (file.good()) { + file.read(buffer, sizeof(buffer)); + auto bytesRead = file.gcount(); + if (bytesRead <= 0) { + LOGE("read file bytes error![filePath=%s][bytesRead=%u]", filePath.c_str(), bytesRead); + ret = ZIP_ERR_FAILURE; + break; + } + if (zipWriteInFileInZip(zipFile_, buffer, bytesRead) < 0) { + LOGE("write file in zip failed!"); + ret = ZIP_ERR_FAILURE; + break; + } + } + zipCloseFileInZip(zipFile_); + file.close(); + return ret; +} + +} // namespace AppPackingTool +} // namespace OHOS