1545fdf9bSopenharmony_ci/*
2545fdf9bSopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3545fdf9bSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4545fdf9bSopenharmony_ci * you may not use this file except in compliance with the License.
5545fdf9bSopenharmony_ci * You may obtain a copy of the License at
6545fdf9bSopenharmony_ci *
7545fdf9bSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8545fdf9bSopenharmony_ci *
9545fdf9bSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10545fdf9bSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11545fdf9bSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12545fdf9bSopenharmony_ci * See the License for the specific language governing permissions and
13545fdf9bSopenharmony_ci * limitations under the License.
14545fdf9bSopenharmony_ci */
15545fdf9bSopenharmony_ci#include "bundle_command.h"
16545fdf9bSopenharmony_ci
17545fdf9bSopenharmony_ci#include <chrono>
18545fdf9bSopenharmony_ci#include <cstdlib>
19545fdf9bSopenharmony_ci#include <cstring>
20545fdf9bSopenharmony_ci#include <future>
21545fdf9bSopenharmony_ci#include <getopt.h>
22545fdf9bSopenharmony_ci#include <unistd.h>
23545fdf9bSopenharmony_ci#include <vector>
24545fdf9bSopenharmony_ci#include "app_log_wrapper.h"
25545fdf9bSopenharmony_ci#include "appexecfwk_errors.h"
26545fdf9bSopenharmony_ci#include "bundle_command_common.h"
27545fdf9bSopenharmony_ci#include "bundle_death_recipient.h"
28545fdf9bSopenharmony_ci#include "bundle_mgr_client.h"
29545fdf9bSopenharmony_ci#include "bundle_mgr_proxy.h"
30545fdf9bSopenharmony_ci#include "clean_cache_callback_host.h"
31545fdf9bSopenharmony_ci#include "json_serializer.h"
32545fdf9bSopenharmony_ci#include "nlohmann/json.hpp"
33545fdf9bSopenharmony_ci#include "parameter.h"
34545fdf9bSopenharmony_ci#include "parameters.h"
35545fdf9bSopenharmony_ci#include "quick_fix_command.h"
36545fdf9bSopenharmony_ci#include "quick_fix_status_callback_host_impl.h"
37545fdf9bSopenharmony_ci#include "status_receiver_impl.h"
38545fdf9bSopenharmony_ci#include "string_ex.h"
39545fdf9bSopenharmony_ci#include "app_mgr_client.h"
40545fdf9bSopenharmony_ci#include "directory_ex.h"
41545fdf9bSopenharmony_ci
42545fdf9bSopenharmony_cinamespace OHOS {
43545fdf9bSopenharmony_cinamespace AppExecFwk {
44545fdf9bSopenharmony_cinamespace {
45545fdf9bSopenharmony_ciconst std::string BUNDLE_NAME_EMPTY = "";
46545fdf9bSopenharmony_ciconst std::string OVERLAY_MODULE_INFOS = "overlayModuleInfos";
47545fdf9bSopenharmony_ciconst std::string OVERLAY_BUNDLE_INFOS = "overlayBundleInfos";
48545fdf9bSopenharmony_ciconst std::string OVERLAY_MODULE_INFO = "overlayModuleInfo";
49545fdf9bSopenharmony_ciconst std::string SHARED_BUNDLE_INFO = "sharedBundleInfo";
50545fdf9bSopenharmony_ciconst std::string DEPENDENCIES = "dependencies";
51545fdf9bSopenharmony_ciconst char* IS_ROOT_MODE_PARAM = "const.debuggable";
52545fdf9bSopenharmony_ciconst std::string IS_DEVELOPER_MODE_PARAM = "const.security.developermode.state";
53545fdf9bSopenharmony_ciconst int32_t ROOT_MODE = 1;
54545fdf9bSopenharmony_ciconst int32_t USER_MODE = 0;
55545fdf9bSopenharmony_ciconst int32_t INDEX_OFFSET = 2;
56545fdf9bSopenharmony_ciconst int32_t MAX_WAITING_TIME = 3000;
57545fdf9bSopenharmony_ciconst int32_t DEVICE_UDID_LENGTH = 65;
58545fdf9bSopenharmony_ciconst int32_t MAX_ARGUEMENTS_NUMBER = 3;
59545fdf9bSopenharmony_ciconst int32_t MAX_OVERLAY_ARGUEMENTS_NUMBER = 8;
60545fdf9bSopenharmony_ciconst int32_t MINIMUM_WAITTING_TIME = 180; // 3 mins
61545fdf9bSopenharmony_ciconst int32_t MAXIMUM_WAITTING_TIME = 600; // 10 mins
62545fdf9bSopenharmony_ciconst int32_t INITIAL_SANDBOX_APP_INDEX = 1000;
63545fdf9bSopenharmony_ci
64545fdf9bSopenharmony_ciconst std::string SHORT_OPTIONS_COMPILE = "hm:r:";
65545fdf9bSopenharmony_ciconst struct option LONG_OPTIONS_COMPILE[] = {
66545fdf9bSopenharmony_ci    {"help", no_argument, nullptr, 'h'},
67545fdf9bSopenharmony_ci    {"mode", required_argument, nullptr, 'm'},
68545fdf9bSopenharmony_ci    {"reset", required_argument, nullptr, 'r'},
69545fdf9bSopenharmony_ci    {nullptr, 0, nullptr, 0},
70545fdf9bSopenharmony_ci};
71545fdf9bSopenharmony_ci
72545fdf9bSopenharmony_ciconst std::string SHORT_OPTIONS_COPY_AP = "hn:a";
73545fdf9bSopenharmony_ciconst struct option LONG_OPTIONS_COPY_AP[] = {
74545fdf9bSopenharmony_ci    {"help", no_argument, nullptr, 'h'},
75545fdf9bSopenharmony_ci    {"bundle-name", required_argument, nullptr, 'n'},
76545fdf9bSopenharmony_ci    {"all", no_argument, nullptr, 'a'},
77545fdf9bSopenharmony_ci    {nullptr, 0, nullptr, 0},
78545fdf9bSopenharmony_ci};
79545fdf9bSopenharmony_ci
80545fdf9bSopenharmony_ciconst std::string SHORT_OPTIONS = "hp:rn:m:a:cdu:w:s:i:";
81545fdf9bSopenharmony_ciconst struct option LONG_OPTIONS[] = {
82545fdf9bSopenharmony_ci    {"help", no_argument, nullptr, 'h'},
83545fdf9bSopenharmony_ci    {"bundle-path", required_argument, nullptr, 'p'},
84545fdf9bSopenharmony_ci    {"replace", no_argument, nullptr, 'r'},
85545fdf9bSopenharmony_ci    {"bundle-name", required_argument, nullptr, 'n'},
86545fdf9bSopenharmony_ci    {"module-name", required_argument, nullptr, 'm'},
87545fdf9bSopenharmony_ci    {"ability-name", required_argument, nullptr, 'a'},
88545fdf9bSopenharmony_ci    {"bundle-info", no_argument, nullptr, 'i'},
89545fdf9bSopenharmony_ci    {"cache", no_argument, nullptr, 'c'},
90545fdf9bSopenharmony_ci    {"data", no_argument, nullptr, 'd'},
91545fdf9bSopenharmony_ci    {"is-removable", required_argument, nullptr, 'i'},
92545fdf9bSopenharmony_ci    {"user-id", required_argument, nullptr, 'u'},
93545fdf9bSopenharmony_ci    {"waitting-time", required_argument, nullptr, 'w'},
94545fdf9bSopenharmony_ci    {"keep-data", no_argument, nullptr, 'k'},
95545fdf9bSopenharmony_ci    {"shared-bundle-dir-path", required_argument, nullptr, 's'},
96545fdf9bSopenharmony_ci    {"app-index", required_argument, nullptr, 'i'},
97545fdf9bSopenharmony_ci    {nullptr, 0, nullptr, 0},
98545fdf9bSopenharmony_ci};
99545fdf9bSopenharmony_ci
100545fdf9bSopenharmony_ciconst std::string UNINSTALL_OPTIONS = "hn:km:u:v:s";
101545fdf9bSopenharmony_ciconst struct option UNINSTALL_LONG_OPTIONS[] = {
102545fdf9bSopenharmony_ci    {"help", no_argument, nullptr, 'h'},
103545fdf9bSopenharmony_ci    {"bundle-name", required_argument, nullptr, 'n'},
104545fdf9bSopenharmony_ci    {"module-name", required_argument, nullptr, 'm'},
105545fdf9bSopenharmony_ci    {"user-id", required_argument, nullptr, 'u'},
106545fdf9bSopenharmony_ci    {"keep-data", no_argument, nullptr, 'k'},
107545fdf9bSopenharmony_ci    {"version", required_argument, nullptr, 'v'},
108545fdf9bSopenharmony_ci    {"shared", no_argument, nullptr, 's'},
109545fdf9bSopenharmony_ci    {nullptr, 0, nullptr, 0},
110545fdf9bSopenharmony_ci};
111545fdf9bSopenharmony_ci
112545fdf9bSopenharmony_ciconst std::string SHORT_OPTIONS_DUMP = "hn:aisu:d:";
113545fdf9bSopenharmony_ciconst struct option LONG_OPTIONS_DUMP[] = {
114545fdf9bSopenharmony_ci    {"help", no_argument, nullptr, 'h'},
115545fdf9bSopenharmony_ci    {"bundle-name", required_argument, nullptr, 'n'},
116545fdf9bSopenharmony_ci    {"all", no_argument, nullptr, 'a'},
117545fdf9bSopenharmony_ci    {"bundle-info", no_argument, nullptr, 'i'},
118545fdf9bSopenharmony_ci    {"shortcut-info", no_argument, nullptr, 's'},
119545fdf9bSopenharmony_ci    {"user-id", required_argument, nullptr, 'u'},
120545fdf9bSopenharmony_ci    {"device-id", required_argument, nullptr, 'd'},
121545fdf9bSopenharmony_ci    {nullptr, 0, nullptr, 0},
122545fdf9bSopenharmony_ci};
123545fdf9bSopenharmony_ci
124545fdf9bSopenharmony_ciconst std::string SHORT_OPTIONS_GET = "hu";
125545fdf9bSopenharmony_ciconst struct option LONG_OPTIONS_GET[] = {
126545fdf9bSopenharmony_ci    {"help", no_argument, nullptr, 'h'},
127545fdf9bSopenharmony_ci    {"udid", no_argument, nullptr, 'u'},
128545fdf9bSopenharmony_ci    {nullptr, 0, nullptr, 0},
129545fdf9bSopenharmony_ci};
130545fdf9bSopenharmony_ci
131545fdf9bSopenharmony_ciconst std::string SHORT_OPTIONS_OVERLAY = "hb:m:t:u:";
132545fdf9bSopenharmony_ciconst struct option LONG_OPTIONS_OVERLAY[] = {
133545fdf9bSopenharmony_ci    {"help", no_argument, nullptr, 'h'},
134545fdf9bSopenharmony_ci    {"bundle-name", required_argument, nullptr, 'b'},
135545fdf9bSopenharmony_ci    {"module-name", required_argument, nullptr, 'm'},
136545fdf9bSopenharmony_ci    {"target-module-name", required_argument, nullptr, 't'},
137545fdf9bSopenharmony_ci    {"user-id", required_argument, nullptr, 'u'},
138545fdf9bSopenharmony_ci    {nullptr, 0, nullptr, 0},
139545fdf9bSopenharmony_ci};
140545fdf9bSopenharmony_ci
141545fdf9bSopenharmony_ciconst std::string SHORT_OPTIONS_OVERLAY_TARGET = "hb:m:u:";
142545fdf9bSopenharmony_ciconst struct option LONG_OPTIONS_OVERLAY_TARGET[] = {
143545fdf9bSopenharmony_ci    {"help", no_argument, nullptr, 'h'},
144545fdf9bSopenharmony_ci    {"bundle-name", required_argument, nullptr, 'b'},
145545fdf9bSopenharmony_ci    {"module-name", required_argument, nullptr, 'm'},
146545fdf9bSopenharmony_ci    {"user-id", required_argument, nullptr, 'u'},
147545fdf9bSopenharmony_ci    {nullptr, 0, nullptr, 0},
148545fdf9bSopenharmony_ci};
149545fdf9bSopenharmony_ci
150545fdf9bSopenharmony_ciconst std::string SHORT_OPTIONS_DUMP_SHARED_DEPENDENCIES = "hn:m:";
151545fdf9bSopenharmony_ciconst struct option LONG_OPTIONS_DUMP_SHARED_DEPENDENCIES[] = {
152545fdf9bSopenharmony_ci    {"help", no_argument, nullptr, 'h'},
153545fdf9bSopenharmony_ci    {"bundle-name", required_argument, nullptr, 'n'},
154545fdf9bSopenharmony_ci    {"module-name", required_argument, nullptr, 'm'},
155545fdf9bSopenharmony_ci    {nullptr, 0, nullptr, 0},
156545fdf9bSopenharmony_ci};
157545fdf9bSopenharmony_ci
158545fdf9bSopenharmony_ciconst std::string SHORT_OPTIONS_DUMP_SHARED = "hn:a";
159545fdf9bSopenharmony_ciconst struct option LONG_OPTIONS_DUMP_SHARED[] = {
160545fdf9bSopenharmony_ci    {"help", no_argument, nullptr, 'h'},
161545fdf9bSopenharmony_ci    {"bundle-name", required_argument, nullptr, 'n'},
162545fdf9bSopenharmony_ci    {"all", no_argument, nullptr, 'a'},
163545fdf9bSopenharmony_ci    {nullptr, 0, nullptr, 0},
164545fdf9bSopenharmony_ci};
165545fdf9bSopenharmony_ci}  // namespace
166545fdf9bSopenharmony_ci
167545fdf9bSopenharmony_ciclass CleanCacheCallbackImpl : public CleanCacheCallbackHost {
168545fdf9bSopenharmony_cipublic:
169545fdf9bSopenharmony_ci    CleanCacheCallbackImpl() : signal_(std::make_shared<std::promise<bool>>())
170545fdf9bSopenharmony_ci    {}
171545fdf9bSopenharmony_ci    ~CleanCacheCallbackImpl() override
172545fdf9bSopenharmony_ci    {}
173545fdf9bSopenharmony_ci    void OnCleanCacheFinished(bool error) override;
174545fdf9bSopenharmony_ci    bool GetResultCode();
175545fdf9bSopenharmony_ciprivate:
176545fdf9bSopenharmony_ci    std::shared_ptr<std::promise<bool>> signal_;
177545fdf9bSopenharmony_ci    DISALLOW_COPY_AND_MOVE(CleanCacheCallbackImpl);
178545fdf9bSopenharmony_ci};
179545fdf9bSopenharmony_ci
180545fdf9bSopenharmony_civoid CleanCacheCallbackImpl::OnCleanCacheFinished(bool error)
181545fdf9bSopenharmony_ci{
182545fdf9bSopenharmony_ci    if (signal_ != nullptr) {
183545fdf9bSopenharmony_ci        signal_->set_value(error);
184545fdf9bSopenharmony_ci    }
185545fdf9bSopenharmony_ci}
186545fdf9bSopenharmony_ci
187545fdf9bSopenharmony_cibool CleanCacheCallbackImpl::GetResultCode()
188545fdf9bSopenharmony_ci{
189545fdf9bSopenharmony_ci    if (signal_ != nullptr) {
190545fdf9bSopenharmony_ci        auto future = signal_->get_future();
191545fdf9bSopenharmony_ci        std::chrono::milliseconds span(MAX_WAITING_TIME);
192545fdf9bSopenharmony_ci        if (future.wait_for(span) == std::future_status::timeout) {
193545fdf9bSopenharmony_ci            return false;
194545fdf9bSopenharmony_ci        }
195545fdf9bSopenharmony_ci        return future.get();
196545fdf9bSopenharmony_ci    }
197545fdf9bSopenharmony_ci    return false;
198545fdf9bSopenharmony_ci}
199545fdf9bSopenharmony_ci
200545fdf9bSopenharmony_ciBundleManagerShellCommand::BundleManagerShellCommand(int argc, char *argv[]) : ShellCommand(argc, argv, TOOL_NAME)
201545fdf9bSopenharmony_ci{}
202545fdf9bSopenharmony_ci
203545fdf9bSopenharmony_ciErrCode BundleManagerShellCommand::CreateCommandMap()
204545fdf9bSopenharmony_ci{
205545fdf9bSopenharmony_ci    commandMap_ = {
206545fdf9bSopenharmony_ci        {"help", [this] { return this->RunAsHelpCommand(); } },
207545fdf9bSopenharmony_ci        {"install", [this] { return this->RunAsInstallCommand(); } },
208545fdf9bSopenharmony_ci        {"uninstall", [this] { return this->RunAsUninstallCommand(); } },
209545fdf9bSopenharmony_ci        {"dump", [this] { return this->RunAsDumpCommand(); } },
210545fdf9bSopenharmony_ci        {"clean", [this] { return this->RunAsCleanCommand(); } },
211545fdf9bSopenharmony_ci        {"enable", [this] { return this->RunAsEnableCommand(); } },
212545fdf9bSopenharmony_ci        {"disable", [this] { return this->RunAsDisableCommand(); } },
213545fdf9bSopenharmony_ci        {"get", [this] { return this->RunAsGetCommand(); } },
214545fdf9bSopenharmony_ci        {"quickfix", [this] { return this->RunAsQuickFixCommand(); } },
215545fdf9bSopenharmony_ci        {"compile", [this] { return this->RunAsCompileCommand(); } },
216545fdf9bSopenharmony_ci        {"copy-ap", [this] { return this->RunAsCopyApCommand(); } },
217545fdf9bSopenharmony_ci        {"dump-overlay", [this] { return this->RunAsDumpOverlay(); } },
218545fdf9bSopenharmony_ci        {"dump-target-overlay", [this] { return this->RunAsDumpTargetOverlay(); } },
219545fdf9bSopenharmony_ci        {"dump-dependencies", [this] { return this->RunAsDumpSharedDependenciesCommand(); } },
220545fdf9bSopenharmony_ci        {"dump-shared", [this] { return this->RunAsDumpSharedCommand(); } },
221545fdf9bSopenharmony_ci    };
222545fdf9bSopenharmony_ci
223545fdf9bSopenharmony_ci    return OHOS::ERR_OK;
224545fdf9bSopenharmony_ci}
225545fdf9bSopenharmony_ci
226545fdf9bSopenharmony_ciErrCode BundleManagerShellCommand::CreateMessageMap()
227545fdf9bSopenharmony_ci{
228545fdf9bSopenharmony_ci    messageMap_ = BundleCommandCommon::bundleMessageMap_;
229545fdf9bSopenharmony_ci    return OHOS::ERR_OK;
230545fdf9bSopenharmony_ci}
231545fdf9bSopenharmony_ci
232545fdf9bSopenharmony_ciErrCode BundleManagerShellCommand::Init()
233545fdf9bSopenharmony_ci{
234545fdf9bSopenharmony_ci    ErrCode result = OHOS::ERR_OK;
235545fdf9bSopenharmony_ci
236545fdf9bSopenharmony_ci    if (bundleMgrProxy_ == nullptr) {
237545fdf9bSopenharmony_ci        bundleMgrProxy_ = BundleCommandCommon::GetBundleMgrProxy();
238545fdf9bSopenharmony_ci        if (bundleMgrProxy_) {
239545fdf9bSopenharmony_ci            if (bundleInstallerProxy_ == nullptr) {
240545fdf9bSopenharmony_ci                bundleInstallerProxy_ = bundleMgrProxy_->GetBundleInstaller();
241545fdf9bSopenharmony_ci            }
242545fdf9bSopenharmony_ci        }
243545fdf9bSopenharmony_ci    }
244545fdf9bSopenharmony_ci
245545fdf9bSopenharmony_ci    if ((bundleMgrProxy_ == nullptr) || (bundleInstallerProxy_ == nullptr) ||
246545fdf9bSopenharmony_ci        (bundleInstallerProxy_->AsObject() == nullptr)) {
247545fdf9bSopenharmony_ci        result = OHOS::ERR_INVALID_VALUE;
248545fdf9bSopenharmony_ci    }
249545fdf9bSopenharmony_ci
250545fdf9bSopenharmony_ci    return result;
251545fdf9bSopenharmony_ci}
252545fdf9bSopenharmony_ci
253545fdf9bSopenharmony_ciErrCode BundleManagerShellCommand::RunAsHelpCommand()
254545fdf9bSopenharmony_ci{
255545fdf9bSopenharmony_ci    resultReceiver_.append(HELP_MSG);
256545fdf9bSopenharmony_ci
257545fdf9bSopenharmony_ci    int32_t mode = GetIntParameter(IS_ROOT_MODE_PARAM, USER_MODE);
258545fdf9bSopenharmony_ci    APP_LOGI("current mode is: %{public}d", mode);
259545fdf9bSopenharmony_ci    if (mode == ROOT_MODE) {
260545fdf9bSopenharmony_ci        resultReceiver_.append(ENABLE_DISABLE_HELP_MSG);
261545fdf9bSopenharmony_ci    }
262545fdf9bSopenharmony_ci
263545fdf9bSopenharmony_ci    bool isDeveloperMode = system::GetBoolParameter(IS_DEVELOPER_MODE_PARAM, false);
264545fdf9bSopenharmony_ci    APP_LOGD("current developer mode is: %{public}d", isDeveloperMode);
265545fdf9bSopenharmony_ci    if (mode == ROOT_MODE || isDeveloperMode) {
266545fdf9bSopenharmony_ci        resultReceiver_.append(CLEAN_HELP_MSG);
267545fdf9bSopenharmony_ci    }
268545fdf9bSopenharmony_ci
269545fdf9bSopenharmony_ci    return OHOS::ERR_OK;
270545fdf9bSopenharmony_ci}
271545fdf9bSopenharmony_ci
272545fdf9bSopenharmony_cibool BundleManagerShellCommand::IsInstallOption(int index) const
273545fdf9bSopenharmony_ci{
274545fdf9bSopenharmony_ci    if (index >= argc_ || index < INDEX_OFFSET) {
275545fdf9bSopenharmony_ci        return false;
276545fdf9bSopenharmony_ci    }
277545fdf9bSopenharmony_ci    if (argList_[index - INDEX_OFFSET] == "-r" || argList_[index - INDEX_OFFSET] == "--replace" ||
278545fdf9bSopenharmony_ci        argList_[index - INDEX_OFFSET] == "-p" || argList_[index - INDEX_OFFSET] == "--bundle-path" ||
279545fdf9bSopenharmony_ci        argList_[index - INDEX_OFFSET] == "-u" || argList_[index - INDEX_OFFSET] == "--user-id" ||
280545fdf9bSopenharmony_ci        argList_[index - INDEX_OFFSET] == "-w" || argList_[index - INDEX_OFFSET] == "--waitting-time" ||
281545fdf9bSopenharmony_ci        argList_[index - INDEX_OFFSET] == "-s" || argList_[index - INDEX_OFFSET] == "--shared-bundle-dir-path") {
282545fdf9bSopenharmony_ci        return true;
283545fdf9bSopenharmony_ci    }
284545fdf9bSopenharmony_ci    return false;
285545fdf9bSopenharmony_ci}
286545fdf9bSopenharmony_ci
287545fdf9bSopenharmony_ciErrCode BundleManagerShellCommand::RunAsCopyApCommand()
288545fdf9bSopenharmony_ci{
289545fdf9bSopenharmony_ci    int32_t mode = GetIntParameter(IS_ROOT_MODE_PARAM, USER_MODE);
290545fdf9bSopenharmony_ci    bool isDeveloperMode = system::GetBoolParameter(IS_DEVELOPER_MODE_PARAM, false);
291545fdf9bSopenharmony_ci    if (mode != ROOT_MODE && !isDeveloperMode) {
292545fdf9bSopenharmony_ci        APP_LOGI("in user mode but not in developer mode");
293545fdf9bSopenharmony_ci        return ERR_OK;
294545fdf9bSopenharmony_ci    }
295545fdf9bSopenharmony_ci    APP_LOGI("begin to RunAsCopyApCommand");
296545fdf9bSopenharmony_ci    int result = OHOS::ERR_OK;
297545fdf9bSopenharmony_ci    int counter = 0;
298545fdf9bSopenharmony_ci    std::string bundleName = "";
299545fdf9bSopenharmony_ci    bool isAllBundle = false;
300545fdf9bSopenharmony_ci    int32_t option;
301545fdf9bSopenharmony_ci    while ((option = getopt_long(argc_, argv_, SHORT_OPTIONS_COPY_AP.c_str(),
302545fdf9bSopenharmony_ci        LONG_OPTIONS_COPY_AP, nullptr)) != -1) {
303545fdf9bSopenharmony_ci        counter++;
304545fdf9bSopenharmony_ci        if (optind < 0 || optind > argc_) {
305545fdf9bSopenharmony_ci            return OHOS::ERR_INVALID_VALUE;
306545fdf9bSopenharmony_ci        }
307545fdf9bSopenharmony_ci        result = ParseCopyApCommand(option, bundleName, isAllBundle);
308545fdf9bSopenharmony_ci        if (option == '?') {
309545fdf9bSopenharmony_ci            break;
310545fdf9bSopenharmony_ci        }
311545fdf9bSopenharmony_ci    }
312545fdf9bSopenharmony_ci
313545fdf9bSopenharmony_ci    if ((option == -1) && (counter == 0)) {
314545fdf9bSopenharmony_ci        if (optind < 0 || optind > argc_) {
315545fdf9bSopenharmony_ci            return OHOS::ERR_INVALID_VALUE;
316545fdf9bSopenharmony_ci        }
317545fdf9bSopenharmony_ci        if (strcmp(argv_[optind], cmd_.c_str()) == 0) {
318545fdf9bSopenharmony_ci            // 1.'bm copy-ap' with no option: bm copy-ap
319545fdf9bSopenharmony_ci            // 2.'bm copy-ap' with a wrong argument: bm copy-ap -xxx
320545fdf9bSopenharmony_ci            APP_LOGD("'bm copy-ap' %{public}s", HELP_MSG_NO_OPTION.c_str());
321545fdf9bSopenharmony_ci            resultReceiver_.append(HELP_MSG_NO_OPTION + "\n");
322545fdf9bSopenharmony_ci            result = OHOS::ERR_INVALID_VALUE;
323545fdf9bSopenharmony_ci        }
324545fdf9bSopenharmony_ci    }
325545fdf9bSopenharmony_ci
326545fdf9bSopenharmony_ci    if (result != OHOS::ERR_OK) {
327545fdf9bSopenharmony_ci        resultReceiver_.append(HELP_MSG_COPY_AP);
328545fdf9bSopenharmony_ci    } else {
329545fdf9bSopenharmony_ci        std::string copyApResult = "";
330545fdf9bSopenharmony_ci        copyApResult = CopyAp(bundleName, isAllBundle);
331545fdf9bSopenharmony_ci        if (copyApResult.empty() || (copyApResult == "")) {
332545fdf9bSopenharmony_ci            copyApResult = "parameters may be wrong\n";
333545fdf9bSopenharmony_ci        }
334545fdf9bSopenharmony_ci        resultReceiver_.append(copyApResult);
335545fdf9bSopenharmony_ci    }
336545fdf9bSopenharmony_ci    APP_LOGI("end to RunAsCopyApCommand");
337545fdf9bSopenharmony_ci    return result;
338545fdf9bSopenharmony_ci}
339545fdf9bSopenharmony_ci
340545fdf9bSopenharmony_ciErrCode BundleManagerShellCommand::ParseCopyApCommand(int32_t option, std::string &bundleName, bool &isAllBundle)
341545fdf9bSopenharmony_ci{
342545fdf9bSopenharmony_ci    int32_t result = OHOS::ERR_OK;
343545fdf9bSopenharmony_ci    if (option == '?') {
344545fdf9bSopenharmony_ci        switch (optopt) {
345545fdf9bSopenharmony_ci            case 'n': {
346545fdf9bSopenharmony_ci                // 'bm copy-ap -n' with no argument: bm copy-ap -n
347545fdf9bSopenharmony_ci                // 'bm copy-ap --bundle-name' with no argument: bm copy-ap --bundle-name
348545fdf9bSopenharmony_ci                APP_LOGD("'bm copy-ap -n' with no argument.");
349545fdf9bSopenharmony_ci                resultReceiver_.append(STRING_REQUIRE_CORRECT_VALUE);
350545fdf9bSopenharmony_ci                result = OHOS::ERR_INVALID_VALUE;
351545fdf9bSopenharmony_ci                break;
352545fdf9bSopenharmony_ci            }
353545fdf9bSopenharmony_ci            default: {
354545fdf9bSopenharmony_ci                // 'bm copy-ap' with an unknown option: bm copy-ap -x
355545fdf9bSopenharmony_ci                // 'bm copy-ap' with an unknown option: bm copy-ap -xxx
356545fdf9bSopenharmony_ci                std::string unknownOption = "";
357545fdf9bSopenharmony_ci                std::string unknownOptionMsg = GetUnknownOptionMsg(unknownOption);
358545fdf9bSopenharmony_ci                APP_LOGE("'bm copy-ap' with an unknown option.");
359545fdf9bSopenharmony_ci                resultReceiver_.append(unknownOptionMsg);
360545fdf9bSopenharmony_ci                result = OHOS::ERR_INVALID_VALUE;
361545fdf9bSopenharmony_ci                break;
362545fdf9bSopenharmony_ci            }
363545fdf9bSopenharmony_ci        }
364545fdf9bSopenharmony_ci    } else {
365545fdf9bSopenharmony_ci        switch (option) {
366545fdf9bSopenharmony_ci            case 'h': {
367545fdf9bSopenharmony_ci                // 'bm copy-ap -h'
368545fdf9bSopenharmony_ci                // 'bm copy-ap --help'
369545fdf9bSopenharmony_ci                APP_LOGD("'bm copy-ap %{public}s'", argv_[optind - 1]);
370545fdf9bSopenharmony_ci                result = OHOS::ERR_INVALID_VALUE;
371545fdf9bSopenharmony_ci                break;
372545fdf9bSopenharmony_ci            }
373545fdf9bSopenharmony_ci            case 'a': {
374545fdf9bSopenharmony_ci                // 'bm copy-ap -a'
375545fdf9bSopenharmony_ci                // 'bm copy-ap --all'
376545fdf9bSopenharmony_ci                APP_LOGD("'bm copy-ap %{public}s'", argv_[optind - 1]);
377545fdf9bSopenharmony_ci                isAllBundle = true;
378545fdf9bSopenharmony_ci                break;
379545fdf9bSopenharmony_ci            }
380545fdf9bSopenharmony_ci            case 'n': {
381545fdf9bSopenharmony_ci                // 'bm copy-ap -n xxx'
382545fdf9bSopenharmony_ci                // 'bm copy-ap --bundle-name xxx'
383545fdf9bSopenharmony_ci                APP_LOGD("'bm copy-ap %{public}s %{public}s'", argv_[optind - OFFSET_REQUIRED_ARGUMENT], optarg);
384545fdf9bSopenharmony_ci                bundleName = optarg;
385545fdf9bSopenharmony_ci                break;
386545fdf9bSopenharmony_ci            }
387545fdf9bSopenharmony_ci            default: {
388545fdf9bSopenharmony_ci                APP_LOGD("'bm copy-ap %{public}s'", argv_[optind - 1]);
389545fdf9bSopenharmony_ci                result = OHOS::ERR_INVALID_VALUE;
390545fdf9bSopenharmony_ci                break;
391545fdf9bSopenharmony_ci            }
392545fdf9bSopenharmony_ci        }
393545fdf9bSopenharmony_ci    }
394545fdf9bSopenharmony_ci    return result;
395545fdf9bSopenharmony_ci}
396545fdf9bSopenharmony_ci
397545fdf9bSopenharmony_ciErrCode BundleManagerShellCommand::RunAsCompileCommand()
398545fdf9bSopenharmony_ci{
399545fdf9bSopenharmony_ci    APP_LOGI("begin to RunAsCompileCommand");
400545fdf9bSopenharmony_ci    int result = OHOS::ERR_OK;
401545fdf9bSopenharmony_ci    int counter = 0;
402545fdf9bSopenharmony_ci    std::string compileMode = "";
403545fdf9bSopenharmony_ci    std::string bundleName = "";
404545fdf9bSopenharmony_ci    bool bundleCompile = false;
405545fdf9bSopenharmony_ci    bool resetCompile = false;
406545fdf9bSopenharmony_ci    bool isAllBundle = false;
407545fdf9bSopenharmony_ci    while (true) {
408545fdf9bSopenharmony_ci        counter++;
409545fdf9bSopenharmony_ci        int32_t option = getopt_long(argc_, argv_, SHORT_OPTIONS_COMPILE.c_str(), LONG_OPTIONS_COMPILE, nullptr);
410545fdf9bSopenharmony_ci        APP_LOGD("option: %{public}d, optopt: %{public}d, optind: %{public}d", option, optopt, optind);
411545fdf9bSopenharmony_ci        if (optind < 0 || optind > argc_) {
412545fdf9bSopenharmony_ci            return OHOS::ERR_INVALID_VALUE;
413545fdf9bSopenharmony_ci        }
414545fdf9bSopenharmony_ci        if (option == -1) {
415545fdf9bSopenharmony_ci            if (counter == 1) {
416545fdf9bSopenharmony_ci                // When scanning the first argument
417545fdf9bSopenharmony_ci                if (strcmp(argv_[optind], cmd_.c_str()) == 0) {
418545fdf9bSopenharmony_ci                    // 'bm compile' with no option: bm compile
419545fdf9bSopenharmony_ci                    // 'bm compile' with a wrong argument: bm compile xxx
420545fdf9bSopenharmony_ci                    APP_LOGD("'bm compile' %{public}s", HELP_MSG_NO_OPTION.c_str());
421545fdf9bSopenharmony_ci                    resultReceiver_.append(HELP_MSG_NO_OPTION + "\n");
422545fdf9bSopenharmony_ci                    result = OHOS::ERR_INVALID_VALUE;
423545fdf9bSopenharmony_ci                }
424545fdf9bSopenharmony_ci            }
425545fdf9bSopenharmony_ci            break;
426545fdf9bSopenharmony_ci        }
427545fdf9bSopenharmony_ci        if (option == '?') {
428545fdf9bSopenharmony_ci            switch (optopt) {
429545fdf9bSopenharmony_ci                case 'a': {
430545fdf9bSopenharmony_ci                    // 'bm compile -m' with no argument: bm compile -m
431545fdf9bSopenharmony_ci                    // 'bm compile --mode' with no argument: bm compile --mode
432545fdf9bSopenharmony_ci                    APP_LOGD("'bm compile %{public}s'", argv_[optind - 1]);
433545fdf9bSopenharmony_ci                    isAllBundle = true;
434545fdf9bSopenharmony_ci                    break;
435545fdf9bSopenharmony_ci                }
436545fdf9bSopenharmony_ci                default: {
437545fdf9bSopenharmony_ci                    // 'bm compile' with an unknown option: bm compile -x
438545fdf9bSopenharmony_ci                    // 'bm compile' with an unknown option: bm compile -xxx
439545fdf9bSopenharmony_ci                    std::string unknownOption = "";
440545fdf9bSopenharmony_ci                    std::string unknownOptionMsg = GetUnknownOptionMsg(unknownOption);
441545fdf9bSopenharmony_ci                    APP_LOGE("'bm compile' with an unknown option.");
442545fdf9bSopenharmony_ci                    resultReceiver_.append(unknownOptionMsg);
443545fdf9bSopenharmony_ci                    result = OHOS::ERR_INVALID_VALUE;
444545fdf9bSopenharmony_ci                    break;
445545fdf9bSopenharmony_ci                }
446545fdf9bSopenharmony_ci            }
447545fdf9bSopenharmony_ci            break;
448545fdf9bSopenharmony_ci        }
449545fdf9bSopenharmony_ci        switch (option) {
450545fdf9bSopenharmony_ci            case 'h': {
451545fdf9bSopenharmony_ci                // 'bm compile -h'
452545fdf9bSopenharmony_ci                // 'bm compile --help'
453545fdf9bSopenharmony_ci                APP_LOGD("'bm compile %{public}s'", argv_[optind - 1]);
454545fdf9bSopenharmony_ci                result = OHOS::ERR_INVALID_VALUE;
455545fdf9bSopenharmony_ci                break;
456545fdf9bSopenharmony_ci            }
457545fdf9bSopenharmony_ci            case 'm': {
458545fdf9bSopenharmony_ci                // 'bm compile -m xxx'
459545fdf9bSopenharmony_ci                // 'bm compile --mode xxx'
460545fdf9bSopenharmony_ci                APP_LOGD("'bm compile %{public}s %{public}s %{public}s'",
461545fdf9bSopenharmony_ci                    argv_[optind - OFFSET_REQUIRED_ARGUMENT], optarg, argv_[optind + 1]);
462545fdf9bSopenharmony_ci                bundleCompile = true;
463545fdf9bSopenharmony_ci                compileMode = optarg;
464545fdf9bSopenharmony_ci                bundleName = argv_[optind + 1];
465545fdf9bSopenharmony_ci                break;
466545fdf9bSopenharmony_ci            }
467545fdf9bSopenharmony_ci            case 'r': {
468545fdf9bSopenharmony_ci                // 'bm compile -r xxx'
469545fdf9bSopenharmony_ci                // 'bm compile --reset xxx'
470545fdf9bSopenharmony_ci                APP_LOGD("'bm compile %{public}s'", argv_[optind - 1]);
471545fdf9bSopenharmony_ci                resetCompile = true;
472545fdf9bSopenharmony_ci                bundleName = optarg;
473545fdf9bSopenharmony_ci                if (bundleName == "-a") {
474545fdf9bSopenharmony_ci                    isAllBundle = true;
475545fdf9bSopenharmony_ci                }
476545fdf9bSopenharmony_ci                break;
477545fdf9bSopenharmony_ci            }
478545fdf9bSopenharmony_ci            default: {
479545fdf9bSopenharmony_ci                APP_LOGD("'bm compile %{public}s'", argv_[optind - 1]);
480545fdf9bSopenharmony_ci                result = OHOS::ERR_INVALID_VALUE;
481545fdf9bSopenharmony_ci                break;
482545fdf9bSopenharmony_ci            }
483545fdf9bSopenharmony_ci        }
484545fdf9bSopenharmony_ci    }
485545fdf9bSopenharmony_ci    if (result != OHOS::ERR_OK) {
486545fdf9bSopenharmony_ci        resultReceiver_.append(HELP_MSG_COMPILE);
487545fdf9bSopenharmony_ci    } else {
488545fdf9bSopenharmony_ci        std::string compileResults = "";
489545fdf9bSopenharmony_ci        APP_LOGD("compileResults: %{public}s", compileResults.c_str());
490545fdf9bSopenharmony_ci        if (bundleCompile) {
491545fdf9bSopenharmony_ci            compileResults = CompileProcessAot(bundleName, compileMode, isAllBundle);
492545fdf9bSopenharmony_ci        } else if (resetCompile) {
493545fdf9bSopenharmony_ci            compileResults = CompileReset(bundleName, isAllBundle);
494545fdf9bSopenharmony_ci        }
495545fdf9bSopenharmony_ci        if (compileResults.empty() || (compileResults == "")) {
496545fdf9bSopenharmony_ci            compileResults = HELP_MSG_COMPILE_FAILED + "\n";
497545fdf9bSopenharmony_ci        }
498545fdf9bSopenharmony_ci        resultReceiver_.append(compileResults);
499545fdf9bSopenharmony_ci    }
500545fdf9bSopenharmony_ci    APP_LOGI("end");
501545fdf9bSopenharmony_ci    return result;
502545fdf9bSopenharmony_ci}
503545fdf9bSopenharmony_ci
504545fdf9bSopenharmony_ciErrCode BundleManagerShellCommand::RunAsInstallCommand()
505545fdf9bSopenharmony_ci{
506545fdf9bSopenharmony_ci    APP_LOGI("begin to RunAsInstallCommand");
507545fdf9bSopenharmony_ci    int result = OHOS::ERR_OK;
508545fdf9bSopenharmony_ci    InstallFlag installFlag = InstallFlag::REPLACE_EXISTING;
509545fdf9bSopenharmony_ci    int counter = 0;
510545fdf9bSopenharmony_ci    std::vector<std::string> bundlePath;
511545fdf9bSopenharmony_ci    std::vector<std::string> sharedBundleDirPaths;
512545fdf9bSopenharmony_ci    int index = 0;
513545fdf9bSopenharmony_ci    int hspIndex = 0;
514545fdf9bSopenharmony_ci    int32_t userId = BundleCommandCommon::GetCurrentUserId(Constants::UNSPECIFIED_USERID);
515545fdf9bSopenharmony_ci    int32_t waittingTime = MINIMUM_WAITTING_TIME;
516545fdf9bSopenharmony_ci    while (true) {
517545fdf9bSopenharmony_ci        counter++;
518545fdf9bSopenharmony_ci        int32_t option = getopt_long(argc_, argv_, SHORT_OPTIONS.c_str(), LONG_OPTIONS, nullptr);
519545fdf9bSopenharmony_ci        APP_LOGD("option: %{public}d, optopt: %{public}d, optind: %{public}d", option, optopt, optind);
520545fdf9bSopenharmony_ci        if (optind < 0 || optind > argc_) {
521545fdf9bSopenharmony_ci            return OHOS::ERR_INVALID_VALUE;
522545fdf9bSopenharmony_ci        }
523545fdf9bSopenharmony_ci        if (option == -1) {
524545fdf9bSopenharmony_ci            if (counter == 1) {
525545fdf9bSopenharmony_ci                // When scanning the first argument
526545fdf9bSopenharmony_ci                if (strcmp(argv_[optind], cmd_.c_str()) == 0) {
527545fdf9bSopenharmony_ci                    // 'bm install' with no option: bm install
528545fdf9bSopenharmony_ci                    // 'bm install' with a wrong argument: bm install xxx
529545fdf9bSopenharmony_ci                    APP_LOGD("'bm install' with no option.");
530545fdf9bSopenharmony_ci                    resultReceiver_.append(HELP_MSG_NO_OPTION + "\n");
531545fdf9bSopenharmony_ci                    result = OHOS::ERR_INVALID_VALUE;
532545fdf9bSopenharmony_ci                }
533545fdf9bSopenharmony_ci            }
534545fdf9bSopenharmony_ci            break;
535545fdf9bSopenharmony_ci        }
536545fdf9bSopenharmony_ci
537545fdf9bSopenharmony_ci        if (option == '?') {
538545fdf9bSopenharmony_ci            switch (optopt) {
539545fdf9bSopenharmony_ci                case 'p': {
540545fdf9bSopenharmony_ci                    // 'bm install -p' with no argument: bm install -p
541545fdf9bSopenharmony_ci                    // 'bm install --bundle-path' with no argument: bm install --bundle-path
542545fdf9bSopenharmony_ci                    APP_LOGD("'bm install' with no argument.");
543545fdf9bSopenharmony_ci                    resultReceiver_.append(STRING_REQUIRE_CORRECT_VALUE);
544545fdf9bSopenharmony_ci                    result = OHOS::ERR_INVALID_VALUE;
545545fdf9bSopenharmony_ci                    break;
546545fdf9bSopenharmony_ci                }
547545fdf9bSopenharmony_ci                case 'u': {
548545fdf9bSopenharmony_ci                    // 'bm install -u' with no argument: bm install -u
549545fdf9bSopenharmony_ci                    // 'bm install --user-id' with no argument: bm install --user-id
550545fdf9bSopenharmony_ci                    APP_LOGD("'bm install -u' with no argument.");
551545fdf9bSopenharmony_ci                    resultReceiver_.append(STRING_REQUIRE_CORRECT_VALUE);
552545fdf9bSopenharmony_ci                    result = OHOS::ERR_INVALID_VALUE;
553545fdf9bSopenharmony_ci                    break;
554545fdf9bSopenharmony_ci                }
555545fdf9bSopenharmony_ci                case 'w': {
556545fdf9bSopenharmony_ci                    // 'bm install -w' with no argument: bm install -w
557545fdf9bSopenharmony_ci                    // 'bm install --waitting-time' with no argument: bm install --waitting-time
558545fdf9bSopenharmony_ci                    APP_LOGD("'bm install -w' with no argument.");
559545fdf9bSopenharmony_ci                    resultReceiver_.append(STRING_REQUIRE_CORRECT_VALUE);
560545fdf9bSopenharmony_ci                    result = OHOS::ERR_INVALID_VALUE;
561545fdf9bSopenharmony_ci                    break;
562545fdf9bSopenharmony_ci                }
563545fdf9bSopenharmony_ci                default: {
564545fdf9bSopenharmony_ci                    // 'bm install' with an unknown option: bm install -x
565545fdf9bSopenharmony_ci                    // 'bm install' with an unknown option: bm install -xxx
566545fdf9bSopenharmony_ci                    std::string unknownOption = "";
567545fdf9bSopenharmony_ci                    std::string unknownOptionMsg = GetUnknownOptionMsg(unknownOption);
568545fdf9bSopenharmony_ci                    APP_LOGD("'bm install' with an unknown option.");
569545fdf9bSopenharmony_ci                    resultReceiver_.append(unknownOptionMsg);
570545fdf9bSopenharmony_ci                    result = OHOS::ERR_INVALID_VALUE;
571545fdf9bSopenharmony_ci                    break;
572545fdf9bSopenharmony_ci                }
573545fdf9bSopenharmony_ci            }
574545fdf9bSopenharmony_ci            break;
575545fdf9bSopenharmony_ci        }
576545fdf9bSopenharmony_ci
577545fdf9bSopenharmony_ci        switch (option) {
578545fdf9bSopenharmony_ci            case 'h': {
579545fdf9bSopenharmony_ci                // 'bm install -h'
580545fdf9bSopenharmony_ci                // 'bm install --help'
581545fdf9bSopenharmony_ci                APP_LOGD("'bm install %{public}s'", argv_[optind - 1]);
582545fdf9bSopenharmony_ci                result = OHOS::ERR_INVALID_VALUE;
583545fdf9bSopenharmony_ci                break;
584545fdf9bSopenharmony_ci            }
585545fdf9bSopenharmony_ci            case 'p': {
586545fdf9bSopenharmony_ci                // 'bm install -p <bundle-file-path>'
587545fdf9bSopenharmony_ci                // 'bm install --bundle-path <bundle-file-path>'
588545fdf9bSopenharmony_ci                APP_LOGD("'bm install %{public}s'", argv_[optind - 1]);
589545fdf9bSopenharmony_ci                if (GetBundlePath(optarg, bundlePath) != OHOS::ERR_OK) {
590545fdf9bSopenharmony_ci                    APP_LOGD("'bm install' with no argument.");
591545fdf9bSopenharmony_ci                    resultReceiver_.append(STRING_REQUIRE_CORRECT_VALUE);
592545fdf9bSopenharmony_ci                    return OHOS::ERR_INVALID_VALUE;
593545fdf9bSopenharmony_ci                }
594545fdf9bSopenharmony_ci                index = optind;
595545fdf9bSopenharmony_ci                break;
596545fdf9bSopenharmony_ci            }
597545fdf9bSopenharmony_ci            case 'r': {
598545fdf9bSopenharmony_ci                // 'bm install -r'
599545fdf9bSopenharmony_ci                // 'bm install --replace'
600545fdf9bSopenharmony_ci                installFlag = InstallFlag::REPLACE_EXISTING;
601545fdf9bSopenharmony_ci                break;
602545fdf9bSopenharmony_ci            }
603545fdf9bSopenharmony_ci            case 'u': {
604545fdf9bSopenharmony_ci                // 'bm install -p <bundle-file-path> -u userId'
605545fdf9bSopenharmony_ci                // 'bm install --bundle-path <bundle-file-path> --user-id userId'
606545fdf9bSopenharmony_ci                APP_LOGW("'bm install -u only support user 0'");
607545fdf9bSopenharmony_ci                if (!OHOS::StrToInt(optarg, userId) || userId < 0) {
608545fdf9bSopenharmony_ci                    APP_LOGE("bm install with error userId %{private}s", optarg);
609545fdf9bSopenharmony_ci                    resultReceiver_.append(STRING_REQUIRE_CORRECT_VALUE);
610545fdf9bSopenharmony_ci                    return OHOS::ERR_INVALID_VALUE;
611545fdf9bSopenharmony_ci                }
612545fdf9bSopenharmony_ci                if (userId != Constants::DEFAULT_USERID) {
613545fdf9bSopenharmony_ci                    userId = BundleCommandCommon::GetCurrentUserId(Constants::UNSPECIFIED_USERID);
614545fdf9bSopenharmony_ci                }
615545fdf9bSopenharmony_ci                break;
616545fdf9bSopenharmony_ci            }
617545fdf9bSopenharmony_ci            case 'w': {
618545fdf9bSopenharmony_ci                APP_LOGD("'bm install %{public}s %{public}s'", argv_[optind - OFFSET_REQUIRED_ARGUMENT], optarg);
619545fdf9bSopenharmony_ci                if (!OHOS::StrToInt(optarg, waittingTime) || waittingTime < MINIMUM_WAITTING_TIME ||
620545fdf9bSopenharmony_ci                    waittingTime > MAXIMUM_WAITTING_TIME) {
621545fdf9bSopenharmony_ci                    APP_LOGE("bm install with error waittingTime %{private}s", optarg);
622545fdf9bSopenharmony_ci                    resultReceiver_.append(STRING_REQUIRE_CORRECT_VALUE);
623545fdf9bSopenharmony_ci                    return OHOS::ERR_INVALID_VALUE;
624545fdf9bSopenharmony_ci                }
625545fdf9bSopenharmony_ci                break;
626545fdf9bSopenharmony_ci            }
627545fdf9bSopenharmony_ci            case 's': {
628545fdf9bSopenharmony_ci                // 'bm install -s <hsp-dir-path>'
629545fdf9bSopenharmony_ci                // 'bm install --shared-bundle-dir-path <hsp-dir-path>'
630545fdf9bSopenharmony_ci                APP_LOGD("'bm install %{public}s %{public}s'", argv_[optind - OFFSET_REQUIRED_ARGUMENT], optarg);
631545fdf9bSopenharmony_ci                if (GetBundlePath(optarg, sharedBundleDirPaths) != OHOS::ERR_OK) {
632545fdf9bSopenharmony_ci                    APP_LOGD("'bm install -s' with no argument.");
633545fdf9bSopenharmony_ci                    resultReceiver_.append(STRING_REQUIRE_CORRECT_VALUE);
634545fdf9bSopenharmony_ci                    return OHOS::ERR_INVALID_VALUE;
635545fdf9bSopenharmony_ci                }
636545fdf9bSopenharmony_ci                hspIndex = optind;
637545fdf9bSopenharmony_ci                break;
638545fdf9bSopenharmony_ci            }
639545fdf9bSopenharmony_ci            default: {
640545fdf9bSopenharmony_ci                result = OHOS::ERR_INVALID_VALUE;
641545fdf9bSopenharmony_ci                break;
642545fdf9bSopenharmony_ci            }
643545fdf9bSopenharmony_ci        }
644545fdf9bSopenharmony_ci    }
645545fdf9bSopenharmony_ci
646545fdf9bSopenharmony_ci    for (; index < argc_ && index >= INDEX_OFFSET; ++index) {
647545fdf9bSopenharmony_ci        if (IsInstallOption(index)) {
648545fdf9bSopenharmony_ci            break;
649545fdf9bSopenharmony_ci        }
650545fdf9bSopenharmony_ci        if (GetBundlePath(argList_[index - INDEX_OFFSET], bundlePath) != OHOS::ERR_OK) {
651545fdf9bSopenharmony_ci            bundlePath.clear();
652545fdf9bSopenharmony_ci            APP_LOGD("'bm install' with error arguments.");
653545fdf9bSopenharmony_ci            resultReceiver_.append("error value for the chosen option");
654545fdf9bSopenharmony_ci            result = OHOS::ERR_INVALID_VALUE;
655545fdf9bSopenharmony_ci        }
656545fdf9bSopenharmony_ci    }
657545fdf9bSopenharmony_ci
658545fdf9bSopenharmony_ci    // hsp list
659545fdf9bSopenharmony_ci    for (; hspIndex < argc_ && hspIndex >= INDEX_OFFSET; ++hspIndex) {
660545fdf9bSopenharmony_ci        if (IsInstallOption(hspIndex)) {
661545fdf9bSopenharmony_ci            break;
662545fdf9bSopenharmony_ci        }
663545fdf9bSopenharmony_ci        if (GetBundlePath(argList_[hspIndex - INDEX_OFFSET], sharedBundleDirPaths) != OHOS::ERR_OK) {
664545fdf9bSopenharmony_ci            sharedBundleDirPaths.clear();
665545fdf9bSopenharmony_ci            APP_LOGD("'bm install -s' with error arguments.");
666545fdf9bSopenharmony_ci            resultReceiver_.append("error value for the chosen option");
667545fdf9bSopenharmony_ci            result = OHOS::ERR_INVALID_VALUE;
668545fdf9bSopenharmony_ci        }
669545fdf9bSopenharmony_ci    }
670545fdf9bSopenharmony_ci
671545fdf9bSopenharmony_ci    for (auto &path : bundlePath) {
672545fdf9bSopenharmony_ci        APP_LOGD("install hap path %{private}s", path.c_str());
673545fdf9bSopenharmony_ci    }
674545fdf9bSopenharmony_ci
675545fdf9bSopenharmony_ci    for (auto &path : sharedBundleDirPaths) {
676545fdf9bSopenharmony_ci        APP_LOGD("install hsp path %{private}s", path.c_str());
677545fdf9bSopenharmony_ci    }
678545fdf9bSopenharmony_ci
679545fdf9bSopenharmony_ci    if (result == OHOS::ERR_OK) {
680545fdf9bSopenharmony_ci        if (resultReceiver_ == "" && bundlePath.empty() && sharedBundleDirPaths.empty()) {
681545fdf9bSopenharmony_ci            // 'bm install ...' with no bundle path option
682545fdf9bSopenharmony_ci            APP_LOGD("'bm install' with no bundle path option.");
683545fdf9bSopenharmony_ci            resultReceiver_.append(HELP_MSG_NO_BUNDLE_PATH_OPTION + "\n");
684545fdf9bSopenharmony_ci            result = OHOS::ERR_INVALID_VALUE;
685545fdf9bSopenharmony_ci        }
686545fdf9bSopenharmony_ci    }
687545fdf9bSopenharmony_ci
688545fdf9bSopenharmony_ci    if (result != OHOS::ERR_OK) {
689545fdf9bSopenharmony_ci        resultReceiver_.append(HELP_MSG_INSTALL);
690545fdf9bSopenharmony_ci    } else {
691545fdf9bSopenharmony_ci        InstallParam installParam;
692545fdf9bSopenharmony_ci        installParam.installFlag = installFlag;
693545fdf9bSopenharmony_ci        installParam.userId = userId;
694545fdf9bSopenharmony_ci        installParam.sharedBundleDirPaths = sharedBundleDirPaths;
695545fdf9bSopenharmony_ci        std::string resultMsg;
696545fdf9bSopenharmony_ci        int32_t installResult = InstallOperation(bundlePath, installParam, waittingTime, resultMsg);
697545fdf9bSopenharmony_ci        if (installResult == OHOS::ERR_OK) {
698545fdf9bSopenharmony_ci            resultReceiver_ = STRING_INSTALL_BUNDLE_OK + "\n";
699545fdf9bSopenharmony_ci        } else {
700545fdf9bSopenharmony_ci            resultReceiver_ = STRING_INSTALL_BUNDLE_NG + "\n";
701545fdf9bSopenharmony_ci            resultReceiver_.append(GetMessageFromCode(installResult));
702545fdf9bSopenharmony_ci            if (!resultMsg.empty() && resultMsg[0] != '[') {
703545fdf9bSopenharmony_ci                resultReceiver_.append(resultMsg + "\n");
704545fdf9bSopenharmony_ci            }
705545fdf9bSopenharmony_ci        }
706545fdf9bSopenharmony_ci    }
707545fdf9bSopenharmony_ci    APP_LOGI("end");
708545fdf9bSopenharmony_ci    return result;
709545fdf9bSopenharmony_ci}
710545fdf9bSopenharmony_ci
711545fdf9bSopenharmony_ciErrCode BundleManagerShellCommand::GetBundlePath(const std::string& param,
712545fdf9bSopenharmony_ci    std::vector<std::string>& bundlePaths) const
713545fdf9bSopenharmony_ci{
714545fdf9bSopenharmony_ci    if (param.empty()) {
715545fdf9bSopenharmony_ci        return OHOS::ERR_INVALID_VALUE;
716545fdf9bSopenharmony_ci    }
717545fdf9bSopenharmony_ci    if (param == "-r" || param == "--replace" || param == "-p" ||
718545fdf9bSopenharmony_ci        param == "--bundle-path" || param == "-u" || param == "--user-id" ||
719545fdf9bSopenharmony_ci        param == "-w" || param == "--waitting-time") {
720545fdf9bSopenharmony_ci        return OHOS::ERR_INVALID_VALUE;
721545fdf9bSopenharmony_ci    }
722545fdf9bSopenharmony_ci    bundlePaths.emplace_back(param);
723545fdf9bSopenharmony_ci    return OHOS::ERR_OK;
724545fdf9bSopenharmony_ci}
725545fdf9bSopenharmony_ci
726545fdf9bSopenharmony_ciErrCode BundleManagerShellCommand::RunAsUninstallCommand()
727545fdf9bSopenharmony_ci{
728545fdf9bSopenharmony_ci    APP_LOGI("begin to RunAsUninstallCommand");
729545fdf9bSopenharmony_ci    int result = OHOS::ERR_OK;
730545fdf9bSopenharmony_ci    int counter = 0;
731545fdf9bSopenharmony_ci    std::string bundleName = "";
732545fdf9bSopenharmony_ci    std::string moduleName = "";
733545fdf9bSopenharmony_ci    int32_t userId = BundleCommandCommon::GetCurrentUserId(Constants::UNSPECIFIED_USERID);
734545fdf9bSopenharmony_ci    bool isKeepData = false;
735545fdf9bSopenharmony_ci    bool isShared = false;
736545fdf9bSopenharmony_ci    int32_t versionCode = Constants::ALL_VERSIONCODE;
737545fdf9bSopenharmony_ci    while (true) {
738545fdf9bSopenharmony_ci        counter++;
739545fdf9bSopenharmony_ci        int32_t option = getopt_long(argc_, argv_, UNINSTALL_OPTIONS.c_str(), UNINSTALL_LONG_OPTIONS, nullptr);
740545fdf9bSopenharmony_ci        APP_LOGD("option: %{public}d, optopt: %{public}d, optind: %{public}d", option, optopt, optind);
741545fdf9bSopenharmony_ci        if (optind < 0 || optind > argc_) {
742545fdf9bSopenharmony_ci            return OHOS::ERR_INVALID_VALUE;
743545fdf9bSopenharmony_ci        }
744545fdf9bSopenharmony_ci        if (option == -1) {
745545fdf9bSopenharmony_ci            if (counter == 1) {
746545fdf9bSopenharmony_ci                // When scanning the first argument
747545fdf9bSopenharmony_ci                if (strcmp(argv_[optind], cmd_.c_str()) == 0) {
748545fdf9bSopenharmony_ci                    // 'bm uninstall' with no option: bm uninstall
749545fdf9bSopenharmony_ci                    // 'bm uninstall' with a wrong argument: bm uninstall xxx
750545fdf9bSopenharmony_ci                    APP_LOGD("'bm uninstall' %{public}s", HELP_MSG_NO_OPTION.c_str());
751545fdf9bSopenharmony_ci                    resultReceiver_.append(HELP_MSG_NO_OPTION + "\n");
752545fdf9bSopenharmony_ci                    result = OHOS::ERR_INVALID_VALUE;
753545fdf9bSopenharmony_ci                }
754545fdf9bSopenharmony_ci            }
755545fdf9bSopenharmony_ci            break;
756545fdf9bSopenharmony_ci        }
757545fdf9bSopenharmony_ci
758545fdf9bSopenharmony_ci        if (option == '?') {
759545fdf9bSopenharmony_ci            switch (optopt) {
760545fdf9bSopenharmony_ci                case 'n': {
761545fdf9bSopenharmony_ci                    // 'bm uninstall -n' with no argument: bm uninstall -n
762545fdf9bSopenharmony_ci                    // 'bm uninstall --bundle-name' with no argument: bm uninstall --bundle-name
763545fdf9bSopenharmony_ci                    APP_LOGD("'bm uninstall -n' with no argument.");
764545fdf9bSopenharmony_ci                    resultReceiver_.append(STRING_REQUIRE_CORRECT_VALUE);
765545fdf9bSopenharmony_ci                    result = OHOS::ERR_INVALID_VALUE;
766545fdf9bSopenharmony_ci                    break;
767545fdf9bSopenharmony_ci                }
768545fdf9bSopenharmony_ci                case 'm': {
769545fdf9bSopenharmony_ci                    // 'bm uninstall -m' with no argument: bm uninstall -m
770545fdf9bSopenharmony_ci                    // 'bm uninstall --module-name' with no argument: bm uninstall --module-name
771545fdf9bSopenharmony_ci                    APP_LOGD("'bm uninstall -m' with no argument.");
772545fdf9bSopenharmony_ci                    resultReceiver_.append(STRING_REQUIRE_CORRECT_VALUE);
773545fdf9bSopenharmony_ci                    result = OHOS::ERR_INVALID_VALUE;
774545fdf9bSopenharmony_ci                    break;
775545fdf9bSopenharmony_ci                }
776545fdf9bSopenharmony_ci                case 'u': {
777545fdf9bSopenharmony_ci                    // 'bm uninstall -n <bundleName> -u userId'
778545fdf9bSopenharmony_ci                    // 'bm uninstall --bundle-name <bundleName> --user-id userId'
779545fdf9bSopenharmony_ci                    APP_LOGD("'bm uninstall -u' with no argument.");
780545fdf9bSopenharmony_ci                    resultReceiver_.append(STRING_REQUIRE_CORRECT_VALUE);
781545fdf9bSopenharmony_ci                    result = OHOS::ERR_INVALID_VALUE;
782545fdf9bSopenharmony_ci                    break;
783545fdf9bSopenharmony_ci                }
784545fdf9bSopenharmony_ci                case 'k': {
785545fdf9bSopenharmony_ci                    // 'bm uninstall -n <bundleName> -k'
786545fdf9bSopenharmony_ci                    // 'bm uninstall --bundle-name <bundleName> --keep-data'
787545fdf9bSopenharmony_ci                    APP_LOGD("'bm uninstall -k'");
788545fdf9bSopenharmony_ci                    isKeepData = true;
789545fdf9bSopenharmony_ci                    break;
790545fdf9bSopenharmony_ci                }
791545fdf9bSopenharmony_ci                case 's': {
792545fdf9bSopenharmony_ci                    APP_LOGD("'bm uninstall -s'");
793545fdf9bSopenharmony_ci                    isShared = true;
794545fdf9bSopenharmony_ci                    break;
795545fdf9bSopenharmony_ci                }
796545fdf9bSopenharmony_ci                case 'v': {
797545fdf9bSopenharmony_ci                    APP_LOGD("'bm uninstall -v'");
798545fdf9bSopenharmony_ci                    resultReceiver_.append(STRING_REQUIRE_CORRECT_VALUE);
799545fdf9bSopenharmony_ci                    result = OHOS::ERR_INVALID_VALUE;
800545fdf9bSopenharmony_ci                    break;
801545fdf9bSopenharmony_ci                }
802545fdf9bSopenharmony_ci                default: {
803545fdf9bSopenharmony_ci                    // 'bm uninstall' with an unknown option: bm uninstall -x
804545fdf9bSopenharmony_ci                    // 'bm uninstall' with an unknown option: bm uninstall -xxx
805545fdf9bSopenharmony_ci                    std::string unknownOption = "";
806545fdf9bSopenharmony_ci                    std::string unknownOptionMsg = GetUnknownOptionMsg(unknownOption);
807545fdf9bSopenharmony_ci                    APP_LOGD("'bm uninstall' with an unknown option.");
808545fdf9bSopenharmony_ci                    resultReceiver_.append(unknownOptionMsg);
809545fdf9bSopenharmony_ci                    result = OHOS::ERR_INVALID_VALUE;
810545fdf9bSopenharmony_ci                    break;
811545fdf9bSopenharmony_ci                }
812545fdf9bSopenharmony_ci            }
813545fdf9bSopenharmony_ci            break;
814545fdf9bSopenharmony_ci        }
815545fdf9bSopenharmony_ci
816545fdf9bSopenharmony_ci        switch (option) {
817545fdf9bSopenharmony_ci            case 'h': {
818545fdf9bSopenharmony_ci                // 'bm uninstall -h'
819545fdf9bSopenharmony_ci                // 'bm uninstall --help'
820545fdf9bSopenharmony_ci                APP_LOGD("'bm uninstall %{public}s'", argv_[optind - 1]);
821545fdf9bSopenharmony_ci                result = OHOS::ERR_INVALID_VALUE;
822545fdf9bSopenharmony_ci                break;
823545fdf9bSopenharmony_ci            }
824545fdf9bSopenharmony_ci            case 'n': {
825545fdf9bSopenharmony_ci                // 'bm uninstall -n xxx'
826545fdf9bSopenharmony_ci                // 'bm uninstall --bundle-name xxx'
827545fdf9bSopenharmony_ci                APP_LOGD("'bm uninstall %{public}s %{public}s'", argv_[optind - OFFSET_REQUIRED_ARGUMENT], optarg);
828545fdf9bSopenharmony_ci                bundleName = optarg;
829545fdf9bSopenharmony_ci                break;
830545fdf9bSopenharmony_ci            }
831545fdf9bSopenharmony_ci            case 'm': {
832545fdf9bSopenharmony_ci                // 'bm uninstall -m xxx'
833545fdf9bSopenharmony_ci                // 'bm uninstall --module-name xxx'
834545fdf9bSopenharmony_ci                APP_LOGD("'bm uninstall %{public}s %{public}s'", argv_[optind - OFFSET_REQUIRED_ARGUMENT], optarg);
835545fdf9bSopenharmony_ci                moduleName = optarg;
836545fdf9bSopenharmony_ci                break;
837545fdf9bSopenharmony_ci            }
838545fdf9bSopenharmony_ci            case 'u': {
839545fdf9bSopenharmony_ci                // 'bm uninstall -n <bundleName> -u userId'
840545fdf9bSopenharmony_ci                // 'bm uninstall --bundle-name <bundleName> --user-id userId'
841545fdf9bSopenharmony_ci                APP_LOGW("'bm uninstall -u only support user 0'");
842545fdf9bSopenharmony_ci                if (!OHOS::StrToInt(optarg, userId) || userId < 0) {
843545fdf9bSopenharmony_ci                    APP_LOGE("bm uninstall with error userId %{private}s", optarg);
844545fdf9bSopenharmony_ci                    resultReceiver_.append(STRING_REQUIRE_CORRECT_VALUE);
845545fdf9bSopenharmony_ci                    return OHOS::ERR_INVALID_VALUE;
846545fdf9bSopenharmony_ci                }
847545fdf9bSopenharmony_ci                if (userId != Constants::DEFAULT_USERID) {
848545fdf9bSopenharmony_ci                    userId = BundleCommandCommon::GetCurrentUserId(Constants::UNSPECIFIED_USERID);
849545fdf9bSopenharmony_ci                }
850545fdf9bSopenharmony_ci                break;
851545fdf9bSopenharmony_ci            }
852545fdf9bSopenharmony_ci            case 'k': {
853545fdf9bSopenharmony_ci                // 'bm uninstall -n <bundleName> -k'
854545fdf9bSopenharmony_ci                // 'bm uninstall --bundle-name <bundleName> --keep-data'
855545fdf9bSopenharmony_ci                APP_LOGD("'bm uninstall %{public}s'", argv_[optind - OFFSET_REQUIRED_ARGUMENT]);
856545fdf9bSopenharmony_ci                isKeepData = true;
857545fdf9bSopenharmony_ci                break;
858545fdf9bSopenharmony_ci            }
859545fdf9bSopenharmony_ci            case 's': {
860545fdf9bSopenharmony_ci                APP_LOGD("'bm uninstall -s'");
861545fdf9bSopenharmony_ci                isShared = true;
862545fdf9bSopenharmony_ci                break;
863545fdf9bSopenharmony_ci            }
864545fdf9bSopenharmony_ci            case 'v': {
865545fdf9bSopenharmony_ci                APP_LOGD("'bm uninstall %{public}s %{public}s'", argv_[optind - OFFSET_REQUIRED_ARGUMENT], optarg);
866545fdf9bSopenharmony_ci                if (!OHOS::StrToInt(optarg, versionCode) || versionCode < 0) {
867545fdf9bSopenharmony_ci                    APP_LOGE("bm uninstall with error versionCode %{private}s", optarg);
868545fdf9bSopenharmony_ci                    resultReceiver_.append(STRING_REQUIRE_CORRECT_VALUE);
869545fdf9bSopenharmony_ci                    return OHOS::ERR_INVALID_VALUE;
870545fdf9bSopenharmony_ci                }
871545fdf9bSopenharmony_ci                break;
872545fdf9bSopenharmony_ci            }
873545fdf9bSopenharmony_ci            default: {
874545fdf9bSopenharmony_ci                result = OHOS::ERR_INVALID_VALUE;
875545fdf9bSopenharmony_ci                break;
876545fdf9bSopenharmony_ci            }
877545fdf9bSopenharmony_ci        }
878545fdf9bSopenharmony_ci    }
879545fdf9bSopenharmony_ci
880545fdf9bSopenharmony_ci    if (result == OHOS::ERR_OK) {
881545fdf9bSopenharmony_ci        if (resultReceiver_ == "" && bundleName.size() == 0) {
882545fdf9bSopenharmony_ci            // 'bm uninstall ...' with no bundle name option
883545fdf9bSopenharmony_ci            APP_LOGD("'bm uninstall' with bundle name option.");
884545fdf9bSopenharmony_ci            resultReceiver_.append(HELP_MSG_NO_BUNDLE_NAME_OPTION + "\n");
885545fdf9bSopenharmony_ci            result = OHOS::ERR_INVALID_VALUE;
886545fdf9bSopenharmony_ci        }
887545fdf9bSopenharmony_ci    }
888545fdf9bSopenharmony_ci    if (result != OHOS::ERR_OK) {
889545fdf9bSopenharmony_ci        resultReceiver_.append(HELP_MSG_UNINSTALL);
890545fdf9bSopenharmony_ci        return result;
891545fdf9bSopenharmony_ci    }
892545fdf9bSopenharmony_ci
893545fdf9bSopenharmony_ci    if (isShared) {
894545fdf9bSopenharmony_ci        UninstallParam uninstallParam;
895545fdf9bSopenharmony_ci        uninstallParam.bundleName = bundleName;
896545fdf9bSopenharmony_ci        uninstallParam.versionCode = versionCode;
897545fdf9bSopenharmony_ci        APP_LOGE("version code is %{public}d", versionCode);
898545fdf9bSopenharmony_ci        int32_t uninstallResult = UninstallSharedOperation(uninstallParam);
899545fdf9bSopenharmony_ci        if (uninstallResult == OHOS::ERR_OK) {
900545fdf9bSopenharmony_ci            resultReceiver_ = STRING_UNINSTALL_BUNDLE_OK + "\n";
901545fdf9bSopenharmony_ci        } else {
902545fdf9bSopenharmony_ci            resultReceiver_ = STRING_UNINSTALL_BUNDLE_NG + "\n";
903545fdf9bSopenharmony_ci            resultReceiver_.append(GetMessageFromCode(uninstallResult));
904545fdf9bSopenharmony_ci        }
905545fdf9bSopenharmony_ci    } else {
906545fdf9bSopenharmony_ci        InstallParam installParam;
907545fdf9bSopenharmony_ci        installParam.userId = userId;
908545fdf9bSopenharmony_ci        installParam.isKeepData = isKeepData;
909545fdf9bSopenharmony_ci        int32_t uninstallResult = UninstallOperation(bundleName, moduleName, installParam);
910545fdf9bSopenharmony_ci        if (uninstallResult == OHOS::ERR_OK) {
911545fdf9bSopenharmony_ci            resultReceiver_ = STRING_UNINSTALL_BUNDLE_OK + "\n";
912545fdf9bSopenharmony_ci        } else {
913545fdf9bSopenharmony_ci            resultReceiver_ = STRING_UNINSTALL_BUNDLE_NG + "\n";
914545fdf9bSopenharmony_ci            resultReceiver_.append(GetMessageFromCode(uninstallResult));
915545fdf9bSopenharmony_ci        }
916545fdf9bSopenharmony_ci    }
917545fdf9bSopenharmony_ci    APP_LOGI("end");
918545fdf9bSopenharmony_ci    return result;
919545fdf9bSopenharmony_ci}
920545fdf9bSopenharmony_ci
921545fdf9bSopenharmony_ciErrCode BundleManagerShellCommand::RunAsDumpCommand()
922545fdf9bSopenharmony_ci{
923545fdf9bSopenharmony_ci    APP_LOGI("begin to RunAsDumpCommand");
924545fdf9bSopenharmony_ci    int result = OHOS::ERR_OK;
925545fdf9bSopenharmony_ci    int counter = 0;
926545fdf9bSopenharmony_ci    std::string bundleName = "";
927545fdf9bSopenharmony_ci    bool bundleDumpAll = false;
928545fdf9bSopenharmony_ci    bool bundleDumpInfo = false;
929545fdf9bSopenharmony_ci    bool bundleDumpShortcut = false;
930545fdf9bSopenharmony_ci    bool bundleDumpDistributedBundleInfo = false;
931545fdf9bSopenharmony_ci    std::string deviceId = "";
932545fdf9bSopenharmony_ci    int32_t userId = BundleCommandCommon::GetCurrentUserId(Constants::UNSPECIFIED_USERID);
933545fdf9bSopenharmony_ci    while (true) {
934545fdf9bSopenharmony_ci        counter++;
935545fdf9bSopenharmony_ci        int32_t option = getopt_long(argc_, argv_, SHORT_OPTIONS_DUMP.c_str(), LONG_OPTIONS_DUMP, nullptr);
936545fdf9bSopenharmony_ci        APP_LOGD("option: %{public}d, optopt: %{public}d, optind: %{public}d", option, optopt, optind);
937545fdf9bSopenharmony_ci        if (optind < 0 || optind > argc_) {
938545fdf9bSopenharmony_ci            return OHOS::ERR_INVALID_VALUE;
939545fdf9bSopenharmony_ci        }
940545fdf9bSopenharmony_ci        if (option == -1) {
941545fdf9bSopenharmony_ci            if (counter == 1) {
942545fdf9bSopenharmony_ci                // When scanning the first argument
943545fdf9bSopenharmony_ci                if (strcmp(argv_[optind], cmd_.c_str()) == 0) {
944545fdf9bSopenharmony_ci                    // 'bm dump' with no option: bm dump
945545fdf9bSopenharmony_ci                    // 'bm dump' with a wrong argument: bm dump xxx
946545fdf9bSopenharmony_ci                    APP_LOGD("'bm dump' %{public}s", HELP_MSG_NO_OPTION.c_str());
947545fdf9bSopenharmony_ci                    resultReceiver_.append(HELP_MSG_NO_OPTION + "\n");
948545fdf9bSopenharmony_ci                    result = OHOS::ERR_INVALID_VALUE;
949545fdf9bSopenharmony_ci                }
950545fdf9bSopenharmony_ci            }
951545fdf9bSopenharmony_ci            break;
952545fdf9bSopenharmony_ci        }
953545fdf9bSopenharmony_ci        if (option == '?') {
954545fdf9bSopenharmony_ci            switch (optopt) {
955545fdf9bSopenharmony_ci                case 'n': {
956545fdf9bSopenharmony_ci                    // 'bm dump -n' with no argument: bm dump -n
957545fdf9bSopenharmony_ci                    // 'bm dump --bundle-name' with no argument: bm dump --bundle-name
958545fdf9bSopenharmony_ci                    APP_LOGD("'bm dump -n' with no argument.");
959545fdf9bSopenharmony_ci                    resultReceiver_.append(STRING_REQUIRE_CORRECT_VALUE);
960545fdf9bSopenharmony_ci                    result = OHOS::ERR_INVALID_VALUE;
961545fdf9bSopenharmony_ci                    break;
962545fdf9bSopenharmony_ci                }
963545fdf9bSopenharmony_ci                case 'u': {
964545fdf9bSopenharmony_ci                    // 'bm dump -u' with no argument: bm dump -u
965545fdf9bSopenharmony_ci                    // 'bm dump --user-id' with no argument: bm dump --user-id
966545fdf9bSopenharmony_ci                    APP_LOGD("'bm dump -u' with no argument.");
967545fdf9bSopenharmony_ci                    resultReceiver_.append(STRING_REQUIRE_CORRECT_VALUE);
968545fdf9bSopenharmony_ci                    result = OHOS::ERR_INVALID_VALUE;
969545fdf9bSopenharmony_ci                    break;
970545fdf9bSopenharmony_ci                }
971545fdf9bSopenharmony_ci                case 'd': {
972545fdf9bSopenharmony_ci                    // 'bm dump -d' with no argument: bm dump -d
973545fdf9bSopenharmony_ci                    // 'bm dump --device-id' with no argument: bm dump --device-id
974545fdf9bSopenharmony_ci                    APP_LOGD("'bm dump -d' with no argument.");
975545fdf9bSopenharmony_ci                    resultReceiver_.append(STRING_REQUIRE_CORRECT_VALUE);
976545fdf9bSopenharmony_ci                    result = OHOS::ERR_INVALID_VALUE;
977545fdf9bSopenharmony_ci                    break;
978545fdf9bSopenharmony_ci                }
979545fdf9bSopenharmony_ci                default: {
980545fdf9bSopenharmony_ci                    // 'bm dump' with an unknown option: bm dump -x
981545fdf9bSopenharmony_ci                    // 'bm dump' with an unknown option: bm dump -xxx
982545fdf9bSopenharmony_ci                    std::string unknownOption = "";
983545fdf9bSopenharmony_ci                    std::string unknownOptionMsg = GetUnknownOptionMsg(unknownOption);
984545fdf9bSopenharmony_ci                    APP_LOGD("'bm dump' with an unknown option.");
985545fdf9bSopenharmony_ci                    resultReceiver_.append(unknownOptionMsg);
986545fdf9bSopenharmony_ci                    result = OHOS::ERR_INVALID_VALUE;
987545fdf9bSopenharmony_ci                    break;
988545fdf9bSopenharmony_ci                }
989545fdf9bSopenharmony_ci            }
990545fdf9bSopenharmony_ci            break;
991545fdf9bSopenharmony_ci        }
992545fdf9bSopenharmony_ci        switch (option) {
993545fdf9bSopenharmony_ci            case 'h': {
994545fdf9bSopenharmony_ci                // 'bm dump -h'
995545fdf9bSopenharmony_ci                // 'bm dump --help'
996545fdf9bSopenharmony_ci                APP_LOGD("'bm dump %{public}s'", argv_[optind - 1]);
997545fdf9bSopenharmony_ci                result = OHOS::ERR_INVALID_VALUE;
998545fdf9bSopenharmony_ci                break;
999545fdf9bSopenharmony_ci            }
1000545fdf9bSopenharmony_ci            case 'a': {
1001545fdf9bSopenharmony_ci                // 'bm dump -a'
1002545fdf9bSopenharmony_ci                // 'bm dump --all'
1003545fdf9bSopenharmony_ci                APP_LOGD("'bm dump %{public}s'", argv_[optind - 1]);
1004545fdf9bSopenharmony_ci                bundleDumpAll = true;
1005545fdf9bSopenharmony_ci                break;
1006545fdf9bSopenharmony_ci            }
1007545fdf9bSopenharmony_ci            case 'n': {
1008545fdf9bSopenharmony_ci                // 'bm dump -n xxx'
1009545fdf9bSopenharmony_ci                // 'bm dump --bundle-name xxx'
1010545fdf9bSopenharmony_ci                APP_LOGD("'bm dump %{public}s %{public}s'", argv_[optind - OFFSET_REQUIRED_ARGUMENT], optarg);
1011545fdf9bSopenharmony_ci                bundleName = optarg;
1012545fdf9bSopenharmony_ci                bundleDumpInfo = true;
1013545fdf9bSopenharmony_ci                break;
1014545fdf9bSopenharmony_ci            }
1015545fdf9bSopenharmony_ci            case 's': {
1016545fdf9bSopenharmony_ci                // 'bm dump -n xxx -s'
1017545fdf9bSopenharmony_ci                // 'bm dump --bundle-name xxx --shortcut-info'
1018545fdf9bSopenharmony_ci                APP_LOGD("'bm dump %{public}s %{public}s'", argv_[optind - OFFSET_REQUIRED_ARGUMENT], optarg);
1019545fdf9bSopenharmony_ci                bundleDumpShortcut = true;
1020545fdf9bSopenharmony_ci                break;
1021545fdf9bSopenharmony_ci            }
1022545fdf9bSopenharmony_ci            case 'u': {
1023545fdf9bSopenharmony_ci                // 'bm dump -n <bundleName> -u userId'
1024545fdf9bSopenharmony_ci                // 'bm dump --bundle-name <bundleName> --user-id userId'
1025545fdf9bSopenharmony_ci                APP_LOGW("'bm dump -u is not supported'");
1026545fdf9bSopenharmony_ci                break;
1027545fdf9bSopenharmony_ci            }
1028545fdf9bSopenharmony_ci            case 'd': {
1029545fdf9bSopenharmony_ci                // 'bm dump -n <bundleName> -d deviceId'
1030545fdf9bSopenharmony_ci                // 'bm dump --bundle-name <bundleName> --device-id deviceId'
1031545fdf9bSopenharmony_ci                APP_LOGD("'bm dump %{public}s %{public}s'", argv_[optind - OFFSET_REQUIRED_ARGUMENT], optarg);
1032545fdf9bSopenharmony_ci                deviceId = optarg;
1033545fdf9bSopenharmony_ci                bundleDumpDistributedBundleInfo = true;
1034545fdf9bSopenharmony_ci                break;
1035545fdf9bSopenharmony_ci            }
1036545fdf9bSopenharmony_ci            default: {
1037545fdf9bSopenharmony_ci                result = OHOS::ERR_INVALID_VALUE;
1038545fdf9bSopenharmony_ci                break;
1039545fdf9bSopenharmony_ci            }
1040545fdf9bSopenharmony_ci        }
1041545fdf9bSopenharmony_ci    }
1042545fdf9bSopenharmony_ci    if (result == OHOS::ERR_OK) {
1043545fdf9bSopenharmony_ci        if ((resultReceiver_ == "") && bundleDumpShortcut && (bundleName.size() == 0)) {
1044545fdf9bSopenharmony_ci            // 'bm dump -s ...' with no bundle name option
1045545fdf9bSopenharmony_ci            APP_LOGD("'bm dump -s' with no bundle name option.");
1046545fdf9bSopenharmony_ci            resultReceiver_.append(HELP_MSG_NO_BUNDLE_NAME_OPTION + "\n");
1047545fdf9bSopenharmony_ci            result = OHOS::ERR_INVALID_VALUE;
1048545fdf9bSopenharmony_ci        }
1049545fdf9bSopenharmony_ci        if ((resultReceiver_ == "") && bundleDumpDistributedBundleInfo && (bundleName.size() == 0)) {
1050545fdf9bSopenharmony_ci            // 'bm dump d ...' with no bundle name option
1051545fdf9bSopenharmony_ci            APP_LOGD("'bm dump -d' with no bundle name option.");
1052545fdf9bSopenharmony_ci            resultReceiver_.append(HELP_MSG_NO_BUNDLE_NAME_OPTION + "\n");
1053545fdf9bSopenharmony_ci            result = OHOS::ERR_INVALID_VALUE;
1054545fdf9bSopenharmony_ci        }
1055545fdf9bSopenharmony_ci    }
1056545fdf9bSopenharmony_ci    if (result != OHOS::ERR_OK) {
1057545fdf9bSopenharmony_ci        resultReceiver_.append(HELP_MSG_DUMP);
1058545fdf9bSopenharmony_ci    } else {
1059545fdf9bSopenharmony_ci        std::string dumpResults = "";
1060545fdf9bSopenharmony_ci        APP_LOGD("dumpResults: %{public}s", dumpResults.c_str());
1061545fdf9bSopenharmony_ci        if (bundleDumpShortcut) {
1062545fdf9bSopenharmony_ci            dumpResults = DumpShortcutInfos(bundleName, userId);
1063545fdf9bSopenharmony_ci        } else if (bundleDumpDistributedBundleInfo) {
1064545fdf9bSopenharmony_ci            dumpResults = DumpDistributedBundleInfo(deviceId, bundleName);
1065545fdf9bSopenharmony_ci        } else if (bundleDumpAll) {
1066545fdf9bSopenharmony_ci            dumpResults = DumpBundleList(userId);
1067545fdf9bSopenharmony_ci        } else if (bundleDumpInfo) {
1068545fdf9bSopenharmony_ci            dumpResults = DumpBundleInfo(bundleName, userId);
1069545fdf9bSopenharmony_ci        }
1070545fdf9bSopenharmony_ci        if (dumpResults.empty() || (dumpResults == "")) {
1071545fdf9bSopenharmony_ci            dumpResults = HELP_MSG_DUMP_FAILED + "\n";
1072545fdf9bSopenharmony_ci        }
1073545fdf9bSopenharmony_ci        resultReceiver_.append(dumpResults);
1074545fdf9bSopenharmony_ci    }
1075545fdf9bSopenharmony_ci    APP_LOGI("end");
1076545fdf9bSopenharmony_ci    return result;
1077545fdf9bSopenharmony_ci}
1078545fdf9bSopenharmony_ci
1079545fdf9bSopenharmony_ciErrCode BundleManagerShellCommand::RunAsCleanCommand()
1080545fdf9bSopenharmony_ci{
1081545fdf9bSopenharmony_ci    int32_t mode = GetIntParameter(IS_ROOT_MODE_PARAM, USER_MODE);
1082545fdf9bSopenharmony_ci    bool isDeveloperMode = system::GetBoolParameter(IS_DEVELOPER_MODE_PARAM, false);
1083545fdf9bSopenharmony_ci    if (mode != ROOT_MODE && !isDeveloperMode) {
1084545fdf9bSopenharmony_ci        APP_LOGI("in user mode but not in developer mode");
1085545fdf9bSopenharmony_ci        return ERR_OK;
1086545fdf9bSopenharmony_ci    }
1087545fdf9bSopenharmony_ci
1088545fdf9bSopenharmony_ci    APP_LOGI("begin to RunAsCleanCommand");
1089545fdf9bSopenharmony_ci    int32_t result = OHOS::ERR_OK;
1090545fdf9bSopenharmony_ci    int32_t counter = 0;
1091545fdf9bSopenharmony_ci    int32_t userId = BundleCommandCommon::GetCurrentUserId(Constants::UNSPECIFIED_USERID);
1092545fdf9bSopenharmony_ci    int32_t appIndex = 0;
1093545fdf9bSopenharmony_ci    bool cleanCache = false;
1094545fdf9bSopenharmony_ci    bool cleanData = false;
1095545fdf9bSopenharmony_ci    std::string bundleName = "";
1096545fdf9bSopenharmony_ci    while (true) {
1097545fdf9bSopenharmony_ci        counter++;
1098545fdf9bSopenharmony_ci        int32_t option = getopt_long(argc_, argv_, SHORT_OPTIONS.c_str(), LONG_OPTIONS, nullptr);
1099545fdf9bSopenharmony_ci        APP_LOGD("option: %{public}d, optopt: %{public}d, optind: %{public}d", option, optopt, optind);
1100545fdf9bSopenharmony_ci        if (optind < 0 || optind > argc_) {
1101545fdf9bSopenharmony_ci            return OHOS::ERR_INVALID_VALUE;
1102545fdf9bSopenharmony_ci        }
1103545fdf9bSopenharmony_ci        if (option == -1) {
1104545fdf9bSopenharmony_ci            if (counter == 1) {
1105545fdf9bSopenharmony_ci                // When scanning the first argument
1106545fdf9bSopenharmony_ci                if (strcmp(argv_[optind], cmd_.c_str()) == 0) {
1107545fdf9bSopenharmony_ci                    // 'bm clean' with no option: bm clean
1108545fdf9bSopenharmony_ci                    // 'bm clean' with a wrong argument: bm clean xxx
1109545fdf9bSopenharmony_ci                    APP_LOGD("'bm clean' %{public}s", HELP_MSG_NO_OPTION.c_str());
1110545fdf9bSopenharmony_ci
1111545fdf9bSopenharmony_ci                    resultReceiver_.append(HELP_MSG_NO_OPTION + "\n");
1112545fdf9bSopenharmony_ci                    result = OHOS::ERR_INVALID_VALUE;
1113545fdf9bSopenharmony_ci                }
1114545fdf9bSopenharmony_ci            }
1115545fdf9bSopenharmony_ci            break;
1116545fdf9bSopenharmony_ci        }
1117545fdf9bSopenharmony_ci
1118545fdf9bSopenharmony_ci        if (option == '?') {
1119545fdf9bSopenharmony_ci            switch (optopt) {
1120545fdf9bSopenharmony_ci                case 'n': {
1121545fdf9bSopenharmony_ci                    // 'bm clean -n' with no argument: bm clean -n
1122545fdf9bSopenharmony_ci                    // 'bm clean --bundle-name' with no argument: bm clean --bundle-name
1123545fdf9bSopenharmony_ci                    APP_LOGD("'bm clean -n' with no argument.");
1124545fdf9bSopenharmony_ci                    resultReceiver_.append(STRING_REQUIRE_CORRECT_VALUE);
1125545fdf9bSopenharmony_ci                    result = OHOS::ERR_INVALID_VALUE;
1126545fdf9bSopenharmony_ci                    break;
1127545fdf9bSopenharmony_ci                }
1128545fdf9bSopenharmony_ci                case 'u': {
1129545fdf9bSopenharmony_ci                    // 'bm clean -u' with no argument: bm clean -u
1130545fdf9bSopenharmony_ci                    // 'bm clean --user-id' with no argument: bm clean --user-id
1131545fdf9bSopenharmony_ci                    APP_LOGD("'bm clean -u' with no argument.");
1132545fdf9bSopenharmony_ci                    resultReceiver_.append(STRING_REQUIRE_CORRECT_VALUE);
1133545fdf9bSopenharmony_ci                    result = OHOS::ERR_INVALID_VALUE;
1134545fdf9bSopenharmony_ci                    break;
1135545fdf9bSopenharmony_ci                }
1136545fdf9bSopenharmony_ci                case 'i': {
1137545fdf9bSopenharmony_ci                    // 'bm clean -i' with no argument: bm clean -i
1138545fdf9bSopenharmony_ci                    // 'bm clean --app-index' with no argument: bm clean --app-index
1139545fdf9bSopenharmony_ci                    APP_LOGD("'bm clean -i' with no argument.");
1140545fdf9bSopenharmony_ci                    resultReceiver_.append(STRING_REQUIRE_CORRECT_VALUE);
1141545fdf9bSopenharmony_ci                    result = OHOS::ERR_INVALID_VALUE;
1142545fdf9bSopenharmony_ci                    break;
1143545fdf9bSopenharmony_ci                }
1144545fdf9bSopenharmony_ci                default: {
1145545fdf9bSopenharmony_ci                    // 'bm clean' with an unknown option: bm clear -x
1146545fdf9bSopenharmony_ci                    // 'bm clean' with an unknown option: bm clear -xxx
1147545fdf9bSopenharmony_ci                    std::string unknownOption = "";
1148545fdf9bSopenharmony_ci                    std::string unknownOptionMsg = GetUnknownOptionMsg(unknownOption);
1149545fdf9bSopenharmony_ci                    APP_LOGD("'bm clean' with an unknown option.");
1150545fdf9bSopenharmony_ci                    resultReceiver_.append(unknownOptionMsg);
1151545fdf9bSopenharmony_ci                    result = OHOS::ERR_INVALID_VALUE;
1152545fdf9bSopenharmony_ci                    break;
1153545fdf9bSopenharmony_ci                }
1154545fdf9bSopenharmony_ci            }
1155545fdf9bSopenharmony_ci            break;
1156545fdf9bSopenharmony_ci        }
1157545fdf9bSopenharmony_ci
1158545fdf9bSopenharmony_ci        switch (option) {
1159545fdf9bSopenharmony_ci            case 'h': {
1160545fdf9bSopenharmony_ci                // 'bm clean -h'
1161545fdf9bSopenharmony_ci                // 'bm clean --help'
1162545fdf9bSopenharmony_ci                APP_LOGD("'bm clean %{public}s'", argv_[optind - 1]);
1163545fdf9bSopenharmony_ci                result = OHOS::ERR_INVALID_VALUE;
1164545fdf9bSopenharmony_ci                break;
1165545fdf9bSopenharmony_ci            }
1166545fdf9bSopenharmony_ci            case 'n': {
1167545fdf9bSopenharmony_ci                // 'bm clean -n xxx'
1168545fdf9bSopenharmony_ci                // 'bm clean --bundle-name xxx'
1169545fdf9bSopenharmony_ci                APP_LOGD("'bm clean %{public}s %{public}s'", argv_[optind - OFFSET_REQUIRED_ARGUMENT], optarg);
1170545fdf9bSopenharmony_ci                bundleName = optarg;
1171545fdf9bSopenharmony_ci                break;
1172545fdf9bSopenharmony_ci            }
1173545fdf9bSopenharmony_ci            case 'c': {
1174545fdf9bSopenharmony_ci                // 'bm clean -c'
1175545fdf9bSopenharmony_ci                // 'bm clean --cache'
1176545fdf9bSopenharmony_ci                APP_LOGD("'bm clean %{public}s'", argv_[optind - OFFSET_REQUIRED_ARGUMENT]);
1177545fdf9bSopenharmony_ci                cleanCache = cleanData ? false : true;
1178545fdf9bSopenharmony_ci                break;
1179545fdf9bSopenharmony_ci            }
1180545fdf9bSopenharmony_ci            case 'd': {
1181545fdf9bSopenharmony_ci                // 'bm clean -d'
1182545fdf9bSopenharmony_ci                // 'bm clean --data'
1183545fdf9bSopenharmony_ci                APP_LOGD("'bm clean %{public}s '", argv_[optind - OFFSET_REQUIRED_ARGUMENT]);
1184545fdf9bSopenharmony_ci                cleanData = cleanCache ? false : true;
1185545fdf9bSopenharmony_ci                break;
1186545fdf9bSopenharmony_ci            }
1187545fdf9bSopenharmony_ci            case 'u': {
1188545fdf9bSopenharmony_ci                // 'bm clean -u userId'
1189545fdf9bSopenharmony_ci                // 'bm clean --user-id userId'
1190545fdf9bSopenharmony_ci                APP_LOGW("'bm clean -u is not supported'");
1191545fdf9bSopenharmony_ci                break;
1192545fdf9bSopenharmony_ci            }
1193545fdf9bSopenharmony_ci            case 'i': {
1194545fdf9bSopenharmony_ci                if (!OHOS::StrToInt(optarg, appIndex) || (appIndex < 0 || appIndex > INITIAL_SANDBOX_APP_INDEX)) {
1195545fdf9bSopenharmony_ci                    APP_LOGE("bm clean with error appIndex %{private}s", optarg);
1196545fdf9bSopenharmony_ci                    resultReceiver_.append(STRING_REQUIRE_CORRECT_VALUE);
1197545fdf9bSopenharmony_ci                    return OHOS::ERR_INVALID_VALUE;
1198545fdf9bSopenharmony_ci                }
1199545fdf9bSopenharmony_ci                break;
1200545fdf9bSopenharmony_ci            }
1201545fdf9bSopenharmony_ci            default: {
1202545fdf9bSopenharmony_ci                result = OHOS::ERR_INVALID_VALUE;
1203545fdf9bSopenharmony_ci                break;
1204545fdf9bSopenharmony_ci            }
1205545fdf9bSopenharmony_ci        }
1206545fdf9bSopenharmony_ci    }
1207545fdf9bSopenharmony_ci
1208545fdf9bSopenharmony_ci    if (result == OHOS::ERR_OK) {
1209545fdf9bSopenharmony_ci        if (resultReceiver_ == "" && bundleName.size() == 0) {
1210545fdf9bSopenharmony_ci            // 'bm clean ...' with no bundle name option
1211545fdf9bSopenharmony_ci            APP_LOGD("'bm clean' with no bundle name option.");
1212545fdf9bSopenharmony_ci            resultReceiver_.append(HELP_MSG_NO_BUNDLE_NAME_OPTION + "\n");
1213545fdf9bSopenharmony_ci            result = OHOS::ERR_INVALID_VALUE;
1214545fdf9bSopenharmony_ci        }
1215545fdf9bSopenharmony_ci        if (!cleanCache && !cleanData) {
1216545fdf9bSopenharmony_ci            APP_LOGD("'bm clean' with no '-c' or '-d' option.");
1217545fdf9bSopenharmony_ci            resultReceiver_.append(HELP_MSG_NO_DATA_OR_CACHE_OPTION + "\n");
1218545fdf9bSopenharmony_ci            result = OHOS::ERR_INVALID_VALUE;
1219545fdf9bSopenharmony_ci        }
1220545fdf9bSopenharmony_ci    }
1221545fdf9bSopenharmony_ci
1222545fdf9bSopenharmony_ci    if (result != OHOS::ERR_OK) {
1223545fdf9bSopenharmony_ci        resultReceiver_.append(HELP_MSG_CLEAN);
1224545fdf9bSopenharmony_ci    } else {
1225545fdf9bSopenharmony_ci        // bm clean -c
1226545fdf9bSopenharmony_ci        if (cleanCache) {
1227545fdf9bSopenharmony_ci            if (CleanBundleCacheFilesOperation(bundleName, userId, appIndex)) {
1228545fdf9bSopenharmony_ci                resultReceiver_ = STRING_CLEAN_CACHE_BUNDLE_OK + "\n";
1229545fdf9bSopenharmony_ci            } else {
1230545fdf9bSopenharmony_ci                resultReceiver_ = STRING_CLEAN_CACHE_BUNDLE_NG + "\n";
1231545fdf9bSopenharmony_ci            }
1232545fdf9bSopenharmony_ci        }
1233545fdf9bSopenharmony_ci        // bm clean -d
1234545fdf9bSopenharmony_ci        if (cleanData) {
1235545fdf9bSopenharmony_ci            if (CleanBundleDataFilesOperation(bundleName, userId, appIndex)) {
1236545fdf9bSopenharmony_ci                resultReceiver_.append(STRING_CLEAN_DATA_BUNDLE_OK + "\n");
1237545fdf9bSopenharmony_ci            } else {
1238545fdf9bSopenharmony_ci                resultReceiver_.append(STRING_CLEAN_DATA_BUNDLE_NG + "\n");
1239545fdf9bSopenharmony_ci            }
1240545fdf9bSopenharmony_ci        }
1241545fdf9bSopenharmony_ci    }
1242545fdf9bSopenharmony_ci    APP_LOGI("end");
1243545fdf9bSopenharmony_ci    return result;
1244545fdf9bSopenharmony_ci}
1245545fdf9bSopenharmony_ci
1246545fdf9bSopenharmony_ciErrCode BundleManagerShellCommand::RunAsEnableCommand()
1247545fdf9bSopenharmony_ci{
1248545fdf9bSopenharmony_ci    int32_t mode = GetIntParameter(IS_ROOT_MODE_PARAM, USER_MODE);
1249545fdf9bSopenharmony_ci    if (mode != ROOT_MODE) {
1250545fdf9bSopenharmony_ci        APP_LOGI("in user mode");
1251545fdf9bSopenharmony_ci        return ERR_OK;
1252545fdf9bSopenharmony_ci    }
1253545fdf9bSopenharmony_ci    APP_LOGI("begin to RunAsEnableCommand");
1254545fdf9bSopenharmony_ci    int result = OHOS::ERR_OK;
1255545fdf9bSopenharmony_ci    int counter = 0;
1256545fdf9bSopenharmony_ci    std::string bundleName = "";
1257545fdf9bSopenharmony_ci    std::string abilityName = "";
1258545fdf9bSopenharmony_ci    int32_t userId = BundleCommandCommon::GetCurrentUserId(Constants::UNSPECIFIED_USERID);
1259545fdf9bSopenharmony_ci    while (true) {
1260545fdf9bSopenharmony_ci        counter++;
1261545fdf9bSopenharmony_ci        int32_t option = getopt_long(argc_, argv_, SHORT_OPTIONS.c_str(), LONG_OPTIONS, nullptr);
1262545fdf9bSopenharmony_ci        APP_LOGD("option: %{public}d, optopt: %{public}d, optind: %{public}d", option, optopt, optind);
1263545fdf9bSopenharmony_ci        if (optind < 0 || optind > argc_) {
1264545fdf9bSopenharmony_ci            return OHOS::ERR_INVALID_VALUE;
1265545fdf9bSopenharmony_ci        }
1266545fdf9bSopenharmony_ci
1267545fdf9bSopenharmony_ci        if (option == -1) {
1268545fdf9bSopenharmony_ci            if (counter == 1) {
1269545fdf9bSopenharmony_ci                // When scanning the first argument
1270545fdf9bSopenharmony_ci                if (strcmp(argv_[optind], cmd_.c_str()) == 0) {
1271545fdf9bSopenharmony_ci                    // 'bm enable' with no option: bm enable
1272545fdf9bSopenharmony_ci                    // 'bm enable' with a wrong argument: bm enable xxx
1273545fdf9bSopenharmony_ci                    APP_LOGD("'bm enable' with no option.");
1274545fdf9bSopenharmony_ci                    resultReceiver_.append(HELP_MSG_NO_OPTION + "\n");
1275545fdf9bSopenharmony_ci                    result = OHOS::ERR_INVALID_VALUE;
1276545fdf9bSopenharmony_ci                }
1277545fdf9bSopenharmony_ci            }
1278545fdf9bSopenharmony_ci            break;
1279545fdf9bSopenharmony_ci        }
1280545fdf9bSopenharmony_ci
1281545fdf9bSopenharmony_ci        if (option == '?') {
1282545fdf9bSopenharmony_ci            switch (optopt) {
1283545fdf9bSopenharmony_ci                case 'n': {
1284545fdf9bSopenharmony_ci                    // 'bm enable -n' with no argument: bm enable -n
1285545fdf9bSopenharmony_ci                    // 'bm enable --bundle-name' with no argument: bm enable --bundle-name
1286545fdf9bSopenharmony_ci                    APP_LOGD("'bm enable -n' with no argument.");
1287545fdf9bSopenharmony_ci                    resultReceiver_.append(STRING_REQUIRE_CORRECT_VALUE);
1288545fdf9bSopenharmony_ci                    result = OHOS::ERR_INVALID_VALUE;
1289545fdf9bSopenharmony_ci                    break;
1290545fdf9bSopenharmony_ci                }
1291545fdf9bSopenharmony_ci                case 'a': {
1292545fdf9bSopenharmony_ci                    // 'bm enable -a' with no argument: bm enable -a
1293545fdf9bSopenharmony_ci                    // 'bm enable --ability-name' with no argument: bm enable --ability-name
1294545fdf9bSopenharmony_ci                    APP_LOGD("'bm enable -a' with no argument.");
1295545fdf9bSopenharmony_ci                    resultReceiver_.append(STRING_REQUIRE_CORRECT_VALUE);
1296545fdf9bSopenharmony_ci                    result = OHOS::ERR_INVALID_VALUE;
1297545fdf9bSopenharmony_ci                    break;
1298545fdf9bSopenharmony_ci                }
1299545fdf9bSopenharmony_ci                case 'u': {
1300545fdf9bSopenharmony_ci                    // 'bm enable -u' with no argument: bm enable -u
1301545fdf9bSopenharmony_ci                    // 'bm enable --user-id' with no argument: bm enable --user-id
1302545fdf9bSopenharmony_ci                    APP_LOGD("'bm enable -u' with no argument.");
1303545fdf9bSopenharmony_ci                    resultReceiver_.append(STRING_REQUIRE_CORRECT_VALUE);
1304545fdf9bSopenharmony_ci                    result = OHOS::ERR_INVALID_VALUE;
1305545fdf9bSopenharmony_ci                    break;
1306545fdf9bSopenharmony_ci                }
1307545fdf9bSopenharmony_ci                default: {
1308545fdf9bSopenharmony_ci                    // 'bm enable' with an unknown option: bm enable -x
1309545fdf9bSopenharmony_ci                    // 'bm enable' with an unknown option: bm enable -xxx
1310545fdf9bSopenharmony_ci                    std::string unknownOption = "";
1311545fdf9bSopenharmony_ci                    std::string unknownOptionMsg = GetUnknownOptionMsg(unknownOption);
1312545fdf9bSopenharmony_ci                    APP_LOGD("'bm enable' with an unknown option.");
1313545fdf9bSopenharmony_ci                    resultReceiver_.append(unknownOptionMsg);
1314545fdf9bSopenharmony_ci                    result = OHOS::ERR_INVALID_VALUE;
1315545fdf9bSopenharmony_ci                    break;
1316545fdf9bSopenharmony_ci                }
1317545fdf9bSopenharmony_ci            }
1318545fdf9bSopenharmony_ci            break;
1319545fdf9bSopenharmony_ci        }
1320545fdf9bSopenharmony_ci
1321545fdf9bSopenharmony_ci        switch (option) {
1322545fdf9bSopenharmony_ci            case 'h': {
1323545fdf9bSopenharmony_ci                // 'bm enable-h'
1324545fdf9bSopenharmony_ci                // 'bm enable --help'
1325545fdf9bSopenharmony_ci                APP_LOGD("'bm enable %{public}s'", argv_[optind - 1]);
1326545fdf9bSopenharmony_ci                result = OHOS::ERR_INVALID_VALUE;
1327545fdf9bSopenharmony_ci                break;
1328545fdf9bSopenharmony_ci            }
1329545fdf9bSopenharmony_ci            case 'n': {
1330545fdf9bSopenharmony_ci                // 'bm enable -n <bundle-name>'
1331545fdf9bSopenharmony_ci                // 'bm enable --bundle-name <bundle-name>'
1332545fdf9bSopenharmony_ci                bundleName = optarg;
1333545fdf9bSopenharmony_ci                break;
1334545fdf9bSopenharmony_ci            }
1335545fdf9bSopenharmony_ci            case 'a': {
1336545fdf9bSopenharmony_ci                // 'bm enable -a <ability-name>'
1337545fdf9bSopenharmony_ci                // 'bm enable --ability-name <ability-name>'
1338545fdf9bSopenharmony_ci                abilityName = optarg;
1339545fdf9bSopenharmony_ci                break;
1340545fdf9bSopenharmony_ci            }
1341545fdf9bSopenharmony_ci            case 'u': {
1342545fdf9bSopenharmony_ci                // 'bm enable -u userId'
1343545fdf9bSopenharmony_ci                // 'bm enable --user-id userId'
1344545fdf9bSopenharmony_ci                APP_LOGW("'bm enable -u is not supported'");
1345545fdf9bSopenharmony_ci                break;
1346545fdf9bSopenharmony_ci            }
1347545fdf9bSopenharmony_ci            default: {
1348545fdf9bSopenharmony_ci                result = OHOS::ERR_INVALID_VALUE;
1349545fdf9bSopenharmony_ci                break;
1350545fdf9bSopenharmony_ci            }
1351545fdf9bSopenharmony_ci        }
1352545fdf9bSopenharmony_ci    }
1353545fdf9bSopenharmony_ci
1354545fdf9bSopenharmony_ci    if (result == OHOS::ERR_OK) {
1355545fdf9bSopenharmony_ci        if (resultReceiver_ == "" && bundleName.size() == 0) {
1356545fdf9bSopenharmony_ci            // 'bm enable ...' with no bundle name option
1357545fdf9bSopenharmony_ci            APP_LOGD("'bm enable' with no bundle name option.");
1358545fdf9bSopenharmony_ci
1359545fdf9bSopenharmony_ci            resultReceiver_.append(HELP_MSG_NO_BUNDLE_NAME_OPTION + "\n");
1360545fdf9bSopenharmony_ci            result = OHOS::ERR_INVALID_VALUE;
1361545fdf9bSopenharmony_ci        }
1362545fdf9bSopenharmony_ci    }
1363545fdf9bSopenharmony_ci
1364545fdf9bSopenharmony_ci    if (result != OHOS::ERR_OK) {
1365545fdf9bSopenharmony_ci        resultReceiver_.append(HELP_MSG_ENABLE);
1366545fdf9bSopenharmony_ci    } else {
1367545fdf9bSopenharmony_ci        AbilityInfo abilityInfo;
1368545fdf9bSopenharmony_ci        abilityInfo.name = abilityName;
1369545fdf9bSopenharmony_ci        abilityInfo.bundleName = bundleName;
1370545fdf9bSopenharmony_ci        bool enableResult = SetApplicationEnabledOperation(abilityInfo, true, userId);
1371545fdf9bSopenharmony_ci        if (enableResult) {
1372545fdf9bSopenharmony_ci            resultReceiver_ = STRING_ENABLE_BUNDLE_OK + "\n";
1373545fdf9bSopenharmony_ci        } else {
1374545fdf9bSopenharmony_ci            resultReceiver_ = STRING_ENABLE_BUNDLE_NG + "\n";
1375545fdf9bSopenharmony_ci        }
1376545fdf9bSopenharmony_ci    }
1377545fdf9bSopenharmony_ci    APP_LOGI("end");
1378545fdf9bSopenharmony_ci    return result;
1379545fdf9bSopenharmony_ci}
1380545fdf9bSopenharmony_ci
1381545fdf9bSopenharmony_ciErrCode BundleManagerShellCommand::RunAsDisableCommand()
1382545fdf9bSopenharmony_ci{
1383545fdf9bSopenharmony_ci    int32_t mode = GetIntParameter(IS_ROOT_MODE_PARAM, USER_MODE);
1384545fdf9bSopenharmony_ci    if (mode != ROOT_MODE) {
1385545fdf9bSopenharmony_ci        APP_LOGI("in user mode");
1386545fdf9bSopenharmony_ci        return ERR_OK;
1387545fdf9bSopenharmony_ci    }
1388545fdf9bSopenharmony_ci    APP_LOGI("begin to RunAsDisableCommand");
1389545fdf9bSopenharmony_ci    int result = OHOS::ERR_OK;
1390545fdf9bSopenharmony_ci    int counter = 0;
1391545fdf9bSopenharmony_ci    std::string bundleName = "";
1392545fdf9bSopenharmony_ci    std::string abilityName = "";
1393545fdf9bSopenharmony_ci    int32_t userId = BundleCommandCommon::GetCurrentUserId(Constants::UNSPECIFIED_USERID);
1394545fdf9bSopenharmony_ci    while (true) {
1395545fdf9bSopenharmony_ci        counter++;
1396545fdf9bSopenharmony_ci        int32_t option = getopt_long(argc_, argv_, SHORT_OPTIONS.c_str(), LONG_OPTIONS, nullptr);
1397545fdf9bSopenharmony_ci        APP_LOGD("option: %{public}d, optopt: %{public}d, optind: %{public}d", option, optopt, optind);
1398545fdf9bSopenharmony_ci        if (optind < 0 || optind > argc_) {
1399545fdf9bSopenharmony_ci            return OHOS::ERR_INVALID_VALUE;
1400545fdf9bSopenharmony_ci        }
1401545fdf9bSopenharmony_ci        if (option == -1) {
1402545fdf9bSopenharmony_ci            if (counter == 1) {
1403545fdf9bSopenharmony_ci                // When scanning the first argument
1404545fdf9bSopenharmony_ci                if (strcmp(argv_[optind], cmd_.c_str()) == 0) {
1405545fdf9bSopenharmony_ci                    // 'bm disable' with no option: bm disable
1406545fdf9bSopenharmony_ci                    // 'bm disable' with a wrong argument: bm disable xxx
1407545fdf9bSopenharmony_ci                    APP_LOGD("'bm disable' with no option.");
1408545fdf9bSopenharmony_ci                    resultReceiver_.append(HELP_MSG_NO_OPTION + "\n");
1409545fdf9bSopenharmony_ci                    result = OHOS::ERR_INVALID_VALUE;
1410545fdf9bSopenharmony_ci                }
1411545fdf9bSopenharmony_ci            }
1412545fdf9bSopenharmony_ci            break;
1413545fdf9bSopenharmony_ci        }
1414545fdf9bSopenharmony_ci        if (option == '?') {
1415545fdf9bSopenharmony_ci            switch (optopt) {
1416545fdf9bSopenharmony_ci                case 'n': {
1417545fdf9bSopenharmony_ci                    // 'bm disable -n' with no argument: bm disable -n
1418545fdf9bSopenharmony_ci                    // 'bm disable --bundle-name' with no argument: bm disable --bundle-name
1419545fdf9bSopenharmony_ci                    APP_LOGD("'bm disable' with no argument.");
1420545fdf9bSopenharmony_ci                    resultReceiver_.append(STRING_REQUIRE_CORRECT_VALUE);
1421545fdf9bSopenharmony_ci                    result = OHOS::ERR_INVALID_VALUE;
1422545fdf9bSopenharmony_ci                    break;
1423545fdf9bSopenharmony_ci                }
1424545fdf9bSopenharmony_ci                case 'a': {
1425545fdf9bSopenharmony_ci                    // 'bm disable -a' with no argument: bm disable -a
1426545fdf9bSopenharmony_ci                    // 'bm disable --ability-name' with no argument: bm disable --ability-name
1427545fdf9bSopenharmony_ci                    APP_LOGD("'bm disable -a' with no argument.");
1428545fdf9bSopenharmony_ci                    resultReceiver_.append(STRING_REQUIRE_CORRECT_VALUE);
1429545fdf9bSopenharmony_ci                    result = OHOS::ERR_INVALID_VALUE;
1430545fdf9bSopenharmony_ci                    break;
1431545fdf9bSopenharmony_ci                }
1432545fdf9bSopenharmony_ci                case 'u': {
1433545fdf9bSopenharmony_ci                    // 'bm disable -u' with no argument: bm disable -u
1434545fdf9bSopenharmony_ci                    // 'bm disable --user-id' with no argument: bm disable --user-id
1435545fdf9bSopenharmony_ci                    APP_LOGD("'bm disable -u' with no argument.");
1436545fdf9bSopenharmony_ci                    resultReceiver_.append(STRING_REQUIRE_CORRECT_VALUE);
1437545fdf9bSopenharmony_ci                    result = OHOS::ERR_INVALID_VALUE;
1438545fdf9bSopenharmony_ci                    break;
1439545fdf9bSopenharmony_ci                }
1440545fdf9bSopenharmony_ci                default: {
1441545fdf9bSopenharmony_ci                    // 'bm disable' with an unknown option: bm disable -x
1442545fdf9bSopenharmony_ci                    // 'bm disable' with an unknown option: bm disable -xxx
1443545fdf9bSopenharmony_ci                    std::string unknownOption = "";
1444545fdf9bSopenharmony_ci                    std::string unknownOptionMsg = GetUnknownOptionMsg(unknownOption);
1445545fdf9bSopenharmony_ci                    APP_LOGD("'bm disable' with an unknown option.");
1446545fdf9bSopenharmony_ci                    resultReceiver_.append(unknownOptionMsg);
1447545fdf9bSopenharmony_ci                    result = OHOS::ERR_INVALID_VALUE;
1448545fdf9bSopenharmony_ci                    break;
1449545fdf9bSopenharmony_ci                }
1450545fdf9bSopenharmony_ci            }
1451545fdf9bSopenharmony_ci            break;
1452545fdf9bSopenharmony_ci        }
1453545fdf9bSopenharmony_ci        switch (option) {
1454545fdf9bSopenharmony_ci            case 'h': {
1455545fdf9bSopenharmony_ci                // 'bm disable -h'
1456545fdf9bSopenharmony_ci                // 'bm disable --help'
1457545fdf9bSopenharmony_ci                APP_LOGD("'bm disable %{public}s'", argv_[optind - 1]);
1458545fdf9bSopenharmony_ci                result = OHOS::ERR_INVALID_VALUE;
1459545fdf9bSopenharmony_ci                break;
1460545fdf9bSopenharmony_ci            }
1461545fdf9bSopenharmony_ci            case 'n': {
1462545fdf9bSopenharmony_ci                // 'bm disable -n <bundle-name>'
1463545fdf9bSopenharmony_ci                // 'bm disable --bundle-name <bundle-name>'
1464545fdf9bSopenharmony_ci                bundleName = optarg;
1465545fdf9bSopenharmony_ci                break;
1466545fdf9bSopenharmony_ci            }
1467545fdf9bSopenharmony_ci            case 'a': {
1468545fdf9bSopenharmony_ci                // 'bm disable -a <ability-name>'
1469545fdf9bSopenharmony_ci                // 'bm disable --ability-name <ability-name>'
1470545fdf9bSopenharmony_ci                abilityName = optarg;
1471545fdf9bSopenharmony_ci                break;
1472545fdf9bSopenharmony_ci            }
1473545fdf9bSopenharmony_ci            case 'u': {
1474545fdf9bSopenharmony_ci                // 'bm disable -u userId'
1475545fdf9bSopenharmony_ci                // 'bm disable --user-id userId'
1476545fdf9bSopenharmony_ci                APP_LOGW("'bm disable -u is not supported'");
1477545fdf9bSopenharmony_ci                break;
1478545fdf9bSopenharmony_ci            }
1479545fdf9bSopenharmony_ci            default: {
1480545fdf9bSopenharmony_ci                result = OHOS::ERR_INVALID_VALUE;
1481545fdf9bSopenharmony_ci                break;
1482545fdf9bSopenharmony_ci            }
1483545fdf9bSopenharmony_ci        }
1484545fdf9bSopenharmony_ci    }
1485545fdf9bSopenharmony_ci    if (result == OHOS::ERR_OK) {
1486545fdf9bSopenharmony_ci        if (resultReceiver_ == "" && bundleName.size() == 0) {
1487545fdf9bSopenharmony_ci            // 'bm disable ...' with no bundle name option
1488545fdf9bSopenharmony_ci            APP_LOGD("'bm disable' with no bundle name option.");
1489545fdf9bSopenharmony_ci            resultReceiver_.append(HELP_MSG_NO_BUNDLE_NAME_OPTION + "\n");
1490545fdf9bSopenharmony_ci            result = OHOS::ERR_INVALID_VALUE;
1491545fdf9bSopenharmony_ci        }
1492545fdf9bSopenharmony_ci    }
1493545fdf9bSopenharmony_ci    if (result != OHOS::ERR_OK) {
1494545fdf9bSopenharmony_ci        resultReceiver_.append(HELP_MSG_DISABLE);
1495545fdf9bSopenharmony_ci    } else {
1496545fdf9bSopenharmony_ci        AbilityInfo abilityInfo;
1497545fdf9bSopenharmony_ci        abilityInfo.name = abilityName;
1498545fdf9bSopenharmony_ci        abilityInfo.bundleName = bundleName;
1499545fdf9bSopenharmony_ci        bool enableResult = SetApplicationEnabledOperation(abilityInfo, false, userId);
1500545fdf9bSopenharmony_ci        if (enableResult) {
1501545fdf9bSopenharmony_ci            resultReceiver_ = STRING_DISABLE_BUNDLE_OK + "\n";
1502545fdf9bSopenharmony_ci        } else {
1503545fdf9bSopenharmony_ci            resultReceiver_ = STRING_DISABLE_BUNDLE_NG + "\n";
1504545fdf9bSopenharmony_ci        }
1505545fdf9bSopenharmony_ci    }
1506545fdf9bSopenharmony_ci    APP_LOGI("end");
1507545fdf9bSopenharmony_ci    return result;
1508545fdf9bSopenharmony_ci}
1509545fdf9bSopenharmony_ci
1510545fdf9bSopenharmony_ciErrCode BundleManagerShellCommand::RunAsGetCommand()
1511545fdf9bSopenharmony_ci{
1512545fdf9bSopenharmony_ci    APP_LOGI("begin to RunAsGetCommand");
1513545fdf9bSopenharmony_ci    int result = OHOS::ERR_OK;
1514545fdf9bSopenharmony_ci    int counter = 0;
1515545fdf9bSopenharmony_ci    while (true) {
1516545fdf9bSopenharmony_ci        counter++;
1517545fdf9bSopenharmony_ci        if (argc_ > MAX_ARGUEMENTS_NUMBER) {
1518545fdf9bSopenharmony_ci            resultReceiver_.append(HELP_MSG_GET);
1519545fdf9bSopenharmony_ci            return OHOS::ERR_INVALID_VALUE;
1520545fdf9bSopenharmony_ci        }
1521545fdf9bSopenharmony_ci        int32_t option = getopt_long(argc_, argv_, SHORT_OPTIONS_GET.c_str(), LONG_OPTIONS_GET, nullptr);
1522545fdf9bSopenharmony_ci        APP_LOGD("option: %{public}d, optopt: %{public}d, optind: %{public}d", option, optopt, optind);
1523545fdf9bSopenharmony_ci        if (optind < 0 || optind > argc_) {
1524545fdf9bSopenharmony_ci            return OHOS::ERR_INVALID_VALUE;
1525545fdf9bSopenharmony_ci        }
1526545fdf9bSopenharmony_ci        if (option == -1) {
1527545fdf9bSopenharmony_ci            if (counter == 1) {
1528545fdf9bSopenharmony_ci                if (strcmp(argv_[optind], cmd_.c_str()) == 0) {
1529545fdf9bSopenharmony_ci                    // 1.'bm get' with no option: bm get
1530545fdf9bSopenharmony_ci                    // 2.'bm get' with a wrong argument: bm get -xxx
1531545fdf9bSopenharmony_ci                    APP_LOGD("'bm get' %{public}s", HELP_MSG_NO_OPTION.c_str());
1532545fdf9bSopenharmony_ci                    resultReceiver_.append(HELP_MSG_NO_OPTION + "\n");
1533545fdf9bSopenharmony_ci                    result = OHOS::ERR_INVALID_VALUE;
1534545fdf9bSopenharmony_ci                }
1535545fdf9bSopenharmony_ci            }
1536545fdf9bSopenharmony_ci            break;
1537545fdf9bSopenharmony_ci        }
1538545fdf9bSopenharmony_ci        switch (option) {
1539545fdf9bSopenharmony_ci            case 'h': {
1540545fdf9bSopenharmony_ci                result = OHOS::ERR_INVALID_VALUE;
1541545fdf9bSopenharmony_ci                break;
1542545fdf9bSopenharmony_ci            }
1543545fdf9bSopenharmony_ci            case 'u': {
1544545fdf9bSopenharmony_ci                break;
1545545fdf9bSopenharmony_ci            }
1546545fdf9bSopenharmony_ci            default: {
1547545fdf9bSopenharmony_ci                result = OHOS::ERR_INVALID_VALUE;
1548545fdf9bSopenharmony_ci                resultReceiver_.append(STRING_INCORRECT_OPTION + "\n");
1549545fdf9bSopenharmony_ci                break;
1550545fdf9bSopenharmony_ci            }
1551545fdf9bSopenharmony_ci        }
1552545fdf9bSopenharmony_ci    }
1553545fdf9bSopenharmony_ci    if (result != OHOS::ERR_OK) {
1554545fdf9bSopenharmony_ci        resultReceiver_.append(HELP_MSG_GET);
1555545fdf9bSopenharmony_ci        return result;
1556545fdf9bSopenharmony_ci    }
1557545fdf9bSopenharmony_ci    resultReceiver_.append(STRING_GET_UDID_OK + "\n");
1558545fdf9bSopenharmony_ci    resultReceiver_.append(GetUdid() + "\n");
1559545fdf9bSopenharmony_ci    APP_LOGI("end");
1560545fdf9bSopenharmony_ci    return result;
1561545fdf9bSopenharmony_ci}
1562545fdf9bSopenharmony_ci
1563545fdf9bSopenharmony_ciErrCode BundleManagerShellCommand::RunAsQuickFixCommand()
1564545fdf9bSopenharmony_ci{
1565545fdf9bSopenharmony_ci    APP_LOGI("begin to RunAsQuickFixCommand");
1566545fdf9bSopenharmony_ci    for (auto index = INDEX_OFFSET; index < argc_; ++index) {
1567545fdf9bSopenharmony_ci        APP_LOGD("argv_[%{public}d]: %{public}s", index, argv_[index]);
1568545fdf9bSopenharmony_ci        std::string opt = argv_[index];
1569545fdf9bSopenharmony_ci        if ((opt == "-h") || (opt == "--help")) {
1570545fdf9bSopenharmony_ci            resultReceiver_.append(HELP_MSG_QUICK_FIX);
1571545fdf9bSopenharmony_ci            APP_LOGI("end");
1572545fdf9bSopenharmony_ci            return ERR_OK;
1573545fdf9bSopenharmony_ci        } else if ((opt == "-a") || (opt == "--apply")) {
1574545fdf9bSopenharmony_ci            if (index >= argc_ - INDEX_OFFSET) {
1575545fdf9bSopenharmony_ci                resultReceiver_.append("error: option [--apply] is incorrect.\n");
1576545fdf9bSopenharmony_ci                resultReceiver_.append(HELP_MSG_QUICK_FIX);
1577545fdf9bSopenharmony_ci                APP_LOGI("end");
1578545fdf9bSopenharmony_ci                return ERR_INVALID_VALUE;
1579545fdf9bSopenharmony_ci            }
1580545fdf9bSopenharmony_ci
1581545fdf9bSopenharmony_ci            std::string argKey = argv_[++index];
1582545fdf9bSopenharmony_ci            index++;
1583545fdf9bSopenharmony_ci            if (argKey == "-f" || argKey == "--file-path") {
1584545fdf9bSopenharmony_ci                std::vector<std::string> quickFixFiles;
1585545fdf9bSopenharmony_ci                bool isDebug = false;
1586545fdf9bSopenharmony_ci                std::string targetPath;
1587545fdf9bSopenharmony_ci                // collect value of multi file-path.
1588545fdf9bSopenharmony_ci                for (; index < argc_ && index >= INDEX_OFFSET; ++index) {
1589545fdf9bSopenharmony_ci                    if (argList_[index - INDEX_OFFSET] == "-q" || argList_[index - INDEX_OFFSET] == "--query" ||
1590545fdf9bSopenharmony_ci                        argList_[index - INDEX_OFFSET] == "-b" || argList_[index - INDEX_OFFSET] == "--bundle-name" ||
1591545fdf9bSopenharmony_ci                        argList_[index - INDEX_OFFSET] == "-a" || argList_[index - INDEX_OFFSET] == "--apply" ||
1592545fdf9bSopenharmony_ci                        argList_[index - INDEX_OFFSET] == "-f" || argList_[index - INDEX_OFFSET] == "--file-path") {
1593545fdf9bSopenharmony_ci                        break;
1594545fdf9bSopenharmony_ci                    } else if (argList_[index - INDEX_OFFSET] == "-d" || argList_[index - INDEX_OFFSET] == "--debug") {
1595545fdf9bSopenharmony_ci                        isDebug = true;
1596545fdf9bSopenharmony_ci                        continue;
1597545fdf9bSopenharmony_ci                    } else if (argList_[index - INDEX_OFFSET] == "-t" || argList_[index - INDEX_OFFSET] == "--target") {
1598545fdf9bSopenharmony_ci                        if (index + 1 - INDEX_OFFSET >= static_cast<int32_t>(argList_.size())) {
1599545fdf9bSopenharmony_ci                            continue;
1600545fdf9bSopenharmony_ci                        }
1601545fdf9bSopenharmony_ci                        targetPath = argList_[index + 1 - INDEX_OFFSET];
1602545fdf9bSopenharmony_ci                        index++;
1603545fdf9bSopenharmony_ci                        continue;
1604545fdf9bSopenharmony_ci                    }
1605545fdf9bSopenharmony_ci                    quickFixFiles.emplace_back(argList_[index - INDEX_OFFSET]);
1606545fdf9bSopenharmony_ci                }
1607545fdf9bSopenharmony_ci                APP_LOGI("end");
1608545fdf9bSopenharmony_ci                if (!targetPath.empty()) {
1609545fdf9bSopenharmony_ci                    std::shared_ptr<QuickFixResult> deployRes = nullptr;
1610545fdf9bSopenharmony_ci                    int32_t result = OHOS::ERR_OK;
1611545fdf9bSopenharmony_ci                    result = DeployQuickFixDisable(quickFixFiles, deployRes, isDebug, targetPath);
1612545fdf9bSopenharmony_ci                    resultReceiver_.append((result == OHOS::ERR_OK) ? "apply quickfix succeed.\n" :
1613545fdf9bSopenharmony_ci                        ("apply quickfix failed with errno: " + std::to_string(result) + ".\n"));
1614545fdf9bSopenharmony_ci                    return result;
1615545fdf9bSopenharmony_ci                }
1616545fdf9bSopenharmony_ci                return QuickFixCommand::ApplyQuickFix(quickFixFiles, resultReceiver_, isDebug);
1617545fdf9bSopenharmony_ci            }
1618545fdf9bSopenharmony_ci        } else if ((opt == "-q") || (opt == "--query")) {
1619545fdf9bSopenharmony_ci            if (index >= argc_ - INDEX_OFFSET) {
1620545fdf9bSopenharmony_ci                resultReceiver_.append("error: option [--query] is incorrect.\n");
1621545fdf9bSopenharmony_ci                resultReceiver_.append(HELP_MSG_QUICK_FIX);
1622545fdf9bSopenharmony_ci                APP_LOGI("end");
1623545fdf9bSopenharmony_ci                return ERR_INVALID_VALUE;
1624545fdf9bSopenharmony_ci            }
1625545fdf9bSopenharmony_ci
1626545fdf9bSopenharmony_ci            std::string bundleName;
1627545fdf9bSopenharmony_ci            std::string argKey = argv_[++index];
1628545fdf9bSopenharmony_ci            std::string argValue = argv_[++index];
1629545fdf9bSopenharmony_ci            if (argKey == "-b" || argKey == "--bundle-name") {
1630545fdf9bSopenharmony_ci                bundleName = argValue;
1631545fdf9bSopenharmony_ci            }
1632545fdf9bSopenharmony_ci            APP_LOGI("end");
1633545fdf9bSopenharmony_ci            return QuickFixCommand::GetApplyedQuickFixInfo(bundleName, resultReceiver_);
1634545fdf9bSopenharmony_ci        } else if ((opt == "-r") || (opt == "--remove")) {
1635545fdf9bSopenharmony_ci            if (index >= argc_ - INDEX_OFFSET) {
1636545fdf9bSopenharmony_ci                resultReceiver_.append("error: option [--remove] is incorrect.\n");
1637545fdf9bSopenharmony_ci                resultReceiver_.append(HELP_MSG_QUICK_FIX);
1638545fdf9bSopenharmony_ci                APP_LOGI("end");
1639545fdf9bSopenharmony_ci                return ERR_INVALID_VALUE;
1640545fdf9bSopenharmony_ci            }
1641545fdf9bSopenharmony_ci
1642545fdf9bSopenharmony_ci            std::string bundleName;
1643545fdf9bSopenharmony_ci            std::string argKey = argv_[++index];
1644545fdf9bSopenharmony_ci            std::string argValue = argv_[++index];
1645545fdf9bSopenharmony_ci            if (argKey == "-b" || argKey == "--bundle-name") {
1646545fdf9bSopenharmony_ci                bundleName = argValue;
1647545fdf9bSopenharmony_ci            }
1648545fdf9bSopenharmony_ci            APP_LOGI("end");
1649545fdf9bSopenharmony_ci            std::shared_ptr<QuickFixResult> deleteRes = nullptr;
1650545fdf9bSopenharmony_ci            int32_t result = OHOS::ERR_OK;
1651545fdf9bSopenharmony_ci            result = DeleteQuickFix(bundleName, deleteRes);
1652545fdf9bSopenharmony_ci            resultReceiver_ = (result == OHOS::ERR_OK) ? "delete quick fix successfully\n" :
1653545fdf9bSopenharmony_ci                "delete quickfix failed with errno: " + std::to_string(result) + ".\n";
1654545fdf9bSopenharmony_ci            return result;
1655545fdf9bSopenharmony_ci        } else {
1656545fdf9bSopenharmony_ci            resultReceiver_.append("error: unknown option.\n");
1657545fdf9bSopenharmony_ci            resultReceiver_.append(HELP_MSG_QUICK_FIX);
1658545fdf9bSopenharmony_ci            APP_LOGI("end");
1659545fdf9bSopenharmony_ci            return ERR_INVALID_VALUE;
1660545fdf9bSopenharmony_ci        }
1661545fdf9bSopenharmony_ci    }
1662545fdf9bSopenharmony_ci
1663545fdf9bSopenharmony_ci    resultReceiver_.append("error: parameter is not enough.\n");
1664545fdf9bSopenharmony_ci    resultReceiver_.append(HELP_MSG_QUICK_FIX);
1665545fdf9bSopenharmony_ci    APP_LOGI("end");
1666545fdf9bSopenharmony_ci    return ERR_INVALID_VALUE;
1667545fdf9bSopenharmony_ci}
1668545fdf9bSopenharmony_ci
1669545fdf9bSopenharmony_ciErrCode BundleManagerShellCommand::RunAsDumpOverlay()
1670545fdf9bSopenharmony_ci{
1671545fdf9bSopenharmony_ci    APP_LOGI("begin to RunAsDumpOverlay");
1672545fdf9bSopenharmony_ci    int result = OHOS::ERR_OK;
1673545fdf9bSopenharmony_ci    int counter = 0;
1674545fdf9bSopenharmony_ci    int32_t userId = BundleCommandCommon::GetCurrentUserId(Constants::UNSPECIFIED_USERID);
1675545fdf9bSopenharmony_ci    std::string bundleName = "";
1676545fdf9bSopenharmony_ci    std::string moduleName = "";
1677545fdf9bSopenharmony_ci    std::string targetModuleName = "";
1678545fdf9bSopenharmony_ci    while (true) {
1679545fdf9bSopenharmony_ci        counter++;
1680545fdf9bSopenharmony_ci        if (argc_ > MAX_OVERLAY_ARGUEMENTS_NUMBER) {
1681545fdf9bSopenharmony_ci            resultReceiver_.append(HELP_MSG_OVERLAY);
1682545fdf9bSopenharmony_ci            return OHOS::ERR_INVALID_VALUE;
1683545fdf9bSopenharmony_ci        }
1684545fdf9bSopenharmony_ci        int32_t option = getopt_long(argc_, argv_, SHORT_OPTIONS_OVERLAY.c_str(), LONG_OPTIONS_OVERLAY,
1685545fdf9bSopenharmony_ci            nullptr);
1686545fdf9bSopenharmony_ci        APP_LOGD("option: %{public}d, optopt: %{public}d, optind: %{public}d", option, optopt, optind);
1687545fdf9bSopenharmony_ci        if (optind < 0 || optind > argc_) {
1688545fdf9bSopenharmony_ci            return OHOS::ERR_INVALID_VALUE;
1689545fdf9bSopenharmony_ci        }
1690545fdf9bSopenharmony_ci        if (option == -1) {
1691545fdf9bSopenharmony_ci            if (counter == 1) {
1692545fdf9bSopenharmony_ci                if (strcmp(argv_[optind], cmd_.c_str()) == 0) {
1693545fdf9bSopenharmony_ci                    // 1.'bm dump-overlay' with no option: bm dump-overlay
1694545fdf9bSopenharmony_ci                    // 2.'bm dump-overlay' with a wrong argument: bm dump-overlay -xxx
1695545fdf9bSopenharmony_ci                    APP_LOGD("'bm dump-overlay' %{public}s", HELP_MSG_NO_OPTION.c_str());
1696545fdf9bSopenharmony_ci                    resultReceiver_.append(HELP_MSG_NO_OPTION + "\n");
1697545fdf9bSopenharmony_ci                    result = OHOS::ERR_INVALID_VALUE;
1698545fdf9bSopenharmony_ci                }
1699545fdf9bSopenharmony_ci            }
1700545fdf9bSopenharmony_ci            break;
1701545fdf9bSopenharmony_ci        }
1702545fdf9bSopenharmony_ci        if (option == '?') {
1703545fdf9bSopenharmony_ci            switch (optopt) {
1704545fdf9bSopenharmony_ci                case 'b': {
1705545fdf9bSopenharmony_ci                    // 'bm dump-overlay -b' with no argument
1706545fdf9bSopenharmony_ci                    // 'bm dump-overlay --bundle-name' with no argument
1707545fdf9bSopenharmony_ci                    APP_LOGD("'bm dump-overlay -b' with no argument.");
1708545fdf9bSopenharmony_ci                    resultReceiver_.append(STRING_REQUIRE_CORRECT_VALUE);
1709545fdf9bSopenharmony_ci                    result = OHOS::ERR_INVALID_VALUE;
1710545fdf9bSopenharmony_ci                    break;
1711545fdf9bSopenharmony_ci                }
1712545fdf9bSopenharmony_ci                case 'm': {
1713545fdf9bSopenharmony_ci                    // 'bm dump-overlay -m' with no argument: bm enable -m
1714545fdf9bSopenharmony_ci                    // 'bm dump-overlay --bundle-name' with no argument: bm dump-overlay --bundle-name
1715545fdf9bSopenharmony_ci                    APP_LOGD("'bm dump-overlay -m' with no argument.");
1716545fdf9bSopenharmony_ci                    resultReceiver_.append(STRING_REQUIRE_CORRECT_VALUE);
1717545fdf9bSopenharmony_ci                    result = OHOS::ERR_INVALID_VALUE;
1718545fdf9bSopenharmony_ci                    break;
1719545fdf9bSopenharmony_ci                }
1720545fdf9bSopenharmony_ci                case 't': {
1721545fdf9bSopenharmony_ci                    // 'bm dump-overlay -t' with no argument
1722545fdf9bSopenharmony_ci                    // 'bm dump-overlay --target-module-name' with no argument
1723545fdf9bSopenharmony_ci                    APP_LOGD("'bm dump-overlay -t' with no argument.");
1724545fdf9bSopenharmony_ci                    resultReceiver_.append(STRING_REQUIRE_CORRECT_VALUE);
1725545fdf9bSopenharmony_ci                    result = OHOS::ERR_INVALID_VALUE;
1726545fdf9bSopenharmony_ci                    break;
1727545fdf9bSopenharmony_ci                }
1728545fdf9bSopenharmony_ci                case 'u': {
1729545fdf9bSopenharmony_ci                    // 'bm dump-overlay -u' with no argument: bm dump-overlay -u
1730545fdf9bSopenharmony_ci                    // 'bm dump-overlay --user-id' with no argument: bm dump-overlay --user-id
1731545fdf9bSopenharmony_ci                    APP_LOGD("'bm dump-overlay -u' with no argument.");
1732545fdf9bSopenharmony_ci                    resultReceiver_.append(STRING_REQUIRE_CORRECT_VALUE);
1733545fdf9bSopenharmony_ci                    result = OHOS::ERR_INVALID_VALUE;
1734545fdf9bSopenharmony_ci                    break;
1735545fdf9bSopenharmony_ci                }
1736545fdf9bSopenharmony_ci                default: {
1737545fdf9bSopenharmony_ci                    // 'bm dump-overlay' with an unknown option
1738545fdf9bSopenharmony_ci                    // 'bm dump-overlay' with an unknown option
1739545fdf9bSopenharmony_ci                    std::string unknownOption = "";
1740545fdf9bSopenharmony_ci                    std::string unknownOptionMsg = GetUnknownOptionMsg(unknownOption);
1741545fdf9bSopenharmony_ci                    APP_LOGD("'bm dump-overlay' with an unknown option.");
1742545fdf9bSopenharmony_ci                    resultReceiver_.append(unknownOptionMsg);
1743545fdf9bSopenharmony_ci                    result = OHOS::ERR_INVALID_VALUE;
1744545fdf9bSopenharmony_ci                    break;
1745545fdf9bSopenharmony_ci                }
1746545fdf9bSopenharmony_ci            }
1747545fdf9bSopenharmony_ci            break;
1748545fdf9bSopenharmony_ci        }
1749545fdf9bSopenharmony_ci
1750545fdf9bSopenharmony_ci        switch (option) {
1751545fdf9bSopenharmony_ci            case 'h': {
1752545fdf9bSopenharmony_ci                // 'bm dump-overlay -h'
1753545fdf9bSopenharmony_ci                // 'bm dump-overlay --help'
1754545fdf9bSopenharmony_ci                APP_LOGD("'bm dump-overlay %{public}s'", argv_[optind - 1]);
1755545fdf9bSopenharmony_ci                result = OHOS::ERR_INVALID_VALUE;
1756545fdf9bSopenharmony_ci                break;
1757545fdf9bSopenharmony_ci            }
1758545fdf9bSopenharmony_ci            case 'b': {
1759545fdf9bSopenharmony_ci                // 'bm dump-overlay -b <bundle-name>'
1760545fdf9bSopenharmony_ci                // 'bm dump-overlay --bundle-name <bundle-name>'
1761545fdf9bSopenharmony_ci                bundleName = optarg;
1762545fdf9bSopenharmony_ci                break;
1763545fdf9bSopenharmony_ci            }
1764545fdf9bSopenharmony_ci            case 'm': {
1765545fdf9bSopenharmony_ci                // 'bm dump-overlay -m <module-name>'
1766545fdf9bSopenharmony_ci                // 'bm dump-overlay --module-name <module-name>'
1767545fdf9bSopenharmony_ci                moduleName = optarg;
1768545fdf9bSopenharmony_ci                break;
1769545fdf9bSopenharmony_ci            }
1770545fdf9bSopenharmony_ci            case 't': {
1771545fdf9bSopenharmony_ci                // 'bm dump-overlay -t <target-module-name>'
1772545fdf9bSopenharmony_ci                // 'bm dump-overlay --target-module-name <target-module-name>'
1773545fdf9bSopenharmony_ci                targetModuleName = optarg;
1774545fdf9bSopenharmony_ci                break;
1775545fdf9bSopenharmony_ci            }
1776545fdf9bSopenharmony_ci            case 'u': {
1777545fdf9bSopenharmony_ci                APP_LOGW("'bm dump-overlay -u is not supported'");
1778545fdf9bSopenharmony_ci                break;
1779545fdf9bSopenharmony_ci            }
1780545fdf9bSopenharmony_ci            default: {
1781545fdf9bSopenharmony_ci                result = OHOS::ERR_INVALID_VALUE;
1782545fdf9bSopenharmony_ci                break;
1783545fdf9bSopenharmony_ci            }
1784545fdf9bSopenharmony_ci        }
1785545fdf9bSopenharmony_ci    }
1786545fdf9bSopenharmony_ci    if (result != OHOS::ERR_OK) {
1787545fdf9bSopenharmony_ci        resultReceiver_.append(HELP_MSG_OVERLAY);
1788545fdf9bSopenharmony_ci        return result;
1789545fdf9bSopenharmony_ci    }
1790545fdf9bSopenharmony_ci#ifdef BUNDLE_FRAMEWORK_OVERLAY_INSTALLATION
1791545fdf9bSopenharmony_ci    auto res = DumpOverlayInfo(bundleName, moduleName, targetModuleName, userId);
1792545fdf9bSopenharmony_ci    if (res.empty()) {
1793545fdf9bSopenharmony_ci        resultReceiver_.append(STRING_DUMP_OVERLAY_NG + "\n");
1794545fdf9bSopenharmony_ci    } else {
1795545fdf9bSopenharmony_ci        resultReceiver_.append(STRING_DUMP_OVERLAY_OK + "\n");
1796545fdf9bSopenharmony_ci        resultReceiver_.append(res + "\n");
1797545fdf9bSopenharmony_ci    }
1798545fdf9bSopenharmony_ci#else
1799545fdf9bSopenharmony_ci    resultReceiver_.append(MSG_ERR_BUNDLEMANAGER_OVERLAY_FEATURE_IS_NOT_SUPPORTED);
1800545fdf9bSopenharmony_ci#endif
1801545fdf9bSopenharmony_ci    APP_LOGI("end");
1802545fdf9bSopenharmony_ci    return result;
1803545fdf9bSopenharmony_ci}
1804545fdf9bSopenharmony_ci
1805545fdf9bSopenharmony_ciErrCode BundleManagerShellCommand::RunAsDumpTargetOverlay()
1806545fdf9bSopenharmony_ci{
1807545fdf9bSopenharmony_ci    APP_LOGI("begin to RunAsDumpTargetOverlay");
1808545fdf9bSopenharmony_ci    int result = OHOS::ERR_OK;
1809545fdf9bSopenharmony_ci    int counter = 0;
1810545fdf9bSopenharmony_ci    int32_t userId = BundleCommandCommon::GetCurrentUserId(Constants::UNSPECIFIED_USERID);
1811545fdf9bSopenharmony_ci    std::string bundleName = "";
1812545fdf9bSopenharmony_ci    std::string moduleName = "";
1813545fdf9bSopenharmony_ci    while (true) {
1814545fdf9bSopenharmony_ci        counter++;
1815545fdf9bSopenharmony_ci        if (argc_ > MAX_OVERLAY_ARGUEMENTS_NUMBER) {
1816545fdf9bSopenharmony_ci            resultReceiver_.append(HELP_MSG_OVERLAY_TARGET);
1817545fdf9bSopenharmony_ci            return OHOS::ERR_INVALID_VALUE;
1818545fdf9bSopenharmony_ci        }
1819545fdf9bSopenharmony_ci        int32_t option = getopt_long(argc_, argv_, SHORT_OPTIONS_OVERLAY_TARGET.c_str(), LONG_OPTIONS_OVERLAY_TARGET,
1820545fdf9bSopenharmony_ci            nullptr);
1821545fdf9bSopenharmony_ci        APP_LOGD("option: %{public}d, optopt: %{public}d, optind: %{public}d", option, optopt, optind);
1822545fdf9bSopenharmony_ci        if (optind < 0 || optind > argc_) {
1823545fdf9bSopenharmony_ci            return OHOS::ERR_INVALID_VALUE;
1824545fdf9bSopenharmony_ci        }
1825545fdf9bSopenharmony_ci        if (option == -1) {
1826545fdf9bSopenharmony_ci            if (counter == 1) {
1827545fdf9bSopenharmony_ci                if (strcmp(argv_[optind], cmd_.c_str()) == 0) {
1828545fdf9bSopenharmony_ci                    // 1.'bm dump-target-overlay' with no option: bm dump-target-overlay
1829545fdf9bSopenharmony_ci                    // 2.'bm dump-target-overlay' with a wrong argument: bm dump-target-overlay -xxx
1830545fdf9bSopenharmony_ci                    APP_LOGD("'bm dump-target-overlay' %{public}s", HELP_MSG_NO_OPTION.c_str());
1831545fdf9bSopenharmony_ci                    resultReceiver_.append(HELP_MSG_NO_OPTION + "\n");
1832545fdf9bSopenharmony_ci                    result = OHOS::ERR_INVALID_VALUE;
1833545fdf9bSopenharmony_ci                }
1834545fdf9bSopenharmony_ci            }
1835545fdf9bSopenharmony_ci            break;
1836545fdf9bSopenharmony_ci        }
1837545fdf9bSopenharmony_ci        if (option == '?') {
1838545fdf9bSopenharmony_ci            switch (optopt) {
1839545fdf9bSopenharmony_ci                case 'b': {
1840545fdf9bSopenharmony_ci                    // 'bm dump-target-overlay -b' with no argument
1841545fdf9bSopenharmony_ci                    // 'bm dump-target-overlay --bundle-name' with no argument
1842545fdf9bSopenharmony_ci                    APP_LOGD("'bm dump-target-overlay -b' with no argument.");
1843545fdf9bSopenharmony_ci                    resultReceiver_.append(STRING_REQUIRE_CORRECT_VALUE);
1844545fdf9bSopenharmony_ci                    result = OHOS::ERR_INVALID_VALUE;
1845545fdf9bSopenharmony_ci                    break;
1846545fdf9bSopenharmony_ci                }
1847545fdf9bSopenharmony_ci                case 'm': {
1848545fdf9bSopenharmony_ci                    // 'bm dump-target-overlay -m' with no argument: bm enable -m
1849545fdf9bSopenharmony_ci                    // 'bm dump-target-overlay --bundle-name' with no argument: bm dump-target-overlay --bundle-name
1850545fdf9bSopenharmony_ci                    APP_LOGD("'bm dump-target-overlay -m' with no argument.");
1851545fdf9bSopenharmony_ci                    resultReceiver_.append(STRING_REQUIRE_CORRECT_VALUE);
1852545fdf9bSopenharmony_ci                    result = OHOS::ERR_INVALID_VALUE;
1853545fdf9bSopenharmony_ci                    break;
1854545fdf9bSopenharmony_ci                }
1855545fdf9bSopenharmony_ci                case 'u': {
1856545fdf9bSopenharmony_ci                    // 'bm dump-target-overlay -u' with no argument: bm dump-target-overlay -u
1857545fdf9bSopenharmony_ci                    // 'bm dump-target-overlay --user-id' with no argument: bm  dump-target-overlay --user-id
1858545fdf9bSopenharmony_ci                    APP_LOGD("'bm dump-target-overlay -u' with no argument.");
1859545fdf9bSopenharmony_ci                    resultReceiver_.append(STRING_REQUIRE_CORRECT_VALUE);
1860545fdf9bSopenharmony_ci                    result = OHOS::ERR_INVALID_VALUE;
1861545fdf9bSopenharmony_ci                    break;
1862545fdf9bSopenharmony_ci                }
1863545fdf9bSopenharmony_ci                default: {
1864545fdf9bSopenharmony_ci                    // 'bm dump-target-overlay' with an unknown option
1865545fdf9bSopenharmony_ci                    // 'bm dump-target-overlay' with an unknown option
1866545fdf9bSopenharmony_ci                    std::string unknownOption = "";
1867545fdf9bSopenharmony_ci                    std::string unknownOptionMsg = GetUnknownOptionMsg(unknownOption);
1868545fdf9bSopenharmony_ci                    APP_LOGD("'bm dump-target-overlay' with an unknown option.");
1869545fdf9bSopenharmony_ci                    resultReceiver_.append(unknownOptionMsg);
1870545fdf9bSopenharmony_ci                    result = OHOS::ERR_INVALID_VALUE;
1871545fdf9bSopenharmony_ci                    break;
1872545fdf9bSopenharmony_ci                }
1873545fdf9bSopenharmony_ci            }
1874545fdf9bSopenharmony_ci            break;
1875545fdf9bSopenharmony_ci        }
1876545fdf9bSopenharmony_ci
1877545fdf9bSopenharmony_ci        switch (option) {
1878545fdf9bSopenharmony_ci            case 'h': {
1879545fdf9bSopenharmony_ci                // 'bm dump-target-overlay -h'
1880545fdf9bSopenharmony_ci                // 'bm dump-target-overlay --help'
1881545fdf9bSopenharmony_ci                APP_LOGD("'bm dump-target-overlay %{public}s'", argv_[optind - 1]);
1882545fdf9bSopenharmony_ci                result = OHOS::ERR_INVALID_VALUE;
1883545fdf9bSopenharmony_ci                break;
1884545fdf9bSopenharmony_ci            }
1885545fdf9bSopenharmony_ci            case 'b': {
1886545fdf9bSopenharmony_ci                // 'bm dump-target-overlay -b <bundle-name>'
1887545fdf9bSopenharmony_ci                // 'bm dump-target-overlay --bundle-name <bundle-name>'
1888545fdf9bSopenharmony_ci                bundleName = optarg;
1889545fdf9bSopenharmony_ci                break;
1890545fdf9bSopenharmony_ci            }
1891545fdf9bSopenharmony_ci            case 'm': {
1892545fdf9bSopenharmony_ci                // 'bm dump-target-overlay -m <module-name>'
1893545fdf9bSopenharmony_ci                // 'bm dump-target-overlay --module-name <module-name>'
1894545fdf9bSopenharmony_ci                moduleName = optarg;
1895545fdf9bSopenharmony_ci                break;
1896545fdf9bSopenharmony_ci            }
1897545fdf9bSopenharmony_ci            case 'u': {
1898545fdf9bSopenharmony_ci                APP_LOGW("'bm dump-target-overlay -u is not supported'");
1899545fdf9bSopenharmony_ci                break;
1900545fdf9bSopenharmony_ci            }
1901545fdf9bSopenharmony_ci            default: {
1902545fdf9bSopenharmony_ci                result = OHOS::ERR_INVALID_VALUE;
1903545fdf9bSopenharmony_ci                break;
1904545fdf9bSopenharmony_ci            }
1905545fdf9bSopenharmony_ci        }
1906545fdf9bSopenharmony_ci    }
1907545fdf9bSopenharmony_ci    if (result != OHOS::ERR_OK) {
1908545fdf9bSopenharmony_ci        resultReceiver_.append(HELP_MSG_OVERLAY_TARGET);
1909545fdf9bSopenharmony_ci        return result;
1910545fdf9bSopenharmony_ci    }
1911545fdf9bSopenharmony_ci#ifdef BUNDLE_FRAMEWORK_OVERLAY_INSTALLATION
1912545fdf9bSopenharmony_ci    auto res = DumpTargetOverlayInfo(bundleName, moduleName, userId);
1913545fdf9bSopenharmony_ci    if (res.empty()) {
1914545fdf9bSopenharmony_ci        resultReceiver_.append(STRING_DUMP_TARGET_OVERLAY_NG + "\n");
1915545fdf9bSopenharmony_ci    } else {
1916545fdf9bSopenharmony_ci        resultReceiver_.append(STRING_DUMP_TARGET_OVERLAY_OK + "\n");
1917545fdf9bSopenharmony_ci        resultReceiver_.append(res + "\n");
1918545fdf9bSopenharmony_ci    }
1919545fdf9bSopenharmony_ci#else
1920545fdf9bSopenharmony_ci    resultReceiver_.append(MSG_ERR_BUNDLEMANAGER_OVERLAY_FEATURE_IS_NOT_SUPPORTED);
1921545fdf9bSopenharmony_ci#endif
1922545fdf9bSopenharmony_ci    APP_LOGI("end");
1923545fdf9bSopenharmony_ci    return result;
1924545fdf9bSopenharmony_ci}
1925545fdf9bSopenharmony_ci
1926545fdf9bSopenharmony_ci
1927545fdf9bSopenharmony_cistd::string BundleManagerShellCommand::GetUdid() const
1928545fdf9bSopenharmony_ci{
1929545fdf9bSopenharmony_ci    char innerUdid[DEVICE_UDID_LENGTH] = { 0 };
1930545fdf9bSopenharmony_ci    int ret = GetDevUdid(innerUdid, DEVICE_UDID_LENGTH);
1931545fdf9bSopenharmony_ci    if (ret != 0) {
1932545fdf9bSopenharmony_ci        APP_LOGE("GetUdid failed! ret = %{public}d.", ret);
1933545fdf9bSopenharmony_ci        return STRING_GET_UDID_NG;
1934545fdf9bSopenharmony_ci    }
1935545fdf9bSopenharmony_ci    std::string udid = innerUdid;
1936545fdf9bSopenharmony_ci    return udid;
1937545fdf9bSopenharmony_ci}
1938545fdf9bSopenharmony_ci
1939545fdf9bSopenharmony_cistd::string BundleManagerShellCommand::CopyAp(const std::string &bundleName, bool isAllBundle) const
1940545fdf9bSopenharmony_ci{
1941545fdf9bSopenharmony_ci    std::string result = "";
1942545fdf9bSopenharmony_ci    std::vector<std::string> copyApResults;
1943545fdf9bSopenharmony_ci    ErrCode ret = bundleMgrProxy_->CopyAp(bundleName, isAllBundle, copyApResults);
1944545fdf9bSopenharmony_ci    if (ret != ERR_OK) {
1945545fdf9bSopenharmony_ci        APP_LOGE("failed to copy ap! ret = = %{public}d.", ret);
1946545fdf9bSopenharmony_ci        return "";
1947545fdf9bSopenharmony_ci    }
1948545fdf9bSopenharmony_ci    for (const auto &copyApResult : copyApResults) {
1949545fdf9bSopenharmony_ci        result.append("\t");
1950545fdf9bSopenharmony_ci        result.append(copyApResult);
1951545fdf9bSopenharmony_ci        result.append("\n");
1952545fdf9bSopenharmony_ci    }
1953545fdf9bSopenharmony_ci    return result;
1954545fdf9bSopenharmony_ci}
1955545fdf9bSopenharmony_ci
1956545fdf9bSopenharmony_cistd::string BundleManagerShellCommand::CompileProcessAot(
1957545fdf9bSopenharmony_ci    const std::string &bundleName, const std::string &compileMode, bool isAllBundle) const
1958545fdf9bSopenharmony_ci{
1959545fdf9bSopenharmony_ci    std::vector<std::string> compileResults;
1960545fdf9bSopenharmony_ci    ErrCode CompileRet = bundleMgrProxy_->CompileProcessAOT(bundleName, compileMode, isAllBundle, compileResults);
1961545fdf9bSopenharmony_ci    if (CompileRet != ERR_OK) {
1962545fdf9bSopenharmony_ci        std::string result = "error: compile AOT:\n";
1963545fdf9bSopenharmony_ci        for (const auto &compileResult : compileResults) {
1964545fdf9bSopenharmony_ci            result.append("\t");
1965545fdf9bSopenharmony_ci            result.append(compileResult);
1966545fdf9bSopenharmony_ci            result.append("\n");
1967545fdf9bSopenharmony_ci        }
1968545fdf9bSopenharmony_ci        return result;
1969545fdf9bSopenharmony_ci    }
1970545fdf9bSopenharmony_ci    return COMPILE_SUCCESS_OK;
1971545fdf9bSopenharmony_ci}
1972545fdf9bSopenharmony_ci
1973545fdf9bSopenharmony_cistd::string BundleManagerShellCommand::CompileReset(const std::string &bundleName, bool isAllBundle) const
1974545fdf9bSopenharmony_ci{
1975545fdf9bSopenharmony_ci    std::string ResetResults;
1976545fdf9bSopenharmony_ci    ErrCode ResetRet = bundleMgrProxy_->CompileReset(bundleName, isAllBundle);
1977545fdf9bSopenharmony_ci    if (ResetRet == ERR_APPEXECFWK_PARCEL_ERROR) {
1978545fdf9bSopenharmony_ci        APP_LOGE("failed to reset AOT.");
1979545fdf9bSopenharmony_ci        return ResetResults;
1980545fdf9bSopenharmony_ci    }
1981545fdf9bSopenharmony_ci    ResetResults = COMPILE_RESET;
1982545fdf9bSopenharmony_ci    return ResetResults;
1983545fdf9bSopenharmony_ci}
1984545fdf9bSopenharmony_ci
1985545fdf9bSopenharmony_cistd::string BundleManagerShellCommand::DumpBundleList(int32_t userId) const
1986545fdf9bSopenharmony_ci{
1987545fdf9bSopenharmony_ci    std::string dumpResults;
1988545fdf9bSopenharmony_ci    bool dumpRet = bundleMgrProxy_->DumpInfos(
1989545fdf9bSopenharmony_ci        DumpFlag::DUMP_BUNDLE_LIST, BUNDLE_NAME_EMPTY, userId, dumpResults);
1990545fdf9bSopenharmony_ci    if (!dumpRet) {
1991545fdf9bSopenharmony_ci        APP_LOGE("failed to dump bundle list.");
1992545fdf9bSopenharmony_ci    }
1993545fdf9bSopenharmony_ci    return dumpResults;
1994545fdf9bSopenharmony_ci}
1995545fdf9bSopenharmony_ci
1996545fdf9bSopenharmony_cistd::string BundleManagerShellCommand::DumpBundleInfo(const std::string &bundleName, int32_t userId) const
1997545fdf9bSopenharmony_ci{
1998545fdf9bSopenharmony_ci    std::string dumpResults;
1999545fdf9bSopenharmony_ci    bool dumpRet = bundleMgrProxy_->DumpInfos(
2000545fdf9bSopenharmony_ci        DumpFlag::DUMP_BUNDLE_INFO, bundleName, userId, dumpResults);
2001545fdf9bSopenharmony_ci    if (!dumpRet) {
2002545fdf9bSopenharmony_ci        APP_LOGE("failed to dump bundle info.");
2003545fdf9bSopenharmony_ci    }
2004545fdf9bSopenharmony_ci    return dumpResults;
2005545fdf9bSopenharmony_ci}
2006545fdf9bSopenharmony_ci
2007545fdf9bSopenharmony_cistd::string BundleManagerShellCommand::DumpShortcutInfos(const std::string &bundleName, int32_t userId) const
2008545fdf9bSopenharmony_ci{
2009545fdf9bSopenharmony_ci    std::string dumpResults;
2010545fdf9bSopenharmony_ci    bool dumpRet = bundleMgrProxy_->DumpInfos(
2011545fdf9bSopenharmony_ci        DumpFlag::DUMP_SHORTCUT_INFO, bundleName, userId, dumpResults);
2012545fdf9bSopenharmony_ci    if (!dumpRet) {
2013545fdf9bSopenharmony_ci        APP_LOGE("failed to dump shortcut infos.");
2014545fdf9bSopenharmony_ci    }
2015545fdf9bSopenharmony_ci    return dumpResults;
2016545fdf9bSopenharmony_ci}
2017545fdf9bSopenharmony_ci
2018545fdf9bSopenharmony_cistd::string BundleManagerShellCommand::DumpDistributedBundleInfo(
2019545fdf9bSopenharmony_ci    const std::string &deviceId, const std::string &bundleName)
2020545fdf9bSopenharmony_ci{
2021545fdf9bSopenharmony_ci    std::string dumpResults = "";
2022545fdf9bSopenharmony_ci    DistributedBundleInfo distributedBundleInfo;
2023545fdf9bSopenharmony_ci    bool dumpRet = bundleMgrProxy_->GetDistributedBundleInfo(deviceId, bundleName, distributedBundleInfo);
2024545fdf9bSopenharmony_ci    if (!dumpRet) {
2025545fdf9bSopenharmony_ci        APP_LOGE("failed to dump distributed bundleInfo.");
2026545fdf9bSopenharmony_ci    } else {
2027545fdf9bSopenharmony_ci        dumpResults.append("distributed bundleInfo");
2028545fdf9bSopenharmony_ci        dumpResults.append(":\n");
2029545fdf9bSopenharmony_ci        dumpResults.append(distributedBundleInfo.ToString());
2030545fdf9bSopenharmony_ci        dumpResults.append("\n");
2031545fdf9bSopenharmony_ci    }
2032545fdf9bSopenharmony_ci    return dumpResults;
2033545fdf9bSopenharmony_ci}
2034545fdf9bSopenharmony_ci
2035545fdf9bSopenharmony_cistd::string BundleManagerShellCommand::DumpDependentModuleNames(
2036545fdf9bSopenharmony_ci    const std::string &bundleName,
2037545fdf9bSopenharmony_ci    const std::string &moduleName) const
2038545fdf9bSopenharmony_ci{
2039545fdf9bSopenharmony_ci    APP_LOGD("DumpDependentModuleNames bundleName: %{public}s, moduleName: %{public}s",
2040545fdf9bSopenharmony_ci        bundleName.c_str(), moduleName.c_str());
2041545fdf9bSopenharmony_ci    std::string dumpResults = "";
2042545fdf9bSopenharmony_ci    std::vector<std::string> dependentModuleNames;
2043545fdf9bSopenharmony_ci    bool dumpRet = bundleMgrProxy_->GetAllDependentModuleNames(bundleName, moduleName, dependentModuleNames);
2044545fdf9bSopenharmony_ci    if (!dumpRet) {
2045545fdf9bSopenharmony_ci        APP_LOGE("failed to dump dependent module name.");
2046545fdf9bSopenharmony_ci    } else {
2047545fdf9bSopenharmony_ci        dumpResults.append("dependent moduleNames:");
2048545fdf9bSopenharmony_ci        for (const auto &name : dependentModuleNames) {
2049545fdf9bSopenharmony_ci            dumpResults.append("\n");
2050545fdf9bSopenharmony_ci            dumpResults.append(name);
2051545fdf9bSopenharmony_ci        }
2052545fdf9bSopenharmony_ci        dumpResults.append("\n");
2053545fdf9bSopenharmony_ci    }
2054545fdf9bSopenharmony_ci    return dumpResults;
2055545fdf9bSopenharmony_ci}
2056545fdf9bSopenharmony_ci
2057545fdf9bSopenharmony_civoid BundleManagerShellCommand::GetAbsPaths(
2058545fdf9bSopenharmony_ci    const std::vector<std::string> &paths, std::vector<std::string> &absPaths) const
2059545fdf9bSopenharmony_ci{
2060545fdf9bSopenharmony_ci    std::vector<std::string> realPathVec;
2061545fdf9bSopenharmony_ci    for (auto &bundlePath : paths) {
2062545fdf9bSopenharmony_ci        std::string absoluteBundlePath = "";
2063545fdf9bSopenharmony_ci        if (bundlePath.empty()) {
2064545fdf9bSopenharmony_ci            continue;
2065545fdf9bSopenharmony_ci        }
2066545fdf9bSopenharmony_ci        if (bundlePath.at(0) == '/') {
2067545fdf9bSopenharmony_ci            // absolute path
2068545fdf9bSopenharmony_ci            absoluteBundlePath.append(bundlePath);
2069545fdf9bSopenharmony_ci        } else {
2070545fdf9bSopenharmony_ci            // relative path
2071545fdf9bSopenharmony_ci            char *currentPathPtr = getcwd(nullptr, 0);
2072545fdf9bSopenharmony_ci
2073545fdf9bSopenharmony_ci            if (currentPathPtr != nullptr) {
2074545fdf9bSopenharmony_ci                absoluteBundlePath.append(currentPathPtr);
2075545fdf9bSopenharmony_ci                absoluteBundlePath.append('/' + bundlePath);
2076545fdf9bSopenharmony_ci
2077545fdf9bSopenharmony_ci                free(currentPathPtr);
2078545fdf9bSopenharmony_ci                currentPathPtr = nullptr;
2079545fdf9bSopenharmony_ci            }
2080545fdf9bSopenharmony_ci        }
2081545fdf9bSopenharmony_ci        realPathVec.emplace_back(absoluteBundlePath);
2082545fdf9bSopenharmony_ci    }
2083545fdf9bSopenharmony_ci
2084545fdf9bSopenharmony_ci    for (const auto &path : realPathVec) {
2085545fdf9bSopenharmony_ci        if (std::find(absPaths.begin(), absPaths.end(), path) == absPaths.end()) {
2086545fdf9bSopenharmony_ci            absPaths.emplace_back(path);
2087545fdf9bSopenharmony_ci        }
2088545fdf9bSopenharmony_ci    }
2089545fdf9bSopenharmony_ci}
2090545fdf9bSopenharmony_ci
2091545fdf9bSopenharmony_ciint32_t BundleManagerShellCommand::InstallOperation(const std::vector<std::string> &bundlePaths,
2092545fdf9bSopenharmony_ci    InstallParam &installParam, int32_t waittingTime, std::string &resultMsg) const
2093545fdf9bSopenharmony_ci{
2094545fdf9bSopenharmony_ci    std::vector<std::string> pathVec;
2095545fdf9bSopenharmony_ci    GetAbsPaths(bundlePaths, pathVec);
2096545fdf9bSopenharmony_ci
2097545fdf9bSopenharmony_ci    std::vector<std::string> hspPathVec;
2098545fdf9bSopenharmony_ci    GetAbsPaths(installParam.sharedBundleDirPaths, hspPathVec);
2099545fdf9bSopenharmony_ci    installParam.sharedBundleDirPaths = hspPathVec;
2100545fdf9bSopenharmony_ci
2101545fdf9bSopenharmony_ci    sptr<StatusReceiverImpl> statusReceiver(new (std::nothrow) StatusReceiverImpl(waittingTime));
2102545fdf9bSopenharmony_ci    if (statusReceiver == nullptr) {
2103545fdf9bSopenharmony_ci        APP_LOGE("statusReceiver is null");
2104545fdf9bSopenharmony_ci        return IStatusReceiver::ERR_UNKNOWN;
2105545fdf9bSopenharmony_ci    }
2106545fdf9bSopenharmony_ci    sptr<BundleDeathRecipient> recipient(new (std::nothrow) BundleDeathRecipient(statusReceiver));
2107545fdf9bSopenharmony_ci    if (recipient == nullptr) {
2108545fdf9bSopenharmony_ci        APP_LOGE("recipient is null");
2109545fdf9bSopenharmony_ci        return IStatusReceiver::ERR_UNKNOWN;
2110545fdf9bSopenharmony_ci    }
2111545fdf9bSopenharmony_ci    bundleInstallerProxy_->AsObject()->AddDeathRecipient(recipient);
2112545fdf9bSopenharmony_ci    ErrCode res = bundleInstallerProxy_->StreamInstall(pathVec, installParam, statusReceiver);
2113545fdf9bSopenharmony_ci    APP_LOGD("StreamInstall result is %{public}d", res);
2114545fdf9bSopenharmony_ci    if (res == ERR_OK) {
2115545fdf9bSopenharmony_ci        resultMsg = statusReceiver->GetResultMsg();
2116545fdf9bSopenharmony_ci        return statusReceiver->GetResultCode();
2117545fdf9bSopenharmony_ci    }
2118545fdf9bSopenharmony_ci    if (res == ERR_APPEXECFWK_INSTALL_PARAM_ERROR) {
2119545fdf9bSopenharmony_ci        APP_LOGE("install param error");
2120545fdf9bSopenharmony_ci        return IStatusReceiver::ERR_INSTALL_PARAM_ERROR;
2121545fdf9bSopenharmony_ci    }
2122545fdf9bSopenharmony_ci    if (res == ERR_APPEXECFWK_INSTALL_INTERNAL_ERROR) {
2123545fdf9bSopenharmony_ci        APP_LOGE("install internal error");
2124545fdf9bSopenharmony_ci        return IStatusReceiver::ERR_INSTALL_INTERNAL_ERROR;
2125545fdf9bSopenharmony_ci    }
2126545fdf9bSopenharmony_ci    if (res == ERR_APPEXECFWK_INSTALL_FILE_PATH_INVALID) {
2127545fdf9bSopenharmony_ci        APP_LOGE("install invalid path");
2128545fdf9bSopenharmony_ci        return IStatusReceiver::ERR_INSTALL_FILE_PATH_INVALID;
2129545fdf9bSopenharmony_ci    }
2130545fdf9bSopenharmony_ci    if (res == ERR_APPEXECFWK_INSTALL_DISK_MEM_INSUFFICIENT) {
2131545fdf9bSopenharmony_ci        APP_LOGE("install failed due to no space left");
2132545fdf9bSopenharmony_ci        return IStatusReceiver::ERR_INSTALL_DISK_MEM_INSUFFICIENT;
2133545fdf9bSopenharmony_ci    }
2134545fdf9bSopenharmony_ci
2135545fdf9bSopenharmony_ci    return res;
2136545fdf9bSopenharmony_ci}
2137545fdf9bSopenharmony_ci
2138545fdf9bSopenharmony_ciint32_t BundleManagerShellCommand::UninstallOperation(
2139545fdf9bSopenharmony_ci    const std::string &bundleName, const std::string &moduleName, InstallParam &installParam) const
2140545fdf9bSopenharmony_ci{
2141545fdf9bSopenharmony_ci    sptr<StatusReceiverImpl> statusReceiver(new (std::nothrow) StatusReceiverImpl());
2142545fdf9bSopenharmony_ci    if (statusReceiver == nullptr) {
2143545fdf9bSopenharmony_ci        APP_LOGE("statusReceiver is null");
2144545fdf9bSopenharmony_ci        return IStatusReceiver::ERR_UNKNOWN;
2145545fdf9bSopenharmony_ci    }
2146545fdf9bSopenharmony_ci
2147545fdf9bSopenharmony_ci    APP_LOGD("bundleName: %{public}s", bundleName.c_str());
2148545fdf9bSopenharmony_ci    APP_LOGD("moduleName: %{public}s", moduleName.c_str());
2149545fdf9bSopenharmony_ci
2150545fdf9bSopenharmony_ci    sptr<BundleDeathRecipient> recipient(new (std::nothrow) BundleDeathRecipient(statusReceiver));
2151545fdf9bSopenharmony_ci    if (recipient == nullptr) {
2152545fdf9bSopenharmony_ci        APP_LOGE("recipient is null");
2153545fdf9bSopenharmony_ci        return IStatusReceiver::ERR_UNKNOWN;
2154545fdf9bSopenharmony_ci    }
2155545fdf9bSopenharmony_ci    bundleInstallerProxy_->AsObject()->AddDeathRecipient(recipient);
2156545fdf9bSopenharmony_ci    if (moduleName.size() != 0) {
2157545fdf9bSopenharmony_ci        bundleInstallerProxy_->Uninstall(bundleName, moduleName, installParam, statusReceiver);
2158545fdf9bSopenharmony_ci    } else {
2159545fdf9bSopenharmony_ci        bundleInstallerProxy_->Uninstall(bundleName, installParam, statusReceiver);
2160545fdf9bSopenharmony_ci    }
2161545fdf9bSopenharmony_ci
2162545fdf9bSopenharmony_ci    return statusReceiver->GetResultCode();
2163545fdf9bSopenharmony_ci}
2164545fdf9bSopenharmony_ci
2165545fdf9bSopenharmony_ciint32_t BundleManagerShellCommand::UninstallSharedOperation(const UninstallParam &uninstallParam) const
2166545fdf9bSopenharmony_ci{
2167545fdf9bSopenharmony_ci    sptr<StatusReceiverImpl> statusReceiver(new (std::nothrow) StatusReceiverImpl());
2168545fdf9bSopenharmony_ci    if (statusReceiver == nullptr) {
2169545fdf9bSopenharmony_ci        APP_LOGE("statusReceiver is null");
2170545fdf9bSopenharmony_ci        return IStatusReceiver::ERR_UNKNOWN;
2171545fdf9bSopenharmony_ci    }
2172545fdf9bSopenharmony_ci
2173545fdf9bSopenharmony_ci    sptr<BundleDeathRecipient> recipient(new (std::nothrow) BundleDeathRecipient(statusReceiver));
2174545fdf9bSopenharmony_ci    if (recipient == nullptr) {
2175545fdf9bSopenharmony_ci        APP_LOGE("recipient is null");
2176545fdf9bSopenharmony_ci        return IStatusReceiver::ERR_UNKNOWN;
2177545fdf9bSopenharmony_ci    }
2178545fdf9bSopenharmony_ci    bundleInstallerProxy_->AsObject()->AddDeathRecipient(recipient);
2179545fdf9bSopenharmony_ci
2180545fdf9bSopenharmony_ci    bundleInstallerProxy_->Uninstall(uninstallParam, statusReceiver);
2181545fdf9bSopenharmony_ci    return statusReceiver->GetResultCode();
2182545fdf9bSopenharmony_ci}
2183545fdf9bSopenharmony_ci
2184545fdf9bSopenharmony_cibool BundleManagerShellCommand::CleanBundleCacheFilesOperation(const std::string &bundleName, int32_t userId,
2185545fdf9bSopenharmony_ci    int32_t appIndex) const
2186545fdf9bSopenharmony_ci{
2187545fdf9bSopenharmony_ci    userId = BundleCommandCommon::GetCurrentUserId(userId);
2188545fdf9bSopenharmony_ci    APP_LOGD("bundleName: %{public}s, userId:%{public}d, appIndex:%{public}d", bundleName.c_str(), userId, appIndex);
2189545fdf9bSopenharmony_ci    sptr<CleanCacheCallbackImpl> cleanCacheCallBack(new (std::nothrow) CleanCacheCallbackImpl());
2190545fdf9bSopenharmony_ci    if (cleanCacheCallBack == nullptr) {
2191545fdf9bSopenharmony_ci        APP_LOGE("cleanCacheCallBack is null");
2192545fdf9bSopenharmony_ci        return false;
2193545fdf9bSopenharmony_ci    }
2194545fdf9bSopenharmony_ci    ErrCode cleanRet = bundleMgrProxy_->CleanBundleCacheFiles(bundleName, cleanCacheCallBack, userId, appIndex);
2195545fdf9bSopenharmony_ci    if (cleanRet == ERR_OK) {
2196545fdf9bSopenharmony_ci        return cleanCacheCallBack->GetResultCode();
2197545fdf9bSopenharmony_ci    }
2198545fdf9bSopenharmony_ci    APP_LOGE("clean bundle cache files operation failed, cleanRet = %{public}d", cleanRet);
2199545fdf9bSopenharmony_ci    return false;
2200545fdf9bSopenharmony_ci}
2201545fdf9bSopenharmony_ci
2202545fdf9bSopenharmony_cibool BundleManagerShellCommand::CleanBundleDataFilesOperation(const std::string &bundleName, int32_t userId,
2203545fdf9bSopenharmony_ci    int32_t appIndex) const
2204545fdf9bSopenharmony_ci{
2205545fdf9bSopenharmony_ci    userId = BundleCommandCommon::GetCurrentUserId(userId);
2206545fdf9bSopenharmony_ci    APP_LOGD("bundleName: %{public}s, userId:%{public}d, appIndex:%{public}d", bundleName.c_str(), userId, appIndex);
2207545fdf9bSopenharmony_ci    auto appMgrClient = std::make_unique<AppMgrClient>();
2208545fdf9bSopenharmony_ci    APP_LOGI("clear start");
2209545fdf9bSopenharmony_ci    ErrCode cleanRetAms = appMgrClient->ClearUpApplicationData(bundleName, appIndex, userId);
2210545fdf9bSopenharmony_ci    APP_LOGI("clear end");
2211545fdf9bSopenharmony_ci    bool cleanRetBms = bundleMgrProxy_->CleanBundleDataFiles(bundleName, userId, appIndex);
2212545fdf9bSopenharmony_ci    APP_LOGD("cleanRetAms: %{public}d, cleanRetBms: %{public}d", cleanRetAms, cleanRetBms);
2213545fdf9bSopenharmony_ci    if ((cleanRetAms == ERR_OK) && cleanRetBms) {
2214545fdf9bSopenharmony_ci        return true;
2215545fdf9bSopenharmony_ci    }
2216545fdf9bSopenharmony_ci    APP_LOGE("clean bundle data files operation failed");
2217545fdf9bSopenharmony_ci    return false;
2218545fdf9bSopenharmony_ci}
2219545fdf9bSopenharmony_ci
2220545fdf9bSopenharmony_cibool BundleManagerShellCommand::SetApplicationEnabledOperation(const AbilityInfo &abilityInfo,
2221545fdf9bSopenharmony_ci    bool isEnable, int32_t userId) const
2222545fdf9bSopenharmony_ci{
2223545fdf9bSopenharmony_ci    APP_LOGD("bundleName: %{public}s", abilityInfo.bundleName.c_str());
2224545fdf9bSopenharmony_ci    userId = BundleCommandCommon::GetCurrentUserId(userId);
2225545fdf9bSopenharmony_ci    int32_t ret;
2226545fdf9bSopenharmony_ci    if (abilityInfo.name.size() == 0) {
2227545fdf9bSopenharmony_ci        ret = bundleMgrProxy_->SetApplicationEnabled(abilityInfo.bundleName, isEnable, userId);
2228545fdf9bSopenharmony_ci    } else {
2229545fdf9bSopenharmony_ci        ret = bundleMgrProxy_->SetAbilityEnabled(abilityInfo, isEnable, userId);
2230545fdf9bSopenharmony_ci    }
2231545fdf9bSopenharmony_ci    if (ret != 0) {
2232545fdf9bSopenharmony_ci        if (isEnable) {
2233545fdf9bSopenharmony_ci            APP_LOGE("enable bundle failed");
2234545fdf9bSopenharmony_ci        } else {
2235545fdf9bSopenharmony_ci            APP_LOGE("disable bundle failed");
2236545fdf9bSopenharmony_ci        }
2237545fdf9bSopenharmony_ci        return false;
2238545fdf9bSopenharmony_ci    }
2239545fdf9bSopenharmony_ci    return true;
2240545fdf9bSopenharmony_ci}
2241545fdf9bSopenharmony_ci
2242545fdf9bSopenharmony_cistd::string BundleManagerShellCommand::DumpOverlayInfo(const std::string &bundleName, const std::string &moduleName,
2243545fdf9bSopenharmony_ci    const std::string &targetModuleName, int32_t userId)
2244545fdf9bSopenharmony_ci{
2245545fdf9bSopenharmony_ci    std::string res = "";
2246545fdf9bSopenharmony_ci    if ((bundleName.empty()) || (!moduleName.empty() && !targetModuleName.empty())) {
2247545fdf9bSopenharmony_ci        APP_LOGE("error value of the dump-overlay command options");
2248545fdf9bSopenharmony_ci        return res;
2249545fdf9bSopenharmony_ci    }
2250545fdf9bSopenharmony_ci
2251545fdf9bSopenharmony_ci    auto overlayManagerProxy = bundleMgrProxy_->GetOverlayManagerProxy();
2252545fdf9bSopenharmony_ci    if (overlayManagerProxy == nullptr) {
2253545fdf9bSopenharmony_ci        APP_LOGE("overlayManagerProxy is null");
2254545fdf9bSopenharmony_ci        return res;
2255545fdf9bSopenharmony_ci    }
2256545fdf9bSopenharmony_ci    std::vector<OverlayModuleInfo> overlayModuleInfos;
2257545fdf9bSopenharmony_ci    OverlayModuleInfo overlayModuleInfo;
2258545fdf9bSopenharmony_ci    ErrCode ret = ERR_OK;
2259545fdf9bSopenharmony_ci    userId = BundleCommandCommon::GetCurrentUserId(userId);
2260545fdf9bSopenharmony_ci    if (moduleName.empty() && targetModuleName.empty()) {
2261545fdf9bSopenharmony_ci        ret = overlayManagerProxy->GetAllOverlayModuleInfo(bundleName, overlayModuleInfos, userId);
2262545fdf9bSopenharmony_ci        if (overlayModuleInfos.empty()) {
2263545fdf9bSopenharmony_ci            ret = ERR_BUNDLEMANAGER_OVERLAY_QUERY_FAILED_NO_OVERLAY_MODULE_INFO;
2264545fdf9bSopenharmony_ci        }
2265545fdf9bSopenharmony_ci    } else if (!moduleName.empty()) {
2266545fdf9bSopenharmony_ci        ret = overlayManagerProxy->GetOverlayModuleInfo(bundleName, moduleName, overlayModuleInfo, userId);
2267545fdf9bSopenharmony_ci    } else {
2268545fdf9bSopenharmony_ci        ret = overlayManagerProxy->GetOverlayModuleInfoForTarget(bundleName, targetModuleName, overlayModuleInfos,
2269545fdf9bSopenharmony_ci            userId);
2270545fdf9bSopenharmony_ci        if (overlayModuleInfos.empty()) {
2271545fdf9bSopenharmony_ci            ret = ERR_BUNDLEMANAGER_OVERLAY_QUERY_FAILED_NO_OVERLAY_MODULE_INFO;
2272545fdf9bSopenharmony_ci        }
2273545fdf9bSopenharmony_ci    }
2274545fdf9bSopenharmony_ci    if (ret != ERR_OK) {
2275545fdf9bSopenharmony_ci        APP_LOGE("dump-overlay failed due to errcode %{public}d", ret);
2276545fdf9bSopenharmony_ci        return res;
2277545fdf9bSopenharmony_ci    }
2278545fdf9bSopenharmony_ci
2279545fdf9bSopenharmony_ci    nlohmann::json overlayInfoJson;
2280545fdf9bSopenharmony_ci    if (!overlayModuleInfos.empty()) {
2281545fdf9bSopenharmony_ci        overlayInfoJson = nlohmann::json {{OVERLAY_MODULE_INFOS, overlayModuleInfos}};
2282545fdf9bSopenharmony_ci    } else {
2283545fdf9bSopenharmony_ci        overlayInfoJson = nlohmann::json {{OVERLAY_MODULE_INFO, overlayModuleInfo}};
2284545fdf9bSopenharmony_ci    }
2285545fdf9bSopenharmony_ci    return overlayInfoJson.dump(Constants::DUMP_INDENT);
2286545fdf9bSopenharmony_ci}
2287545fdf9bSopenharmony_ci
2288545fdf9bSopenharmony_cistd::string BundleManagerShellCommand::DumpTargetOverlayInfo(const std::string &bundleName,
2289545fdf9bSopenharmony_ci    const std::string &moduleName, int32_t userId)
2290545fdf9bSopenharmony_ci{
2291545fdf9bSopenharmony_ci    std::string res = "";
2292545fdf9bSopenharmony_ci    if (bundleName.empty()) {
2293545fdf9bSopenharmony_ci        APP_LOGE("error value of the dump-target-overlay command options");
2294545fdf9bSopenharmony_ci        return res;
2295545fdf9bSopenharmony_ci    }
2296545fdf9bSopenharmony_ci    auto overlayManagerProxy = bundleMgrProxy_->GetOverlayManagerProxy();
2297545fdf9bSopenharmony_ci    if (overlayManagerProxy == nullptr) {
2298545fdf9bSopenharmony_ci        APP_LOGE("overlayManagerProxy is null");
2299545fdf9bSopenharmony_ci        return res;
2300545fdf9bSopenharmony_ci    }
2301545fdf9bSopenharmony_ci
2302545fdf9bSopenharmony_ci    userId = BundleCommandCommon::GetCurrentUserId(userId);
2303545fdf9bSopenharmony_ci    ErrCode ret = ERR_OK;
2304545fdf9bSopenharmony_ci    nlohmann::json overlayInfoJson;
2305545fdf9bSopenharmony_ci    if (moduleName.empty()) {
2306545fdf9bSopenharmony_ci        std::vector<OverlayBundleInfo> overlayBundleInfos;
2307545fdf9bSopenharmony_ci        ret = overlayManagerProxy->GetOverlayBundleInfoForTarget(bundleName, overlayBundleInfos, userId);
2308545fdf9bSopenharmony_ci        if (ret != ERR_OK || overlayBundleInfos.empty()) {
2309545fdf9bSopenharmony_ci            APP_LOGE("dump-target-overlay failed due to errcode %{public}d", ret);
2310545fdf9bSopenharmony_ci            return res;
2311545fdf9bSopenharmony_ci        }
2312545fdf9bSopenharmony_ci        overlayInfoJson = nlohmann::json {{OVERLAY_BUNDLE_INFOS, overlayBundleInfos}};
2313545fdf9bSopenharmony_ci    } else {
2314545fdf9bSopenharmony_ci        std::vector<OverlayModuleInfo> overlayModuleInfos;
2315545fdf9bSopenharmony_ci        ret = overlayManagerProxy->GetOverlayModuleInfoForTarget(bundleName, moduleName, overlayModuleInfos, userId);
2316545fdf9bSopenharmony_ci        if (ret != ERR_OK || overlayModuleInfos.empty()) {
2317545fdf9bSopenharmony_ci            APP_LOGE("dump-target-overlay failed due to errcode %{public}d", ret);
2318545fdf9bSopenharmony_ci            return res;
2319545fdf9bSopenharmony_ci        }
2320545fdf9bSopenharmony_ci        overlayInfoJson = nlohmann::json {{OVERLAY_MODULE_INFOS, overlayModuleInfos}};
2321545fdf9bSopenharmony_ci    }
2322545fdf9bSopenharmony_ci    return overlayInfoJson.dump(Constants::DUMP_INDENT);
2323545fdf9bSopenharmony_ci}
2324545fdf9bSopenharmony_ci
2325545fdf9bSopenharmony_ciErrCode BundleManagerShellCommand::RunAsDumpSharedDependenciesCommand()
2326545fdf9bSopenharmony_ci{
2327545fdf9bSopenharmony_ci    APP_LOGI("begin to RunAsDumpSharedDependenciesCommand");
2328545fdf9bSopenharmony_ci    int32_t result = OHOS::ERR_OK;
2329545fdf9bSopenharmony_ci    int32_t counter = 0;
2330545fdf9bSopenharmony_ci    std::string bundleName;
2331545fdf9bSopenharmony_ci    std::string moduleName;
2332545fdf9bSopenharmony_ci    while (true) {
2333545fdf9bSopenharmony_ci        counter++;
2334545fdf9bSopenharmony_ci        int32_t option = getopt_long(argc_, argv_, SHORT_OPTIONS_DUMP_SHARED_DEPENDENCIES.c_str(),
2335545fdf9bSopenharmony_ci            LONG_OPTIONS_DUMP_SHARED_DEPENDENCIES, nullptr);
2336545fdf9bSopenharmony_ci        if (optind < 0 || optind > argc_) {
2337545fdf9bSopenharmony_ci            return OHOS::ERR_INVALID_VALUE;
2338545fdf9bSopenharmony_ci        }
2339545fdf9bSopenharmony_ci        if (option == -1) {
2340545fdf9bSopenharmony_ci            if (counter == 1) {
2341545fdf9bSopenharmony_ci                // When scanning the first argument
2342545fdf9bSopenharmony_ci                if (strcmp(argv_[optind], cmd_.c_str()) == 0) {
2343545fdf9bSopenharmony_ci                    // 'bm dump-dependencies' with no option: bm dump-dependencies
2344545fdf9bSopenharmony_ci                    // 'bm dump-dependencies' with a wrong argument: bm dump-dependencies xxx
2345545fdf9bSopenharmony_ci                    resultReceiver_.append(HELP_MSG_NO_OPTION + "\n");
2346545fdf9bSopenharmony_ci                    result = OHOS::ERR_INVALID_VALUE;
2347545fdf9bSopenharmony_ci                }
2348545fdf9bSopenharmony_ci            }
2349545fdf9bSopenharmony_ci            break;
2350545fdf9bSopenharmony_ci        }
2351545fdf9bSopenharmony_ci        result = ParseSharedDependenciesCommand(option, bundleName, moduleName);
2352545fdf9bSopenharmony_ci        if (option == '?') {
2353545fdf9bSopenharmony_ci            break;
2354545fdf9bSopenharmony_ci        }
2355545fdf9bSopenharmony_ci    }
2356545fdf9bSopenharmony_ci    if (result == OHOS::ERR_OK) {
2357545fdf9bSopenharmony_ci        if ((resultReceiver_ == "") && (bundleName.size() == 0 || moduleName.size() == 0)) {
2358545fdf9bSopenharmony_ci            // 'bm dump-dependencies -n -m ...' with no bundle name option
2359545fdf9bSopenharmony_ci            resultReceiver_.append(HELP_MSG_NO_REMOVABLE_OPTION);
2360545fdf9bSopenharmony_ci            result = OHOS::ERR_INVALID_VALUE;
2361545fdf9bSopenharmony_ci        }
2362545fdf9bSopenharmony_ci    }
2363545fdf9bSopenharmony_ci    if (result != OHOS::ERR_OK) {
2364545fdf9bSopenharmony_ci        resultReceiver_.append(HELP_MSG_DUMP_SHARED_DEPENDENCIES);
2365545fdf9bSopenharmony_ci    } else {
2366545fdf9bSopenharmony_ci        std::string dumpResults = DumpSharedDependencies(bundleName, moduleName);
2367545fdf9bSopenharmony_ci        if (dumpResults.empty() || (dumpResults == "")) {
2368545fdf9bSopenharmony_ci            dumpResults = HELP_MSG_DUMP_FAILED + "\n";
2369545fdf9bSopenharmony_ci        }
2370545fdf9bSopenharmony_ci        resultReceiver_.append(dumpResults);
2371545fdf9bSopenharmony_ci    }
2372545fdf9bSopenharmony_ci    APP_LOGI("end");
2373545fdf9bSopenharmony_ci    return result;
2374545fdf9bSopenharmony_ci}
2375545fdf9bSopenharmony_ci
2376545fdf9bSopenharmony_ciErrCode BundleManagerShellCommand::ParseSharedDependenciesCommand(int32_t option, std::string &bundleName,
2377545fdf9bSopenharmony_ci    std::string &moduleName)
2378545fdf9bSopenharmony_ci{
2379545fdf9bSopenharmony_ci    int32_t result = OHOS::ERR_OK;
2380545fdf9bSopenharmony_ci    if (option == '?') {
2381545fdf9bSopenharmony_ci        switch (optopt) {
2382545fdf9bSopenharmony_ci            case 'n': {
2383545fdf9bSopenharmony_ci                // 'bm dump-dependencies -n' with no argument: bm dump-dependencies -n
2384545fdf9bSopenharmony_ci                // 'bm dump-dependencies --bundle-name' with no argument: bm dump-dependencies --bundle-name
2385545fdf9bSopenharmony_ci                resultReceiver_.append(STRING_REQUIRE_CORRECT_VALUE);
2386545fdf9bSopenharmony_ci                result = OHOS::ERR_INVALID_VALUE;
2387545fdf9bSopenharmony_ci                break;
2388545fdf9bSopenharmony_ci            }
2389545fdf9bSopenharmony_ci            case 'm': {
2390545fdf9bSopenharmony_ci                // 'bm dump-dependencies -m' with no argument: bm dump-dependencies -m
2391545fdf9bSopenharmony_ci                // 'bm dump-dependencies --module-name' with no argument: bm dump-dependencies --module-name
2392545fdf9bSopenharmony_ci                resultReceiver_.append(STRING_REQUIRE_CORRECT_VALUE);
2393545fdf9bSopenharmony_ci                result = OHOS::ERR_INVALID_VALUE;
2394545fdf9bSopenharmony_ci                break;
2395545fdf9bSopenharmony_ci            }
2396545fdf9bSopenharmony_ci            default: {
2397545fdf9bSopenharmony_ci                // 'bm dump-dependencies' with an unknown option: bm dump-dependencies -x
2398545fdf9bSopenharmony_ci                // 'bm dump-dependencies' with an unknown option: bm dump-dependencies -xxx
2399545fdf9bSopenharmony_ci                std::string unknownOption = "";
2400545fdf9bSopenharmony_ci                std::string unknownOptionMsg = GetUnknownOptionMsg(unknownOption);
2401545fdf9bSopenharmony_ci                resultReceiver_.append(unknownOptionMsg);
2402545fdf9bSopenharmony_ci                result = OHOS::ERR_INVALID_VALUE;
2403545fdf9bSopenharmony_ci                break;
2404545fdf9bSopenharmony_ci            }
2405545fdf9bSopenharmony_ci        }
2406545fdf9bSopenharmony_ci    } else {
2407545fdf9bSopenharmony_ci        switch (option) {
2408545fdf9bSopenharmony_ci            case 'h': {
2409545fdf9bSopenharmony_ci                // 'bm dump-dependencies -h'
2410545fdf9bSopenharmony_ci                // 'bm dump-dependencies --help'
2411545fdf9bSopenharmony_ci                result = OHOS::ERR_INVALID_VALUE;
2412545fdf9bSopenharmony_ci                break;
2413545fdf9bSopenharmony_ci            }
2414545fdf9bSopenharmony_ci            case 'n': {
2415545fdf9bSopenharmony_ci                // 'bm dump-dependencies -n xxx'
2416545fdf9bSopenharmony_ci                // 'bm dump-dependencies --bundle-name xxx'
2417545fdf9bSopenharmony_ci                bundleName = optarg;
2418545fdf9bSopenharmony_ci                break;
2419545fdf9bSopenharmony_ci            }
2420545fdf9bSopenharmony_ci            case 'm': {
2421545fdf9bSopenharmony_ci                // 'bm dump-dependencies -m xxx'
2422545fdf9bSopenharmony_ci                // 'bm dump-dependencies --module-name xxx'
2423545fdf9bSopenharmony_ci                moduleName = optarg;
2424545fdf9bSopenharmony_ci                break;
2425545fdf9bSopenharmony_ci            }
2426545fdf9bSopenharmony_ci            default: {
2427545fdf9bSopenharmony_ci                result = OHOS::ERR_INVALID_VALUE;
2428545fdf9bSopenharmony_ci                break;
2429545fdf9bSopenharmony_ci            }
2430545fdf9bSopenharmony_ci        }
2431545fdf9bSopenharmony_ci    }
2432545fdf9bSopenharmony_ci    return result;
2433545fdf9bSopenharmony_ci}
2434545fdf9bSopenharmony_ci
2435545fdf9bSopenharmony_cistd::string BundleManagerShellCommand::DumpSharedDependencies(const std::string &bundleName,
2436545fdf9bSopenharmony_ci    const std::string &moduleName) const
2437545fdf9bSopenharmony_ci{
2438545fdf9bSopenharmony_ci    APP_LOGD("DumpSharedDependencies bundleName: %{public}s, moduleName: %{public}s",
2439545fdf9bSopenharmony_ci        bundleName.c_str(), moduleName.c_str());
2440545fdf9bSopenharmony_ci    std::vector<Dependency> dependencies;
2441545fdf9bSopenharmony_ci    ErrCode ret = bundleMgrProxy_->GetSharedDependencies(bundleName, moduleName, dependencies);
2442545fdf9bSopenharmony_ci    nlohmann::json dependenciesJson;
2443545fdf9bSopenharmony_ci    if (ret != ERR_OK) {
2444545fdf9bSopenharmony_ci        APP_LOGE("dump shared dependencies failed due to errcode %{public}d", ret);
2445545fdf9bSopenharmony_ci        return std::string();
2446545fdf9bSopenharmony_ci    } else {
2447545fdf9bSopenharmony_ci        dependenciesJson = nlohmann::json {{DEPENDENCIES, dependencies}};
2448545fdf9bSopenharmony_ci    }
2449545fdf9bSopenharmony_ci    return dependenciesJson.dump(Constants::DUMP_INDENT);
2450545fdf9bSopenharmony_ci}
2451545fdf9bSopenharmony_ci
2452545fdf9bSopenharmony_ciErrCode BundleManagerShellCommand::RunAsDumpSharedCommand()
2453545fdf9bSopenharmony_ci{
2454545fdf9bSopenharmony_ci    APP_LOGI("begin to RunAsDumpSharedCommand");
2455545fdf9bSopenharmony_ci    int32_t result = OHOS::ERR_OK;
2456545fdf9bSopenharmony_ci    int32_t counter = 0;
2457545fdf9bSopenharmony_ci    std::string bundleName;
2458545fdf9bSopenharmony_ci    bool dumpSharedAll = false;
2459545fdf9bSopenharmony_ci    while (true) {
2460545fdf9bSopenharmony_ci        counter++;
2461545fdf9bSopenharmony_ci        int32_t option = getopt_long(argc_, argv_, SHORT_OPTIONS_DUMP_SHARED.c_str(),
2462545fdf9bSopenharmony_ci            LONG_OPTIONS_DUMP_SHARED, nullptr);
2463545fdf9bSopenharmony_ci        if (optind < 0 || optind > argc_) {
2464545fdf9bSopenharmony_ci            return OHOS::ERR_INVALID_VALUE;
2465545fdf9bSopenharmony_ci        }
2466545fdf9bSopenharmony_ci        if (option == -1) {
2467545fdf9bSopenharmony_ci            if (counter == 1) {
2468545fdf9bSopenharmony_ci                // When scanning the first argument
2469545fdf9bSopenharmony_ci                if (strcmp(argv_[optind], cmd_.c_str()) == 0) {
2470545fdf9bSopenharmony_ci                    // 'bm dump-shared' with no option: bm dump-shared
2471545fdf9bSopenharmony_ci                    // 'bm dump-shared' with a wrong argument: bm dump-shared xxx
2472545fdf9bSopenharmony_ci                    resultReceiver_.append(HELP_MSG_NO_OPTION + "\n");
2473545fdf9bSopenharmony_ci                    result = OHOS::ERR_INVALID_VALUE;
2474545fdf9bSopenharmony_ci                }
2475545fdf9bSopenharmony_ci            }
2476545fdf9bSopenharmony_ci            break;
2477545fdf9bSopenharmony_ci        }
2478545fdf9bSopenharmony_ci        result = ParseSharedCommand(option, bundleName, dumpSharedAll);
2479545fdf9bSopenharmony_ci        if (option == '?') {
2480545fdf9bSopenharmony_ci            break;
2481545fdf9bSopenharmony_ci        }
2482545fdf9bSopenharmony_ci    }
2483545fdf9bSopenharmony_ci    if (result != OHOS::ERR_OK) {
2484545fdf9bSopenharmony_ci        resultReceiver_.append(HELP_MSG_DUMP_SHARED);
2485545fdf9bSopenharmony_ci    } else if (dumpSharedAll) {
2486545fdf9bSopenharmony_ci        std::string dumpResults = DumpSharedAll();
2487545fdf9bSopenharmony_ci        resultReceiver_.append(dumpResults);
2488545fdf9bSopenharmony_ci    } else {
2489545fdf9bSopenharmony_ci        if ((resultReceiver_ == "") && (bundleName.size() == 0)) {
2490545fdf9bSopenharmony_ci            // 'bm dump-shared -n ...' with no bundle name option
2491545fdf9bSopenharmony_ci            resultReceiver_.append(HELP_MSG_NO_REMOVABLE_OPTION);
2492545fdf9bSopenharmony_ci            result = OHOS::ERR_INVALID_VALUE;
2493545fdf9bSopenharmony_ci            return result;
2494545fdf9bSopenharmony_ci        }
2495545fdf9bSopenharmony_ci        std::string dumpResults = DumpShared(bundleName);
2496545fdf9bSopenharmony_ci        if (dumpResults.empty() || (dumpResults == "")) {
2497545fdf9bSopenharmony_ci            dumpResults = HELP_MSG_DUMP_FAILED + "\n";
2498545fdf9bSopenharmony_ci        }
2499545fdf9bSopenharmony_ci        resultReceiver_.append(dumpResults);
2500545fdf9bSopenharmony_ci    }
2501545fdf9bSopenharmony_ci    APP_LOGI("end");
2502545fdf9bSopenharmony_ci    return result;
2503545fdf9bSopenharmony_ci}
2504545fdf9bSopenharmony_ci
2505545fdf9bSopenharmony_ciErrCode BundleManagerShellCommand::ParseSharedCommand(int32_t option, std::string &bundleName, bool &dumpSharedAll)
2506545fdf9bSopenharmony_ci{
2507545fdf9bSopenharmony_ci    int32_t result = OHOS::ERR_OK;
2508545fdf9bSopenharmony_ci    if (option == '?') {
2509545fdf9bSopenharmony_ci        switch (optopt) {
2510545fdf9bSopenharmony_ci            case 'n': {
2511545fdf9bSopenharmony_ci                // 'bm dump-shared -n' with no argument: bm dump-shared -n
2512545fdf9bSopenharmony_ci                // 'bm dump-shared --bundle-name' with no argument: bm dump-shared --bundle-name
2513545fdf9bSopenharmony_ci                resultReceiver_.append(STRING_REQUIRE_CORRECT_VALUE);
2514545fdf9bSopenharmony_ci                result = OHOS::ERR_INVALID_VALUE;
2515545fdf9bSopenharmony_ci                break;
2516545fdf9bSopenharmony_ci            }
2517545fdf9bSopenharmony_ci            default: {
2518545fdf9bSopenharmony_ci                // 'bm dump-shared' with an unknown option: bm dump-shared -x
2519545fdf9bSopenharmony_ci                // 'bm dump-shared' with an unknown option: bm dump-shared -xxx
2520545fdf9bSopenharmony_ci                std::string unknownOption = "";
2521545fdf9bSopenharmony_ci                std::string unknownOptionMsg = GetUnknownOptionMsg(unknownOption);
2522545fdf9bSopenharmony_ci                resultReceiver_.append(unknownOptionMsg);
2523545fdf9bSopenharmony_ci                result = OHOS::ERR_INVALID_VALUE;
2524545fdf9bSopenharmony_ci                break;
2525545fdf9bSopenharmony_ci            }
2526545fdf9bSopenharmony_ci        }
2527545fdf9bSopenharmony_ci    } else {
2528545fdf9bSopenharmony_ci        switch (option) {
2529545fdf9bSopenharmony_ci            case 'h': {
2530545fdf9bSopenharmony_ci                // 'bm dump-shared -h'
2531545fdf9bSopenharmony_ci                // 'bm dump-shared --help'
2532545fdf9bSopenharmony_ci                result = OHOS::ERR_INVALID_VALUE;
2533545fdf9bSopenharmony_ci                break;
2534545fdf9bSopenharmony_ci            }
2535545fdf9bSopenharmony_ci            case 'n': {
2536545fdf9bSopenharmony_ci                // 'bm dump-shared -n xxx'
2537545fdf9bSopenharmony_ci                // 'bm dump-shared --bundle-name xxx'
2538545fdf9bSopenharmony_ci                bundleName = optarg;
2539545fdf9bSopenharmony_ci                break;
2540545fdf9bSopenharmony_ci            }
2541545fdf9bSopenharmony_ci            case 'a': {
2542545fdf9bSopenharmony_ci                // 'bm dump-shared -a'
2543545fdf9bSopenharmony_ci                // 'bm dump-shared --all'
2544545fdf9bSopenharmony_ci                dumpSharedAll = true;
2545545fdf9bSopenharmony_ci                break;
2546545fdf9bSopenharmony_ci            }
2547545fdf9bSopenharmony_ci            default: {
2548545fdf9bSopenharmony_ci                result = OHOS::ERR_INVALID_VALUE;
2549545fdf9bSopenharmony_ci                break;
2550545fdf9bSopenharmony_ci            }
2551545fdf9bSopenharmony_ci        }
2552545fdf9bSopenharmony_ci    }
2553545fdf9bSopenharmony_ci    return result;
2554545fdf9bSopenharmony_ci}
2555545fdf9bSopenharmony_ci
2556545fdf9bSopenharmony_cistd::string BundleManagerShellCommand::DumpShared(const std::string &bundleName) const
2557545fdf9bSopenharmony_ci{
2558545fdf9bSopenharmony_ci    APP_LOGD("DumpShared bundleName: %{public}s", bundleName.c_str());
2559545fdf9bSopenharmony_ci    SharedBundleInfo sharedBundleInfo;
2560545fdf9bSopenharmony_ci    ErrCode ret = bundleMgrProxy_->GetSharedBundleInfoBySelf(bundleName, sharedBundleInfo);
2561545fdf9bSopenharmony_ci    nlohmann::json sharedBundleInfoJson;
2562545fdf9bSopenharmony_ci    if (ret != ERR_OK) {
2563545fdf9bSopenharmony_ci        APP_LOGE("dump-shared failed due to errcode %{public}d", ret);
2564545fdf9bSopenharmony_ci        return std::string();
2565545fdf9bSopenharmony_ci    } else {
2566545fdf9bSopenharmony_ci        sharedBundleInfoJson = nlohmann::json {{SHARED_BUNDLE_INFO, sharedBundleInfo}};
2567545fdf9bSopenharmony_ci    }
2568545fdf9bSopenharmony_ci    return sharedBundleInfoJson.dump(Constants::DUMP_INDENT);
2569545fdf9bSopenharmony_ci}
2570545fdf9bSopenharmony_ci
2571545fdf9bSopenharmony_cistd::string BundleManagerShellCommand::DumpSharedAll() const
2572545fdf9bSopenharmony_ci{
2573545fdf9bSopenharmony_ci    APP_LOGD("DumpSharedAll");
2574545fdf9bSopenharmony_ci    std::string dumpResults = "";
2575545fdf9bSopenharmony_ci    std::vector<SharedBundleInfo> sharedBundleInfos;
2576545fdf9bSopenharmony_ci    ErrCode ret = bundleMgrProxy_->GetAllSharedBundleInfo(sharedBundleInfos);
2577545fdf9bSopenharmony_ci    if (ret != ERR_OK) {
2578545fdf9bSopenharmony_ci        APP_LOGE("dump-shared all failed due to errcode %{public}d", ret);
2579545fdf9bSopenharmony_ci        return dumpResults;
2580545fdf9bSopenharmony_ci    }
2581545fdf9bSopenharmony_ci    for (const auto& item : sharedBundleInfos) {
2582545fdf9bSopenharmony_ci        dumpResults.append("\t");
2583545fdf9bSopenharmony_ci        dumpResults.append(item.name);
2584545fdf9bSopenharmony_ci        dumpResults.append("\n");
2585545fdf9bSopenharmony_ci    }
2586545fdf9bSopenharmony_ci    return dumpResults;
2587545fdf9bSopenharmony_ci}
2588545fdf9bSopenharmony_ci
2589545fdf9bSopenharmony_ciErrCode BundleManagerShellCommand::DeployQuickFixDisable(const std::vector<std::string> &quickFixFiles,
2590545fdf9bSopenharmony_ci    std::shared_ptr<QuickFixResult> &quickFixRes, bool isDebug, const std::string &targetPath) const
2591545fdf9bSopenharmony_ci{
2592545fdf9bSopenharmony_ci    std::set<std::string> realPathSet;
2593545fdf9bSopenharmony_ci    for (const auto &quickFixPath : quickFixFiles) {
2594545fdf9bSopenharmony_ci        std::string realPath;
2595545fdf9bSopenharmony_ci        if (!PathToRealPath(quickFixPath, realPath)) {
2596545fdf9bSopenharmony_ci            APP_LOGW("quickFixPath %{public}s is invalid", quickFixPath.c_str());
2597545fdf9bSopenharmony_ci            continue;
2598545fdf9bSopenharmony_ci        }
2599545fdf9bSopenharmony_ci        APP_LOGD("realPath is %{public}s", realPath.c_str());
2600545fdf9bSopenharmony_ci        realPathSet.insert(realPath);
2601545fdf9bSopenharmony_ci    }
2602545fdf9bSopenharmony_ci    std::vector<std::string> pathVec(realPathSet.begin(), realPathSet.end());
2603545fdf9bSopenharmony_ci
2604545fdf9bSopenharmony_ci    sptr<QuickFixStatusCallbackHostlmpl> callback(new (std::nothrow) QuickFixStatusCallbackHostlmpl());
2605545fdf9bSopenharmony_ci    if (callback == nullptr || bundleMgrProxy_ == nullptr) {
2606545fdf9bSopenharmony_ci        APP_LOGE("callback or bundleMgrProxy is null");
2607545fdf9bSopenharmony_ci        return ERR_BUNDLEMANAGER_QUICK_FIX_INTERNAL_ERROR;
2608545fdf9bSopenharmony_ci    }
2609545fdf9bSopenharmony_ci    sptr<BundleDeathRecipient> recipient(new (std::nothrow) BundleDeathRecipient(nullptr, callback));
2610545fdf9bSopenharmony_ci    if (recipient == nullptr) {
2611545fdf9bSopenharmony_ci        APP_LOGE("recipient is null");
2612545fdf9bSopenharmony_ci        return ERR_BUNDLEMANAGER_QUICK_FIX_INTERNAL_ERROR;
2613545fdf9bSopenharmony_ci    }
2614545fdf9bSopenharmony_ci    bundleMgrProxy_->AsObject()->AddDeathRecipient(recipient);
2615545fdf9bSopenharmony_ci    auto quickFixProxy = bundleMgrProxy_->GetQuickFixManagerProxy();
2616545fdf9bSopenharmony_ci    if (quickFixProxy == nullptr) {
2617545fdf9bSopenharmony_ci        APP_LOGE("quickFixProxy is null");
2618545fdf9bSopenharmony_ci        return ERR_BUNDLEMANAGER_QUICK_FIX_INTERNAL_ERROR;
2619545fdf9bSopenharmony_ci    }
2620545fdf9bSopenharmony_ci    std::vector<std::string> destFiles;
2621545fdf9bSopenharmony_ci    auto res = quickFixProxy->CopyFiles(pathVec, destFiles);
2622545fdf9bSopenharmony_ci    if (res != ERR_OK) {
2623545fdf9bSopenharmony_ci        APP_LOGE("Copy files failed with %{public}d.", res);
2624545fdf9bSopenharmony_ci        return res;
2625545fdf9bSopenharmony_ci    }
2626545fdf9bSopenharmony_ci    res = quickFixProxy->DeployQuickFix(destFiles, callback, isDebug, targetPath);
2627545fdf9bSopenharmony_ci    if (res != ERR_OK) {
2628545fdf9bSopenharmony_ci        APP_LOGE("DeployQuickFix failed");
2629545fdf9bSopenharmony_ci        return res;
2630545fdf9bSopenharmony_ci    }
2631545fdf9bSopenharmony_ci    return callback->GetResultCode(quickFixRes);
2632545fdf9bSopenharmony_ci}
2633545fdf9bSopenharmony_ci
2634545fdf9bSopenharmony_ciErrCode BundleManagerShellCommand::DeleteQuickFix(const std::string &bundleName,
2635545fdf9bSopenharmony_ci    std::shared_ptr<QuickFixResult> &quickFixRes) const
2636545fdf9bSopenharmony_ci{
2637545fdf9bSopenharmony_ci    APP_LOGD("DeleteQuickFix bundleName: %{public}s", bundleName.c_str());
2638545fdf9bSopenharmony_ci    sptr<QuickFixStatusCallbackHostlmpl> callback(new (std::nothrow) QuickFixStatusCallbackHostlmpl());
2639545fdf9bSopenharmony_ci    if (callback == nullptr || bundleMgrProxy_ == nullptr) {
2640545fdf9bSopenharmony_ci        APP_LOGE("callback or bundleMgrProxy is null");
2641545fdf9bSopenharmony_ci        return ERR_BUNDLEMANAGER_QUICK_FIX_INTERNAL_ERROR;
2642545fdf9bSopenharmony_ci    }
2643545fdf9bSopenharmony_ci    sptr<BundleDeathRecipient> recipient(new (std::nothrow) BundleDeathRecipient(nullptr, callback));
2644545fdf9bSopenharmony_ci    if (recipient == nullptr) {
2645545fdf9bSopenharmony_ci        APP_LOGE("recipient is null");
2646545fdf9bSopenharmony_ci        return ERR_BUNDLEMANAGER_QUICK_FIX_INTERNAL_ERROR;
2647545fdf9bSopenharmony_ci    }
2648545fdf9bSopenharmony_ci    bundleMgrProxy_->AsObject()->AddDeathRecipient(recipient);
2649545fdf9bSopenharmony_ci    auto quickFixProxy = bundleMgrProxy_->GetQuickFixManagerProxy();
2650545fdf9bSopenharmony_ci    if (quickFixProxy == nullptr) {
2651545fdf9bSopenharmony_ci        APP_LOGE("quickFixProxy is null");
2652545fdf9bSopenharmony_ci        return ERR_BUNDLEMANAGER_QUICK_FIX_INTERNAL_ERROR;
2653545fdf9bSopenharmony_ci    }
2654545fdf9bSopenharmony_ci    auto res = quickFixProxy->DeleteQuickFix(bundleName, callback);
2655545fdf9bSopenharmony_ci    if (res != ERR_OK) {
2656545fdf9bSopenharmony_ci        APP_LOGE("DeleteQuickFix failed");
2657545fdf9bSopenharmony_ci        return res;
2658545fdf9bSopenharmony_ci    }
2659545fdf9bSopenharmony_ci    return callback->GetResultCode(quickFixRes);
2660545fdf9bSopenharmony_ci}
2661545fdf9bSopenharmony_ci}  // namespace AppExecFwk
2662545fdf9bSopenharmony_ci}  // namespace OHOS
2663