1 /* 2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef FOUNDATION_APPEXECFWK_STANDARD_TOOLS_BM_INCLUDE_BUNDLE_COMMAND_H 17 #define FOUNDATION_APPEXECFWK_STANDARD_TOOLS_BM_INCLUDE_BUNDLE_COMMAND_H 18 19 #include "shell_command.h" 20 #include "bundle_mgr_interface.h" 21 #include "bundle_installer_interface.h" 22 23 namespace OHOS { 24 namespace AppExecFwk { 25 namespace { 26 const std::string TOOL_NAME = "bm"; 27 28 const std::string HELP_MSG = "usage: bm <command> <options>\n" 29 "These are common bm commands list:\n" 30 " help list available commands\n" 31 " install install a bundle with options\n" 32 " uninstall uninstall a bundle with options\n" 33 " dump dump the bundle info\n" 34 " dump-dependencies dump the dependent moduleNames by given bundle name and module name\n" 35 " clean clean the bundle data\n" 36 " enable enable the bundle\n" 37 " disable disable the bundle\n" 38 " get obtain device udid\n" 39 " quickfix quick fix, including query and install\n"; 40 41 const std::string HELP_MSG_INSTALL = 42 "usage: bm install <options>\n" 43 "options list:\n" 44 " -h, --help list available commands\n" 45 " -p, --bundle-path <file-path> install a hap or hsp by a specified path\n" 46 " -p, --bundle-path <file-path> <file-path> ... install one bundle by some hap or hsp paths\n" 47 " -p, --bundle-path <bundle-direction> install one bundle by a direction,\n" 48 " under which are some hap or hsp files\n" 49 " -r -p <bundle-file-path> replace an existing bundle\n" 50 " -r --bundle-path <bundle-file-path> replace an existing bundle\n" 51 " -u, --user-id <user-id> specify a user id\n" 52 " -w, --waitting-time <waitting-time> specify waitting time for installation, the minimum\n" 53 " waitting time is 5s, the maximum waitting\n" 54 " time is 600s\n"; 55 56 const std::string HELP_MSG_UNINSTALL = 57 "usage: bm uninstall <options>\n" 58 "options list:\n" 59 " -h, --help list available commands\n" 60 " -n, --bundle-name <bundle-name> uninstall a bundle by bundle name\n" 61 " -m, --module-name <module-name> uninstall a module by module name\n" 62 " -u, --user-id <user-id> specify a user id\n" 63 " -k, --keep-data keep the user data after uninstall\n"; 64 65 const std::string HELP_MSG_DUMP = 66 "usage: bm dump <options>\n" 67 "options list:\n" 68 " -h, --help list available commands\n" 69 " -a, --all list all bundles in system\n" 70 " -n, --bundle-name <bundle-name> list the bundle info by a bundle name\n" 71 " -s, --shortcut-info list the shortcut info\n" 72 " -d, --device-id <device-id> specify a device id\n" 73 " -u, --user-id <user-id> specify a user id\n"; 74 75 const std::string HELP_MSG_CLEAN = 76 "usage: bm clean <options>\n" 77 "options list:\n" 78 " -h, --help list available commands\n" 79 " -n, --bundle-name <bundle-name> bundle name\n" 80 " -c, --cache clean bundle cache files by bundle name\n" 81 " -d, --data clean bundle data files by bundle name\n" 82 " -u, --user-id <user-id> specify a user id\n"; 83 84 const std::string HELP_MSG_ENABLE = 85 "usage: bm enable <options>\n" 86 "options list:\n" 87 " -h, --help list available commands\n" 88 " -n, --bundle-name <bundle-name> enable bundle by bundle name\n" 89 " -a, --ability-name <ability-name> enable ability by ability name\n" 90 " -u, --user-id <user-id> specify a user id\n"; 91 92 const std::string HELP_MSG_DISABLE = 93 "usage: bm disable <options>\n" 94 "options list:\n" 95 " -h, --help list available commands\n" 96 " -n, --bundle-name <bundle-name> disable bundle by bundle name\n" 97 " -a, --ability-name <ability-name> disable ability by ability name\n" 98 " -u, --user-id <user-id> specify a user id\n"; 99 100 const std::string HELP_MSG_GET = 101 "usage: bm get <options>\n" 102 "options list:\n" 103 " -u, --udid obtain udid of the current device\n"; 104 105 const std::string HELP_MSG_DUMP_DEPENDENCIES = 106 "usage: bm dump-dependencies <options>\n" 107 "eg:bm dump-dependencies -n <bundle-name> -m <module-name> \n" 108 "options list:\n" 109 " -h, --help list available commands\n" 110 " -n, --bundle-name <bundle-name> dump dependent moduleNames by bundleName and moduleName\n" 111 " -m, --module-name <module-name> dump dependent moduleNames by bundleName and moduleName\n"; 112 113 const std::string HELP_MSG_QUICK_FIX = 114 "usage: bm quickfix <options>\n" 115 "options list:\n" 116 "-h, --help list available commands\n" 117 "-q, --query indicates query quickfix, used with -b or --bundle-name\n" 118 "-b, --bundle-name <bundle-name> query quickfix status and information by a specified bundle name\n" 119 "-a, --apply indicates apply quickfix, used with -f or --file-path\n" 120 "-f, --file-path <file-path> apply a quickfix file by a specified path\n" 121 "-f, --file-path <file-path> <file-path> ... apply some quickfix files of one bundle\n" 122 "-f, --file-path <bundle-direction> apply quickfix files by direction, under which are quickfix files\n"; 123 124 const std::string STRING_INCORRECT_OPTION = "error: incorrect option"; 125 const std::string HELP_MSG_NO_BUNDLE_PATH_OPTION = 126 "error: you must specify a bundle path with '-p' or '--bundle-path'."; 127 128 const std::string HELP_MSG_NO_BUNDLE_NAME_OPTION = 129 "error: you must specify a bundle name with '-n' or '--bundle-name'."; 130 131 const std::string STRING_INSTALL_BUNDLE_OK = "install bundle successfully."; 132 const std::string STRING_INSTALL_BUNDLE_NG = "error: failed to install bundle."; 133 134 const std::string STRING_UNINSTALL_BUNDLE_OK = "uninstall bundle successfully."; 135 const std::string STRING_UNINSTALL_BUNDLE_NG = "error: failed to uninstall bundle."; 136 137 const std::string HELP_MSG_NO_DATA_OR_CACHE_OPTION = 138 "error: you must specify '-c' or '-d' for 'bm clean' option."; 139 const std::string STRING_CLEAN_CACHE_BUNDLE_OK = "clean bundle cache files successfully."; 140 const std::string STRING_CLEAN_CACHE_BUNDLE_NG = "error: failed to clean bundle cache files."; 141 142 const std::string STRING_CLEAN_DATA_BUNDLE_OK = "clean bundle data files successfully."; 143 const std::string STRING_CLEAN_DATA_BUNDLE_NG = "error: failed to clean bundle data files."; 144 145 const std::string STRING_ENABLE_BUNDLE_OK = "enable bundle successfully."; 146 const std::string STRING_ENABLE_BUNDLE_NG = "error: failed to enable bundle."; 147 148 const std::string STRING_DISABLE_BUNDLE_OK = "disable bundle successfully."; 149 const std::string STRING_DISABLE_BUNDLE_NG = "error: failed to disable bundle."; 150 151 const std::string STRING_GET_UDID_OK = "udid of current device is :"; 152 const std::string STRING_GET_UDID_NG = "error: failed to get udid"; 153 154 const std::string HELP_MSG_NO_REMOVABLE_OPTION = 155 "error: you must specify a bundle name with '-n' or '--bundle-name' \n" 156 "and a module name with '-m' or '--module-name' \n"; 157 158 const std::string HELP_MSG_DUMP_FAILED = "error: failed to get information and the parameters may be wrong."; 159 const std::string STRING_REQUIRE_CORRECT_VALUE = "error: option requires a correct value.\n"; 160 } // namespace 161 162 class BundleManagerShellCommand : public ShellCommand { 163 public: 164 BundleManagerShellCommand(int argc, char *argv[]); ~BundleManagerShellCommand()165 ~BundleManagerShellCommand() override 166 {} 167 168 private: 169 ErrCode CreateCommandMap() override; 170 ErrCode CreateMessageMap() override; 171 ErrCode Init() override; 172 173 ErrCode RunAsHelpCommand(); 174 ErrCode RunAsInstallCommand(); 175 ErrCode RunAsUninstallCommand(); 176 ErrCode RunAsDumpCommand(); 177 ErrCode RunAsDumpDependenciesCommand(); 178 ErrCode RunAsCleanCommand(); 179 ErrCode RunAsEnableCommand(); 180 ErrCode RunAsDisableCommand(); 181 ErrCode RunAsGetCommand(); 182 ErrCode RunAsQuickFixCommand(); 183 184 std::string DumpBundleList(int32_t userId) const; 185 std::string DumpBundleInfo(const std::string &bundleName, int32_t userId) const; 186 std::string DumpShortcutInfos(const std::string &bundleName, int32_t userId) const; 187 std::string DumpDistributedBundleInfo(const std::string &deviceId, const std::string &bundleName); 188 std::string DumpDependentModuleNames(const std::string &bundleName, const std::string &moduleName) const; 189 190 int32_t InstallOperation(const std::vector<std::string> &bundlePaths, InstallParam &installParam, 191 int32_t waittingTime) const; 192 int32_t UninstallOperation(const std::string &bundleName, const std::string &moduleName, 193 InstallParam &installParam) const; 194 std::string GetUdid() const; 195 196 ErrCode GetBundlePath(const std::string& param, std::vector<std::string>& bundlePaths) const; 197 198 bool CleanBundleCacheFilesOperation(const std::string &bundleName, int32_t userId) const; 199 bool CleanBundleDataFilesOperation(const std::string &bundleName, int32_t userId) const; 200 201 bool SetApplicationEnabledOperation(const AbilityInfo &abilityInfo, bool isEnable, int32_t userId) const; 202 203 ErrCode ParseDependenciesCommand(int32_t option, std::string &bundleName, std::string &moduleName); 204 sptr<IBundleMgr> bundleMgrProxy_; 205 sptr<IBundleInstaller> bundleInstallerProxy_; 206 }; 207 } // namespace AppExecFwk 208 } // namespace OHOS 209 210 #endif // FOUNDATION_APPEXECFWK_STANDARD_TOOLS_BM_INCLUDE_BUNDLE_COMMAND_H