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 " get obtain device udid\n" 35 " quickfix quick fix, including query and install\n" 36 " compile Compile the software package\n" 37 " copy-ap Copy software ap file to /data/local/pgo\n" 38 " dump-overlay dump overlay info of the specific overlay bundle\n" 39 " dump-target-overlay dump overlay info of the specific target bundle\n" 40 " dump-dependencies dump dependencies by given bundle name and module name\n" 41 " dump-shared dump inter-application shared library information by bundle name\n"; 42 43 const std::string ENABLE_DISABLE_HELP_MSG = " enable enable the bundle\n" 44 " disable disable the bundle\n"; 45 46 const std::string CLEAN_HELP_MSG = " clean clean the bundle data\n"; 47 48 const std::string HELP_MSG_COMPILE = 49 "usage: bm compile [-m mode] [-r reset] (bundle-name | -a)\n" 50 "options list:\n" 51 " -h, --help list available commands.\n" 52 " -m, --mode <mode-name> select partial or full mode.\n" 53 " -r, --reset clear bundle configuration file data.\n" 54 " -a, --all compile or reset all software packages.\n"; 55 56 const std::string HELP_MSG_COPY_AP = 57 "usage: bm copy-ap (bundle-name | -a)\n" 58 "options list:\n" 59 " -h, --help list available commands.\n" 60 " -n, --bundle-name <bundle-name> copy ap by bundle name\n" 61 " -a, --all copy all software packages.\n"; 62 63 const std::string HELP_MSG_INSTALL = 64 "usage: bm install <options>\n" 65 "options list:\n" 66 " -h, --help list available commands\n" 67 " -p, --bundle-path <file-path> install a hap or hsp by a specified path\n" 68 " -p, --bundle-path <file-path> <file-path> ... install one bundle by some hap or hsp paths\n" 69 " -p, --bundle-path <bundle-direction> install one bundle by a direction,\n" 70 " under which are some hap or hsp files\n" 71 " -r -p <bundle-file-path> replace an existing bundle\n" 72 " -r --bundle-path <bundle-file-path> replace an existing bundle\n" 73 " -s, --shared-bundle-dir-path <shared-bundle-dir-path> install inter-application hsp files\n" 74 " -w, --waitting-time <waitting-time> specify waitting time for installation,\n" 75 " the minimum waitting time is 180s,\n" 76 " the maximum waitting time is 600s\n"; 77 78 const std::string HELP_MSG_UNINSTALL = 79 "usage: bm uninstall <options>\n" 80 "options list:\n" 81 " -h, --help list available commands\n" 82 " -n, --bundle-name <bundle-name> uninstall a bundle by bundle name\n" 83 " -m, --module-name <module-name> uninstall a module by module name\n" 84 " -k, --keep-data keep the user data after uninstall\n" 85 " -s, --shared uninstall inter-application shared library\n" 86 " -v, --version uninstall a inter-application shared library by versionCode\n"; 87 88 const std::string HELP_MSG_UNINSTALL_SHARE = 89 "usage: bm uninstall-shared <options>\n" 90 "options list:\n" 91 " -h, --help list available commands\n" 92 " -n, --bundle-name <bundle-name> uninstall a share library by bundle name\n" 93 " -v, --version <versionCode> uninstall a share library by versionCode\n"; 94 95 const std::string HELP_MSG_DUMP = 96 "usage: bm dump <options>\n" 97 "options list:\n" 98 " -h, --help list available commands\n" 99 " -a, --all list all bundles in system\n" 100 " -g, --debug-bundle list debug bundles in system\n" 101 " -n, --bundle-name <bundle-name> list the bundle info by a bundle name\n" 102 " -s, --shortcut-info list the shortcut info\n" 103 " -d, --device-id <device-id> specify a device id\n"; 104 105 const std::string HELP_MSG_CLEAN = 106 "usage: bm clean <options>\n" 107 "options list:\n" 108 " -h, --help list available commands\n" 109 " -n, --bundle-name <bundle-name> bundle name\n" 110 " -c, --cache clean bundle cache files by bundle name\n" 111 " -d, --data clean bundle data files by bundle name\n" 112 " -i, --app-index <app-index> specify a app index\n"; 113 114 const std::string HELP_MSG_ENABLE = 115 "usage: bm enable <options>\n" 116 "options list:\n" 117 " -h, --help list available commands\n" 118 " -n, --bundle-name <bundle-name> enable bundle by bundle name\n" 119 " -a, --ability-name <ability-name> enable ability by ability name\n"; 120 121 const std::string HELP_MSG_DISABLE = 122 "usage: bm disable <options>\n" 123 "options list:\n" 124 " -h, --help list available commands\n" 125 " -n, --bundle-name <bundle-name> disable bundle by bundle name\n" 126 " -a, --ability-name <ability-name> disable ability by ability name\n"; 127 128 const std::string HELP_MSG_GET = 129 "usage: bm get <options>\n" 130 "options list:\n" 131 " -h, --help list available commands\n" 132 " -u, --udid obtain udid of the current device\n"; 133 134 const std::string HELP_MSG_QUICK_FIX = 135 "usage: bm quickfix <options>\n" 136 "options list:\n" 137 "-h, --help list available commands\n" 138 "-q, --query indicates query quickfix, used with -b or --bundle-name\n" 139 "-r, --remove indicates remove quickfix, used with -b or --bundle-name\n" 140 "-b, --bundle-name <bundle-name> query quickfix status and information by a specified bundle name\n" 141 "-d, --debug apply a quickfix debug mode\n" 142 "-a, --apply indicates apply quickfix, used with -f or --file-path\n" 143 "-t, --target <target-path> indicates a target path to apply quickfix\n" 144 "-o, --overwrite apply a quickfix in overwrite mode\n" 145 "-f, --file-path <file-path> apply a quickfix file by a specified path\n" 146 "-f, --file-path <file-path> <file-path> ... apply some quickfix files of one bundle\n" 147 "-f, --file-path <bundle-direction> apply quickfix files by direction, under which are quickfix files\n"; 148 149 const std::string HELP_MSG_OVERLAY = 150 "usage: bm dump-overlay <options>\n" 151 "options list:\n" 152 " -h, --help list available commands\n" 153 " -b, --bundle-name <bundle-name> bundle name of the overlay bundle\n" 154 " -m, --module-name <module-name> module name of the overlay bundle\n" 155 " -t, --target-module-name <target-module-name> target module name of overlay bundle\n"; 156 157 const std::string HELP_MSG_OVERLAY_TARGET = 158 "usage: bm dump-target-overlay <options>\n" 159 "options list:\n" 160 " -h, --help list available commands\n" 161 " -b, --bundle-name <bundle-name> bundle name of the target overlay bundle\n" 162 " -m, --module-name <module-name> module name of the target overlay bundle\n"; 163 164 const std::string HELP_MSG_DUMP_SHARED = 165 "usage: bm dump-shared <options>\n" 166 "eg:bm dump-shared -n <bundle-name> \n" 167 "options list:\n" 168 " -h, --help list available commands\n" 169 " -a, --all list all inter-application shared library name in system\n" 170 " -n, --bundle-name <bundle-name> dump inter-application shared library information by bundleName\n"; 171 172 const std::string HELP_MSG_DUMP_SHARED_DEPENDENCIES = 173 "usage: bm dump-dependencies <options>\n" 174 "eg:bm dump-dependencies -n <bundle-name> -m <module-name> \n" 175 "options list:\n" 176 " -h, --help list available commands\n" 177 " -n, --bundle-name <bundle-name> dump dependencies by bundleName and moduleName\n" 178 " -m, --module-name <module-name> dump dependencies by bundleName and moduleName\n"; 179 180 const std::string STRING_INCORRECT_OPTION = "error: incorrect option"; 181 const std::string HELP_MSG_NO_BUNDLE_PATH_OPTION = 182 "error: you must specify a bundle path with '-p' or '--bundle-path'."; 183 184 const std::string HELP_MSG_NO_BUNDLE_NAME_OPTION = 185 "error: you must specify a bundle name with '-n' or '--bundle-name'."; 186 187 const std::string STRING_INSTALL_BUNDLE_OK = "install bundle successfully."; 188 const std::string STRING_INSTALL_BUNDLE_NG = "error: failed to install bundle."; 189 190 const std::string STRING_UNINSTALL_BUNDLE_OK = "uninstall bundle successfully."; 191 const std::string STRING_UNINSTALL_BUNDLE_NG = "error: failed to uninstall bundle."; 192 193 const std::string HELP_MSG_NO_DATA_OR_CACHE_OPTION = 194 "error: you must specify '-c' or '-d' for 'bm clean' option."; 195 const std::string STRING_CLEAN_CACHE_BUNDLE_OK = "clean bundle cache files successfully."; 196 const std::string STRING_CLEAN_CACHE_BUNDLE_NG = "error: failed to clean bundle cache files."; 197 198 const std::string STRING_CLEAN_DATA_BUNDLE_OK = "clean bundle data files successfully."; 199 const std::string STRING_CLEAN_DATA_BUNDLE_NG = "error: failed to clean bundle data files."; 200 201 const std::string STRING_ENABLE_BUNDLE_OK = "enable bundle successfully."; 202 const std::string STRING_ENABLE_BUNDLE_NG = "error: failed to enable bundle."; 203 204 const std::string STRING_DISABLE_BUNDLE_OK = "disable bundle successfully."; 205 const std::string STRING_DISABLE_BUNDLE_NG = "error: failed to disable bundle."; 206 207 const std::string STRING_GET_UDID_OK = "udid of current device is :"; 208 const std::string STRING_GET_UDID_NG = "error: failed to get udid"; 209 210 const std::string HELP_MSG_NO_REMOVABLE_OPTION = 211 "error: you must specify a bundle name with '-n' or '--bundle-name' \n" 212 "and a module name with '-m' or '--module-name' \n"; 213 214 const std::string HELP_MSG_DUMP_FAILED = "error: failed to get information and the parameters may be wrong."; 215 const std::string HELP_MSG_COMPILE_FAILED = "error: failed to get information and the parameters may be wrong."; 216 const std::string STRING_REQUIRE_CORRECT_VALUE = "error: option requires a correct value.\n"; 217 218 const std::string STRING_DUMP_OVERLAY_OK = "overlay info is:"; 219 const std::string STRING_DUMP_OVERLAY_NG = "error: failed to get overlay info"; 220 221 const std::string STRING_DUMP_TARGET_OVERLAY_OK = "target overlay info is:"; 222 const std::string STRING_DUMP_TARGET_OVERLAY_NG = "error: failed to get target overlay info"; 223 const std::string MSG_ERR_BUNDLEMANAGER_OVERLAY_FEATURE_IS_NOT_SUPPORTED = "feature is not supported.\n"; 224 const std::string COMPILE_SUCCESS_OK = "compile AOT success.\n"; 225 const std::string COMPILE_RESET = "reset AOT success.\n"; 226 const std::string WARNING_USER = 227 "Warning: The current user is %. If you want to set the userId as $, please switch to $.\n"; 228 } // namespace 229 230 class BundleManagerShellCommand : public ShellCommand { 231 public: 232 BundleManagerShellCommand(int argc, char *argv[]); ~BundleManagerShellCommand()233 ~BundleManagerShellCommand() override 234 {} 235 236 private: 237 ErrCode CreateCommandMap() override; 238 ErrCode CreateMessageMap() override; 239 ErrCode Init() override; 240 241 ErrCode RunAsHelpCommand(); 242 ErrCode RunAsInstallCommand(); 243 ErrCode RunAsUninstallCommand(); 244 ErrCode RunAsDumpCommand(); 245 ErrCode RunAsCleanCommand(); 246 ErrCode RunAsEnableCommand(); 247 ErrCode RunAsDisableCommand(); 248 ErrCode RunAsGetCommand(); 249 ErrCode RunAsQuickFixCommand(); 250 ErrCode RunAsDumpOverlay(); 251 ErrCode RunAsDumpTargetOverlay(); 252 ErrCode RunAsDumpSharedDependenciesCommand(); 253 ErrCode RunAsDumpSharedCommand(); 254 ErrCode RunAsCompileCommand(); 255 ErrCode RunAsCopyApCommand(); 256 257 std::string CopyAp(const std::string &bundleName, bool isAllBundle) const; 258 259 std::string CompileProcessAot( 260 const std::string &bundleName, const std::string &compileMode, bool isAllBundle) const; 261 std::string CompileReset(const std::string &bundleName, bool isAllBundle) const; 262 263 std::string DumpBundleList(int32_t userId) const; 264 std::string DumpDebugBundleList(int32_t userId) const; 265 std::string DumpBundleInfo(const std::string &bundleName, int32_t userId) const; 266 std::string DumpShortcutInfos(const std::string &bundleName, int32_t userId) const; 267 std::string DumpDistributedBundleInfo(const std::string &deviceId, const std::string &bundleName); 268 std::string DumpDependentModuleNames(const std::string &bundleName, const std::string &moduleName) const; 269 std::string DumpSharedDependencies(const std::string &bundleName, const std::string &moduleName) const; 270 std::string DumpShared(const std::string &bundleName) const; 271 std::string DumpSharedAll() const; 272 273 int32_t InstallOperation(const std::vector<std::string> &bundlePaths, InstallParam &installParam, 274 int32_t waittingTime, std::string &resultMsg) const; 275 int32_t UninstallOperation(const std::string &bundleName, const std::string &moduleName, 276 InstallParam &installParam) const; 277 int32_t UninstallSharedOperation(const UninstallParam &uninstallParam) const; 278 std::string GetUdid() const; 279 bool IsInstallOption(int index) const; 280 void GetAbsPaths(const std::vector<std::string> &paths, std::vector<std::string> &absPaths) const; 281 282 ErrCode GetBundlePath(const std::string& param, std::vector<std::string>& bundlePaths) const; 283 284 bool CleanBundleCacheFilesOperation(const std::string &bundleName, int32_t userId, int32_t appIndex = 0) const; 285 bool CleanBundleDataFilesOperation(const std::string &bundleName, int32_t userId, int32_t appIndex = 0) const; 286 287 bool SetApplicationEnabledOperation(const AbilityInfo &abilityInfo, bool isEnable, int32_t userId) const; 288 std::string DumpOverlayInfo(const std::string &bundleName, const std::string &moduleName, 289 const std::string &targetModuleName, int32_t userId); 290 std::string DumpTargetOverlayInfo(const std::string &bundleName, const std::string &moduleName, int32_t userId); 291 ErrCode ParseSharedDependenciesCommand(int32_t option, std::string &bundleName, std::string &moduleName); 292 ErrCode ParseSharedCommand(int32_t option, std::string &bundleName, bool &dumpSharedAll); 293 ErrCode ParseCopyApCommand(int32_t option, std::string &bundleName, bool &isAllBundle); 294 ErrCode DeployQuickFixDisable(const std::vector<std::string> &quickFixFiles, 295 std::shared_ptr<QuickFixResult> &quickFixRes, bool isDebug, const std::string &targetPath) const; 296 ErrCode DeleteQuickFix(const std::string &bundleName, std::shared_ptr<QuickFixResult> &quickFixRes) const; 297 std::string GetWaringString(int32_t currentUserId, int32_t specifedUserId) const; 298 299 sptr<IBundleMgr> bundleMgrProxy_; 300 sptr<IBundleInstaller> bundleInstallerProxy_; 301 }; 302 } // namespace AppExecFwk 303 } // namespace OHOS 304 305 #endif // FOUNDATION_APPEXECFWK_STANDARD_TOOLS_BM_INCLUDE_BUNDLE_COMMAND_H