IssueNo: #I56WF6:modify bm dump-dependencies

Description:modify bm dump-dependencies
Sig:SIG_ApplicaitonFramework
Feature or Bugfix:Bugfix
Binary Source:No
Signed-off-by: wangtiantian <wangtiantian19@huawei.com>
This commit is contained in:
wangtiantian 2022-05-23 11:14:12 +08:00
parent 36ec283864
commit 1b28684d3f
2 changed files with 132 additions and 29 deletions

View File

@ -31,6 +31,7 @@ const std::string HELP_MSG = "usage: bm <command> <options>\n"
" install install a bundle with options\n"
" uninstall uninstall a bundle with options\n"
" dump dump the bundle info\n"
" dump-dependencies dump the dependent moduleNames by given bundle name and module name\n"
" clean clean the bundle data\n"
" enable enable the bundle\n"
" disable disable the bundle\n"
@ -66,7 +67,6 @@ const std::string HELP_MSG_DUMP =
" -a, --all list all bundles in system\n"
" -i, --bundle-info list all bundles info in system\n"
" -n, --bundle-name <bundle-name> list the bundle info by a bundle name\n"
" -m, --module-name <module-name> list the dependent modulenames by a module name\n"
" -s, --shortcut-info list the shortcut info\n"
" -d, --device-id <device-id> specify a device id\n"
" -u, --user-id <user-id> specify a user id\n";
@ -118,6 +118,14 @@ const std::string HELP_MSG_GET_REMOVABLE =
" -n, --bundle-name <bundle-name> get isRemovable by moduleNmae and bundleName\n"
" -m, --module-name <module-name> get isRemovable by moduleNmae and bundleName\n";
const std::string HELP_MSG_DUMP_DEPENDENCIES =
"usage: bm dump-dependencies <options>\n"
"eg:bm dump-dependencies -n <bundle-name> -m <module-name> \n"
"options list:\n"
" -h, --help list available commands\n"
" -n, --bundle-name <bundle-name> dump dependent moduleNames by bundleName and moduleName\n"
" -m, --module-name <module-name> dump dependent moduleNames by bundleName and moduleName\n";
const std::string STRING_INCORRECT_OPTION = "error: incorrect option";
const std::string HELP_MSG_NO_BUNDLE_PATH_OPTION =
"error: you must specify a bundle path with '-p' or '--bundle-path'.";
@ -180,6 +188,7 @@ private:
ErrCode RunAsInstallCommand();
ErrCode RunAsUninstallCommand();
ErrCode RunAsDumpCommand();
ErrCode RunAsDumpDependenciesCommand();
ErrCode RunAsCleanCommand();
ErrCode RunAsEnableCommand();
ErrCode RunAsDisableCommand();
@ -211,6 +220,7 @@ private:
const std::string &bundleName, const std::string &moduleName, std::string &result) const;
int32_t GetCurrentUserId(int32_t userId) const;
ErrCode ParseDependenciesCommand(int32_t option, std::string &bundleName, std::string &moduleName);
sptr<IBundleMgr> bundleMgrProxy_;
sptr<IBundleInstaller> bundleInstallerProxy_;
};

View File

