From 061e57ddc540907f113864b9916dc100d413d3ec Mon Sep 17 00:00:00 2001 From: small_leek Date: Tue, 24 Jun 2025 12:02:37 +0800 Subject: [PATCH] replace conf format Signed-off-by: small_leek --- hisysevent.yaml | 3 +- services/bundlemgr/include/bundle_parser.h | 9 ++ .../include/bundle_service_constants.h | 2 +- services/bundlemgr/include/bundle_util.h | 1 - .../bundlemgr/include/pre_bundle_profile.h | 11 ++- .../bundlemgr/src/base_bundle_installer.cpp | 6 +- services/bundlemgr/src/bundle_parser.cpp | 14 +++ services/bundlemgr/src/bundle_util.cpp | 35 -------- services/bundlemgr/src/pre_bundle_profile.cpp | 30 +++++++ .../bms_bundle_installer_permission_test.cpp | 55 +++++------- .../bms_bundle_installer_test.cpp | 85 +++++-------------- .../bms_bundle_parser_test.cpp | 39 +++++++++ 12 files changed, 147 insertions(+), 143 deletions(-) diff --git a/hisysevent.yaml b/hisysevent.yaml index d969d34ccc..a0d6351beb 100644 --- a/hisysevent.yaml +++ b/hisysevent.yaml @@ -303,4 +303,5 @@ DEFAULT_APP: USERID: {type: INT32, desc: current userId} CALLING_NAME: {type: STRING, desc: calling name} WANT: {type: STRING, desc: want} - UTD: {type: STRING, desc: utd} \ No newline at end of file + UTD: {type: STRING, desc: utd} + diff --git a/services/bundlemgr/include/bundle_parser.h b/services/bundlemgr/include/bundle_parser.h index 3359018c9d..99c5d6b14b 100644 --- a/services/bundlemgr/include/bundle_parser.h +++ b/services/bundlemgr/include/bundle_parser.h @@ -18,6 +18,7 @@ #include #include +#include #include "app_privilege_capability.h" #include "appexecfwk_errors.h" @@ -123,6 +124,14 @@ public: const std::string &configFile, std::vector &routerArray) const; static ErrCode ParseNoDisablingList(const std::string &configPath, std::vector &noDisablingList); + /** + * @brief Parse bundleNames by the configFile. + * @param configFile Indicates the path of configFile. + * @param bundleNames Indicates the ark startup cache list. + * @return Returns ERR_OK if the bundle successfully parsed; returns ErrCode otherwise. + */ + ErrCode ParseArkStartupCacheConfig(const std::string &configFile, + std::unordered_set &bundleNames) const; }; } // namespace AppExecFwk } // namespace OHOS diff --git a/services/bundlemgr/include/bundle_service_constants.h b/services/bundlemgr/include/bundle_service_constants.h index 8a68d25f4f..eb817dcae8 100644 --- a/services/bundlemgr/include/bundle_service_constants.h +++ b/services/bundlemgr/include/bundle_service_constants.h @@ -168,7 +168,7 @@ constexpr int32_t SYSTEM_OPTIMIZE_MODE = (S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH constexpr const char* ARK_STARTUP_CACHE_DIR = "/ark_startup_cache/"; constexpr const char* SYSTEM_OPTIMIZE_PATH = "/data/app/el1/%/system_optimize/"; // configuration of app startup list -constexpr const char* APP_STARTUP_CACHE_CONG = "/system/etc/ark/app_startup_snapshot.conf"; +constexpr const char* APP_STARTUP_CACHE_CONG = "/system/etc/ark/app_startup_snapshot.json"; // code signature constexpr const char* CODE_SIGNATURE_FILE_SUFFIX = ".sig"; constexpr const char* CODE_SIGNATURE_HAP = "Hap"; diff --git a/services/bundlemgr/include/bundle_util.h b/services/bundlemgr/include/bundle_util.h index d0ca31bed8..aab49944db 100644 --- a/services/bundlemgr/include/bundle_util.h +++ b/services/bundlemgr/include/bundle_util.h @@ -245,7 +245,6 @@ public: static bool StrToUint32(const std::string &str, uint32_t &value); static std::string ExtractStringFromJson(nlohmann::json &jsonObject, const std::string &key); static std::unordered_map ParseMapFromJson(const std::string &jsonStr); - static std::unordered_set ParseAppStartupBundleNames(const std::string &confFilePath); private: static std::mutex g_mutex; }; diff --git a/services/bundlemgr/include/pre_bundle_profile.h b/services/bundlemgr/include/pre_bundle_profile.h index b2f986a742..bfcd4ede3f 100644 --- a/services/bundlemgr/include/pre_bundle_profile.h +++ b/services/bundlemgr/include/pre_bundle_profile.h @@ -21,7 +21,7 @@ #include "appexecfwk_errors.h" #include "json_util.h" #include "pre_scan_info.h" - +#include namespace OHOS { namespace AppExecFwk { class PreBundleProfile { @@ -79,6 +79,15 @@ public: const nlohmann::json &jsonBuf, std::set &extensionTypeList) const; void ProcessOnDemandList(std::set &scanAppInfos, std::set &scanDemandInfos) const; + /** + * @brief Transform the jsonBuf to bundleNames. + * @param jsonBuf Indicates the jsonBuf. + * @param arkStartupCacheList Indicates the ark atsrtup cache bundlenames. + * @return Returns ERR_OK if the information transformed successfully; returns error code otherwise. + */ + ErrCode TransToArkStartupCacheList( + const nlohmann::json &jsonBuf, + std::unordered_set &arkStartupCacheList) const; }; } // namespace AppExecFwk } // namespace OHOS diff --git a/services/bundlemgr/src/base_bundle_installer.cpp b/services/bundlemgr/src/base_bundle_installer.cpp index 80533c0836..72d2912ac2 100644 --- a/services/bundlemgr/src/base_bundle_installer.cpp +++ b/services/bundlemgr/src/base_bundle_installer.cpp @@ -44,6 +44,7 @@ #include "bms_extension_data_mgr.h" #include "bundle_clone_installer.h" #include "bundle_permission_mgr.h" +#include "bundle_parser.h" #include "bundle_resource_helper.h" #include "bundle_util.h" #include "code_protect_bundle_info.h" @@ -6657,8 +6658,9 @@ ErrCode BaseBundleInstaller::CreateArkStartupCache(const ArkStartupCache &create return ERR_APPEXECFWK_ARK_STARTUP_CACHE_ONLY_ALLOW_CREATE_APP_OR_ATOMIC; } - std::unordered_set startupBundles = - BundleUtil::ParseAppStartupBundleNames(ServiceConstants::APP_STARTUP_CACHE_CONG); + std::unordered_set startupBundles; + BundleParser bundleParser; + bundleParser.ParseArkStartupCacheConfig(ServiceConstants::APP_STARTUP_CACHE_CONG, startupBundles); if (startupBundles.find(createArk.bundleName) == startupBundles.end()) { LOG_W(BMS_TAG_INSTALLER, "%{public}s is not in startupBundles", createArk.bundleName.c_str()); return ERR_APPEXECFWK_ARK_STARTUP_CACHE_ONLY_ALLOW_CREATE_IN_WHITE_LIST; diff --git a/services/bundlemgr/src/bundle_parser.cpp b/services/bundlemgr/src/bundle_parser.cpp index 5b27b38339..935638eeb7 100644 --- a/services/bundlemgr/src/bundle_parser.cpp +++ b/services/bundlemgr/src/bundle_parser.cpp @@ -424,5 +424,19 @@ ErrCode BundleParser::ParseNoDisablingList(const std::string &configPath, std::v } return ERR_OK; } + +ErrCode BundleParser::ParseArkStartupCacheConfig( + const std::string &configFile, + std::unordered_set &bundleNames) const +{ + APP_LOGD("Parse ParseArkStartupCacheConfig from %{public}s", configFile.c_str()); + nlohmann::json jsonBuf; + if (!ReadFileIntoJson(configFile, jsonBuf)) { + return ERR_APPEXECFWK_PARSE_FILE_FAILED; + } + + PreBundleProfile preBundleProfile; + return preBundleProfile.TransToArkStartupCacheList(jsonBuf, bundleNames); +} } // namespace AppExecFwk } // namespace OHOS diff --git a/services/bundlemgr/src/bundle_util.cpp b/services/bundlemgr/src/bundle_util.cpp index 67d1972cac..6aa1521c5a 100644 --- a/services/bundlemgr/src/bundle_util.cpp +++ b/services/bundlemgr/src/bundle_util.cpp @@ -1222,40 +1222,5 @@ bool BundleUtil::GetBitValue(const uint8_t num, const uint8_t pos) { return (num & (1U << pos)) != 0; } - -std::unordered_set BundleUtil::ParseAppStartupBundleNames(const std::string &confFilePath) -{ - std::unordered_set bundleNames; - std::ifstream file(confFilePath); - - if (!file.is_open()) { - APP_LOGE("fail to open %{public}s file, errno:%{public}d", - confFilePath.c_str(), errno); - return bundleNames; - } - - std::string line; - while (std::getline(file, line)) { - // Skip empty lines and comments - if (line.empty() || line[0] == '#') { - continue; - } - // Remove leading and trailing whitespace - line.erase(0, line.find_first_not_of(" \t")); - line.erase(line.find_last_not_of(" \t") + 1); - - // Find the end of bundle name (before' ' or '#') - size_t endPos = line.find_first_of("# \t"); - if (endPos == std::string::npos) { - endPos = line.length(); - } - - std::string bundleName = line.substr(0, endPos); - if (!bundleName.empty()) { - bundleNames.insert(bundleName); - } - } - return bundleNames; -} } // namespace AppExecFwk } // namespace OHOS diff --git a/services/bundlemgr/src/pre_bundle_profile.cpp b/services/bundlemgr/src/pre_bundle_profile.cpp index ad76bebc8c..62880c223f 100644 --- a/services/bundlemgr/src/pre_bundle_profile.cpp +++ b/services/bundlemgr/src/pre_bundle_profile.cpp @@ -26,6 +26,7 @@ constexpr const char* ON_DEMAND_INSTALL_LIST = "on_demand_install_list"; constexpr const char* UNINSTALL_LIST = "uninstall_list"; constexpr const char* EXTENSION_TYPE = "extensionType"; constexpr const char* RECOVER_LIST = "recover_list"; +constexpr const char* ARK_STARTUP_CACHE_LIST = "ark_startup_cache_list"; constexpr const char* APP_DIR = "app_dir"; constexpr const char* REMOVABLE = "removable"; constexpr const char* APPIDENTIFIER = "appIdentifier"; @@ -548,5 +549,34 @@ void PreBundleProfile::ProcessOnDemandList(std::set &scanAppInfos, } } } + +ErrCode PreBundleProfile::TransToArkStartupCacheList( + const nlohmann::json &jsonBuf, + std::unordered_set &arkStartupCacheList) const +{ + APP_LOGD("transform jsonBuf to bundleNames"); + if (jsonBuf.is_discarded()) { + APP_LOGE("profile format error"); + return ERR_APPEXECFWK_PARSE_BAD_PROFILE; + } + + const auto &jsonObjectEnd = jsonBuf.end(); + int32_t parseResult = ERR_OK; + std::vector names; + GetValueIfFindKey>(jsonBuf, + jsonObjectEnd, + ARK_STARTUP_CACHE_LIST, + names, + JsonType::ARRAY, + false, + parseResult, + ArrayType::STRING); + for (const auto &name : names) { + APP_LOGD("ark startup cache bundleName %{public}s", name.c_str()); + arkStartupCacheList.insert(name); + } + names.clear(); + return parseResult; +} } // namespace AppExecFwk } // namespace OHOS diff --git a/services/bundlemgr/test/unittest/bms_bundle_installer_test/bms_bundle_installer_permission_test.cpp b/services/bundlemgr/test/unittest/bms_bundle_installer_test/bms_bundle_installer_permission_test.cpp index e46ec455b5..c56d07992d 100644 --- a/services/bundlemgr/test/unittest/bms_bundle_installer_test/bms_bundle_installer_permission_test.cpp +++ b/services/bundlemgr/test/unittest/bms_bundle_installer_test/bms_bundle_installer_permission_test.cpp @@ -91,8 +91,7 @@ public: void ClearBundleInfo(); void ClearDataMgr(); void ResetDataMgr(); - void WriteToConfigFile(const std::string& filename, - const std::vector& newEntries) const; + bool WriteToConfigFile(const std::string &bundleName) const void GetExistedEntries(const std::string &filename, std::set &existingEntries, std::vector &allLines) const; @@ -360,39 +359,26 @@ void BmsBundleInstallerPermissionTest::GetExistedEntries(const std::string &file } } -void BmsBundleInstallerPermissionTest::WriteToConfigFile(const std::string &filename, - const std::vector &newEntries) const +bool BmsBundleInstallerPermissionTest::WriteToConfigFile(const std::string &bundleName) const { - bool fileExists = std::filesystem::exists(filename); - std::set existingBundleNames; - std::vector allLines; - if (fileExists) { - GetExistedEntries(filename, existingBundleNames, allLines); - } - std::ofstream outFile(filename); - if (!outFile.is_open()) { - LOG_E(BMS_TAG_INSTALLD, "%{public}s can not open, errno: %{public}d", - filename.c_str(), errno); - return; - } - - for (const auto& line : allLines) { - outFile << line << "\n"; + const std::string filename = ServiceConstants::APP_STARTUP_CACHE_CONG; + nlohmann::json jsonObject; + std::ifstream in(filename); + if (!in.is_open()) { + APP_LOGE("Copy file failed due to open conf file failed errno:%{public}d", errno); + return false; } - for (const auto& entry : newEntries) { - std::string bundleName; - size_t endPos = entry.find_first_of(":# \t"); - if (endPos == std::string::npos) { - bundleName = entry; - } else { - bundleName = entry.substr(0, endPos); - } - if (existingBundleNames.find(bundleName) == existingBundleNames.end()) { - outFile << entry << "\n"; - existingBundleNames.insert(bundleName); - } + in_file >> jsonObject; + if (!jsonObject.contains("ark_startup_cache_list") || !jsonObject["ark_startup_cache_list"].is_array()) { + jsonObject["ark_startup_cache_list"] = nlohmann::json::array(); } - outFile.close(); + + jsonObject["ark_startup_cache_list"].push_back(bundleName); + + std::ofstream out_file(filename); + out_file << std::setw(4) << jsonObject; + out_file.close(); + return 0; } /** @@ -1157,10 +1143,7 @@ HWTEST_F(BmsBundleInstallerPermissionTest, CreateArkStartupCache_0030, Function ceateArk.mode = ServiceConstants::SYSTEM_OPTIMIZE_MODE; ceateArk.uid = 0; ceateArk.gid = 0; - std::vector entries = { - "com.test # Keep" - }; - WriteToConfigFile(ServiceConstants::APP_STARTUP_CACHE_CONG, entries); + WriteToConfigFile("com.test"); // test bundlename in white list BaseBundleInstaller installer3; diff --git a/services/bundlemgr/test/unittest/bms_bundle_installer_test/bms_bundle_installer_test.cpp b/services/bundlemgr/test/unittest/bms_bundle_installer_test/bms_bundle_installer_test.cpp index 0bf206f7a3..6f51821e45 100644 --- a/services/bundlemgr/test/unittest/bms_bundle_installer_test/bms_bundle_installer_test.cpp +++ b/services/bundlemgr/test/unittest/bms_bundle_installer_test/bms_bundle_installer_test.cpp @@ -268,8 +268,7 @@ public: void ResetDataMgr(); bool CheckShaderCachePathExist(const std::string &bundleName, const int32_t appIndex, const int32_t &userId) const; - void WriteToConfigFile(const std::string& filename, - const std::vector& newEntries) const; + bool WriteToConfigFile(const std::string& bundleName) const; ErrCode MkdirIfNotExist(const std::string &dir) const; void GetExistedEntries(const std::string &filename, std::set &existingEntries, @@ -672,39 +671,26 @@ void BmsBundleInstallerTest::GetExistedEntries(const std::string &filename, } } -void BmsBundleInstallerTest::WriteToConfigFile(const std::string &filename, - const std::vector &newEntries) const +bool BmsBundleInstallerTest::WriteToConfigFile(const std::string &bundleName) const { - bool fileExists = std::filesystem::exists(filename); - std::set existingBundleNames; - std::vector allLines; - if (fileExists) { - GetExistedEntries(filename, existingBundleNames, allLines); - } - std::ofstream outFile(filename); - if (!outFile.is_open()) { - LOG_E(BMS_TAG_INSTALLD, "%{public}s can not open, errno: %{public}d", - filename.c_str(), errno); - return; + const std::string filename = ServiceConstants::APP_STARTUP_CACHE_CONG; + nlohmann::json jsonObject; + std::ifstream in(filename); + if (!in.is_open()) { + APP_LOGE("Copy file failed due to open conf file failed errno:%{public}d", errno); + return false; } - - for (const auto& line : allLines) { - outFile << line << "\n"; + in_file >> jsonObject; + if (!jsonObject.contains("ark_startup_cache_list") || !jsonObject["ark_startup_cache_list"].is_array()) { + jsonObject["ark_startup_cache_list"] = nlohmann::json::array(); } - for (const auto& entry : newEntries) { - std::string bundleName; - size_t endPos = entry.find_first_of(":# \t"); - if (endPos == std::string::npos) { - bundleName = entry; - } else { - bundleName = entry.substr(0, endPos); - } - if (existingBundleNames.find(bundleName) == existingBundleNames.end()) { - outFile << entry << "\n"; - existingBundleNames.insert(bundleName); - } - } - outFile.close(); + + jsonObject["ark_startup_cache_list"].push_back(bundleName); + + std::ofstream out_file(filename); + out_file << std::setw(4) << jsonObject; + out_file.close(); + return 0; } ErrCode BmsBundleInstallerTest::MkdirIfNotExist(const std::string &dir) const @@ -11911,36 +11897,6 @@ HWTEST_F(BmsBundleInstallerTest, GetConfirmUserId_0002, Function | SmallTest | L auto res = installer.GetConfirmUserId(TEST_U100, infos); EXPECT_EQ(res, TEST_U1); } - -/** - * @tc.number: ParseAppStartupBundleNames_0100 - * @tc.name: test ParseAppStartupBundleNames - * @tc.desc: 1.parse the conf file - */ -HWTEST_F(BmsBundleInstallerTest, ParseAppStartupBundleNames_0100, Function | SmallTest | Level0) -{ - std::string dir = "/data/test/bms_insatller_test"; - BaseBundleInstaller installer; - auto ret2 = MkdirIfNotExist(dir); - EXPECT_EQ(ret2, ERR_OK); - - BundleUtil bundleUtil; - std::string confPath = "/system/etc/ark/test.conf"; - std::unordered_set res = bundleUtil.ParseAppStartupBundleNames(confPath); - EXPECT_TRUE(res.empty()); - - std::vector entries = { - "", - "#", - " # ", - "cn.123.cnrnetohos:aot # ", - "com.456.harmony # ", - }; - - WriteToConfigFile(ServiceConstants::APP_STARTUP_CACHE_CONG, entries); - res = bundleUtil.ParseAppStartupBundleNames(ServiceConstants::APP_STARTUP_CACHE_CONG); - EXPECT_FALSE(res.empty()); -} /** * @tc.number: CleanArkStartupCache_0010 @@ -12031,10 +11987,7 @@ HWTEST_F(BmsBundleInstallerTest, CreateArkStartupCache_0030, Function | SmallTes ceateArk.mode = ServiceConstants::SYSTEM_OPTIMIZE_MODE; ceateArk.uid = 0; ceateArk.gid = 0; - std::vector entries = { - "com.test2 # Keep" - }; - WriteToConfigFile(ServiceConstants::APP_STARTUP_CACHE_CONG, entries); + WriteToConfigFile("com.test2"); // test bundlename in white list BaseBundleInstaller installer3; diff --git a/services/bundlemgr/test/unittest/bms_bundle_parser_test/bms_bundle_parser_test.cpp b/services/bundlemgr/test/unittest/bms_bundle_parser_test/bms_bundle_parser_test.cpp index 2e3a84c753..cc844cf8cd 100644 --- a/services/bundlemgr/test/unittest/bms_bundle_parser_test/bms_bundle_parser_test.cpp +++ b/services/bundlemgr/test/unittest/bms_bundle_parser_test/bms_bundle_parser_test.cpp @@ -15,6 +15,7 @@ #define private public +#include #include #include #include @@ -1312,6 +1313,7 @@ protected: void CheckProfileModule(const nlohmann::json &checkedProfileJson, const ErrCode code) const; ErrCode CheckProfileDefaultPermission(const nlohmann::json &checkedProfileJson, std::set &defaultPermissions) const; + bool WriteToConfigFile(const std::string &bundleName) const; protected: std::ostringstream pathStream_; }; @@ -1556,6 +1558,26 @@ ErrCode BmsBundleParserTest::CheckProfileDefaultPermission(const nlohmann::json return profile.TransformTo(jsonObject, defaultPermissions); } +bool BmsBundleParserTest::WriteToConfigFile(const std::string &bundleName) const +{ + const std::string filename = ServiceConstants::APP_STARTUP_CACHE_CONG; + nlohmann::json jsonObject; + std::ifstream in(filename); + if (!in.is_open()) { + APP_LOGE("Copy file failed due to open conf file failed errno:%{public}d", errno); + return false; + } + in >> jsonObject; + if (!jsonObject.contains("ark_startup_cache_list") || !jsonObject["ark_startup_cache_list"].is_array()) { + jsonObject["ark_startup_cache_list"] = nlohmann::json::array(); + } + jsonObject["ark_startup_cache_list"].push_back(bundleName); + std::ofstream out_file(filename); + out_file << std::setw(4) << jsonObject; + out_file.close(); + return 0; +} + /** * @tc.number: TestParse_0200 * @tc.name: parse bundle package by config.json @@ -4314,4 +4336,21 @@ HWTEST_F(BmsBundleParserTest, FormInfo_0600, Function | MediumTest | Level1) EXPECT_EQ(jsonObject["groupId"], "123"); EXPECT_EQ(jsonObject["name"], "testName"); } + +/** + * @tc.number: ParseArkStartupCacheConfig_0100 + * @tc.name: Test ParseArkStartupCacheConfig + * @tc.desc: test the ParseArkStartupCacheConfig of BundleParser + */ +HWTEST_F(BmsBundleParserTest, ParseArkStartupCacheConfig_0100, Function | MediumTest | Level1) +{ + BundleParser bundleParser; + std::string configFile = "config.cfg"; + std::unordered_set arkStartupCacheList; + ErrCode ret = bundleParser.ParseArkStartupCacheConfig(configFile, arkStartupCacheList); + EXPECT_EQ(ret, ERR_APPEXECFWK_PARSE_FILE_FAILED); + WriteToConfigFile("com.123"); + ret = bundleParser.ParseArkStartupCacheConfig(configFile, arkStartupCacheList); + EXPECT_EQ(ret, ERR_OK); +} } // OHOS \ No newline at end of file -- Gitee