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