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