1 /*
2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "base_bundle_installer.h"
17
18 #include <unistd.h>
19 #include <set>
20 #include <vector>
21
22 #include "ability_manager_interface.h"
23 #include "app_log_wrapper.h"
24 #include "bundle_constants.h"
25 #include "bundle_extractor.h"
26 #include "bundle_mgr_service.h"
27 #include "bundle_parser.h"
28 #include "bundle_permission_mgr.h"
29 #include "bundle_util.h"
30 #include "bytrace.h"
31 #include "datetime_ex.h"
32 #include "installd_client.h"
33 #include "nlohmann/json.hpp"
34 #include "perf_profile.h"
35 #include "string_ex.h"
36 #include "system_ability_definition.h"
37 #include "system_ability_helper.h"
38 #include "systemcapability.h"
39 #include "bundle_clone_mgr.h"
40 #include "scope_guard.h"
41 #include "bundle_verify_mgr.h"
42 namespace OHOS {
43 namespace AppExecFwk {
44 using namespace OHOS::Security;
45 namespace {
UninstallApplicationProcesses(const std::string & bundleName,const int uid)46 bool UninstallApplicationProcesses(const std::string &bundleName, const int uid)
47 {
48 APP_LOGI("uninstall kill running processes, app name is %{public}s", bundleName.c_str());
49 sptr<AAFwk::IAbilityManager> abilityMgrProxy =
50 iface_cast<AAFwk::IAbilityManager>(SystemAbilityHelper::GetSystemAbility(ABILITY_MGR_SERVICE_ID));
51 if (!abilityMgrProxy) {
52 APP_LOGE("fail to find the app mgr service to kill application");
53 return false;
54 }
55 if (abilityMgrProxy->UninstallApp(bundleName, uid) != 0) {
56 APP_LOGE("kill application process failed");
57 return false;
58 }
59 return true;
60 }
61 } // namespace
62
BaseBundleInstaller()63 BaseBundleInstaller::BaseBundleInstaller()
64 {
65 APP_LOGI("base bundle installer instance is created");
66 }
67
~BaseBundleInstaller()68 BaseBundleInstaller::~BaseBundleInstaller()
69 {
70 APP_LOGI("base bundle installer instance is destroyed");
71 }
72
InstallBundle(const std::string & bundlePath,const InstallParam & installParam,const Constants::AppType appType)73 ErrCode BaseBundleInstaller::InstallBundle(
74 const std::string &bundlePath, const InstallParam &installParam, const Constants::AppType appType)
75 {
76 std::vector<std::string> bundlePaths { bundlePath };
77 return InstallBundle(bundlePaths, installParam, appType);
78 }
79
InstallBundle(const std::vector<std::string> & bundlePaths,const InstallParam & installParam,const Constants::AppType appType)80 ErrCode BaseBundleInstaller::InstallBundle(
81 const std::vector<std::string> &bundlePaths, const InstallParam &installParam, const Constants::AppType appType)
82 {
83 BYTRACE_NAME(BYTRACE_TAG_APP, __PRETTY_FUNCTION__);
84 APP_LOGD("begin to process bundle install");
85
86 PerfProfile::GetInstance().SetBundleInstallStartTime(GetTickCount());
87
88 int32_t uid = Constants::INVALID_UID;
89 ErrCode result = ProcessBundleInstall(bundlePaths, installParam, appType, uid);
90 if (installParam.needSendEvent && dataMgr_ && !bundleName_.empty()) {
91 dataMgr_->NotifyBundleStatus(bundleName_,
92 Constants::EMPTY_STRING,
93 mainAbility_,
94 result,
95 isAppExist_ ? NotifyType::UPDATE : NotifyType::INSTALL,
96 uid);
97 }
98
99 PerfProfile::GetInstance().SetBundleInstallEndTime(GetTickCount());
100 APP_LOGD("finish to process bundle install");
101 return result;
102 }
103
Recover(const std::string & bundleName,const InstallParam & installParam)104 ErrCode BaseBundleInstaller::Recover(
105 const std::string &bundleName, const InstallParam &installParam)
106 {
107 APP_LOGD("begin to process bundle install by bundleName, which is %{public}s.", bundleName.c_str());
108 PerfProfile::GetInstance().SetBundleInstallStartTime(GetTickCount());
109
110 int32_t uid = Constants::INVALID_UID;
111 ErrCode result = ProcessRecover(bundleName, installParam, uid);
112 if (installParam.needSendEvent && dataMgr_ && !bundleName_.empty() && !modulePackage_.empty()) {
113 dataMgr_->NotifyBundleStatus(bundleName_,
114 modulePackage_,
115 mainAbility_,
116 result,
117 isAppExist_ ? NotifyType::UPDATE : NotifyType::INSTALL,
118 uid);
119 }
120
121 PerfProfile::GetInstance().SetBundleInstallEndTime(GetTickCount());
122 APP_LOGD("finish to process %{public}s bundle install", bundleName.c_str());
123 return result;
124 }
125
UninstallBundle(const std::string & bundleName,const InstallParam & installParam)126 ErrCode BaseBundleInstaller::UninstallBundle(const std::string &bundleName, const InstallParam &installParam)
127 {
128 APP_LOGD("begin to process %{public}s bundle uninstall", bundleName.c_str());
129 PerfProfile::GetInstance().SetBundleUninstallStartTime(GetTickCount());
130
131 int32_t uid = Constants::INVALID_UID;
132 ErrCode result = ProcessBundleUninstall(bundleName, installParam, uid);
133 if (installParam.needSendEvent && dataMgr_) {
134 dataMgr_->NotifyBundleStatus(
135 bundleName, Constants::EMPTY_STRING, Constants::EMPTY_STRING, result, NotifyType::UNINSTALL_BUNDLE, uid);
136 }
137
138 PerfProfile::GetInstance().SetBundleUninstallEndTime(GetTickCount());
139 APP_LOGD("finish to process %{public}s bundle uninstall", bundleName.c_str());
140 return result;
141 }
142
UninstallBundle(const std::string & bundleName,const std::string & modulePackage,const InstallParam & installParam)143 ErrCode BaseBundleInstaller::UninstallBundle(
144 const std::string &bundleName, const std::string &modulePackage, const InstallParam &installParam)
145 {
146 APP_LOGD("begin to process %{public}s module in %{public}s uninstall", modulePackage.c_str(), bundleName.c_str());
147 PerfProfile::GetInstance().SetBundleUninstallStartTime(GetTickCount());
148
149 int32_t uid = Constants::INVALID_UID;
150 ErrCode result = ProcessBundleUninstall(bundleName, modulePackage, installParam, uid);
151 if (installParam.needSendEvent && dataMgr_) {
152 dataMgr_->NotifyBundleStatus(
153 bundleName, modulePackage, Constants::EMPTY_STRING, result, NotifyType::UNINSTALL_MODULE, uid);
154 }
155
156 PerfProfile::GetInstance().SetBundleUninstallEndTime(GetTickCount());
157 APP_LOGD("finish to process %{public}s module in %{public}s uninstall", modulePackage.c_str(), bundleName.c_str());
158 return result;
159 }
160
UpdateInstallerState(const InstallerState state)161 void BaseBundleInstaller::UpdateInstallerState(const InstallerState state)
162 {
163 APP_LOGD("UpdateInstallerState in BaseBundleInstaller state %{public}d", state);
164 SetInstallerState(state);
165 }
166
InnerProcessBundleInstall(std::unordered_map<std::string,InnerBundleInfo> & newInfos,InnerBundleInfo & oldInfo,const InstallParam & installParam,int32_t & uid)167 ErrCode BaseBundleInstaller::InnerProcessBundleInstall(std::unordered_map<std::string, InnerBundleInfo> &newInfos,
168 InnerBundleInfo &oldInfo, const InstallParam &installParam, int32_t &uid)
169 {
170 BYTRACE_NAME(BYTRACE_TAG_APP, __PRETTY_FUNCTION__);
171 bundleName_ = newInfos.begin()->second.GetBundleName();
172 APP_LOGI("InnerProcessBundleInstall with bundleName %{public}s, userId is %{public}d", bundleName_.c_str(),
173 userId_);
174 if (installParam.needSavePreInstallInfo) {
175 PreInstallBundleInfo preInstallBundleInfo;
176 dataMgr_->GetPreInstallBundleInfo(bundleName_, preInstallBundleInfo);
177 preInstallBundleInfo.SetAppType(newInfos.begin()->second.GetAppType());
178 preInstallBundleInfo.SetVersionCode(newInfos.begin()->second.GetVersionCode());
179 for (const auto &item : newInfos) {
180 preInstallBundleInfo.AddBundlePath(item.first);
181 }
182 dataMgr_->SavePreInstallBundleInfo(bundleName_, preInstallBundleInfo);
183 }
184
185 // singleton app can only be installed in U0 and U0 can only install singleton app.
186 bool isSingleton = newInfos.begin()->second.IsSingleUser();
187 if ((isSingleton && (userId_ != Constants::DEFAULT_USERID)) ||
188 (!isSingleton && (userId_ == Constants::DEFAULT_USERID))) {
189 APP_LOGI("singleton(%{public}d) app(%{public}s) and user(%{public}d) are not matched.",
190 isSingleton, bundleName_.c_str(), userId_);
191 return ERR_APPEXECFWK_INSTALL_ZERO_USER_WITH_NO_SINGLETON;
192 }
193
194 // try to get the bundle info to decide use install or update. Always keep other exceptions below this line.
195 if (!GetInnerBundleInfo(oldInfo, isAppExist_)) {
196 return ERR_APPEXECFWK_INSTALL_BUNDLE_MGR_SERVICE_ERROR;
197 }
198
199 if (isAppExist_) {
200 for (const auto &item : newInfos) {
201 if (oldInfo.GetIsNewVersion() != item.second.GetIsNewVersion()) {
202 return ERR_APPEXECFWK_INSTALL_STATE_ERROR;
203 }
204 }
205 }
206
207 ErrCode result = ERR_OK;
208 if (isAppExist_) {
209 // to guarantee that the hap version can be compatible.
210 result = CheckVersionCompatibility(oldInfo);
211 if (result != ERR_OK) {
212 APP_LOGE("The app has been installed and update lower version bundle.");
213 return result;
214 }
215
216 hasInstalledInUser_ = oldInfo.HasInnerBundleUserInfo(userId_);
217 if (!hasInstalledInUser_) {
218 APP_LOGD("new userInfo with bundleName %{public}s and userId %{public}d",
219 bundleName_.c_str(), userId_);
220 InnerBundleUserInfo newInnerBundleUserInfo;
221 newInnerBundleUserInfo.bundleUserInfo.userId = userId_;
222 newInnerBundleUserInfo.bundleName = bundleName_;
223 oldInfo.AddInnerBundleUserInfo(newInnerBundleUserInfo);
224 uint32_t tokenId = CreateAccessTokenId(oldInfo);
225 oldInfo.SetAccessTokenId(tokenId, userId_);
226 result = GrantRequestPermissions(oldInfo, tokenId);
227 if (result != ERR_OK) {
228 return result;
229 }
230 result = CreateBundleUserData(oldInfo, false);
231 if (result != ERR_OK) {
232 return result;
233 }
234 }
235
236 for (auto &info : newInfos) {
237 std::string packageName = info.second.GetCurrentModulePackage();
238 if (oldInfo.FindModule(packageName)) {
239 installedModules_[packageName] = true;
240 }
241 }
242 }
243
244 auto it = newInfos.begin();
245 if (!isAppExist_) {
246 APP_LOGI("app is not exist");
247 InnerBundleInfo &newInfo = it->second;
248 if (newInfo.IsSingleUser() && (userId_ != Constants::DEFAULT_USERID)) {
249 APP_LOGE("singleton app(%{public}s) must be installed in user 0.", bundleName_.c_str());
250 return ERR_APPEXECFWK_INSTALL_ZERO_USER_WITH_NO_SINGLETON;
251 }
252
253 modulePath_ = it->first;
254 InnerBundleUserInfo newInnerBundleUserInfo;
255 newInnerBundleUserInfo.bundleUserInfo.userId = userId_;
256 newInnerBundleUserInfo.bundleName = bundleName_;
257 newInfo.AddInnerBundleUserInfo(newInnerBundleUserInfo);
258 result = ProcessBundleInstallStatus(newInfo, uid);
259 if (result != ERR_OK) {
260 return result;
261 }
262
263 it++;
264 hasInstalledInUser_ = true;
265 }
266
267 InnerBundleInfo bundleInfo;
268 bool isBundleExist = false;
269 if (!GetInnerBundleInfo(bundleInfo, isBundleExist) || !isBundleExist) {
270 return ERR_APPEXECFWK_INSTALL_BUNDLE_MGR_SERVICE_ERROR;
271 }
272
273 InnerBundleUserInfo innerBundleUserInfo;
274 if (!bundleInfo.GetInnerBundleUserInfo(userId_, innerBundleUserInfo)) {
275 APP_LOGE("oldInfo do not have user");
276 return ERR_APPEXECFWK_USER_NOT_EXIST;
277 }
278
279 // update haps
280 for (; it != newInfos.end(); ++it) {
281 modulePath_ = it->first;
282 InnerBundleInfo &newInfo = it->second;
283 newInfo.AddInnerBundleUserInfo(innerBundleUserInfo);
284 bool isReplace = (installParam.installFlag == InstallFlag::REPLACE_EXISTING);
285 // app exist, but module may not
286 if ((result = ProcessBundleUpdateStatus(
287 bundleInfo, newInfo, isReplace, installParam.noSkipsKill)) != ERR_OK) {
288 break;
289 }
290 }
291
292 uid = bundleInfo.GetUid(userId_);
293 mainAbility_ = bundleInfo.GetMainAbility();
294 return result;
295 }
296
CreateAccessTokenId(const InnerBundleInfo & info)297 uint32_t BaseBundleInstaller::CreateAccessTokenId(const InnerBundleInfo &info)
298 {
299 return BundlePermissionMgr::CreateAccessTokenId(info, bundleName_, userId_);
300 }
301
GrantRequestPermissions(const InnerBundleInfo & info,const uint32_t tokenId)302 ErrCode BaseBundleInstaller::GrantRequestPermissions(const InnerBundleInfo &info, const uint32_t tokenId)
303 {
304 if (!BundlePermissionMgr::GrantRequestPermissions(info, tokenId)) {
305 APP_LOGE("GrantRequestPermissions failed");
306 return ERR_APPEXECFWK_INSTALL_GRANT_REQUEST_PERMISSIONS_FAILED;
307 }
308 return ERR_OK;
309 }
310
ProcessBundleInstall(const std::vector<std::string> & inBundlePaths,const InstallParam & installParam,const Constants::AppType appType,int32_t & uid)311 ErrCode BaseBundleInstaller::ProcessBundleInstall(const std::vector<std::string> &inBundlePaths,
312 const InstallParam &installParam, const Constants::AppType appType, int32_t &uid)
313 {
314 APP_LOGD("ProcessBundleInstall bundlePath install");
315 if (!dataMgr_) {
316 dataMgr_ = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
317 if (!dataMgr_) {
318 APP_LOGE("Get dataMgr shared_ptr nullptr");
319 return ERR_APPEXECFWK_UNINSTALL_BUNDLE_MGR_SERVICE_ERROR;
320 }
321 }
322
323 userId_ = GetUserId(installParam);
324 if (userId_ == Constants::INVALID_USERID) {
325 return ERR_APPEXECFWK_INSTALL_PARAM_ERROR;
326 }
327
328 if (!dataMgr_->HasUserId(userId_)) {
329 APP_LOGE("The user %{public}d does not exist when install.", userId_);
330 return ERR_APPEXECFWK_USER_NOT_EXIST;
331 }
332
333 std::vector<std::string> bundlePaths;
334 // check hap paths
335 ErrCode result = BundleUtil::CheckFilePath(inBundlePaths, bundlePaths);
336 CHECK_RESULT_WITHOUT_ROLLBACK(result, "hap file check failed %{public}d");
337 UpdateInstallerState(InstallerState::INSTALL_BUNDLE_CHECKED); // ---- 5%
338
339 // check syscap
340 result = CheckSysCap(bundlePaths);
341 CHECK_RESULT_WITHOUT_ROLLBACK(result, "hap syscap check failed %{public}d");
342 UpdateInstallerState(InstallerState::INSTALL_SYSCAP_CHECKED); // ---- 10%
343
344 // verify signature info for all haps
345 std::vector<Security::Verify::HapVerifyResult> hapVerifyResults;
346 result = CheckMultipleHapsSignInfo(bundlePaths, installParam, hapVerifyResults);
347 CHECK_RESULT_WITHOUT_ROLLBACK(result, "hap files check signature info failed %{public}d");
348 UpdateInstallerState(InstallerState::INSTALL_SIGNATURE_CHECKED); // ---- 15%
349
350 result = ModifyInstallDirByHapType(installParam, appType);
351 CHECK_RESULT_WITHOUT_ROLLBACK(result, "modify bundle install dir failed %{public}d");
352
353 // parse the bundle infos for all haps
354 // key is bundlePath , value is innerBundleInfo
355 std::unordered_map<std::string, InnerBundleInfo> newInfos;
356 result = ParseHapFiles(bundlePaths, installParam, appType, hapVerifyResults, newInfos);
357 CHECK_RESULT_WITHOUT_ROLLBACK(result, "parse haps file failed %{public}d");
358 UpdateInstallerState(InstallerState::INSTALL_PARSED); // ---- 20%
359
360 // check versioncode and bundleName
361 result = CheckAppLabelInfo(newInfos);
362 CHECK_RESULT_WITHOUT_ROLLBACK(result, "verisoncode or bundleName is different in all haps %{public}d");
363 UpdateInstallerState(InstallerState::INSTALL_VERSION_AND_BUNDLENAME_CHECKED); // ---- 30%
364
365 // this state should always be set when return
366 ScopeGuard stateGuard([&] { dataMgr_->UpdateBundleInstallState(bundleName_, InstallState::INSTALL_SUCCESS); });
367
368 // this state should always be set when return
369 ScopeGuard enableGuard([&] { dataMgr_->EnableBundle(bundleName_); });
370 InnerBundleInfo oldInfo;
371 result = InnerProcessBundleInstall(newInfos, oldInfo, installParam, uid);
372 CHECK_RESULT_WITH_ROLLBACK(result, "internal processing failed with result %{public}d", newInfos, oldInfo);
373 UpdateInstallerState(InstallerState::INSTALL_INFO_SAVED); // ---- 80%
374
375 // rename for all temp dirs
376 for (const auto &info : newInfos) {
377 if (info.second.IsOnlyCreateBundleUser()) {
378 continue;
379 }
380 if ((result = RenameModuleDir(info.second)) != ERR_OK) {
381 break;
382 }
383 }
384 UpdateInstallerState(InstallerState::INSTALL_RENAMED); // ---- 90%
385
386 CHECK_RESULT_WITH_ROLLBACK(result, "rename temp dirs failed with result %{public}d", newInfos, oldInfo);
387 if (!uninstallModuleVec_.empty()) {
388 UninstallLowerVersionFeature(uninstallModuleVec_);
389 }
390 UpdateInstallerState(InstallerState::INSTALL_SUCCESS); // ---- 100%
391 APP_LOGD("finish ProcessBundleInstall bundlePath install");
392 return result;
393 }
394
RollBack(const std::unordered_map<std::string,InnerBundleInfo> & newInfos,InnerBundleInfo & oldInfo)395 void BaseBundleInstaller::RollBack(const std::unordered_map<std::string, InnerBundleInfo> &newInfos,
396 InnerBundleInfo &oldInfo)
397 {
398 APP_LOGD("start rollback due to install failed");
399 if (!isAppExist_) {
400 RemoveBundleAndDataDir(newInfos.begin()->second, false);
401 // delete accessTokenId
402 if (BundlePermissionMgr::DeleteAccessTokenId(newInfos.begin()->second.GetAccessTokenId(userId_)) !=
403 AccessToken::AccessTokenKitRet::RET_SUCCESS) {
404 APP_LOGE("delete accessToken failed");
405 }
406 // remove innerBundleInfo
407 RemoveInfo(bundleName_, "");
408 return;
409 }
410 InnerBundleInfo preInfo;
411 bool isExist = false;
412 if (!GetInnerBundleInfo(preInfo, isExist) || !isExist) {
413 APP_LOGI("finish rollback due to install failed");
414 return;
415 }
416 for (const auto &info : newInfos) {
417 RollBack(info.second, oldInfo);
418 }
419 // need delete definePermissions and requestPermissions
420 ErrCode ret = UpdateDefineAndRequestPermissions(preInfo, oldInfo);
421 if (ret != ERR_OK) {
422 return;
423 }
424 APP_LOGD("finish rollback due to install failed");
425 }
426
UpdateDefineAndRequestPermissions(const InnerBundleInfo & oldInfo,const InnerBundleInfo & newInfo)427 ErrCode BaseBundleInstaller::UpdateDefineAndRequestPermissions(const InnerBundleInfo &oldInfo,
428 const InnerBundleInfo &newInfo)
429 {
430 APP_LOGD("UpdateDefineAndRequestPermissions %{public}s start", bundleName_.c_str());
431 auto bundleUserInfos = newInfo.GetInnerBundleUserInfos();
432 for (const auto &uerInfo : bundleUserInfos) {
433 if (uerInfo.second.accessTokenId == 0) {
434 continue;
435 }
436 std::vector<std::string> newRequestPermName;
437 if (!BundlePermissionMgr::UpdateDefineAndRequestPermissions(uerInfo.second.accessTokenId, oldInfo,
438 newInfo, newRequestPermName)) {
439 APP_LOGE("UpdateDefineAndRequestPermissions %{public}s failed", bundleName_.c_str());
440 return ERR_APPEXECFWK_INSTALL_UPDATE_HAP_TOKEN_FAILED;
441 }
442 if (!BundlePermissionMgr::GrantRequestPermissions(newInfo, newRequestPermName, uerInfo.second.accessTokenId)) {
443 APP_LOGE("BundlePermissionMgr::GrantRequestPermissions failed %{public}s", bundleName_.c_str());
444 return ERR_APPEXECFWK_INSTALL_GRANT_REQUEST_PERMISSIONS_FAILED;
445 }
446 }
447 APP_LOGD("UpdateDefineAndRequestPermissions %{public}s end", bundleName_.c_str());
448 return ERR_OK;
449 }
450
RollBack(const InnerBundleInfo & info,InnerBundleInfo & oldInfo)451 void BaseBundleInstaller::RollBack(const InnerBundleInfo &info, InnerBundleInfo &oldInfo)
452 {
453 // rollback hap installed
454 if (installedModules_[info.GetCurrentModulePackage()]) {
455 std::string createModulePath = info.GetAppCodePath() + Constants::PATH_SEPARATOR +
456 info.GetCurrentModulePackage() + Constants::TMP_SUFFIX;
457 RemoveModuleDir(createModulePath);
458 oldInfo.SetCurrentModulePackage(info.GetCurrentModulePackage());
459 RollBackMoudleInfo(bundleName_, oldInfo);
460 } else {
461 auto modulePackage = info.GetCurrentModulePackage();
462 RemoveModuleDir(info.GetModuleDir(modulePackage));
463 RemoveModuleDataDir(info, modulePackage);
464 // remove module info
465 RemoveInfo(bundleName_, modulePackage);
466 }
467 }
468
RemoveInfo(const std::string & bundleName,const std::string & packageName)469 void BaseBundleInstaller::RemoveInfo(const std::string &bundleName, const std::string &packageName)
470 {
471 APP_LOGD("remove innerBundleInfo due to rollback");
472 if (packageName.empty()) {
473 dataMgr_->UpdateBundleInstallState(bundleName, InstallState::UPDATING_FAIL);
474 } else {
475 InnerBundleInfo innerBundleInfo;
476 bool isExist = false;
477 if (!GetInnerBundleInfo(innerBundleInfo, isExist) || !isExist) {
478 APP_LOGI("finish rollback due to install failed");
479 return;
480 }
481 dataMgr_->UpdateBundleInstallState(bundleName, InstallState::ROLL_BACK);
482 dataMgr_->RemoveModuleInfo(bundleName, packageName, innerBundleInfo);
483 }
484 APP_LOGD("finish to remove innerBundleInfo due to rollback");
485 }
486
RollBackMoudleInfo(const std::string & bundleName,InnerBundleInfo & oldInfo)487 void BaseBundleInstaller::RollBackMoudleInfo(const std::string &bundleName, InnerBundleInfo &oldInfo)
488 {
489 APP_LOGD("rollBackMoudleInfo due to rollback");
490 InnerBundleInfo innerBundleInfo;
491 bool isExist = false;
492 if (!GetInnerBundleInfo(innerBundleInfo, isExist) || !isExist) {
493 return;
494 }
495 dataMgr_->UpdateBundleInstallState(bundleName, InstallState::ROLL_BACK);
496 dataMgr_->UpdateInnerBundleInfo(bundleName, oldInfo, innerBundleInfo);
497 APP_LOGD("finsih rollBackMoudleInfo due to rollback");
498 }
499
ProcessBundleUninstall(const std::string & bundleName,const InstallParam & installParam,int32_t & uid)500 ErrCode BaseBundleInstaller::ProcessBundleUninstall(
501 const std::string &bundleName, const InstallParam &installParam, int32_t &uid)
502 {
503 APP_LOGD("start to process %{public}s bundle uninstall", bundleName.c_str());
504 if (bundleName.empty()) {
505 APP_LOGE("uninstall bundle name empty");
506 return ERR_APPEXECFWK_UNINSTALL_INVALID_NAME;
507 }
508
509 dataMgr_ = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
510 if (!dataMgr_) {
511 APP_LOGE("Get dataMgr shared_ptr nullptr");
512 return ERR_APPEXECFWK_UNINSTALL_BUNDLE_MGR_SERVICE_ERROR;
513 }
514
515 userId_ = GetUserId(installParam);
516 if (userId_ == Constants::INVALID_USERID) {
517 return ERR_APPEXECFWK_INSTALL_PARAM_ERROR;
518 }
519
520 if (!dataMgr_->HasUserId(userId_)) {
521 APP_LOGE("The user %{public}d does not exist when uninstall.", userId_);
522 return ERR_APPEXECFWK_USER_NOT_EXIST;
523 }
524
525 auto &mtx = dataMgr_->GetBundleMutex(bundleName);
526 std::lock_guard lock {mtx};
527 InnerBundleInfo oldInfo;
528 if (!dataMgr_->GetInnerBundleInfo(bundleName, Constants::CURRENT_DEVICE_ID, oldInfo)) {
529 APP_LOGE("uninstall bundle info missing");
530 return ERR_APPEXECFWK_UNINSTALL_MISSING_INSTALLED_BUNDLE;
531 }
532
533 ScopeGuard enableGuard([&] { dataMgr_->EnableBundle(bundleName); });
534 InnerBundleUserInfo curInnerBundleUserInfo;
535 if (!oldInfo.GetInnerBundleUserInfo(userId_, curInnerBundleUserInfo)) {
536 APP_LOGE("bundle(%{public}s) get user(%{public}d) failed when uninstall.",
537 oldInfo.GetBundleName().c_str(), userId_);
538 return ERR_APPEXECFWK_USER_NOT_INSTALL_HAP;
539 }
540
541 uid = curInnerBundleUserInfo.uid;
542 oldInfo.SetIsKeepData(installParam.isKeepData);
543 if (!installParam.forceExecuted && oldInfo.GetBaseApplicationInfo().isSystemApp &&
544 !oldInfo.IsRemovable() && installParam.noSkipsKill) {
545 APP_LOGE("uninstall system app");
546 return ERR_APPEXECFWK_UNINSTALL_SYSTEM_APP_ERROR;
547 }
548
549 std::string cloneName;
550 if (dataMgr_->GetClonedBundleName(bundleName, cloneName)) {
551 APP_LOGI("GetClonedBundleName new name %{public}s ", cloneName.c_str());
552 cloneMgr_->RemoveClonedBundle(bundleName, cloneName);
553 }
554
555 if (oldInfo.GetInnerBundleUserInfos().size() > 1) {
556 APP_LOGD("only delete userinfo %{public}d", userId_);
557 return RemoveBundleUserData(oldInfo);
558 }
559
560 if (!dataMgr_->UpdateBundleInstallState(bundleName, InstallState::UNINSTALL_START)) {
561 APP_LOGE("uninstall already start");
562 return ERR_APPEXECFWK_INSTALL_BUNDLE_MGR_SERVICE_ERROR;
563 }
564
565 // reboot scan case will not kill the bundle
566 if (installParam.noSkipsKill) {
567 // kill the bundle process during uninstall.
568 if (!UninstallApplicationProcesses(oldInfo.GetApplicationName(), uid)) {
569 APP_LOGE("can not kill process");
570 dataMgr_->UpdateBundleInstallState(bundleName, InstallState::INSTALL_SUCCESS);
571 return ERR_APPEXECFWK_UNINSTALL_KILLING_APP_ERROR;
572 }
573 }
574
575 enableGuard.Dismiss();
576 std::string packageName;
577 oldInfo.SetInstallMark(bundleName, packageName, InstallExceptionStatus::UNINSTALL_BUNDLE_START);
578 if (!dataMgr_->SaveInstallMark(oldInfo, true)) {
579 APP_LOGE("save install mark failed");
580 return ERR_APPEXECFWK_INSTALL_INTERNAL_ERROR;
581 }
582
583 ErrCode result = RemoveBundle(oldInfo);
584 if (result != ERR_OK) {
585 APP_LOGE("remove whole bundle failed");
586 return result;
587 }
588
589 APP_LOGD("finish to process %{public}s bundle uninstall", bundleName.c_str());
590 return ERR_OK;
591 }
592
ProcessBundleUninstall(const std::string & bundleName,const std::string & modulePackage,const InstallParam & installParam,int32_t & uid)593 ErrCode BaseBundleInstaller::ProcessBundleUninstall(
594 const std::string &bundleName, const std::string &modulePackage, const InstallParam &installParam, int32_t &uid)
595 {
596 APP_LOGD("start to process %{public}s in %{public}s uninstall", bundleName.c_str(), modulePackage.c_str());
597 if (bundleName.empty() || modulePackage.empty()) {
598 APP_LOGE("uninstall bundle name or module name empty");
599 return ERR_APPEXECFWK_UNINSTALL_INVALID_NAME;
600 }
601
602 dataMgr_ = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
603 cloneMgr_ = DelayedSingleton<BundleMgrService>::GetInstance()->GetCloneMgr();
604 if (!dataMgr_) {
605 APP_LOGE("Get dataMgr shared_ptr nullptr");
606 return ERR_APPEXECFWK_UNINSTALL_BUNDLE_MGR_SERVICE_ERROR;
607 }
608
609 userId_ = GetUserId(installParam);
610 if (userId_ == Constants::INVALID_USERID) {
611 return ERR_APPEXECFWK_INSTALL_PARAM_ERROR;
612 }
613
614 if (!dataMgr_->HasUserId(userId_)) {
615 APP_LOGE("The user %{public}d does not exist when uninstall.", userId_);
616 return ERR_APPEXECFWK_USER_NOT_EXIST;
617 }
618
619 auto &mtx = dataMgr_->GetBundleMutex(bundleName);
620 std::lock_guard lock {mtx};
621 InnerBundleInfo oldInfo;
622 if (!dataMgr_->GetInnerBundleInfo(bundleName, Constants::CURRENT_DEVICE_ID, oldInfo)) {
623 APP_LOGE("uninstall bundle info missing");
624 return ERR_APPEXECFWK_UNINSTALL_MISSING_INSTALLED_BUNDLE;
625 }
626
627 ScopeGuard enableGuard([&] { dataMgr_->EnableBundle(bundleName); });
628 InnerBundleUserInfo curInnerBundleUserInfo;
629 if (!oldInfo.GetInnerBundleUserInfo(userId_, curInnerBundleUserInfo)) {
630 APP_LOGE("bundle(%{public}s) get user(%{public}d) failed when uninstall.",
631 oldInfo.GetBundleName().c_str(), userId_);
632 return ERR_APPEXECFWK_USER_NOT_INSTALL_HAP;
633 }
634
635 uid = curInnerBundleUserInfo.uid;
636 oldInfo.SetIsKeepData(installParam.isKeepData);
637 if (!installParam.forceExecuted && oldInfo.GetBaseApplicationInfo().isSystemApp
638 && !oldInfo.IsRemovable() && installParam.noSkipsKill) {
639 APP_LOGE("uninstall system app");
640 return ERR_APPEXECFWK_UNINSTALL_SYSTEM_APP_ERROR;
641 }
642
643 bool isModuleExist = oldInfo.FindModule(modulePackage);
644 if (!isModuleExist) {
645 APP_LOGE("uninstall bundle info missing");
646 return ERR_APPEXECFWK_UNINSTALL_MISSING_INSTALLED_MODULE;
647 }
648
649 if (!dataMgr_->UpdateBundleInstallState(bundleName, InstallState::UNINSTALL_START)) {
650 APP_LOGE("uninstall already start");
651 return ERR_APPEXECFWK_INSTALL_BUNDLE_MGR_SERVICE_ERROR;
652 }
653
654 ScopeGuard stateGuard([&] { dataMgr_->UpdateBundleInstallState(bundleName, InstallState::INSTALL_SUCCESS); });
655
656 // reboot scan case will not kill the bundle
657 if (installParam.noSkipsKill) {
658 // kill the bundle process during uninstall.
659 if (!UninstallApplicationProcesses(oldInfo.GetApplicationName(), uid)) {
660 APP_LOGE("can not kill process");
661 return ERR_APPEXECFWK_UNINSTALL_KILLING_APP_ERROR;
662 }
663 }
664
665 oldInfo.SetInstallMark(bundleName, modulePackage, InstallExceptionStatus::UNINSTALL_PACKAGE_START);
666 if (!dataMgr_->SaveInstallMark(oldInfo, true)) {
667 APP_LOGE("save install mark failed");
668 return ERR_APPEXECFWK_INSTALL_INTERNAL_ERROR;
669 }
670
671 bool onlyInstallInUser = oldInfo.GetInnerBundleUserInfos().size() == 1;
672 // if it is the only module in the bundle
673 if (oldInfo.IsOnlyModule(modulePackage)) {
674 APP_LOGI("%{public}s is only module", modulePackage.c_str());
675 std::string cloneName;
676 if (dataMgr_->GetClonedBundleName(bundleName, cloneName)) {
677 APP_LOGI("GetClonedBundleName new name %{public}s ", cloneName.c_str());
678 cloneMgr_->RemoveClonedBundle(bundleName, cloneName);
679 }
680
681 enableGuard.Dismiss();
682 stateGuard.Dismiss();
683 if (onlyInstallInUser) {
684 return RemoveBundle(oldInfo);
685 }
686
687 return RemoveBundleUserData(oldInfo);
688 }
689
690 ErrCode result = ERR_OK;
691 if (onlyInstallInUser) {
692 result = RemoveModuleAndDataDir(oldInfo, modulePackage, userId_);
693 } else {
694 result = RemoveHapModuleDataDir(oldInfo, modulePackage, userId_);
695 }
696
697 if (result != ERR_OK) {
698 APP_LOGE("remove module dir failed");
699 return result;
700 }
701
702 oldInfo.SetInstallMark(bundleName, modulePackage, InstallExceptionStatus::INSTALL_FINISH);
703 if (!dataMgr_->RemoveModuleInfo(bundleName, modulePackage, oldInfo)) {
704 APP_LOGE("RemoveModuleInfo failed");
705 return ERR_APPEXECFWK_INSTALL_BUNDLE_MGR_SERVICE_ERROR;
706 }
707
708 APP_LOGD("finish to process %{public}s in %{public}s uninstall", bundleName.c_str(), modulePackage.c_str());
709 return ERR_OK;
710 }
711
ProcessRecover(const std::string & bundleName,const InstallParam & installParam,int32_t & uid)712 ErrCode BaseBundleInstaller::ProcessRecover(
713 const std::string &bundleName, const InstallParam &installParam, int32_t &uid)
714 {
715 APP_LOGD("Install Bundle by bundleName start, bundleName: %{public}s.", bundleName.c_str());
716 dataMgr_ = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
717 if (!dataMgr_) {
718 APP_LOGE("Get dataMgr shared_ptr nullptr.");
719 return ERR_APPEXECFWK_UNINSTALL_BUNDLE_MGR_SERVICE_ERROR;
720 }
721
722 userId_ = GetUserId(installParam);
723 if (userId_ == Constants::INVALID_USERID) {
724 return ERR_APPEXECFWK_INSTALL_PARAM_ERROR;
725 }
726
727 if (!dataMgr_->HasUserId(userId_)) {
728 APP_LOGE("The user %{public}d does not exist when recover.", userId_);
729 return ERR_APPEXECFWK_USER_NOT_EXIST;
730 }
731
732 {
733 auto &mtx = dataMgr_->GetBundleMutex(bundleName);
734 std::lock_guard lock {mtx};
735 InnerBundleInfo oldInfo;
736 bool isAppExist = dataMgr_->GetInnerBundleInfo(bundleName, Constants::CURRENT_DEVICE_ID, oldInfo);
737 if (isAppExist) {
738 dataMgr_->EnableBundle(bundleName);
739 if (oldInfo.HasInnerBundleUserInfo(userId_)) {
740 APP_LOGE("App is exist in user(%{public}d) when recover.", userId_);
741 return ERR_APPEXECFWK_INSTALL_ALREADY_EXIST;
742 }
743
744 InnerBundleUserInfo curInnerBundleUserInfo;
745 curInnerBundleUserInfo.bundleUserInfo.userId = userId_;
746 curInnerBundleUserInfo.bundleName = bundleName;
747 oldInfo.AddInnerBundleUserInfo(curInnerBundleUserInfo);
748 uint32_t tokenId = CreateAccessTokenId(oldInfo);
749 oldInfo.SetAccessTokenId(tokenId, userId_);
750 ErrCode result = GrantRequestPermissions(oldInfo, tokenId);
751 if (result != ERR_OK) {
752 return result;
753 }
754 return CreateBundleUserData(oldInfo, true);
755 }
756 }
757
758 PreInstallBundleInfo preInstallBundleInfo;
759 preInstallBundleInfo.SetBundleName(bundleName);
760 if (!dataMgr_->GetPreInstallBundleInfo(bundleName, preInstallBundleInfo)
761 || preInstallBundleInfo.GetBundlePaths().empty()
762 || preInstallBundleInfo.GetAppType() != Constants::AppType::SYSTEM_APP) {
763 APP_LOGE("Get PreInstallBundleInfo faile, bundleName: %{public}s.", bundleName.c_str());
764 return ERR_APPEXECFWK_RECOVER_GET_BUNDLEPATH_ERROR;
765 }
766
767 APP_LOGD("Get bundlePath success when recover.");
768 std::vector<std::string> pathVec { preInstallBundleInfo.GetBundlePaths() };
769 auto recoverInstallParam = installParam;
770 recoverInstallParam.isPreInstallApp = true;
771 return ProcessBundleInstall(
772 pathVec, recoverInstallParam, Constants::AppType::SYSTEM_APP, uid);
773 }
774
RemoveBundle(InnerBundleInfo & info)775 ErrCode BaseBundleInstaller::RemoveBundle(InnerBundleInfo &info)
776 {
777 ErrCode result = RemoveBundleAndDataDir(info, true);
778 if (result != ERR_OK) {
779 APP_LOGE("remove bundle dir failed");
780 dataMgr_->UpdateBundleInstallState(info.GetBundleName(), InstallState::UNINSTALL_FAIL);
781 return result;
782 }
783
784 if (!dataMgr_->UpdateBundleInstallState(info.GetBundleName(), InstallState::UNINSTALL_SUCCESS)) {
785 APP_LOGE("delete inner info failed");
786 return ERR_APPEXECFWK_INSTALL_BUNDLE_MGR_SERVICE_ERROR;
787 }
788
789 if (BundlePermissionMgr::DeleteAccessTokenId(info.GetAccessTokenId(userId_)) !=
790 AccessToken::AccessTokenKitRet::RET_SUCCESS) {
791 APP_LOGE("delete accessToken failed");
792 }
793 return ERR_OK;
794 }
795
ProcessBundleInstallStatus(InnerBundleInfo & info,int32_t & uid)796 ErrCode BaseBundleInstaller::ProcessBundleInstallStatus(InnerBundleInfo &info, int32_t &uid)
797 {
798 if (!verifyUriPrefix(info, userId_)) {
799 APP_LOGE("verifyUriPrefix failed");
800 return ERR_APPEXECFWK_INSTALL_URI_DUPLICATE;
801 }
802 modulePackage_ = info.GetCurrentModulePackage();
803 APP_LOGD("ProcessBundleInstallStatus with bundleName %{public}s and packageName %{public}s",
804 bundleName_.c_str(), modulePackage_.c_str());
805 if (!dataMgr_->UpdateBundleInstallState(bundleName_, InstallState::INSTALL_START)) {
806 APP_LOGE("install already start");
807 return ERR_APPEXECFWK_INSTALL_STATE_ERROR;
808 }
809 info.SetInstallMark(bundleName_, modulePackage_, InstallExceptionStatus::INSTALL_START);
810 if (!dataMgr_->SaveInstallMark(info, isAppExist_)) {
811 APP_LOGE("save install mark to storage failed");
812 return ERR_APPEXECFWK_INSTALL_INTERNAL_ERROR;
813 }
814 ScopeGuard stateGuard([&] { dataMgr_->UpdateBundleInstallState(bundleName_, InstallState::INSTALL_FAIL); });
815 ErrCode result = CreateBundleAndDataDir(info);
816 if (result != ERR_OK) {
817 APP_LOGE("create bundle and data dir failed");
818 return result;
819 }
820
821 ScopeGuard bundleGuard([&] { RemoveBundleAndDataDir(info, false); });
822 std::string modulePath = info.GetAppCodePath() + Constants::PATH_SEPARATOR + modulePackage_;
823 result = ExtractModule(info, modulePath);
824 if (result != ERR_OK) {
825 APP_LOGE("extract module failed");
826 return result;
827 }
828
829 result = CreateModuleDataDir(info);
830 if (result != ERR_OK) {
831 APP_LOGE("create module data dir failed");
832 return result;
833 }
834
835 info.SetInstallMark(bundleName_, modulePackage_, InstallExceptionStatus::INSTALL_FINISH);
836 uid = info.GetUid(userId_);
837 info.SetBundleInstallTime(BundleUtil::GetCurrentTime(), userId_);
838 uint32_t tokenId = CreateAccessTokenId(info);
839 info.SetAccessTokenId(tokenId, userId_);
840 result = GrantRequestPermissions(info, tokenId);
841 if (result != ERR_OK) {
842 return result;
843 }
844 if (!dataMgr_->AddInnerBundleInfo(bundleName_, info)) {
845 APP_LOGE("add bundle %{public}s info failed", bundleName_.c_str());
846 dataMgr_->UpdateBundleInstallState(bundleName_, InstallState::UNINSTALL_START);
847 dataMgr_->UpdateBundleInstallState(bundleName_, InstallState::UNINSTALL_SUCCESS);
848 return ERR_APPEXECFWK_INSTALL_BUNDLE_MGR_SERVICE_ERROR;
849 }
850
851 stateGuard.Dismiss();
852 bundleGuard.Dismiss();
853
854 APP_LOGD("finish to call processBundleInstallStatus");
855 return ERR_OK;
856 }
857
ProcessBundleUpdateStatus(InnerBundleInfo & oldInfo,InnerBundleInfo & newInfo,bool isReplace,bool noSkipsKill)858 ErrCode BaseBundleInstaller::ProcessBundleUpdateStatus(
859 InnerBundleInfo &oldInfo, InnerBundleInfo &newInfo, bool isReplace, bool noSkipsKill)
860 {
861 modulePackage_ = newInfo.GetCurrentModulePackage();
862 if (modulePackage_.empty()) {
863 APP_LOGE("get current package failed");
864 return ERR_APPEXECFWK_INSTALL_PARAM_ERROR;
865 }
866 if (isFeatureNeedUninstall_) {
867 uninstallModuleVec_.emplace_back(modulePackage_);
868 }
869 APP_LOGD("ProcessBundleUpdateStatus with bundleName %{public}s and packageName %{public}s",
870 newInfo.GetBundleName().c_str(), modulePackage_.c_str());
871
872 if (!dataMgr_->UpdateBundleInstallState(bundleName_, InstallState::UPDATING_START)) {
873 APP_LOGE("update already start");
874 return ERR_APPEXECFWK_INSTALL_STATE_ERROR;
875 }
876
877 if (oldInfo.GetProvisionId() != newInfo.GetProvisionId()) {
878 APP_LOGE("the signature of the new bundle is not the same as old one");
879 return ERR_APPEXECFWK_INSTALL_FAILED_INCONSISTENT_SIGNATURE;
880 }
881 APP_LOGD("ProcessBundleUpdateStatus noSkipsKill = %{public}d", noSkipsKill);
882 // now there are two cases for updating:
883 // 1. bundle exist, hap exist, update hap
884 // 2. bundle exist, install new hap
885 bool isModuleExist = oldInfo.FindModule(modulePackage_);
886 newInfo.RestoreFromOldInfo(oldInfo);
887 auto result = isModuleExist ? ProcessModuleUpdate(newInfo, oldInfo,
888 isReplace, noSkipsKill) : ProcessNewModuleInstall(newInfo, oldInfo);
889 if (result != ERR_OK) {
890 APP_LOGE("install module failed %{public}d", result);
891 return result;
892 }
893
894 APP_LOGD("finish to call ProcessBundleUpdateStatus");
895 return ERR_OK;
896 }
897
ProcessNewModuleInstall(InnerBundleInfo & newInfo,InnerBundleInfo & oldInfo)898 ErrCode BaseBundleInstaller::ProcessNewModuleInstall(InnerBundleInfo &newInfo, InnerBundleInfo &oldInfo)
899 {
900 APP_LOGD("ProcessNewModuleInstall %{public}s, userId: %{public}d.",
901 newInfo.GetBundleName().c_str(), userId_);
902 ScopeGuard userGuard([&] {
903 if (!hasInstalledInUser_) {
904 RemoveBundleUserData(oldInfo);
905 }
906 });
907
908 if (!verifyUriPrefix(newInfo, userId_)) {
909 APP_LOGE("verifyUriPrefix failed");
910 return ERR_APPEXECFWK_INSTALL_URI_DUPLICATE;
911 }
912
913 if (newInfo.IsSingleUser() && (userId_ != Constants::DEFAULT_USERID)) {
914 APP_LOGE("singleton app(%{public}s) must be installed in user 0.", bundleName_.c_str());
915 return ERR_APPEXECFWK_INSTALL_ZERO_USER_WITH_NO_SINGLETON;
916 }
917
918 if (newInfo.HasEntry() && oldInfo.HasEntry()) {
919 APP_LOGE("install more than one entry module");
920 return ERR_APPEXECFWK_INSTALL_ENTRY_ALREADY_EXIST;
921 }
922
923 if (IsContainModuleName(newInfo, oldInfo)) {
924 APP_LOGE("moduleName is already existed");
925 return ERR_APPEXECFWK_INSTALL_NOT_UNIQUE_DISTRO_MODULE_NAME;
926 }
927
928 oldInfo.SetInstallMark(bundleName_, modulePackage_, InstallExceptionStatus::UPDATING_NEW_START);
929 if (!dataMgr_->SaveInstallMark(oldInfo, true)) {
930 APP_LOGE("save install mark failed");
931 return ERR_APPEXECFWK_INSTALL_INTERNAL_ERROR;
932 }
933 std::string modulePath = newInfo.GetAppCodePath() + Constants::PATH_SEPARATOR + modulePackage_;
934 ErrCode result = ExtractModule(newInfo, modulePath);
935 if (result != ERR_OK) {
936 APP_LOGE("extract module and rename failed");
937 return result;
938 }
939 ScopeGuard moduleGuard([&] { RemoveModuleDir(modulePath); });
940 result = CreateModuleDataDir(newInfo);
941 if (result != ERR_OK) {
942 APP_LOGE("create module data dir failed");
943 return result;
944 }
945 ScopeGuard moduleDataGuard([&] { RemoveModuleDataDir(newInfo, modulePackage_); });
946 if (!dataMgr_->UpdateBundleInstallState(bundleName_, InstallState::UPDATING_SUCCESS)) {
947 APP_LOGE("new moduleupdate state failed");
948 return ERR_APPEXECFWK_INSTALL_BUNDLE_MGR_SERVICE_ERROR;
949 }
950
951 oldInfo.SetInstallMark(bundleName_, modulePackage_, InstallExceptionStatus::INSTALL_FINISH);
952
953 auto bundleUserInfos = oldInfo.GetInnerBundleUserInfos();
954 for (const auto &info : bundleUserInfos) {
955 if (info.second.accessTokenId == 0) {
956 continue;
957 }
958 std::vector<std::string> newRequestPermName;
959 if (!BundlePermissionMgr::AddDefineAndRequestPermissions(info.second.accessTokenId, newInfo,
960 newRequestPermName)) {
961 APP_LOGE("BundlePermissionMgr::AddDefineAndRequestPermissions failed %{public}s", bundleName_.c_str());
962 return ERR_APPEXECFWK_INSTALL_UPDATE_HAP_TOKEN_FAILED;
963 }
964 if (!BundlePermissionMgr::GrantRequestPermissions(newInfo, newRequestPermName, info.second.accessTokenId)) {
965 APP_LOGE("BundlePermissionMgr::GrantRequestPermissions failed %{public}s", bundleName_.c_str());
966 return ERR_APPEXECFWK_INSTALL_GRANT_REQUEST_PERMISSIONS_FAILED;
967 }
968 }
969
970 oldInfo.SetBundleUpdateTime(BundleUtil::GetCurrentTime(), userId_);
971 if (!dataMgr_->AddNewModuleInfo(bundleName_, newInfo, oldInfo)) {
972 APP_LOGE(
973 "add module %{public}s to innerBundleInfo %{public}s failed", modulePackage_.c_str(), bundleName_.c_str());
974 return ERR_APPEXECFWK_INSTALL_BUNDLE_MGR_SERVICE_ERROR;
975 }
976
977 moduleGuard.Dismiss();
978 moduleDataGuard.Dismiss();
979 userGuard.Dismiss();
980 return ERR_OK;
981 }
982
ProcessModuleUpdate(InnerBundleInfo & newInfo,InnerBundleInfo & oldInfo,bool isReplace,bool noSkipsKill)983 ErrCode BaseBundleInstaller::ProcessModuleUpdate(InnerBundleInfo &newInfo,
984 InnerBundleInfo &oldInfo, bool isReplace, bool noSkipsKill)
985 {
986 APP_LOGD("ProcessModuleUpdate, bundleName : %{public}s, moduleName : %{public}s, userId: %{public}d.",
987 newInfo.GetBundleName().c_str(), newInfo.GetCurrentModulePackage().c_str(), userId_);
988 ScopeGuard userGuard([&] {
989 if (!hasInstalledInUser_) {
990 RemoveBundleUserData(oldInfo);
991 }
992 });
993
994 if (!verifyUriPrefix(newInfo, userId_, true)) {
995 APP_LOGE("verifyUriPrefix failed");
996 return ERR_APPEXECFWK_INSTALL_URI_DUPLICATE;
997 }
998
999 if (!IsExistedDistroModule(newInfo, oldInfo)) {
1000 APP_LOGE("moduleName is inconsistent in the updating hap");
1001 return ERR_APPEXECFWK_INSTALL_INCONSISTENT_MODULE_NAME;
1002 }
1003
1004 if (newInfo.IsSingleUser() && (userId_ != Constants::DEFAULT_USERID)) {
1005 APP_LOGE("singleton app(%{public}s) must be installed in user 0.", bundleName_.c_str());
1006 return ERR_APPEXECFWK_INSTALL_ZERO_USER_WITH_NO_SINGLETON;
1007 }
1008
1009 if (!isReplace && versionCode_ == oldInfo.GetVersionCode()) {
1010 if (hasInstalledInUser_) {
1011 APP_LOGE("fail to install already existing bundle using normal flag");
1012 return ERR_APPEXECFWK_INSTALL_ALREADY_EXIST;
1013 }
1014
1015 // app versionCode equals to the old and do not need to update module
1016 // and only need to update userInfo
1017 newInfo.SetOnlyCreateBundleUser(true);
1018 userGuard.Dismiss();
1019 return ERR_OK;
1020 }
1021 APP_LOGE("ProcessModuleUpdate noSkipsKill = %{public}d", noSkipsKill);
1022 // reboot scan case will not kill the bundle
1023 if (noSkipsKill) {
1024 // kill the bundle process during updating
1025 if (!UninstallApplicationProcesses(oldInfo.GetApplicationName(), oldInfo.GetUid(userId_))) {
1026 APP_LOGE("fail to kill running application");
1027 return ERR_APPEXECFWK_INSTALL_INTERNAL_ERROR;
1028 }
1029 }
1030 oldInfo.SetInstallMark(bundleName_, modulePackage_, InstallExceptionStatus::UPDATING_EXISTED_START);
1031 if (!dataMgr_->SaveInstallMark(oldInfo, true)) {
1032 APP_LOGE("save install mark failed");
1033 return ERR_APPEXECFWK_INSTALL_INTERNAL_ERROR;
1034 }
1035 moduleTmpDir_ = newInfo.GetAppCodePath() + Constants::PATH_SEPARATOR + modulePackage_ + Constants::TMP_SUFFIX;
1036 ErrCode result = ExtractModule(newInfo, moduleTmpDir_);
1037 if (result != ERR_OK) {
1038 APP_LOGE("extract module and rename failed");
1039 return result;
1040 }
1041 if (!dataMgr_->UpdateBundleInstallState(bundleName_, InstallState::UPDATING_SUCCESS)) {
1042 APP_LOGE("old module update state failed");
1043 return ERR_APPEXECFWK_INSTALL_BUNDLE_MGR_SERVICE_ERROR;
1044 }
1045
1046 newInfo.RestoreModuleInfo(oldInfo);
1047 oldInfo.SetInstallMark(bundleName_, modulePackage_, InstallExceptionStatus::UPDATING_FINISH);
1048 oldInfo.SetBundleUpdateTime(BundleUtil::GetCurrentTime(), userId_);
1049 auto noUpdateInfo = oldInfo;
1050 if (!dataMgr_->UpdateInnerBundleInfo(bundleName_, newInfo, oldInfo)) {
1051 APP_LOGE("update innerBundleInfo %{public}s failed", bundleName_.c_str());
1052 return ERR_APPEXECFWK_INSTALL_BUNDLE_MGR_SERVICE_ERROR;
1053 }
1054 auto bundleUserInfos = oldInfo.GetInnerBundleUserInfos();
1055 for (const auto &info : bundleUserInfos) {
1056 if (info.second.accessTokenId == 0) {
1057 continue;
1058 }
1059 std::vector<std::string> newRequestPermName;
1060 if (!BundlePermissionMgr::UpdateDefineAndRequestPermissions(info.second.accessTokenId, noUpdateInfo,
1061 oldInfo, newRequestPermName)) {
1062 APP_LOGE("UpdateDefineAndRequestPermissions %{public}s failed", bundleName_.c_str());
1063 return ERR_APPEXECFWK_INSTALL_UPDATE_HAP_TOKEN_FAILED;
1064 }
1065 if (!BundlePermissionMgr::GrantRequestPermissions(oldInfo, newRequestPermName, info.second.accessTokenId)) {
1066 APP_LOGE("BundlePermissionMgr::GrantRequestPermissions failed %{public}s", bundleName_.c_str());
1067 return ERR_APPEXECFWK_INSTALL_GRANT_REQUEST_PERMISSIONS_FAILED;
1068 }
1069 }
1070 ErrCode ret = SetDirApl(newInfo);
1071 if (ret != ERR_OK) {
1072 APP_LOGE("SetDirApl failed");
1073 return ret;
1074 }
1075 userGuard.Dismiss();
1076 return ERR_OK;
1077 }
1078
SetDirApl(const InnerBundleInfo & info)1079 ErrCode BaseBundleInstaller::SetDirApl(const InnerBundleInfo &info)
1080 {
1081 for (const auto &el : Constants::BUNDLE_EL) {
1082 std::string baseBundleDataDir = Constants::BUNDLE_APP_DATA_BASE_DIR +
1083 el +
1084 Constants::FILE_SEPARATOR_CHAR +
1085 std::to_string(userId_);
1086 std::string baseDataDir = baseBundleDataDir + Constants::BASE + info.GetBundleName();
1087 ErrCode result = InstalldClient::GetInstance()->SetDirApl(
1088 baseDataDir, info.GetBundleName(), info.GetAppPrivilegeLevel());
1089 if (result != ERR_OK) {
1090 APP_LOGE("fail to SetDirApl baseDir dir, error is %{public}d", result);
1091 return result;
1092 }
1093 std::string databaseDataDir = baseBundleDataDir + Constants::DATABASE + info.GetBundleName();
1094 result = InstalldClient::GetInstance()->SetDirApl(
1095 databaseDataDir, info.GetBundleName(), info.GetAppPrivilegeLevel());
1096 if (result != ERR_OK) {
1097 APP_LOGE("fail to SetDirApl databaseDir dir, error is %{public}d", result);
1098 return result;
1099 }
1100 }
1101
1102 return ERR_OK;
1103 }
1104
CreateBundleAndDataDir(InnerBundleInfo & info) const1105 ErrCode BaseBundleInstaller::CreateBundleAndDataDir(InnerBundleInfo &info) const
1106 {
1107 ErrCode result = CreateBundleCodeDir(info);
1108 if (result != ERR_OK) {
1109 APP_LOGE("fail to create bundle code dir, error is %{public}d", result);
1110 return result;
1111 }
1112 ScopeGuard codePathGuard([&] { InstalldClient::GetInstance()->RemoveDir(info.GetAppCodePath()); });
1113 result = CreateBundleDataDir(info);
1114 if (result != ERR_OK) {
1115 APP_LOGE("fail to create bundle data dir, error is %{public}d", result);
1116 return result;
1117 }
1118 codePathGuard.Dismiss();
1119 return ERR_OK;
1120 }
1121
CreateBundleCodeDir(InnerBundleInfo & info) const1122 ErrCode BaseBundleInstaller::CreateBundleCodeDir(InnerBundleInfo &info) const
1123 {
1124 auto appCodePath = Constants::BUNDLE_CODE_DIR + Constants::PATH_SEPARATOR + bundleName_;
1125 APP_LOGD("create bundle dir %{public}s", appCodePath.c_str());
1126 ErrCode result = InstalldClient::GetInstance()->CreateBundleDir(appCodePath);
1127 if (result != ERR_OK) {
1128 APP_LOGE("fail to create bundle dir, error is %{public}d", result);
1129 return result;
1130 }
1131
1132 info.SetAppCodePath(appCodePath);
1133 return ERR_OK;
1134 }
1135
CreateBundleDataDir(InnerBundleInfo & info,bool onlyOneUser) const1136 ErrCode BaseBundleInstaller::CreateBundleDataDir(InnerBundleInfo &info, bool onlyOneUser) const
1137 {
1138 InnerBundleUserInfo newInnerBundleUserInfo;
1139 if (!info.GetInnerBundleUserInfo(userId_, newInnerBundleUserInfo)) {
1140 APP_LOGE("bundle(%{public}s) get user(%{public}d) failed.",
1141 info.GetBundleName().c_str(), userId_);
1142 return ERR_APPEXECFWK_USER_NOT_EXIST;
1143 }
1144
1145 if (!dataMgr_->GenerateUidAndGid(newInnerBundleUserInfo)) {
1146 APP_LOGE("fail to gererate uid and gid");
1147 return ERR_APPEXECFWK_INSTALL_GENERATE_UID_ERROR;
1148 }
1149
1150 auto appDataPath = baseDataPath_ + Constants::PATH_SEPARATOR + info.GetBundleName();
1151 auto result = InstalldClient::GetInstance()->CreateBundleDataDir(appDataPath, userId_,
1152 newInnerBundleUserInfo.uid, newInnerBundleUserInfo.uid, info.GetAppPrivilegeLevel(), onlyOneUser);
1153 if (result != ERR_OK) {
1154 APP_LOGE("fail to create bundle data dir, error is %{public}d", result);
1155 return result;
1156 }
1157
1158 if (onlyOneUser) {
1159 UpdateBundlePaths(info, appDataPath);
1160 }
1161
1162 info.AddInnerBundleUserInfo(newInnerBundleUserInfo);
1163 return ERR_OK;
1164 }
1165
ExtractModule(InnerBundleInfo & info,const std::string & modulePath)1166 ErrCode BaseBundleInstaller::ExtractModule(InnerBundleInfo &info, const std::string &modulePath)
1167 {
1168 std::string targetSoPath;
1169 std::string nativeLibraryPath = info.GetBaseApplicationInfo().nativeLibraryPath;
1170 if (!nativeLibraryPath.empty()) {
1171 targetSoPath.append(Constants::BUNDLE_CODE_DIR).append(Constants::PATH_SEPARATOR)
1172 .append(info.GetBundleName()).append(Constants::PATH_SEPARATOR)
1173 .append(nativeLibraryPath).append(Constants::PATH_SEPARATOR);
1174 }
1175 std::string cpuAbi = info.GetBaseApplicationInfo().cpuAbi;
1176 APP_LOGD("begin to extract module files, modulePath : %{public}s, targetSoPath : %{public}s, cpuAbi : %{public}s",
1177 modulePath.c_str(), targetSoPath.c_str(), cpuAbi.c_str());
1178 auto result = ExtractModuleFiles(info, modulePath, targetSoPath, cpuAbi);
1179 if (result != ERR_OK) {
1180 APP_LOGE("fail to extrace module dir, error is %{public}d", result);
1181 return result;
1182 }
1183 auto moduleDir = info.GetAppCodePath() + Constants::PATH_SEPARATOR + info.GetCurrentModulePackage();
1184 info.AddModuleSrcDir(moduleDir);
1185 info.AddModuleResPath(moduleDir);
1186 return ERR_OK;
1187 }
1188
RemoveBundleAndDataDir(const InnerBundleInfo & info,bool isUninstall) const1189 ErrCode BaseBundleInstaller::RemoveBundleAndDataDir(const InnerBundleInfo &info, bool isUninstall) const
1190 {
1191 // remove bundle dir
1192 auto result = RemoveBundleCodeDir(info);
1193 if (result != ERR_OK) {
1194 APP_LOGE("fail to remove bundle dir %{public}s, error is %{public}d", info.GetAppCodePath().c_str(), result);
1195 return result;
1196 }
1197 if (!info.GetIsKeepData() || !isUninstall) {
1198 // remove data dir
1199 result = InstalldClient::GetInstance()->RemoveDir(info.GetBaseDataDir());
1200 if (result != ERR_OK) {
1201 APP_LOGE("fail to remove bundle dir %{public}s, error is %{public}d",
1202 info.GetBaseDataDir().c_str(), result);
1203 return result;
1204 }
1205
1206 result = RemoveBundleDataDir(info);
1207 if (result != ERR_OK) {
1208 APP_LOGE("fail to remove newbundleName: %{public}s, error is %{public}d",
1209 info.GetBundleName().c_str(), result);
1210 }
1211 }
1212 return ERR_OK;
1213 }
1214
RemoveBundleCodeDir(const InnerBundleInfo & info) const1215 ErrCode BaseBundleInstaller::RemoveBundleCodeDir(const InnerBundleInfo &info) const
1216 {
1217 auto result = InstalldClient::GetInstance()->RemoveDir(info.GetAppCodePath());
1218 if (result != ERR_OK) {
1219 APP_LOGE("fail to remove bundle code dir %{public}s, error is %{public}d",
1220 info.GetAppCodePath().c_str(), result);
1221 }
1222 return result;
1223 }
1224
RemoveBundleDataDir(const InnerBundleInfo & info) const1225 ErrCode BaseBundleInstaller::RemoveBundleDataDir(const InnerBundleInfo &info) const
1226 {
1227 ErrCode result =
1228 InstalldClient::GetInstance()->RemoveBundleDataDir(info.GetBundleName(), userId_);
1229 if (result != ERR_OK) {
1230 APP_LOGE("fail to remove bundleName: %{public}s, error is %{public}d",
1231 info.GetBundleName().c_str(), result);
1232 }
1233 return result;
1234 }
1235
RemoveModuleAndDataDir(const InnerBundleInfo & info,const std::string & modulePackage,int32_t userId) const1236 ErrCode BaseBundleInstaller::RemoveModuleAndDataDir(
1237 const InnerBundleInfo &info, const std::string &modulePackage, int32_t userId) const
1238 {
1239 auto moduleDir = info.GetModuleDir(modulePackage);
1240 auto result = RemoveModuleDir(moduleDir);
1241 if (result != ERR_OK) {
1242 APP_LOGE("fail to remove module dir, error is %{public}d", result);
1243 return result;
1244 }
1245
1246 if (!info.GetIsKeepData()) {
1247 result = RemoveModuleDataDir(info, modulePackage);
1248 if (result != ERR_OK) {
1249 APP_LOGE("fail to remove bundle data dir, error is %{public}d", result);
1250 return result;
1251 }
1252
1253 if (userId != Constants::UNSPECIFIED_USERID) {
1254 RemoveHapModuleDataDir(info, modulePackage, userId);
1255 return ERR_OK;
1256 }
1257
1258 for (auto infoItem : info.GetInnerBundleUserInfos()) {
1259 int32_t installedUserId = infoItem.second.bundleUserInfo.userId;
1260 if (installedUserId == userId_) {
1261 continue;
1262 }
1263
1264 RemoveHapModuleDataDir(info, modulePackage, installedUserId);
1265 }
1266 }
1267 return ERR_OK;
1268 }
1269
RemoveModuleDir(const std::string & modulePath) const1270 ErrCode BaseBundleInstaller::RemoveModuleDir(const std::string &modulePath) const
1271 {
1272 APP_LOGD("module dir %{public}s to be removed", modulePath.c_str());
1273 return InstalldClient::GetInstance()->RemoveDir(modulePath);
1274 }
1275
RemoveModuleDataDir(const InnerBundleInfo & info,const std::string & modulePackage) const1276 ErrCode BaseBundleInstaller::RemoveModuleDataDir(const InnerBundleInfo &info, const std::string &modulePackage) const
1277 {
1278 return InstalldClient::GetInstance()->RemoveDir(info.GetModuleDataDir(modulePackage));
1279 }
1280
RemoveHapModuleDataDir(const InnerBundleInfo & info,const std::string & modulePackage,int32_t userId) const1281 ErrCode BaseBundleInstaller::RemoveHapModuleDataDir(
1282 const InnerBundleInfo &info, const std::string &modulePackage, int32_t userId) const
1283 {
1284 APP_LOGD("RemoveHapModuleDataDir bundleName: %{public}s modulePackage: %{public}s",
1285 info.GetBundleName().c_str(),
1286 modulePackage.c_str());
1287 auto hapModuleInfo = info.FindHapModuleInfo(modulePackage);
1288 if (!hapModuleInfo) {
1289 APP_LOGE("fail to findHapModule info modulePackage: %{public}s", modulePackage.c_str());
1290 return ERR_NO_INIT;
1291 }
1292 std::string moduleDataDir = info.GetBundleName() + Constants::HAPS + (*hapModuleInfo).moduleName;
1293 APP_LOGD("RemoveHapModuleDataDir moduleDataDir: %{public}s", moduleDataDir.c_str());
1294 auto result = InstalldClient::GetInstance()->RemoveModuleDataDir(moduleDataDir, userId);
1295 if (result != ERR_OK) {
1296 APP_LOGE("fail to remove HapModuleData dir, error is %{public}d", result);
1297 }
1298 return result;
1299 }
1300
ParseBundleInfo(const std::string & bundleFilePath,InnerBundleInfo & info) const1301 ErrCode BaseBundleInstaller::ParseBundleInfo(const std::string &bundleFilePath, InnerBundleInfo &info) const
1302 {
1303 BundleParser bundleParser;
1304 ErrCode result = bundleParser.Parse(bundleFilePath, info);
1305 if (result != ERR_OK) {
1306 APP_LOGE("parse bundle info failed, error: %{public}d", result);
1307 return result;
1308 }
1309 return ERR_OK;
1310 }
1311
ExtractModuleFiles(const InnerBundleInfo & info,const std::string & modulePath,const std::string & targetSoPath,const std::string & cpuAbi)1312 ErrCode BaseBundleInstaller::ExtractModuleFiles(const InnerBundleInfo &info, const std::string &modulePath,
1313 const std::string &targetSoPath, const std::string &cpuAbi)
1314 {
1315 APP_LOGD("extract module to %{public}s", modulePath.c_str());
1316 auto result = InstalldClient::GetInstance()->ExtractModuleFiles(modulePath_, modulePath, targetSoPath, cpuAbi);
1317 if (result != ERR_OK) {
1318 APP_LOGE("extract module files failed, error is %{public}d", result);
1319 return result;
1320 }
1321 return ERR_OK;
1322 }
1323
RenameModuleDir(const InnerBundleInfo & info) const1324 ErrCode BaseBundleInstaller::RenameModuleDir(const InnerBundleInfo &info) const
1325 {
1326 auto moduleDir = info.GetAppCodePath() + Constants::PATH_SEPARATOR + info.GetCurrentModulePackage();
1327 APP_LOGD("rename module to %{public}s", moduleDir.c_str());
1328 auto result = InstalldClient::GetInstance()->RenameModuleDir(moduleDir + Constants::TMP_SUFFIX, moduleDir);
1329 if (result != ERR_OK) {
1330 APP_LOGE("rename module dir failed, error is %{public}d", result);
1331 return result;
1332 }
1333 return ERR_OK;
1334 }
1335
CreateModuleDataDir(InnerBundleInfo & info) const1336 ErrCode BaseBundleInstaller::CreateModuleDataDir(InnerBundleInfo &info) const
1337 {
1338 auto moduleDataDir = info.GetBaseDataDir() + Constants::PATH_SEPARATOR + modulePackage_;
1339 InnerBundleUserInfo curInnerBundleUserInfo;
1340 if (!info.GetInnerBundleUserInfo(userId_, curInnerBundleUserInfo)) {
1341 APP_LOGE("bundle(%{public}s) get user(%{public}d) failed.",
1342 info.GetBundleName().c_str(), userId_);
1343 return ERR_APPEXECFWK_USER_NOT_EXIST;
1344 }
1345
1346 auto result = InstalldClient::GetInstance()->CreateModuleDataDir(
1347 moduleDataDir, info.GetAbilityNames(), curInnerBundleUserInfo.uid, curInnerBundleUserInfo.uid);
1348 if (result != ERR_OK) {
1349 APP_LOGE("create module data dir failed, error is %{public}d", result);
1350 return result;
1351 }
1352
1353 info.AddModuleDataDir(moduleDataDir);
1354 return ERR_OK;
1355 }
1356
ModifyInstallDirByHapType(const InstallParam & installParam,const Constants::AppType appType)1357 ErrCode BaseBundleInstaller::ModifyInstallDirByHapType(const InstallParam &installParam,
1358 const Constants::AppType appType)
1359 {
1360 auto dirUser = (userId_ == Constants::START_USERID) ? Constants::DEFAULT_USERID : userId_;
1361 auto internalPath = Constants::PATH_SEPARATOR + Constants::USER_ACCOUNT_DIR + Constants::FILE_UNDERLINE +
1362 std::to_string(dirUser) + Constants::PATH_SEPARATOR;
1363 switch (appType) {
1364 case Constants::AppType::SYSTEM_APP:
1365 baseDataPath_ = Constants::SYSTEM_APP_INSTALL_PATH + internalPath + Constants::APP_DATA_DIR;
1366 break;
1367 case Constants::AppType::THIRD_SYSTEM_APP:
1368 baseDataPath_ = Constants::THIRD_SYSTEM_APP_INSTALL_PATH + internalPath + Constants::APP_DATA_DIR;
1369 break;
1370 case Constants::AppType::THIRD_PARTY_APP:
1371 baseDataPath_ = Constants::THIRD_PARTY_APP_INSTALL_PATH + internalPath + Constants::APP_DATA_DIR;
1372 break;
1373 default:
1374 APP_LOGE("App type error");
1375 return ERR_APPEXECFWK_INSTALL_PARAM_ERROR;
1376 }
1377 return ERR_OK;
1378 }
1379
UpdateBundlePaths(InnerBundleInfo & info,const std::string baseDataPath) const1380 bool BaseBundleInstaller::UpdateBundlePaths(InnerBundleInfo &info, const std::string baseDataPath) const
1381 {
1382 info.SetBaseDataDir(baseDataPath);
1383 info.SetAppDataDir(baseDataPath);
1384 info.SetAppDataBaseDir(baseDataPath + Constants::PATH_SEPARATOR + Constants::DATA_BASE_DIR);
1385 info.SetAppCacheDir(baseDataPath + Constants::PATH_SEPARATOR + Constants::CACHE_DIR);
1386 return true;
1387 }
1388
CheckSysCap(const std::vector<std::string> & bundlePaths)1389 ErrCode BaseBundleInstaller::CheckSysCap(const std::vector<std::string> &bundlePaths)
1390 {
1391 BYTRACE_NAME(BYTRACE_TAG_APP, __PRETTY_FUNCTION__);
1392 APP_LOGD("check hap syscaps start.");
1393 if (bundlePaths.empty()) {
1394 APP_LOGE("check hap syscaps failed due to empty bundlePaths!");
1395 return ERR_APPEXECFWK_INSTALL_PARAM_ERROR;
1396 }
1397
1398 ErrCode result = ERR_OK;
1399 BundleParser bundleParser;
1400 for (auto bundlePath : bundlePaths) {
1401 std::vector<std::string> bundleSysCaps;
1402 result = bundleParser.ParseSysCap(bundlePath, bundleSysCaps);
1403 if (result != ERR_OK) {
1404 APP_LOGE("parse bundle syscap failed, error: %{public}d", result);
1405 return result;
1406 }
1407
1408 for (auto bundleSysCapItem : bundleSysCaps) {
1409 APP_LOGD("check syscap(%{public}s)", bundleSysCapItem.c_str());
1410 if (!HasSystemCapability(bundleSysCapItem.c_str())) {
1411 APP_LOGE("check syscap failed which %{public}s is not exsit",
1412 bundleSysCapItem.c_str());
1413 return ERR_APPEXECFWK_INSTALL_CHECK_SYSCAP_FAILED;
1414 }
1415 }
1416 }
1417
1418 APP_LOGD("finish check hap syscaps");
1419 return result;
1420 }
1421
CheckMultipleHapsSignInfo(const std::vector<std::string> & bundlePaths,const InstallParam & installParam,std::vector<Security::Verify::HapVerifyResult> & hapVerifyRes) const1422 ErrCode BaseBundleInstaller::CheckMultipleHapsSignInfo(const std::vector<std::string> &bundlePaths,
1423 const InstallParam &installParam, std::vector<Security::Verify::HapVerifyResult>& hapVerifyRes) const
1424 {
1425 BYTRACE_NAME(BYTRACE_TAG_APP, __PRETTY_FUNCTION__);
1426 APP_LOGD("Check multiple haps signInfo");
1427 if (bundlePaths.empty()) {
1428 APP_LOGE("check hap sign info failed due to empty bundlePaths!");
1429 return ERR_APPEXECFWK_INSTALL_PARAM_ERROR;
1430 }
1431 for (const std::string &bundlePath : bundlePaths) {
1432 Security::Verify::HapVerifyResult hapVerifyResult;
1433 auto verifyRes = BundleVerifyMgr::HapVerify(bundlePath, hapVerifyResult);
1434 if (verifyRes != ERR_OK) {
1435 APP_LOGE("hap file verify failed");
1436 return verifyRes;
1437 }
1438 hapVerifyRes.emplace_back(hapVerifyResult);
1439 }
1440 if (hapVerifyRes.empty()) {
1441 APP_LOGE("no sign info in the all haps!");
1442 return ERR_APPEXECFWK_INSTALL_FAILED_INCOMPATIBLE_SIGNATURE;
1443 }
1444 auto appId = hapVerifyRes[0].GetProvisionInfo().appId;
1445 APP_LOGD("bundle appid is %{private}s", appId.c_str());
1446 auto isValid = std::any_of(hapVerifyRes.begin(), hapVerifyRes.end(), [&](auto &hapVerifyResult) {
1447 APP_LOGD("module appid is %{private}s", hapVerifyResult.GetProvisionInfo().appId.c_str());
1448 return appId != hapVerifyResult.GetProvisionInfo().appId;
1449 });
1450 if (isValid) {
1451 APP_LOGE("hap files have different signuature info");
1452 return ERR_APPEXECFWK_INSTALL_FAILED_INCONSISTENT_SIGNATURE;
1453 }
1454 auto apl = hapVerifyRes[0].GetProvisionInfo().bundleInfo.apl;
1455 APP_LOGD("bundle apl is %{public}s", apl.c_str());
1456 isValid = std::any_of(hapVerifyRes.begin(), hapVerifyRes.end(), [&](auto &hapVerifyResult) {
1457 APP_LOGD("module appid is %{private}s", hapVerifyResult.GetProvisionInfo().bundleInfo.apl.c_str());
1458 return apl != hapVerifyResult.GetProvisionInfo().bundleInfo.apl;
1459 });
1460 if (isValid) {
1461 APP_LOGE("hap files have different apl info");
1462 return ERR_APPEXECFWK_INSTALL_FAILED_INCONSISTENT_SIGNATURE;
1463 }
1464 APP_LOGD("finish check multiple haps signInfo");
1465 return ERR_OK;
1466 }
1467
ParseHapFiles(const std::vector<std::string> & bundlePaths,const InstallParam & installParam,const Constants::AppType appType,std::vector<Security::Verify::HapVerifyResult> & hapVerifyRes,std::unordered_map<std::string,InnerBundleInfo> & infos)1468 ErrCode BaseBundleInstaller::ParseHapFiles(const std::vector<std::string> &bundlePaths,
1469 const InstallParam &installParam, const Constants::AppType appType,
1470 std::vector<Security::Verify::HapVerifyResult> &hapVerifyRes,
1471 std::unordered_map<std::string, InnerBundleInfo> &infos)
1472 {
1473 BYTRACE_NAME(BYTRACE_TAG_APP, __PRETTY_FUNCTION__);
1474 APP_LOGD("Parse hap file");
1475 ErrCode result = ERR_OK;
1476 for (uint32_t i = 0; i < bundlePaths.size(); ++i) {
1477 InnerBundleInfo newInfo;
1478 newInfo.SetAppType(appType);
1479 Security::Verify::ProvisionInfo provisionInfo = hapVerifyRes[i].GetProvisionInfo();
1480 bool isSystemApp = (provisionInfo.bundleInfo.appFeature == Constants::HOS_SYSTEM_APP ||
1481 provisionInfo.bundleInfo.appFeature == Constants::OHOS_SYSTEM_APP);
1482 if (isSystemApp) {
1483 newInfo.SetAppType(Constants::AppType::SYSTEM_APP);
1484 }
1485 newInfo.SetUserId(installParam.userId);
1486 newInfo.SetIsKeepData(installParam.isKeepData);
1487 newInfo.SetIsPreInstallApp(installParam.isPreInstallApp);
1488 result = ParseBundleInfo(bundlePaths[i], newInfo);
1489 if (result != ERR_OK) {
1490 APP_LOGE("bundle parse failed %{public}d", result);
1491 return result;
1492 }
1493 if (newInfo.HasEntry()) {
1494 if (isContainEntry_) {
1495 APP_LOGE("more than one entry hap in the direction!");
1496 return ERR_APPEXECFWK_INSTALL_INVALID_NUMBER_OF_ENTRY_HAP;
1497 } else {
1498 isContainEntry_ = true;
1499 }
1500 }
1501 newInfo.SetProvisionId(provisionInfo.appId);
1502 newInfo.SetAppFeature(provisionInfo.bundleInfo.appFeature);
1503 newInfo.SetAppPrivilegeLevel(provisionInfo.bundleInfo.apl);
1504 newInfo.SetAllowedAcls(provisionInfo.acls.allowedAcls);
1505
1506 if ((result = CheckSystemSize(bundlePaths[i], appType)) != ERR_OK) {
1507 APP_LOGE("install failed due to insufficient disk memory");
1508 return result;
1509 }
1510
1511 infos.emplace(bundlePaths[i], newInfo);
1512 }
1513 if ((result = CheckModuleNameForMulitHaps(infos)) != ERR_OK) {
1514 APP_LOGE("install failed due to duplicated moduleName");
1515 return result;
1516 }
1517 APP_LOGD("finish parse hap file");
1518 return result;
1519 }
1520
CheckAppLabelInfo(const std::unordered_map<std::string,InnerBundleInfo> & infos)1521 ErrCode BaseBundleInstaller::CheckAppLabelInfo(const std::unordered_map<std::string, InnerBundleInfo> &infos)
1522 {
1523 APP_LOGD("Check APP label");
1524 ErrCode ret = ERR_OK;
1525 std::string bundleName = (infos.begin()->second).GetBundleName();
1526 std::string vendor = (infos.begin()->second).GetVendor();
1527 versionCode_ = (infos.begin()->second).GetVersionCode();
1528 std::string versionName = (infos.begin()->second).GetVersionName();
1529 uint32_t target = (infos.begin()->second).GetTargetVersion();
1530 uint32_t compatible = (infos.begin()->second).GetCompatibleVersion();
1531 bool singleUser = (infos.begin()->second).IsSingleUser();
1532 Constants::AppType appType = (infos.begin()->second).GetAppType();
1533
1534 for (const auto &info :infos) {
1535 // check bundleName
1536 if (bundleName != info.second.GetBundleName()) {
1537 return ERR_APPEXECFWK_INSTALL_BUNDLENAME_NOT_SAME;
1538 }
1539 // check version
1540 if (versionCode_ != info.second.GetVersionCode()) {
1541 return ERR_APPEXECFWK_INSTALL_VERSIONCODE_NOT_SAME;
1542 }
1543 if (versionName != info.second.GetVersionName()) {
1544 return ERR_APPEXECFWK_INSTALL_VERSIONNAME_NOT_SAME;
1545 }
1546 // check vendor
1547 if (vendor != info.second.GetVendor()) {
1548 return ERR_APPEXECFWK_INSTALL_VENDOR_NOT_SAME;
1549 }
1550 // check release type
1551 if (target != info.second.GetTargetVersion()) {
1552 return ERR_APPEXECFWK_INSTALL_RELEASETYPE_TARGET_NOT_SAME;
1553 }
1554 if (compatible != info.second.GetCompatibleVersion()) {
1555 return ERR_APPEXECFWK_INSTALL_RELEASETYPE_COMPATIBLE_NOT_SAME;
1556 }
1557 if (singleUser != info.second.IsSingleUser()) {
1558 return ERR_APPEXECFWK_INSTALL_SINGLETON_NOT_SAME;
1559 }
1560 if (appType != info.second.GetAppType()) {
1561 return ERR_APPEXECFWK_INSTALL_APPTYPE_NOT_SAME;
1562 }
1563 }
1564 APP_LOGD("finish check APP label");
1565 return ret;
1566 }
1567
GetInnerBundleInfo(InnerBundleInfo & info,bool & isAppExist)1568 bool BaseBundleInstaller::GetInnerBundleInfo(InnerBundleInfo &info, bool &isAppExist)
1569 {
1570 if (!dataMgr_) {
1571 dataMgr_ = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
1572 if (!dataMgr_) {
1573 APP_LOGE("Get dataMgr shared_ptr nullptr");
1574 return false;
1575 }
1576 }
1577 auto &mtx = dataMgr_->GetBundleMutex(bundleName_);
1578 std::lock_guard lock { mtx };
1579 isAppExist = dataMgr_->GetInnerBundleInfo(bundleName_, Constants::CURRENT_DEVICE_ID, info);
1580 return true;
1581 }
1582
1583 // In the process of hap updating, the version code of the entry hap which is about to be updated must not less the
1584 // version code of the current entry haps in the device; if no-entry hap in the device, the updating haps should
1585 // have same version code with the current version code; if the no-entry haps is to be updated, which should has the
1586 // same version code with that of the entry hap in the device.
CheckVersionCompatibility(const InnerBundleInfo & oldInfo)1587 ErrCode BaseBundleInstaller::CheckVersionCompatibility(const InnerBundleInfo &oldInfo)
1588 {
1589 APP_LOGD("start to check version compatibility");
1590 if (oldInfo.HasEntry()) {
1591 if (isContainEntry_ && versionCode_ < oldInfo.GetVersionCode()) {
1592 APP_LOGE("fail to update lower version bundle");
1593 return ERR_APPEXECFWK_INSTALL_VERSION_DOWNGRADE;
1594 }
1595 if (!isContainEntry_ && versionCode_ > oldInfo.GetVersionCode()) {
1596 APP_LOGE("version code is not compatible");
1597 return ERR_APPEXECFWK_INSTALL_VERSION_NOT_COMPATIBLE;
1598 }
1599 if (!isContainEntry_ && versionCode_ < oldInfo.GetVersionCode()) {
1600 APP_LOGE("version code is not compatible");
1601 return ERR_APPEXECFWK_INSTALL_VERSION_DOWNGRADE;
1602 }
1603 } else {
1604 if (versionCode_ < oldInfo.GetVersionCode()) {
1605 APP_LOGE("fail to update lower version bundle");
1606 return ERR_APPEXECFWK_INSTALL_VERSION_DOWNGRADE;
1607 }
1608 }
1609
1610 if (versionCode_ > oldInfo.GetVersionCode()) {
1611 APP_LOGD("need to uninstall lower version feature hap");
1612 isFeatureNeedUninstall_ = true;
1613 }
1614 APP_LOGD("finish to check version compatibility");
1615 return ERR_OK;
1616 }
1617
UninstallLowerVersionFeature(const std::vector<std::string> & packageVec)1618 ErrCode BaseBundleInstaller::UninstallLowerVersionFeature(const std::vector<std::string> &packageVec)
1619 {
1620 APP_LOGD("start to uninstall lower version feature hap");
1621 InnerBundleInfo info;
1622 bool isExist = false;
1623 if (!GetInnerBundleInfo(info, isExist) || !isExist) {
1624 return ERR_APPEXECFWK_UNINSTALL_BUNDLE_MGR_SERVICE_ERROR;
1625 }
1626
1627 if (!dataMgr_->UpdateBundleInstallState(bundleName_, InstallState::UNINSTALL_START)) {
1628 APP_LOGE("uninstall already start");
1629 return ERR_APPEXECFWK_INSTALL_BUNDLE_MGR_SERVICE_ERROR;
1630 }
1631
1632 // kill the bundle process during uninstall.
1633 if (!UninstallApplicationProcesses(info.GetApplicationName(), info.GetUid(userId_))) {
1634 APP_LOGW("can not kill process");
1635 }
1636 std::vector<std::string> moduleVec = info.GetModuleNameVec();
1637 InnerBundleInfo oldInfo = info;
1638 for (const auto &package : moduleVec) {
1639 if (find(packageVec.begin(), packageVec.end(), package) == packageVec.end()) {
1640 APP_LOGD("uninstall package %{public}s", package.c_str());
1641 ErrCode result = RemoveModuleAndDataDir(info, package);
1642 if (result != ERR_OK) {
1643 APP_LOGE("remove module dir failed");
1644 return result;
1645 }
1646 if (!dataMgr_->RemoveModuleInfo(bundleName_, package, info)) {
1647 APP_LOGE("RemoveModuleInfo failed");
1648 return ERR_APPEXECFWK_INSTALL_BUNDLE_MGR_SERVICE_ERROR;
1649 }
1650 }
1651 }
1652 // need to delete lower version feature hap definePermissions and requestPermissions
1653 APP_LOGD("delete lower version feature hap definePermissions and requestPermissions");
1654 ErrCode ret = UpdateDefineAndRequestPermissions(oldInfo, info);
1655 if (ret != ERR_OK) {
1656 return ret;
1657 }
1658 APP_LOGD("finish to uninstall lower version feature hap");
1659 return ERR_OK;
1660 }
1661
CheckSystemSize(const std::string & bundlePath,const Constants::AppType appType) const1662 ErrCode BaseBundleInstaller::CheckSystemSize(const std::string &bundlePath, const Constants::AppType appType) const
1663 {
1664 if ((appType == Constants::AppType::SYSTEM_APP) &&
1665 (BundleUtil::CheckSystemSize(bundlePath, Constants::SYSTEM_APP_INSTALL_PATH))) {
1666 return ERR_OK;
1667 }
1668 if ((appType == Constants::AppType::THIRD_SYSTEM_APP) &&
1669 (BundleUtil::CheckSystemSize(bundlePath, Constants::THIRD_SYSTEM_APP_INSTALL_PATH))) {
1670 return ERR_OK;
1671 }
1672 if ((appType == Constants::AppType::THIRD_PARTY_APP) &&
1673 (BundleUtil::CheckSystemSize(bundlePath, Constants::THIRD_PARTY_APP_INSTALL_PATH))) {
1674 return ERR_OK;
1675 }
1676 APP_LOGE("install failed due to insufficient disk memory");
1677 return ERR_APPEXECFWK_INSTALL_DISK_MEM_INSUFFICIENT;
1678 }
1679
GetUserId(const InstallParam & installParam) const1680 int32_t BaseBundleInstaller::GetUserId(const InstallParam& installParam) const
1681 {
1682 int32_t userId = installParam.userId;
1683 if (userId < Constants::DEFAULT_USERID) {
1684 APP_LOGE("userId(%{public}d) is invalid.", userId);
1685 return Constants::INVALID_USERID;
1686 }
1687
1688 APP_LOGD("BundleInstaller GetUserId, now userId is %{public}d", userId);
1689 return userId;
1690 }
1691
CreateBundleUserData(InnerBundleInfo & innerBundleInfo,bool needResetInstallState)1692 ErrCode BaseBundleInstaller::CreateBundleUserData(
1693 InnerBundleInfo &innerBundleInfo, bool needResetInstallState)
1694 {
1695 APP_LOGD("CreateNewUserData %{public}s userId: %{public}d.",
1696 innerBundleInfo.GetBundleName().c_str(), userId_);
1697 if (!innerBundleInfo.HasInnerBundleUserInfo(userId_)) {
1698 return ERR_APPEXECFWK_USER_NOT_EXIST;
1699 }
1700
1701 ErrCode result = CreateBundleDataDir(innerBundleInfo, false);
1702 if (result != ERR_OK) {
1703 return result;
1704 }
1705
1706 innerBundleInfo.SetBundleInstallTime(BundleUtil::GetCurrentTime(), userId_);
1707 if (!dataMgr_->UpdateBundleInstallState(innerBundleInfo.GetBundleName(), InstallState::USER_CHANGE)) {
1708 APP_LOGE("update bundleinfo when user change failed.");
1709 return ERR_APPEXECFWK_INSTALL_BUNDLE_MGR_SERVICE_ERROR;
1710 }
1711
1712 ScopeGuard stateGuard([&] {
1713 dataMgr_->UpdateBundleInstallState(innerBundleInfo.GetBundleName(), InstallState::INSTALL_SUCCESS);
1714 });
1715 InnerBundleUserInfo innerBundleUserInfo;
1716 if (!innerBundleInfo.GetInnerBundleUserInfo(userId_, innerBundleUserInfo)) {
1717 APP_LOGE("oldInfo do not have user");
1718 return ERR_APPEXECFWK_USER_NOT_EXIST;
1719 }
1720
1721 if (!dataMgr_->AddInnerBundleUserInfo(innerBundleInfo.GetBundleName(), innerBundleUserInfo)) {
1722 APP_LOGE("update bundle user info to db failed %{public}s when createNewUser",
1723 innerBundleInfo.GetBundleName().c_str());
1724 return ERR_APPEXECFWK_INSTALL_BUNDLE_MGR_SERVICE_ERROR;
1725 }
1726
1727 if (!needResetInstallState) {
1728 stateGuard.Dismiss();
1729 }
1730
1731 return ERR_OK;
1732 }
1733
UpdateUserInfoToDb(InnerBundleInfo & innerBundleInfo,bool needResetInstallState)1734 ErrCode BaseBundleInstaller::UpdateUserInfoToDb(
1735 InnerBundleInfo &innerBundleInfo, bool needResetInstallState)
1736 {
1737 APP_LOGD("update user(%{public}d) in bundle(%{public}s) to Db start.",
1738 userId_, innerBundleInfo.GetBundleName().c_str());
1739 if (!dataMgr_->UpdateBundleInstallState(innerBundleInfo.GetBundleName(), InstallState::USER_CHANGE)) {
1740 APP_LOGE("update bundleinfo when user change failed.");
1741 return ERR_APPEXECFWK_INSTALL_BUNDLE_MGR_SERVICE_ERROR;
1742 }
1743
1744 ScopeGuard stateGuard([&] {
1745 dataMgr_->UpdateBundleInstallState(innerBundleInfo.GetBundleName(), InstallState::INSTALL_SUCCESS);
1746 });
1747 auto newBundleInfo = innerBundleInfo;
1748 if (!dataMgr_->UpdateInnerBundleInfo(innerBundleInfo.GetBundleName(), newBundleInfo, innerBundleInfo)) {
1749 APP_LOGE("update bundle user info to db failed %{public}s when createNewUser",
1750 innerBundleInfo.GetBundleName().c_str());
1751 return ERR_APPEXECFWK_INSTALL_BUNDLE_MGR_SERVICE_ERROR;
1752 }
1753
1754 if (!needResetInstallState) {
1755 stateGuard.Dismiss();
1756 }
1757
1758 APP_LOGD("update user(%{public}d) in bundle(%{public}s) to Db end.",
1759 userId_, innerBundleInfo.GetBundleName().c_str());
1760 return ERR_OK;
1761 }
1762
RemoveBundleUserData(InnerBundleInfo & innerBundleInfo)1763 ErrCode BaseBundleInstaller::RemoveBundleUserData(InnerBundleInfo &innerBundleInfo)
1764 {
1765 auto bundleName = innerBundleInfo.GetBundleName();
1766 APP_LOGD("remove user(%{public}d) in bundle(%{public}s).", userId_, bundleName.c_str());
1767 if (!innerBundleInfo.HasInnerBundleUserInfo(userId_)) {
1768 return ERR_APPEXECFWK_USER_NOT_EXIST;
1769 }
1770
1771 ErrCode result = RemoveBundleDataDir(innerBundleInfo);
1772 if (result != ERR_OK) {
1773 APP_LOGE("remove user data directory failed.");
1774 return result;
1775 }
1776 // delete accessTokenId
1777 if (BundlePermissionMgr::DeleteAccessTokenId(innerBundleInfo.GetAccessTokenId(userId_)) !=
1778 AccessToken::AccessTokenKitRet::RET_SUCCESS) {
1779 APP_LOGE("delete accessToken failed");
1780 }
1781
1782 innerBundleInfo.RemoveInnerBundleUserInfo(userId_);
1783 if (!dataMgr_->UpdateBundleInstallState(innerBundleInfo.GetBundleName(), InstallState::USER_CHANGE)) {
1784 APP_LOGE("update bundleinfo when user change failed.");
1785 return ERR_APPEXECFWK_INSTALL_BUNDLE_MGR_SERVICE_ERROR;
1786 }
1787
1788 if (!dataMgr_->RemoveInnerBundleUserInfo(innerBundleInfo.GetBundleName(), userId_)) {
1789 APP_LOGE("update bundle user info to db failed %{public}s when remove user",
1790 innerBundleInfo.GetBundleName().c_str());
1791 return ERR_APPEXECFWK_INSTALL_BUNDLE_MGR_SERVICE_ERROR;
1792 }
1793
1794 dataMgr_->UpdateBundleInstallState(innerBundleInfo.GetBundleName(), InstallState::INSTALL_SUCCESS);
1795 return ERR_OK;
1796 }
1797
verifyUriPrefix(const InnerBundleInfo & info,int32_t userId,bool isUpdate) const1798 bool BaseBundleInstaller::verifyUriPrefix(const InnerBundleInfo &info, int32_t userId, bool isUpdate) const
1799 {
1800 // uriPrefix must be unique
1801 // verify current module uriPrefix
1802 std::vector<std::string> currentUriPrefixList;
1803 info.GetUriPrefixList(currentUriPrefixList);
1804 if (currentUriPrefixList.empty()) {
1805 APP_LOGD("current module not include uri, verify uriPrefix success");
1806 return true;
1807 }
1808 std::set<std::string> set;
1809 for (const std::string ¤tUriPrefix : currentUriPrefixList) {
1810 if (currentUriPrefix == Constants::DATA_ABILITY_URI_PREFIX) {
1811 APP_LOGE("uri format invalid");
1812 return false;
1813 }
1814 if (!set.insert(currentUriPrefix).second) {
1815 APP_LOGE("current module contains duplicate uriPrefix, verify uriPrefix failed");
1816 APP_LOGE("bundleName : %{public}s, moduleName : %{public}s, uriPrefix : %{public}s",
1817 info.GetBundleName().c_str(), info.GetCurrentModulePackage().c_str(), currentUriPrefix.c_str());
1818 return false;
1819 }
1820 }
1821 set.clear();
1822 // verify exist bundle uriPrefix
1823 if (dataMgr_ == nullptr) {
1824 APP_LOGE("dataMgr_ is null, verify uriPrefix failed");
1825 return false;
1826 }
1827 std::vector<std::string> uriPrefixList;
1828 std::string excludeModule;
1829 if (isUpdate) {
1830 excludeModule.append(info.GetBundleName()).append(".").append(info.GetCurrentModulePackage()).append(".");
1831 }
1832 dataMgr_->GetAllUriPrefix(uriPrefixList, userId, excludeModule);
1833 if (uriPrefixList.empty()) {
1834 APP_LOGD("uriPrefixList empty, verify uriPrefix success");
1835 return true;
1836 }
1837 for (const std::string ¤tUriPrefix : currentUriPrefixList) {
1838 auto iter = std::find(uriPrefixList.cbegin(), uriPrefixList.cend(), currentUriPrefix);
1839 if (iter != uriPrefixList.cend()) {
1840 APP_LOGE("uriPrefix alread exist in device, uriPrefix : %{public}s", currentUriPrefix.c_str());
1841 APP_LOGE("verify uriPrefix failed");
1842 return false;
1843 }
1844 }
1845 APP_LOGD("verify uriPrefix success");
1846 return true;
1847 }
1848
ResetInstallProperties()1849 void BaseBundleInstaller::ResetInstallProperties()
1850 {
1851 isContainEntry_ = false;
1852 isAppExist_ = false;
1853 hasInstalledInUser_ = false;
1854 isFeatureNeedUninstall_ = false;
1855 versionCode_ = 0;
1856 uninstallModuleVec_.clear();
1857 installedModules_.clear();
1858 state_ = InstallerState::INSTALL_START;
1859 }
1860
CheckModuleNameForMulitHaps(const std::unordered_map<std::string,InnerBundleInfo> & infos) const1861 ErrCode BaseBundleInstaller::CheckModuleNameForMulitHaps(
1862 const std::unordered_map<std::string, InnerBundleInfo> &infos) const
1863 {
1864 std::set<std::string> moduleSet;
1865 for (const auto &info : infos) {
1866 std::vector<std::string> moduleVec = info.second.GetDistroModuleName();
1867 if (moduleVec.empty()) {
1868 APP_LOGE("moduleName vector is empty");
1869 return ERR_APPEXECFWK_INSTALL_INTERNAL_ERROR;
1870 }
1871 moduleSet.insert(moduleVec[0]);
1872 }
1873
1874 if (moduleSet.size() != infos.size()) {
1875 APP_LOGE("someone moduleName is not unique in the haps");
1876 return ERR_APPEXECFWK_INSTALL_NOT_UNIQUE_DISTRO_MODULE_NAME;
1877 }
1878 return ERR_OK;
1879 }
1880
IsExistedDistroModule(const InnerBundleInfo & newInfo,const InnerBundleInfo & info) const1881 bool BaseBundleInstaller::IsExistedDistroModule(const InnerBundleInfo &newInfo, const InnerBundleInfo &info) const
1882 {
1883 std::string moduleName = newInfo.GetCurModuleName();
1884 std::string packageName = newInfo.GetCurrentModulePackage();
1885 if (packageName.empty() || moduleName.empty()) {
1886 APP_LOGE("IsExistedDistroModule failed due to invalid packageName or moduleName");
1887 return false;
1888 }
1889 std::string oldModuleName = info.GetModuleNameByPackage(packageName);
1890 // check consistency of module name
1891 if (moduleName.compare(oldModuleName) != 0) {
1892 APP_LOGE("no moduleName in the innerModuleInfo");
1893 return false;
1894 }
1895 // check consistency of module type
1896 std::string newModuleType = newInfo.GetModuleTypeByPackage(packageName);
1897 std::string oldModuleType = info.GetModuleTypeByPackage(packageName);
1898 if (newModuleType.compare(oldModuleType) != 0) {
1899 APP_LOGE("moduleType is different between the new hap and the original hap");
1900 return false;
1901 }
1902
1903 return true;
1904 }
1905
IsContainModuleName(const InnerBundleInfo & newInfo,const InnerBundleInfo & info) const1906 bool BaseBundleInstaller::IsContainModuleName(const InnerBundleInfo &newInfo, const InnerBundleInfo &info) const
1907 {
1908 std::string moduleName = newInfo.GetCurModuleName();
1909 std::vector<std::string> moduleVec = info.GetDistroModuleName();
1910 if (moduleName.empty() || moduleVec.empty()) {
1911 APP_LOGE("IsContainModuleName failed due to invalid moduleName or modulevec");
1912 return false;
1913 }
1914 return (find(moduleVec.cbegin(), moduleVec.cend(), moduleName) == moduleVec.cend()) ? false : true;
1915 }
1916 } // namespace AppExecFwk
1917 } // namespace OHOS
1918