diff --git a/bundletool.gni b/bundletool.gni index 23f332b7feec18f32f003b6cf97e541c2c8f4f45..af08feafdbe093138bd22fd7cec52750d2d30c88 100644 --- a/bundletool.gni +++ b/bundletool.gni @@ -26,6 +26,7 @@ declare_args() { overlay_install_bm = true quick_fix_bm = true distributed_bundle_framework_bm = true + bundle_framework_auto_clean_cache_tool = true if (defined(global_parts_info) && !defined(global_parts_info.account_os_account)) { diff --git a/frameworks/BUILD.gn b/frameworks/BUILD.gn index 283c3f13ad78e54e5f01490e311e2130740ede06..cb6aad6a9db81c38c5d5999093c7b2ef53ff823a 100644 --- a/frameworks/BUILD.gn +++ b/frameworks/BUILD.gn @@ -139,6 +139,10 @@ ohos_source_set("tools_test_bm_source_set") { sources += [ "src/quick_fix_status_callback_host_impl.cpp" ] } + if (bundle_framework_auto_clean_cache_tool) { + defines += [ "BUNDLE_FRAMEWORK_AUTO_CLEAN_CACHE" ] + } + if (account_enable_bm) { external_deps += [ "os_account:os_account_innerkits" ] defines += [ "ACCOUNT_ENABLE" ] diff --git a/frameworks/include/bundle_test_tool.h b/frameworks/include/bundle_test_tool.h index bfe1094142fbbb54da63eae7b916e5996f23dc3d..770ae86c0503f570e9a1cf74632c23fe9815dac5 100644 --- a/frameworks/include/bundle_test_tool.h +++ b/frameworks/include/bundle_test_tool.h @@ -94,6 +94,9 @@ private: const std::string &type, std::string &msg); ErrCode RunAsImplicitQuerySkillUriInfo(); ErrCode RunAsQueryAbilityInfoByContinueType(); +#ifdef BUNDLE_FRAMEWORK_AUTO_CLEAN_CACHE + ErrCode RunAsCleanBundleCacheFilesAutomaticCommand(); +#endif std::condition_variable cv_; std::mutex mutex_; @@ -139,6 +142,10 @@ private: ErrCode DumpSandboxBundleInfo(const std::string &bundleName, const int32_t appIndex, const int32_t userId, std::string &dumpResults); ErrCode StringToInt(std::string option, const std::string &commandName, int &temp, bool &result); +#ifdef BUNDLE_FRAMEWORK_AUTO_CLEAN_CACHE + ErrCode StringToUnsignedLongLong(std::string optarg, const std::string &commandName, + uint64_t &temp, bool &result); +#endif ErrCode DeployQuickFix(const std::vector &quickFixPaths, std::shared_ptr &quickFixRes, bool isDebug); ErrCode SwitchQuickFix(const std::string &bundleName, int32_t enable, @@ -162,6 +169,10 @@ private: ErrCode CheckGetGroupIdCorrectOption(int32_t option, std::string &dataGroupId); bool GetGroupDir(const std::string &dataGroupId, std::string& msg); ErrCode CheckGetBundleNameOption(int32_t option, std::string &bundleName); +#ifdef BUNDLE_FRAMEWORK_AUTO_CLEAN_CACHE + ErrCode CheckCleanBundleCacheFilesAutomaticOption(int option, const std::string &commandName, + uint64_t &cacheSize); +#endif }; } // namespace AppExecFwk } // namespace OHOS diff --git a/frameworks/src/bundle_test_tool.cpp b/frameworks/src/bundle_test_tool.cpp index 665afc21f8e4588155f79782a06b98df29f5965a..71ec88109bc1e41be7899e8b4c546463bc840613 100644 --- a/frameworks/src/bundle_test_tool.cpp +++ b/frameworks/src/bundle_test_tool.cpp @@ -121,6 +121,55 @@ const std::string MSG_ERR_BUNDLEMANAGER_SET_DEBUG_MODE_SEND_REQUEST_ERROR = "err const std::string MSG_ERR_BUNDLEMANAGER_SET_DEBUG_MODE_UID_CHECK_FAILED = "error: uid check failed.\n"; static const std::string TOOL_NAME = "bundle_test_tool"; +#ifdef BUNDLE_FRAMEWORK_AUTO_CLEAN_CACHE +static const std::string HELP_MSG = + "usage: bundle_test_tool \n" + "These are common bundle_test_tool commands list:\n" + " help list available commands\n" + " setrm set module isRemovable by given bundle name and module name\n" + " getrm obtain the value of isRemovable by given bundle name and module name\n" + " installSandbox indicates install sandbox\n" + " uninstallSandbox indicates uninstall sandbox\n" + " dumpSandbox indicates dump sandbox info\n" + " getStr obtain the value of label by given bundle name, module name and label id\n" + " getIcon obtain the value of icon by given bundle name, module name, " + "density and icon id\n" + " addAppInstallRule obtain the value of install controlRule by given some app id " + "control rule type, user id and euid\n" + " getAppInstallRule obtain the value of install controlRule by given some app id " + "rule type, user id and euid\n" + " deleteAppInstallRule obtain the value of install controlRule by given some app id " + "user id and euid\n" + " cleanAppInstallRule obtain the value of install controlRule by given rule type " + "user id and euid\n" + " addAppRunningRule obtain the value of app running control rule " + "by given controlRule user id and euidn\n" + " deleteAppRunningRule obtain the value of app running control rule " + "by given controlRule user id and euid\n" + " cleanAppRunningRule obtain the value of app running control " + "rule by given user id and euid\n" + " getAppRunningControlRule obtain the value of app running control rule " + "by given user id and euid and some app id\n" + " getAppRunningControlRuleResult obtain the value of app running control rule " + "by given bundleName user id, euid and controlRuleResult\n" + " deployQuickFix deploy a quick fix patch of an already installed bundle\n" + " switchQuickFix switch a quick fix patch of an already installed bundle\n" + " deleteQuickFix delete a quick fix patch of an already installed bundle\n" + " setDebugMode enable signature debug mode\n" + " getBundleStats get bundle stats\n" + " getAppProvisionInfo get appProvisionInfo\n" + " getDistributedBundleName get distributedBundleName\n" + " eventCB register then unregister bundle event callback\n" + " resetAOTCompileStatus reset AOTCompileStatus\n" + " sendCommonEvent send common event\n" + " queryDataGroupInfos obtain the data group infos of the application\n" + " getGroupDir obtain the data group dir path by data group id\n" + " getJsonProfile obtain the json string of the specified module\n" + " getOdid obtain the odid of the application\n" + " implicitQuerySkillUriInfo obtain the skill uri info of the implicit query ability\n" + " queryAbilityInfoByContinueType get ability info by continue type\n" + " cleanBundleCacheFilesAutomatic clear cache data of a specified size\n"; +#else static const std::string HELP_MSG = "usage: bundle_test_tool \n" "These are common bundle_test_tool commands list:\n" @@ -167,6 +216,7 @@ static const std::string HELP_MSG = " getOdid obtain the odid of the application\n" " implicitQuerySkillUriInfo obtain the skill uri info of the implicit query ability\n" " queryAbilityInfoByContinueType get ability info by continue type\n"; +#endif const std::string HELP_MSG_GET_REMOVABLE = "usage: bundle_test_tool getrm \n" @@ -328,6 +378,15 @@ const std::string HELP_MSG_GET_APP_RUNNING_RESULT_RULE = " -n, --bundle-name specify bundle name of the application\n" " -u, --user-id specify a user id\n"; +#ifdef BUNDLE_FRAMEWORK_AUTO_CLEAN_CACHE +const std::string HELP_MSG_AUTO_CLEAN_CACHE_RULE = + "usage: bundle_test_tool \n" + "eg:bundle_test_tool cleanBundleCacheFilesAutomatic -s \n" + "options list:\n" + " -h, --help list available commands\n" + " -s, --cache-size specify the cache size that needs to be cleaned\n"; +#endif + const std::string HELP_MSG_NO_ADD_INSTALL_RULE_OPTION = "error: you must specify a app id with '-a' or '--app-id' \n" "and a control type with '-t' or '--control-rule-type' \n" @@ -360,6 +419,11 @@ const std::string HELP_MSG_NO_GET_APP_RUNNING_RULE_OPTION = "error: you must specify a app running type with '-n' or '--bundle-name' \n" "and a userid with '-u' or '--user-id' \n"; +#ifdef BUNDLE_FRAMEWORK_AUTO_CLEAN_CACHE +const std::string HELP_MSG_NO_AUTO_CLEAN_CACHE_OPTION = + "error: you must specify a cache size with '-s' or '--cache-size' \n"; +#endif + const std::string HELP_MSG_DEPLOY_QUICK_FIX = "usage: bundle_test_tool deploy quick fix \n" "eg:bundle_test_tool deployQuickFix -p \n" @@ -652,6 +716,15 @@ const struct option LONG_OPTIONS_RULE[] = { {nullptr, 0, nullptr, 0}, }; +#ifdef BUNDLE_FRAMEWORK_AUTO_CLEAN_CACHE +const std::string SHORT_OPTIONS_AUTO_CLEAN_CACHE = "hs:"; +const struct option LONG_OPTIONS_AUTO_CLEAN_CACHE[] = { + {"help", no_argument, nullptr, 'h'}, + {"cache-size", required_argument, nullptr, 's'}, + {nullptr, 0, nullptr, 0}, +}; +#endif + const std::string SHORT_OPTIONS_QUICK_FIX = "hp:n:e:d:"; const struct option LONG_OPTIONS_QUICK_FIX[] = { {"help", no_argument, nullptr, 'h'}, @@ -848,7 +921,10 @@ ErrCode BundleTestTool::CreateCommandMap() {"implicitQuerySkillUriInfo", std::bind(&BundleTestTool::RunAsImplicitQuerySkillUriInfo, this)}, {"queryAbilityInfoByContinueType", - std::bind(&BundleTestTool::RunAsQueryAbilityInfoByContinueType, this)} + std::bind(&BundleTestTool::RunAsQueryAbilityInfoByContinueType, this)}, +#ifdef BUNDLE_FRAMEWORK_AUTO_CLEAN_CACHE + {"cleanBundleCacheFilesAutomatic", std::bind(&BundleTestTool::RunAsCleanBundleCacheFilesAutomaticCommand, this)} +#endif }; return OHOS::ERR_OK; @@ -1548,6 +1624,27 @@ ErrCode BundleTestTool::StringToInt( return OHOS::ERR_OK; } +#ifdef BUNDLE_FRAMEWORK_AUTO_CLEAN_CACHE +ErrCode BundleTestTool::StringToUnsignedLongLong( + std::string optarg, const std::string &commandName, uint64_t &temp, bool &result) +{ + try { + APP_LOGI("StringToUnsignedLongLong start, optarg : %{public}s", optarg.c_str()); + if ((optarg == "") || (optarg[0] == '0') || (!isdigit(optarg[0]))) { + resultReceiver_.append("error: parameter error, cache size must be greater than 0\n"); + return OHOS::ERR_INVALID_VALUE; + } + temp = std::stoull(optarg); + APP_LOGI("bundle_test_tool %{public}s %{public}llu %{public}s", + commandName.c_str(), temp, argv_[optind - 1]); + } catch (const std::exception& e) { + std::cerr << e.what() << std::endl; + result = false; + } + return OHOS::ERR_OK; +} +#endif + bool BundleTestTool::CheckGetStringCorrectOption( int option, const std::string &commandName, int &temp, std::string &name) { @@ -2804,6 +2901,76 @@ ErrCode BundleTestTool::RunAsGetAppRunningControlRuleResultCommand() return result; } +#ifdef BUNDLE_FRAMEWORK_AUTO_CLEAN_CACHE +ErrCode BundleTestTool::CheckCleanBundleCacheFilesAutomaticOption( + int option, const std::string &commandName, uint64_t &cacheSize) +{ + bool ret = true; + switch (option) { + case 'h': { + APP_LOGI("bundle_test_tool %{public}s %{public}s", commandName.c_str(), argv_[optind - 1]); + return OHOS::ERR_INVALID_VALUE; + } + case 's': { + APP_LOGI("bundle_test_tool %{public}s %{public}s", commandName.c_str(), argv_[optind - 1]); + StringToUnsignedLongLong(optarg, commandName, cacheSize, ret); + break; + } + default: { + std::string unknownOption = ""; + std::string unknownOptionMsg = GetUnknownOptionMsg(unknownOption); + APP_LOGE("bundle_test_tool %{public}s with an unknown option.", commandName.c_str()); + resultReceiver_.append(unknownOptionMsg); + return OHOS::ERR_INVALID_VALUE; + } + } + return OHOS::ERR_OK; +} + +ErrCode BundleTestTool::RunAsCleanBundleCacheFilesAutomaticCommand() +{ + ErrCode result = OHOS::ERR_OK; + int counter = 0; + std::string commandName = "cleanBundleCacheFilesAutomatic"; + uint64_t cacheSize; + APP_LOGE("RunAsCleanBundleCacheFilesAutomaticCommand is start"); + while (true) { + counter++; + int option = getopt_long(argc_, argv_, SHORT_OPTIONS_AUTO_CLEAN_CACHE.c_str(), + LONG_OPTIONS_AUTO_CLEAN_CACHE, nullptr); + APP_LOGE("option: %{public}d, optopt: %{public}d, optind: %{public}d", option, optopt, optind); + if (optind < 0 || optind > argc_) { + return OHOS::ERR_INVALID_VALUE; + } + if (option == -1) { + if ((counter == 1) && (strcmp(argv_[optind], cmd_.c_str()) == 0)) { + APP_LOGE("bundle_test_tool getRule with no option."); + resultReceiver_.append(HELP_MSG_NO_AUTO_CLEAN_CACHE_OPTION); + return OHOS::ERR_INVALID_VALUE; + } + break; + } + result = CheckCleanBundleCacheFilesAutomaticOption(option, commandName, cacheSize); + if (result != OHOS::ERR_OK) { + resultReceiver_.append(HELP_MSG_AUTO_CLEAN_CACHE_RULE); + return OHOS::ERR_INVALID_VALUE; + } + } + + APP_LOGI("CleanBundleCacheFilesAutomatic cache-size: %{public}llu", cacheSize); + ErrCode res = bundleMgrProxy_->CleanBundleCacheFilesAutomatic(cacheSize); + if (res == ERR_OK) { + resultReceiver_.append("clean fixed size cache successfully\n"); + } else { + resultReceiver_.append("clean fixed size cache failed, errCode is "+ std::to_string(res) + "\n"); + APP_LOGE("CleanBundleCacheFilesAutomatic failed, result: %{public}d", res); + return res; + } + + return res; +} +#endif + ErrCode BundleTestTool::RunAsDeployQuickFix() { int32_t result = OHOS::ERR_OK;