1 /*
2 * Copyright (c) 2021 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 #include "bundle_command.h"
17
18 #include <getopt.h>
19 #include <unistd.h>
20 #include "if_system_ability_manager.h"
21 #include "system_ability_definition.h"
22 #include "iservice_registry.h"
23 #include "app_log_wrapper.h"
24 #include "status_receiver_impl.h"
25
26 using namespace OHOS::AAFwk;
27
28 namespace OHOS {
29 namespace AppExecFwk {
30 namespace {
31 const std::string BUNDLE_NAME_EMPTY = "";
32
33 const std::string SHORT_OPTIONS = "hp:rfn:m:ai";
34 const struct option LONG_OPTIONS[] = {
35 {"help", no_argument, nullptr, 'h'},
36 {"bundle-path", required_argument, nullptr, 'p'},
37 {"replace", no_argument, nullptr, 'r'},
38 {"force", no_argument, nullptr, 'f'},
39 {"bundle-name", required_argument, nullptr, 'n'},
40 {"module-name", required_argument, nullptr, 'm'},
41 {"all", no_argument, nullptr, 'a'},
42 {"bundle-info", no_argument, nullptr, 'i'},
43 };
44 } // namespace
45
BundleManagerShellCommand(int argc,char * argv[])46 BundleManagerShellCommand::BundleManagerShellCommand(int argc, char *argv[]) : ShellCommand(argc, argv, TOOL_NAME)
47 {}
48
CreateCommandMap()49 ErrCode BundleManagerShellCommand::CreateCommandMap()
50 {
51 commandMap_ = {
52 {"help", std::bind(&BundleManagerShellCommand::RunAsHelpCommand, this)},
53 {"install", std::bind(&BundleManagerShellCommand::RunAsInstallCommand, this)},
54 {"uninstall", std::bind(&BundleManagerShellCommand::RunAsUninstallCommand, this)},
55 {"dump", std::bind(&BundleManagerShellCommand::RunAsDumpCommand, this)},
56 };
57
58 return OHOS::ERR_OK;
59 }
60
CreateMessageMap()61 ErrCode BundleManagerShellCommand::CreateMessageMap()
62 {
63 messageMap_ = {
64 // error + message
65 {
66 IStatusReceiver::ERR_INSTALL_INTERNAL_ERROR,
67 "error: install internal error.",
68 },
69 {
70 IStatusReceiver::ERR_INSTALL_HOST_INSTALLER_FAILED,
71 "error: install host installer failed.",
72 },
73 {
74 IStatusReceiver::ERR_INSTALL_PARSE_FAILED,
75 "error: install parse failed.",
76 },
77 {
78 IStatusReceiver::ERR_INSTALL_VERSION_DOWNGRADE,
79 "error: install version downgrade.",
80 },
81 {
82 IStatusReceiver::ERR_INSTALL_VERIFICATION_FAILED,
83 "error: install verification failed.",
84 },
85 {
86 IStatusReceiver::ERR_INSTALL_NO_SIGNATURE_INFO,
87 "error: install no signature info.",
88 },
89 {
90 IStatusReceiver::ERR_INSTALL_UPDATE_INCOMPATIBLE,
91 "error: install update incompatible.",
92 },
93 {
94 IStatusReceiver::ERR_INSTALL_PARAM_ERROR,
95 "error: install param error.",
96 },
97 {
98 IStatusReceiver::ERR_INSTALL_PERMISSION_DENIED,
99 "error: install permission denied.",
100 },
101 {
102 IStatusReceiver::ERR_INSTALL_ENTRY_ALREADY_EXIST,
103 "error: install entry already exist.",
104 },
105 {
106 IStatusReceiver::ERR_INSTALL_STATE_ERROR,
107 "error: install state error.",
108 },
109 {
110 IStatusReceiver::ERR_INSTALL_FILE_PATH_INVALID,
111 "error: install file path invalid.",
112 },
113 {
114 IStatusReceiver::ERR_INSTALL_INVALID_HAP_NAME,
115 "error: install invalid hap name.",
116 },
117 {
118 IStatusReceiver::ERR_INSTALL_INVALID_BUNDLE_FILE,
119 "error: install invalid bundle file.",
120 },
121 {
122 IStatusReceiver::ERR_INSTALL_INVALID_HAP_SIZE,
123 "error: install invalid hap size.",
124 },
125 {
126 IStatusReceiver::ERR_INSTALL_GENERATE_UID_ERROR,
127 "error: install generate uid error.",
128 },
129 {
130 IStatusReceiver::ERR_INSTALL_INSTALLD_SERVICE_ERROR,
131 "error: install installd service error.",
132 },
133 {
134 IStatusReceiver::ERR_INSTALL_BUNDLE_MGR_SERVICE_ERROR,
135 "error: install bundle mgr service error.",
136 },
137 {
138 IStatusReceiver::ERR_INSTALL_ALREADY_EXIST,
139 "error: install already exist.",
140 },
141
142 {
143 IStatusReceiver::ERR_INSTALL_PARSE_UNEXPECTED,
144 "error: install parse unexpected.",
145 },
146 {
147 IStatusReceiver::ERR_INSTALL_PARSE_MISSING_BUNDLE,
148 "error: install parse missing bundle.",
149 },
150 {
151 IStatusReceiver::ERR_INSTALL_PARSE_MISSING_ABILITY,
152 "error: install parse missing ability.",
153 },
154 {
155 IStatusReceiver::ERR_INSTALL_PARSE_NO_PROFILE,
156 "error: install parse no profile.",
157 },
158 {
159 IStatusReceiver::ERR_INSTALL_PARSE_BAD_PROFILE,
160 "error: install parse bad profile.",
161 },
162 {
163 IStatusReceiver::ERR_INSTALL_PARSE_PROFILE_PROP_TYPE_ERROR,
164 "error: install parse profile prop type error.",
165 },
166 {
167 IStatusReceiver::ERR_INSTALL_PARSE_PROFILE_MISSING_PROP,
168 "error: install parse profile missing prop.",
169 },
170 {
171 IStatusReceiver::ERR_INSTALL_PARSE_PROFILE_PROP_CHECK_ERROR,
172 "error: install parse profile prop check error.",
173 },
174 {
175 IStatusReceiver::ERR_INSTALL_PARSE_PERMISSION_ERROR,
176 "error: install parse permission error.",
177 },
178
179 {
180 IStatusReceiver::ERR_INSTALLD_PARAM_ERROR,
181 "error: installd param error.",
182 },
183 {
184 IStatusReceiver::ERR_INSTALLD_GET_PROXY_ERROR,
185 "error: installd get proxy error.",
186 },
187 {
188 IStatusReceiver::ERR_INSTALLD_CREATE_DIR_FAILED,
189 "error: installd create dir failed.",
190 },
191 {
192 IStatusReceiver::ERR_INSTALLD_CREATE_DIR_EXIST,
193 "error: installd create dir exist.",
194 },
195 {
196 IStatusReceiver::ERR_INSTALLD_CHOWN_FAILED,
197 "error: installd chown failed.",
198 },
199 {
200 IStatusReceiver::ERR_INSTALLD_REMOVE_DIR_FAILED,
201 "error: installd remove dir failed.",
202 },
203 {
204 IStatusReceiver::ERR_INSTALLD_EXTRACT_FILES_FAILED,
205 "error: installd extract files failed.",
206 },
207 {
208 IStatusReceiver::ERR_INSTALLD_RNAME_DIR_FAILED,
209 "error: installd rename dir failed.",
210 },
211 {
212 IStatusReceiver::ERR_INSTALLD_CLEAN_DIR_FAILED,
213 "error: installd clean dir failed.",
214 },
215
216 {
217 IStatusReceiver::ERR_UNINSTALL_SYSTEM_APP_ERROR,
218 "error: uninstall system app error.",
219 },
220 {
221 IStatusReceiver::ERR_UNINSTALL_KILLING_APP_ERROR,
222 "error: uninstall killing app error.",
223 },
224 {
225 IStatusReceiver::ERR_UNINSTALL_INVALID_NAME,
226 "error: uninstall invalid name.",
227 },
228 {
229 IStatusReceiver::ERR_UNINSTALL_PARAM_ERROR,
230 "error: uninstall param error.",
231 },
232 {
233 IStatusReceiver::ERR_UNINSTALL_PERMISSION_DENIED,
234 "error: uninstall permission denied.",
235 },
236 {
237 IStatusReceiver::ERR_UNINSTALL_BUNDLE_MGR_SERVICE_ERROR,
238 "error: uninstall bundle mgr service error.",
239 },
240 {
241 IStatusReceiver::ERR_UNINSTALL_MISSING_INSTALLED_BUNDLE,
242 "error: uninstall missing installed bundle.",
243 },
244 {
245 IStatusReceiver::ERR_UNINSTALL_MISSING_INSTALLED_MODULE,
246 "error: uninstall missing installed module.",
247 },
248 {
249 IStatusReceiver::ERR_UNKNOWN,
250 "error: unknown.",
251 }
252 };
253
254 return OHOS::ERR_OK;
255 }
256
init()257 ErrCode BundleManagerShellCommand::init()
258 {
259 ErrCode result = OHOS::ERR_OK;
260
261 if (!bundleMgrProxy_) {
262 bundleMgrProxy_ = GetBundleMgrProxy();
263 if (bundleMgrProxy_) {
264 if (!bundleInstallerProxy_) {
265 bundleInstallerProxy_ = bundleMgrProxy_->GetBundleInstaller();
266 }
267 }
268 }
269
270 if (!bundleMgrProxy_ || !bundleInstallerProxy_) {
271 result = OHOS::ERR_INVALID_VALUE;
272 }
273
274 return result;
275 }
276
GetBundleMgrProxy() const277 sptr<IBundleMgr> BundleManagerShellCommand::GetBundleMgrProxy() const
278 {
279 sptr<ISystemAbilityManager> systemAbilityManager =
280 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
281 if (!systemAbilityManager) {
282 APP_LOGE("failed to get system ability mgr.");
283 return nullptr;
284 }
285
286 sptr<IRemoteObject> remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
287 if (!remoteObject) {
288 APP_LOGE("failed to get bundle manager proxy.");
289 return nullptr;
290 }
291
292 APP_LOGI("get bundle manager proxy success.");
293 return iface_cast<IBundleMgr>(remoteObject);
294 }
295
GetInstallerProxy() const296 sptr<IBundleInstaller> BundleManagerShellCommand::GetInstallerProxy() const
297 {
298 sptr<IBundleMgr> bundleMgrProxy = GetBundleMgrProxy();
299 if (!bundleMgrProxy) {
300 APP_LOGE("bundle mgr proxy is nullptr.");
301 return nullptr;
302 }
303
304 sptr<IBundleInstaller> installerProxy = bundleMgrProxy->GetBundleInstaller();
305 if (!installerProxy) {
306 APP_LOGE("failed to get bundle installer proxy.");
307 return nullptr;
308 }
309
310 APP_LOGI("get bundle installer proxy success.");
311 return installerProxy;
312 }
313
RunAsHelpCommand()314 ErrCode BundleManagerShellCommand::RunAsHelpCommand()
315 {
316 resultReceiver_.append(HELP_MSG);
317
318 return OHOS::ERR_OK;
319 }
320
RunAsInstallCommand()321 ErrCode BundleManagerShellCommand::RunAsInstallCommand()
322 {
323 int result = OHOS::ERR_OK;
324
325 InstallFlag installFlag = InstallFlag::NORMAL;
326 bool noCheckSignature = false;
327
328 int option = -1;
329 int counter = 0;
330 std::string bundlePath = "";
331
332 while (true) {
333 counter++;
334
335 option = getopt_long(argc_, argv_, SHORT_OPTIONS.c_str(), LONG_OPTIONS, nullptr);
336
337 APP_LOGI("option: %{public}d, optopt: %{public}d, optind: %{public}d", option, optopt, optind);
338
339 if (optind < 0 || optind > argc_) {
340 return OHOS::ERR_INVALID_VALUE;
341 }
342
343 for (int i = 0; i < argc_; i++) {
344 APP_LOGI("argv_[%{public}d]: %{public}s", i, argv_[i]);
345 }
346
347 if (option == -1) {
348 if (counter == 1) {
349 // When scanning the first argument
350 if (strcmp(argv_[optind], cmd_.c_str()) == 0) {
351 // 'bm install' with no option: bm install
352 // 'bm install' with a wrong argument: bm install xxx
353 APP_LOGI("'bm install' with no option.");
354
355 resultReceiver_.append(HELP_MSG_NO_OPTION + "\n");
356 result = OHOS::ERR_INVALID_VALUE;
357 }
358 }
359 break;
360 }
361
362 if (option == '?') {
363 switch (optopt) {
364 case 'p': {
365 // 'bm install -p' with no argument: bm install -p
366 // 'bm install --bundle-path' with no argument: bm install --bundle-path
367 APP_LOGI("'bm install' with no argument.");
368
369 resultReceiver_.append("error: option ");
370 resultReceiver_.append("requires a value.\n");
371
372 result = OHOS::ERR_INVALID_VALUE;
373 break;
374 }
375 case 0: {
376 // 'bm install' with an unknown option: bm install --x
377 // 'bm install' with an unknown option: bm install --xxx
378 std::string unknownOption = "";
379 std::string unknownOptionMsg = GetUnknownOptionMsg(unknownOption);
380
381 APP_LOGI("'bm install' with an unknown option.");
382
383 resultReceiver_.append(unknownOptionMsg);
384 result = OHOS::ERR_INVALID_VALUE;
385 break;
386 }
387 default: {
388 // 'bm install' with an unknown option: bm install -x
389 // 'bm install' with an unknown option: bm install -xxx
390 std::string unknownOption = "";
391 std::string unknownOptionMsg = GetUnknownOptionMsg(unknownOption);
392
393 APP_LOGI("'bm install' with an unknown option.");
394
395 resultReceiver_.append(unknownOptionMsg);
396 result = OHOS::ERR_INVALID_VALUE;
397 break;
398 }
399 }
400 break;
401 }
402
403 switch (option) {
404 case 'h': {
405 // 'bm install -h'
406 // 'bm install --help'
407 APP_LOGI("'bm install %{public}s'", argv_[optind - 1]);
408
409 result = OHOS::ERR_INVALID_VALUE;
410 break;
411 }
412 case 'p': {
413 // 'bm install -p <bundle-file-path>'
414 // 'bm install --bundle-path <bundle-file-path>'
415 bundlePath = optarg;
416 break;
417 }
418 case 'r': {
419 // 'bm install -r'
420 // 'bm install -r'
421 installFlag = InstallFlag::REPLACE_EXISTING;
422 break;
423 }
424 case 'f': {
425 // 'bm install -f'
426 // 'bm install -f'
427 noCheckSignature = true;
428 break;
429 }
430 case 0: {
431 break;
432 }
433 default: {
434 break;
435 }
436 }
437 }
438
439 if (result == OHOS::ERR_OK) {
440 if (resultReceiver_ == "" && bundlePath.size() == 0) {
441 // 'bm install ...' with no bundle path option
442 APP_LOGI("'bm install' with no bundle path option.");
443
444 resultReceiver_.append(HELP_MSG_NO_BUNDLE_PATH_OPTION + "\n");
445 result = OHOS::ERR_INVALID_VALUE;
446 }
447 }
448
449 if (result != OHOS::ERR_OK) {
450 resultReceiver_.append(HELP_MSG_INSTALL);
451 } else {
452 InstallParam installParam;
453 installParam.installFlag = installFlag;
454 installParam.noCheckSignature = noCheckSignature;
455
456 int32_t installResult = InstallOperation(bundlePath, installParam);
457 if (installResult == OHOS::ERR_OK) {
458 resultReceiver_ = STRING_INSTALL_BUNDLE_OK + "\n";
459 } else {
460 resultReceiver_ = STRING_INSTALL_BUNDLE_NG + "\n";
461 resultReceiver_.append(GetMessageFromCode(installResult));
462 }
463 }
464
465 return result;
466 }
467
RunAsUninstallCommand()468 ErrCode BundleManagerShellCommand::RunAsUninstallCommand()
469 {
470 int result = OHOS::ERR_OK;
471
472 int option = -1;
473 int counter = 0;
474 std::string bundleName = "";
475 std::string moduleName = "";
476
477 while (true) {
478 counter++;
479
480 option = getopt_long(argc_, argv_, SHORT_OPTIONS.c_str(), LONG_OPTIONS, nullptr);
481
482 APP_LOGI("option: %{public}d, optopt: %{public}d, optind: %{public}d", option, optopt, optind);
483
484 if (optind < 0 || optind > argc_) {
485 return OHOS::ERR_INVALID_VALUE;
486 }
487
488 for (int i = 0; i < argc_; i++) {
489 APP_LOGI("argv_[%{public}d]: %{public}s", i, argv_[i]);
490 }
491
492 if (option == -1) {
493 if (counter == 1) {
494 // When scanning the first argument
495 if (strcmp(argv_[optind], cmd_.c_str()) == 0) {
496 // 'bm uninstall' with no option: bm uninstall
497 // 'bm uninstall' with a wrong argument: bm uninstall xxx
498 APP_LOGI("'bm uninstall' %{public}s", HELP_MSG_NO_OPTION.c_str());
499
500 resultReceiver_.append(HELP_MSG_NO_OPTION + "\n");
501 result = OHOS::ERR_INVALID_VALUE;
502 }
503 }
504 break;
505 }
506
507 if (option == '?') {
508 switch (optopt) {
509 case 'n': {
510 // 'bm uninstall -n' with no argument: bm uninstall -n
511 // 'bm uninstall --bundle-name' with no argument: bm uninstall --bundle-name
512 APP_LOGI("'bm uninstall -n' with no argument.");
513
514 resultReceiver_.append("error: option ");
515 resultReceiver_.append("requires a value.\n");
516 result = OHOS::ERR_INVALID_VALUE;
517 break;
518 }
519 case 'm': {
520 // 'bm uninstall -m' with no argument: bm uninstall -m
521 // 'bm uninstall --module-name' with no argument: bm uninstall --module-name
522 APP_LOGI("'bm uninstall -m' with no argument.");
523
524 resultReceiver_.append("error: option ");
525 resultReceiver_.append("requires a value.\n");
526 result = OHOS::ERR_INVALID_VALUE;
527 break;
528 }
529 case 0: {
530 // 'bm uninstall' with an unknown option: bm uninstall --x
531 // 'bm uninstall' with an unknown option: bm uninstall --xxx
532 std::string unknownOption = "";
533 std::string unknownOptionMsg = GetUnknownOptionMsg(unknownOption);
534
535 APP_LOGI("'bm uninstall' with an unknown option.");
536
537 resultReceiver_.append(unknownOptionMsg);
538 result = OHOS::ERR_INVALID_VALUE;
539 break;
540 }
541 default: {
542 // 'bm uninstall' with an unknown option: bm uninstall -x
543 // 'bm uninstall' with an unknown option: bm uninstall -xxx
544 std::string unknownOption = "";
545 std::string unknownOptionMsg = GetUnknownOptionMsg(unknownOption);
546
547 APP_LOGI("'bm uninstall' with an unknown option.");
548
549 resultReceiver_.append(unknownOptionMsg);
550 result = OHOS::ERR_INVALID_VALUE;
551 break;
552 }
553 }
554 break;
555 }
556
557 switch (option) {
558 case 'h': {
559 // 'bm uninstall -h'
560 // 'bm uninstall --help'
561 APP_LOGI("'bm uninstall %{public}s'", argv_[optind - 1]);
562
563 result = OHOS::ERR_INVALID_VALUE;
564 break;
565 }
566 case 'n': {
567 // 'bm uninstall -n xxx'
568 // 'bm uninstall --bundle-name xxx'
569 APP_LOGI("'bm uninstall %{public}s %{public}s'", argv_[optind - OFFSET_REQUIRED_ARGUMENT], optarg);
570
571 bundleName = optarg;
572 break;
573 }
574 case 'm': {
575 // 'bm uninstall -m xxx'
576 // 'bm uninstall --module-name xxx'
577 APP_LOGI("'bm uninstall %{public}s %{public}s'", argv_[optind - OFFSET_REQUIRED_ARGUMENT], optarg);
578
579 moduleName = optarg;
580 break;
581 }
582 case 0: {
583 break;
584 }
585 default: {
586 break;
587 }
588 }
589 }
590
591 if (result == OHOS::ERR_OK) {
592 if (resultReceiver_ == "" && bundleName.size() == 0) {
593 // 'bm uninstall ...' with no bundle name option
594 APP_LOGI("'bm uninstall' with bundle name option.");
595
596 resultReceiver_.append(HELP_MSG_NO_BUNDLE_NAME_OPTION + "\n");
597 result = OHOS::ERR_INVALID_VALUE;
598 }
599 }
600
601 if (result != OHOS::ERR_OK) {
602 resultReceiver_.append(HELP_MSG_UNINSTALL);
603 } else {
604 int32_t uninstallResult = UninstallOperation(bundleName, moduleName);
605 if (uninstallResult == OHOS::ERR_OK) {
606 resultReceiver_ = STRING_UNINSTALL_BUNDLE_OK + "\n";
607 } else {
608 resultReceiver_ = STRING_UNINSTALL_BUNDLE_NG + "\n";
609 resultReceiver_.append(GetMessageFromCode(uninstallResult));
610 }
611 }
612
613 return result;
614 }
615
RunAsDumpCommand()616 ErrCode BundleManagerShellCommand::RunAsDumpCommand()
617 {
618 int result = OHOS::ERR_OK;
619 std::string dumpResults = "";
620
621 int option = getopt_long(argc_, argv_, SHORT_OPTIONS.c_str(), LONG_OPTIONS, nullptr);
622
623 APP_LOGI("option: %{public}d, optopt: %{public}d, optind: %{public}d", option, optopt, optind);
624
625 if (optind < 0 || optind > argc_) {
626 return OHOS::ERR_INVALID_VALUE;
627 }
628
629 for (int i = 0; i < argc_; i++) {
630 APP_LOGI("argv_[%{public}d]: %{public}s", i, argv_[i]);
631 }
632
633 switch (option) {
634 case 'h': {
635 // 'bm dump -h'
636 // 'bm dump --help'
637 APP_LOGI("'bm dump %{public}s'", argv_[optind - 1]);
638
639 result = OHOS::ERR_INVALID_VALUE;
640 break;
641 }
642 case 'a': {
643 // 'bm dump -a'
644 // 'bm dump --all'
645 APP_LOGI("'bm dump %{public}s'", argv_[optind - 1]);
646
647 dumpResults = DumpBundleList();
648 break;
649 }
650 case 'i': {
651 // 'bm dump -i'
652 // 'bm dump --bundle-info'
653 APP_LOGI("'bm dump %{public}s'", argv_[optind - 1]);
654
655 dumpResults = DumpBundleInfos();
656 break;
657 }
658 case 'n': {
659 // 'bm dump -n xxx'
660 // 'bm dump --bundle-name xxx'
661 APP_LOGI("'bm dump %{public}s %{public}s'", argv_[optind - OFFSET_REQUIRED_ARGUMENT], optarg);
662
663 dumpResults = DumpBundleInfo();
664 break;
665 }
666 case '?': {
667 switch (optopt) {
668 case 'n': {
669 // 'bm dump -n' with no argument: bm dump -n
670 // 'bm dump --bundle-name' with no argument: bm dump --bundle-name
671 APP_LOGI("'bm dump -n' with no argument.");
672
673 resultReceiver_.append("error: option ");
674 resultReceiver_.append("requires a value.\n");
675 result = OHOS::ERR_INVALID_VALUE;
676 break;
677 }
678 case 0: {
679 // 'bm dump' with an unknown option: bm dump --x
680 // 'bm dump' with an unknown option: bm dump --xxx
681 std::string unknownOption = "";
682 std::string unknownOptionMsg = GetUnknownOptionMsg(unknownOption);
683
684 APP_LOGI("'bm dump' with an unknown option.");
685
686 resultReceiver_.append(unknownOptionMsg);
687 result = OHOS::ERR_INVALID_VALUE;
688 break;
689 }
690 default: {
691 // 'bm dump' with an unknown option: bm dump -x
692 // 'bm dump' with an unknown option: bm dump -xxx
693 std::string unknownOption = "";
694 std::string unknownOptionMsg = GetUnknownOptionMsg(unknownOption);
695
696 APP_LOGI("'bm dump' with an unknown option.");
697
698 resultReceiver_.append(unknownOptionMsg);
699 result = OHOS::ERR_INVALID_VALUE;
700 break;
701 }
702 }
703 break;
704 }
705 default: {
706 if (strcmp(argv_[optind], cmd_.c_str()) == 0) {
707 // 'bm dump' with no option: bm dump
708 // 'bm dump' with a wrong argument: bm dump xxx
709 APP_LOGI("'bm dump' %{public}s", HELP_MSG_NO_OPTION.c_str());
710
711 resultReceiver_.append(HELP_MSG_NO_OPTION + "\n");
712 result = OHOS::ERR_INVALID_VALUE;
713 }
714 break;
715 }
716 }
717
718 if (result != OHOS::ERR_OK) {
719 resultReceiver_.append(HELP_MSG_DUMP);
720 } else {
721 APP_LOGI("dumpResults: %{public}s", dumpResults.c_str());
722
723 resultReceiver_.append(dumpResults);
724 }
725
726 return result;
727 }
728
DumpBundleList() const729 std::string BundleManagerShellCommand::DumpBundleList() const
730 {
731 std::string dumpResults;
732 bool dumpRet = bundleMgrProxy_->DumpInfos(DumpFlag::DUMP_BUNDLE_LIST, BUNDLE_NAME_EMPTY, dumpResults);
733 if (!dumpRet) {
734 APP_LOGE("failed to dump bundle list.");
735 }
736 return dumpResults;
737 }
738
DumpBundleInfos() const739 std::string BundleManagerShellCommand::DumpBundleInfos() const
740 {
741 std::string dumpResults;
742 bool dumpRet = bundleMgrProxy_->DumpInfos(DumpFlag::DUMP_ALL_BUNDLE_INFO, BUNDLE_NAME_EMPTY, dumpResults);
743 if (!dumpRet) {
744 APP_LOGE("failed to dump bundle infos.");
745 }
746 return dumpResults;
747 }
748
DumpBundleInfo() const749 std::string BundleManagerShellCommand::DumpBundleInfo() const
750 {
751 std::string bundleName = optarg;
752 std::string dumpResults;
753 bool dumpRet = bundleMgrProxy_->DumpInfos(DumpFlag::DUMP_BUNDLE_INFO, bundleName, dumpResults);
754 if (!dumpRet) {
755 APP_LOGE("failed to dump bundle info.");
756 }
757 return dumpResults;
758 }
759
InstallOperation(const std::string & bundlePath,InstallParam & installParam) const760 int32_t BundleManagerShellCommand::InstallOperation(const std::string &bundlePath, InstallParam &installParam) const
761 {
762 std::string absoluteBundlePath = "";
763 if (bundlePath.size() > 0) {
764 if (bundlePath.at(0) == '/') {
765 // absolute path
766 absoluteBundlePath.append(bundlePath);
767 } else {
768 // relative path
769 char *currentPathPtr = getcwd(nullptr, 0);
770
771 if (currentPathPtr != nullptr) {
772 absoluteBundlePath.append(currentPathPtr);
773 absoluteBundlePath.append('/' + bundlePath);
774
775 free(currentPathPtr);
776 currentPathPtr = nullptr;
777 }
778 }
779 }
780
781 APP_LOGI("bundlePath: %{public}s", bundlePath.c_str());
782 APP_LOGI("absoluteBundlePath: %{public}s", absoluteBundlePath.c_str());
783
784 installParam.userId = 0;
785 sptr<StatusReceiverImpl> statusReceiver(new StatusReceiverImpl());
786
787 bundleInstallerProxy_->Install(absoluteBundlePath, installParam, statusReceiver);
788
789 return statusReceiver->GetResultCode();
790 }
791
UninstallOperation(const std::string & bundleName,const std::string & moduleName) const792 int32_t BundleManagerShellCommand::UninstallOperation(
793 const std::string &bundleName, const std::string &moduleName) const
794 {
795 sptr<StatusReceiverImpl> statusReceiver(new StatusReceiverImpl());
796 InstallParam installParam;
797 installParam.userId = 0;
798
799 APP_LOGI("bundleName: %{public}s", bundleName.c_str());
800 APP_LOGI("moduleName: %{public}s", moduleName.c_str());
801
802 if (moduleName.size() != 0) {
803 bundleInstallerProxy_->Uninstall(bundleName, moduleName, installParam, statusReceiver);
804 } else {
805 bundleInstallerProxy_->Uninstall(bundleName, installParam, statusReceiver);
806 }
807
808 return statusReceiver->GetResultCode();
809 }
810 } // namespace AppExecFwk
811 } // namespace OHOS
812