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