diff --git a/services/backup_sa/src/module_external/bms_adapter.cpp b/services/backup_sa/src/module_external/bms_adapter.cpp index 09a0d132ac82817a43829a226c7d2562fd7e6011..7a3465562920ecb55a7cd315a5b436c3b98ed0be 100644 --- a/services/backup_sa/src/module_external/bms_adapter.cpp +++ b/services/backup_sa/src/module_external/bms_adapter.cpp @@ -331,7 +331,8 @@ vector BundleMgrAdapter::GetBundleInfosForIncrement HILOGI("Begin get bundle infos"); for (auto const &installedBundle : installedBundles) { if (installedBundle.applicationInfo.codePath == HMOS_HAP_CODE_PATH || - installedBundle.applicationInfo.codePath == LINUX_HAP_CODE_PATH) { + installedBundle.applicationInfo.codePath == LINUX_HAP_CODE_PATH || + installedBundle.applicationInfo.singleton == true) { HILOGI("Unsupported applications, name : %{public}s", installedBundle.name.data()); continue; } @@ -451,7 +452,8 @@ bool BundleMgrAdapter::GetCurBundleExtenionInfo(AppExecFwk::BundleInfo &installe return false; } if (installedBundle.applicationInfo.codePath == HMOS_HAP_CODE_PATH || - installedBundle.applicationInfo.codePath == LINUX_HAP_CODE_PATH) { + installedBundle.applicationInfo.codePath == LINUX_HAP_CODE_PATH || + installedBundle.applicationInfo.singleton == true) { HILOGE("Unsupported applications, name : %{public}s", installedBundle.name.data()); return false; } diff --git a/services/backup_sa/src/module_ipc/service.cpp b/services/backup_sa/src/module_ipc/service.cpp index dc7b90a9f97d402103bebfc95914ee7574e9eea3..af95a87f4d9fb8a01a3c65376f50bfa409ed8e00 100644 --- a/services/backup_sa/src/module_ipc/service.cpp +++ b/services/backup_sa/src/module_ipc/service.cpp @@ -318,9 +318,22 @@ static void OnBundleStarted(BError error, sptr session, const session->GetServiceReverseProxy()->IncrementalRestoreOnBundleStarted(error, bundleName); } else if (scenario == IServiceReverse::Scenario::RESTORE) { session->GetServiceReverseProxy()->RestoreOnBundleStarted(error, bundleName); + } else if (scenario == IServiceReverse::Scenario::BACKUP && session->GetIsIncrementalBackup()) { + session->GetServiceReverseProxy()->IncrementalBackupOnBundleStarted(error, bundleName); + } else if (scenario == IServiceReverse::Scenario::BACKUP) { + session->GetServiceReverseProxy()->BackupOnBundleStarted(error, bundleName); } } +static vector GetBackupBundleNames(const vector &bundleInfos) +{ + vector backupBundleNames {}; + for (auto &bundleInfo : bundleInfos) { + backupBundleNames.emplace_back(bundleInfo.name); + } + return backupBundleNames; +} + static vector GetRestoreBundleNames(UniqueFd fd, sptr session, const vector &bundleNames) @@ -550,7 +563,9 @@ ErrCode Service::AppendBundlesBackupSession(const vector &bundleName session_->IncreaseSessionCnt(); // BundleMgrAdapter::GetBundleInfos可能耗时 VerifyCaller(IServiceReverse::Scenario::BACKUP); auto backupInfos = BundleMgrAdapter::GetBundleInfos(bundleNames, session_->GetSessionUserId()); - session_->AppendBundles(bundleNames); + auto backupBundles = GetBackupBundleNames(backupInfos); + HandleExceptionOnAppendBundles(session_, bundleNames, backupBundles); + session_->AppendBundles(backupBundles); for (auto info : backupInfos) { session_->SetBundleDataSize(info.name, info.spaceOccupied); session_->SetBackupExtName(info.name, info.extensionName); @@ -592,7 +607,9 @@ ErrCode Service::AppendBundlesDetailsBackupSession(const vector &bun std::map> bundleNameDetailMap = BJsonUtil::BuildBundleInfos(bundleNames, bundleInfos, bundleNamesOnly, session_->GetSessionUserId()); auto backupInfos = BundleMgrAdapter::GetBundleInfos(bundleNames, session_->GetSessionUserId()); - session_->AppendBundles(bundleNames); + auto backupBundles = GetBackupBundleNames(backupInfos); + HandleExceptionOnAppendBundles(session_, bundleNames, backupBundles); + session_->AppendBundles(backupBundles); for (auto info : backupInfos) { session_->SetBundleDataSize(info.name, info.spaceOccupied); session_->SetBackupExtName(info.name, info.extensionName); diff --git a/services/backup_sa/src/module_ipc/service_incremental.cpp b/services/backup_sa/src/module_ipc/service_incremental.cpp index 07c6f6f87fca4c4faf3a9a15925f0158964ead74..5fbd939d697ddf805e7838e0900f44b6637957c1 100644 --- a/services/backup_sa/src/module_ipc/service_incremental.cpp +++ b/services/backup_sa/src/module_ipc/service_incremental.cpp @@ -239,6 +239,77 @@ ErrCode Service::InitIncrementalBackupSession(sptr remote) } } + +static vector GetBackupBundleNames(const vector &bundleInfos) +{ + vector backupBundleNames {}; + for (auto &bundleInfo : bundleInfos) { + backupBundleNames.emplace_back(bundleInfo.name); + } + return backupBundleNames; +} + +static vector GetBackupBundleNameWithIncData(const std::vector &bundlesToBackup) +{ + vector bundleNames {}; + for (auto &bundle : bundlesToBackup) { + bundleNames.emplace_back(bundle.bundleName); + } + return bundleNames; +} + +static void HandleExceptionOnAppendBundles(sptr session, + const vector &appendBundleNames, const vector &curBundleNames) +{ + if (appendBundleNames.size() != curBundleNames.size()) { + HILOGE("AppendBundleNames not equal curBundleNames."); + for (auto bundleName : appendBundleNames) { + auto it = find_if(curBundleNames.begin(), curBundleNames.end(), + [&bundleName](const auto &obj) { return obj == bundleName; }); + if (it == curBundleNames.end()) { + HILOGE("AppendBundles failed, bundleName = %{public}s.", bundleName.c_str()); + session->GetServiceReverseProxy()->IncrementalBackupOnBundleStarted( + BError(BError::Codes::SA_FORBID_BACKUP_RESTORE), bundleName); + } + } + } +} + +static void SetBundleDataForExt(sptr session, + const std::vector &bundlesToBackup, + std::map> bundleNameDetailMap, + vector bundleInfos, bool needUniCast) +{ + for (auto info : bundleInfos) { + session->SetBundleDataSize(info.name, info.spaceOccupied); + session->SetBackupExtName(info.name, info.extensionName); + if (info.allToBackup == false) { + session->GetServiceReverseProxy()->IncrementalBackupOnBundleStarted( + BError(BError::Codes::SA_FORBID_BACKUP_RESTORE), info.name); + session->RemoveExtInfo(info.name); + } + if (needUniCast) { + BJsonUtil::BundleDetailInfo uniCastInfo; + bool uniCastRet = BJsonUtil::FindBundleInfoByName(bundleNameDetailMap, info.name, UNICAST_TYPE, + uniCastInfo); + if (uniCastRet) { + HILOGI("Current bundle, unicast info:%{public}s", GetAnonyString(uniCastInfo.detail).c_str()); + session->SetBackupExtInfo(info.name, uniCastInfo.detail); + } + } + } + for (auto &bundleInfo : bundlesToBackup) { + auto it = find_if(bundleInfos.begin(), bundleInfos.end(), + [&bundleInfo](const auto &obj) { return obj.name == bundleInfo.bundleName; }); + if (it == bundleInfos.end()) { + HILOGI("Bundle does not need to be setIncrementalData, bundleName = %{public}s.", + bundleInfo.bundleName.c_str()); + continue; + } + session->SetIncrementalData(bundleInfo); + } +} + ErrCode Service::AppendBundlesIncrementalBackupSession(const std::vector &bundlesToBackup) { HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); @@ -249,34 +320,26 @@ ErrCode Service::AppendBundlesIncrementalBackupSession(const std::vectorIncreaseSessionCnt(); // BundleMgrAdapter::GetBundleInfos可能耗时 VerifyCaller(IServiceReverse::Scenario::BACKUP); - vector bundleNames {}; - for (auto &bundle : bundlesToBackup) { - bundleNames.emplace_back(bundle.bundleName); - } + auto bundleNames = GetBackupBundleNameWithIncData(bundlesToBackup); auto backupInfos = BundleMgrAdapter::GetBundleInfos(bundleNames, session_->GetSessionUserId()); - session_->AppendBundles(bundleNames); - for (auto info : backupInfos) { - session_->SetBundleDataSize(info.name, info.spaceOccupied); - session_->SetBackupExtName(info.name, info.extensionName); - if (info.allToBackup == false) { - session_->GetServiceReverseProxy()->IncrementalBackupOnBundleStarted( - BError(BError::Codes::SA_FORBID_BACKUP_RESTORE), info.name); - session_->RemoveExtInfo(info.name); - } - } - for (auto &bundleInfo : bundlesToBackup) { - session_->SetIncrementalData(bundleInfo); - } + auto backupBundles = GetBackupBundleNames(backupInfos); + HandleExceptionOnAppendBundles(session_, bundleNames, backupBundles); + session_->AppendBundles(backupBundles); + SetBundleDataForExt(session_, bundlesToBackup, {}, backupInfos, false); OnStartSched(); session_->DecreaseSessionCnt(); return BError(BError::Codes::OK); } catch (const BError &e) { - session_->DecreaseSessionCnt(); HILOGE("Failed, errCode = %{public}d", e.GetCode()); + auto bundleNames = GetBackupBundleNameWithIncData(bundlesToBackup); + HandleExceptionOnAppendBundles(session_, bundleNames, {}); + session_->DecreaseSessionCnt(); return e.GetCode(); } catch (...) { - session_->DecreaseSessionCnt(); HILOGI("Unexpected exception"); + auto bundleNames = GetBackupBundleNameWithIncData(bundlesToBackup); + HandleExceptionOnAppendBundles(session_, bundleNames, {}); + session_->DecreaseSessionCnt(); return EPERM; } } @@ -288,44 +351,29 @@ ErrCode Service::AppendBundlesIncrementalBackupSession(const std::vectorIncreaseSessionCnt(); // BundleMgrAdapter::GetBundleInfos可能耗时 VerifyCaller(IServiceReverse::Scenario::BACKUP); - vector bundleNames {}; - for (auto &bundle : bundlesToBackup) { - bundleNames.emplace_back(bundle.bundleName); - } + auto bundleNames = GetBackupBundleNameWithIncData(bundlesToBackup); std::vector bundleNamesOnly; std::map> bundleNameDetailMap = BJsonUtil::BuildBundleInfos(bundleNames, infos, bundleNamesOnly, session_->GetSessionUserId()); auto backupInfos = BundleMgrAdapter::GetBundleInfos(bundleNames, session_->GetSessionUserId()); - session_->AppendBundles(bundleNames); - for (auto info : backupInfos) { - session_->SetBundleDataSize(info.name, info.spaceOccupied); - session_->SetBackupExtName(info.name, info.extensionName); - if (info.allToBackup == false) { - session_->GetServiceReverseProxy()->IncrementalBackupOnBundleStarted( - BError(BError::Codes::SA_FORBID_BACKUP_RESTORE), info.name); - session_->RemoveExtInfo(info.name); - } - BJsonUtil::BundleDetailInfo uniCastInfo; - bool uniCastRet = BJsonUtil::FindBundleInfoByName(bundleNameDetailMap, info.name, UNICAST_TYPE, - uniCastInfo); - if (uniCastRet) { - HILOGI("current bundle, unicast info:%{public}s", GetAnonyString(uniCastInfo.detail).c_str()); - session_->SetBackupExtInfo(info.name, uniCastInfo.detail); - } - } - for (auto &bundleInfo : bundlesToBackup) { - session_->SetIncrementalData(bundleInfo); - } + auto backupBundles = GetBackupBundleNames(backupInfos); + HandleExceptionOnAppendBundles(session_, bundleNames, backupBundles); + session_->AppendBundles(backupBundles); + SetBundleDataForExt(session_, bundlesToBackup, bundleNameDetailMap, backupInfos, true); OnStartSched(); session_->DecreaseSessionCnt(); return BError(BError::Codes::OK); } catch (const BError &e) { - session_->DecreaseSessionCnt(); HILOGE("Failed, errCode = %{public}d", e.GetCode()); + auto bundleNames = GetBackupBundleNameWithIncData(bundlesToBackup); + HandleExceptionOnAppendBundles(session_, bundleNames, {}); + session_->DecreaseSessionCnt(); return e.GetCode(); } catch (...) { - session_->DecreaseSessionCnt(); HILOGI("Unexpected exception"); + auto bundleNames = GetBackupBundleNameWithIncData(bundlesToBackup); + HandleExceptionOnAppendBundles(session_, bundleNames, {}); + session_->DecreaseSessionCnt(); return EPERM; } }