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