@ -62,11 +62,10 @@ const struct option LONG_OPTIONS[] = {
{nullptr, 0, nullptr, 0},
};
const std::string SHORT_OPTIONS_DUMP = "hn:m:aisu:d:";
const std::string SHORT_OPTIONS_DUMP = "hn:aisu:d:";
const struct option LONG_OPTIONS_DUMP[] = {
{"help", no_argument, nullptr, 'h'},
{"bundle-name", required_argument, nullptr, 'n'},
{"module-name", required_argument, nullptr, 'm'},
{"all", no_argument, nullptr, 'a'},
{"bundle-info", no_argument, nullptr, 'i'},
{"shortcut-info", no_argument, nullptr, 's'},
@ -81,6 +80,13 @@ const struct option LONG_OPTIONS_GET[] = {
{"udid", no_argument, nullptr, 'u'},
{nullptr, 0, nullptr, 0},
};
const std::string SHORT_OPTIONS_DUMP_DEPENDENCIES = "hn:m:";
const struct option LONG_OPTIONS_DUMP_DEPENDENCIES[] = {
{"help", no_argument, nullptr, 'h'},
{"bundle-name", required_argument, nullptr, 'n'},
{"module-name", required_argument, nullptr, 'm'},
{nullptr, 0, nullptr, 0},
};
} // namespace
class CleanCacheCallbackImpl : public CleanCacheCallbackHost {
@ -128,6 +134,7 @@ ErrCode BundleManagerShellCommand::CreateCommandMap()
{"install", std::bind(&BundleManagerShellCommand::RunAsInstallCommand, this)},
{"uninstall", std::bind(&BundleManagerShellCommand::RunAsUninstallCommand, this)},
{"dump", std::bind(&BundleManagerShellCommand::RunAsDumpCommand, this)},
{"dump-dependencies", std::bind(&BundleManagerShellCommand::RunAsDumpDependenciesCommand, this)},
{"clean", std::bind(&BundleManagerShellCommand::RunAsCleanCommand, this)},
{"enable", std::bind(&BundleManagerShellCommand::RunAsEnableCommand, this)},
{"disable", std::bind(&BundleManagerShellCommand::RunAsDisableCommand, this)},
@ -890,13 +897,11 @@ ErrCode BundleManagerShellCommand::RunAsDumpCommand()
int counter = 0;
std::string dumpResults = "";
std::string bundleName = "";
std::string moduleName = "";
bool bundleDumpAll = false;
bool bundleDumpInfos = false;
bool bundleDumpInfo = false;
bool bundleDumpShortcut = false;
bool bundleDumpDistributedBundleInfo = false;
bool bundleDumpDependentModule = false;
std::string deviceId = "";
int32_t userId = Constants::ALL_USERID;
while (true) {
@ -929,14 +934,6 @@ ErrCode BundleManagerShellCommand::RunAsDumpCommand()
result = OHOS::ERR_INVALID_VALUE;
break;
}
case 'm': {
// 'bm dump -m' with no argument: bm dump -m
// 'bm dump --module-name' with no argument: bm dump --bundle-name
APP_LOGD("'bm dump -m' with no argument.");
resultReceiver_.append(STRING_REQUIRE_CORRECT_VALUE);
result = OHOS::ERR_INVALID_VALUE;
break;
}
case 'u': {
// 'bm dump -u' with no argument: bm dump -u
// 'bm dump --user-id' with no argument: bm dump --user-id
@ -996,14 +993,6 @@ ErrCode BundleManagerShellCommand::RunAsDumpCommand()
bundleDumpInfo = true;
break;
}
case 'm': {
// 'bm dump -m xxx'
// 'bm dump --module-name xxx'
APP_LOGD("'bm dump %{public}s %{public}s'", argv_[optind - OFFSET_REQUIRED_ARGUMENT], optarg);
moduleName = optarg;
bundleDumpDependentModule = true;
break;
}
case 's': {
// 'bm dump -n xxx -s'
// 'bm dump --bundle-name xxx --shortcut-info'
@ -1049,12 +1038,6 @@ ErrCode BundleManagerShellCommand::RunAsDumpCommand()
resultReceiver_.append(HELP_MSG_NO_BUNDLE_NAME_OPTION + "\n");
result = OHOS::ERR_INVALID_VALUE;
}
if ((resultReceiver_ == "") && bundleDumpDependentModule && (bundleName.size() == 0)) {
// 'bm dump -m ...' with no bundle name option
APP_LOGD("'bm dump -m' with no bundle name option.");
resultReceiver_.append(HELP_MSG_NO_BUNDLE_NAME_OPTION + "\n");
result = OHOS::ERR_INVALID_VALUE;
}
}
if (result != OHOS::ERR_OK) {
resultReceiver_.append(HELP_MSG_DUMP);
@ -1068,8 +1051,6 @@ ErrCode BundleManagerShellCommand::RunAsDumpCommand()
dumpResults = DumpBundleList(userId);
} else if (bundleDumpInfos) {
dumpResults = DumpBundleInfos(userId);
} else if (bundleDumpDependentModule) {
dumpResults = DumpDependentModuleNames(bundleName, moduleName);
} else if (bundleDumpInfo) {
dumpResults = DumpBundleInfo(bundleName, userId);
}
@ -1082,6 +1063,116 @@ ErrCode BundleManagerShellCommand::RunAsDumpCommand()
return result;
}
ErrCode BundleManagerShellCommand::RunAsDumpDependenciesCommand()
{
int32_t result = OHOS::ERR_OK;
int32_t option = -1;
int32_t counter = 0;
std::string dumpResults;
std::string bundleName;
std::string moduleName;
while (true) {
counter++;
option = getopt_long(argc_, argv_, SHORT_OPTIONS_DUMP_DEPENDENCIES.c_str(),
LONG_OPTIONS_DUMP_DEPENDENCIES, nullptr);
if (optind < 0 || optind > argc_) {
return OHOS::ERR_INVALID_VALUE;
}
if (option == -1) {
if (counter == 1) {
// When scanning the first argument
if (strcmp(argv_[optind], cmd_.c_str()) == 0) {
// 'bm dump-dependencies' with no option: bm dump-dependencies
// 'bm dump-dependencies' with a wrong argument: bm dump-dependencies xxx
resultReceiver_.append(HELP_MSG_NO_OPTION + "\n");
result = OHOS::ERR_INVALID_VALUE;
}
}
break;
}
result = ParseDependenciesCommand(option, bundleName, moduleName);
if (option == '?') {
break;
}
}
if (result == OHOS::ERR_OK) {
if ((resultReceiver_ == "") && (bundleName.size() == 0 || moduleName.size() == 0)) {
// 'bm dump-dependencies -n -m ...' with no bundle name option
resultReceiver_.append(HELP_MSG_NO_REMOVABLE_OPTION);
result = OHOS::ERR_INVALID_VALUE;
}
}
if (result != OHOS::ERR_OK) {
resultReceiver_.append(HELP_MSG_DUMP_DEPENDENCIES);
} else {
dumpResults = DumpDependentModuleNames(bundleName, moduleName);
if (dumpResults.empty() || (dumpResults == "")) {
dumpResults = HELP_MSG_DUMP_FAILED + "\n";
}
resultReceiver_.append(dumpResults);
}
return result;
}
ErrCode BundleManagerShellCommand::ParseDependenciesCommand(int32_t option, std::string &bundleName,
std::string &moduleName)
{
int32_t result = OHOS::ERR_OK;
if (option == '?') {
switch (optopt) {
case 'n': {
// 'bm dump-dependencies -n' with no argument: bm dump-dependencies -n
// 'bm dump-dependencies --bundle-name' with no argument: bm dump-dependencies --bundle-name
resultReceiver_.append(STRING_REQUIRE_CORRECT_VALUE);
result = OHOS::ERR_INVALID_VALUE;
break;
}
case 'm': {
// 'bm dump-dependencies -m' with no argument: bm dump-dependencies -m
// 'bm dump-dependencies --module-name' with no argument: bm dump-dependencies --module-name
resultReceiver_.append(STRING_REQUIRE_CORRECT_VALUE);
result = OHOS::ERR_INVALID_VALUE;
break;
}
default: {
// 'bm dump-dependencies' with an unknown option: bm dump-dependencies -x
// 'bm dump-dependencies' with an unknown option: bm dump-dependencies -xxx
std::string unknownOption = "";
std::string unknownOptionMsg = GetUnknownOptionMsg(unknownOption);
resultReceiver_.append(unknownOptionMsg);
result = OHOS::ERR_INVALID_VALUE;
break;
}
}
} else {
switch (option) {
case 'h': {
// 'bm dump-dependencies -h'
// 'bm dump-dependencies --help'
result = OHOS::ERR_INVALID_VALUE;
break;
}
case 'n': {
// 'bm dump-dependencies -n xxx'
// 'bm dump-dependencies --bundle-name xxx'
bundleName = optarg;
break;
}
case 'm': {
// 'bm dump-dependencies -m xxx'
// 'bm dump-dependencies --module-name xxx'
moduleName = optarg;
break;
}
default: {
result = OHOS::ERR_INVALID_VALUE;
break;
}
}
}
return result;
}
ErrCode BundleManagerShellCommand::RunAsCleanCommand()
{
int32_t result = OHOS::ERR_OK;
@ -1909,6 +2000,8 @@ std::string BundleManagerShellCommand::DumpDependentModuleNames(
const std::string &bundleName,
const std::string &moduleName) const
{
APP_LOGD("DumpDependentModuleNames bundleName: %{public}s, moduleName: %{public}s",
bundleName.c_str(), moduleName.c_str());
std::string dumpResults = "";
std::vector<std::string> dependentModuleNames;
bool dumpRet = bundleMgrProxy_->GetAllDependentModuleNames(bundleName, moduleName, dependentModuleNames);