diff --git a/frameworks/native/backup_ext/include/ext_backup.h b/frameworks/native/backup_ext/include/ext_backup.h index 441b94c694547ec05924fa07c7699940b5489fb3..a7a015f3ab89ab8859ff7198977851ad4533b238 100644 --- a/frameworks/native/backup_ext/include/ext_backup.h +++ b/frameworks/native/backup_ext/include/ext_backup.h @@ -106,7 +106,7 @@ public: * * @return allowed ro not */ - virtual bool AllowToBackupRestore() const; + virtual bool AllowToBackupRestore(); /** * @brief Get whether FullBackupOnly or not diff --git a/frameworks/native/backup_ext/src/ext_backup.cpp b/frameworks/native/backup_ext/src/ext_backup.cpp index 7220ffdf47e2cd015c08929beb023f499828b45b..b08d04cecb99286862b260a5f6811f53be493b7c 100644 --- a/frameworks/native/backup_ext/src/ext_backup.cpp +++ b/frameworks/native/backup_ext/src/ext_backup.cpp @@ -103,12 +103,12 @@ string ExtBackup::GetUsrConfig() const return config.empty() ? "" : config[0]; } -bool ExtBackup::AllowToBackupRestore() const +bool ExtBackup::AllowToBackupRestore() { string usrConfig = GetUsrConfig(); BJsonCachedEntity cachedEntity(usrConfig); auto cache = cachedEntity.Structuralize(); - if (cache.GetAllowToBackupRestore()) { + if (cache.GetAllowToBackupRestore() || WasFromSpeicalVersion() || SpeicalVersionForCloneAndCloud()) { return true; } return false; @@ -172,13 +172,13 @@ sptr ExtBackup::OnConnect(const AAFwk::Want &want) HILOGE("Verification action failed."); return nullptr; } + extAction_ = extAction; + GetParament(want); // 应用必须配置支持备份恢复 if (!AllowToBackupRestore()) { HILOGE("The application does not allow to backup and restore."); return nullptr; } - extAction_ = extAction; - GetParament(want); Extension::OnConnect(want); diff --git a/frameworks/native/backup_ext/src/ext_extension.cpp b/frameworks/native/backup_ext/src/ext_extension.cpp index 65b593f48c035d496b4df5d6b0b35304cf715b74..646d0266a60d8a09ae4485b8727861864a7b6784 100644 --- a/frameworks/native/backup_ext/src/ext_extension.cpp +++ b/frameworks/native/backup_ext/src/ext_extension.cpp @@ -724,10 +724,7 @@ ErrCode BackupExtExtension::HandleRestore() throw BError(BError::Codes::EXT_INVAL_ARG, "Action is invalid"); } // read backup_config is allow to backup or restore - string usrConfig = extension_->GetUsrConfig(); - BJsonCachedEntity cachedEntity(usrConfig); - auto cache = cachedEntity.Structuralize(); - if (!cache.GetAllowToBackupRestore()) { + if (!extension_->AllowToBackupRestore()) { HILOGI("Application does not allow backup or restore"); return BError(BError::Codes::EXT_INVAL_ARG, "Application does not allow backup or restore").GetCode(); } diff --git a/services/backup_sa/src/module_ipc/service.cpp b/services/backup_sa/src/module_ipc/service.cpp index 93d5c85dcd5a68f5edade60424099b291d065863..2c60852b5f73d85af64cfcd0337ee00375da7a63 100644 --- a/services/backup_sa/src/module_ipc/service.cpp +++ b/services/backup_sa/src/module_ipc/service.cpp @@ -234,21 +234,40 @@ ErrCode Service::Start() return BError(BError::Codes::OK); } -static bool GetRestoreBundleNames(const vector &bundleInfos, - vector &restoreBundleNames, - RestoreTypeEnum restoreType, - const vector &restoreInfos) +static bool SpeicalVersion(const string &versionName, uint32_t versionCode) { + auto iter = find_if(BConstants::DEFAULT_VERSION_NAMES_VEC.begin(), BConstants::DEFAULT_VERSION_NAMES_VEC.end(), + [&versionName](const auto &version) { return version == versionName; }); + if (versionCode == BConstants::DEFAULT_VERSION_CODE && iter != BConstants::DEFAULT_VERSION_NAMES_VEC.end()) { + return true; + } + return false; +} + +static vector GetRestoreBundleNames(UniqueFd fd, + sptr session, + const vector &bundleNames) +{ + // BundleMgrAdapter::GetBundleInfos可能耗时 + auto restoreInfos = BundleMgrAdapter::GetBundleInfos(bundleNames, session->GetSessionUserId()); + BJsonCachedEntity cachedEntity(move(fd)); + auto cache = cachedEntity.Structuralize(); + auto bundleInfos = cache.GetBundleInfos(); + if (!bundleInfos.size()) { + throw BError(BError::Codes::SA_INVAL_ARG, "Json entity caps is empty"); + } vector restoreBundleInfos {}; for (auto &restoreInfo : restoreInfos) { auto it = find_if(bundleInfos.begin(), bundleInfos.end(), [&restoreInfo](auto &obj) { return obj.name == restoreInfo.name; }); if (it == bundleInfos.end()) { - HILOGE("Can't find %{public}s, append bundles error", restoreInfo.name.data()); - return false; + session->GetServiceReverseProxy()->RestoreOnBundleStarted(BError(BError::Codes::SA_INVAL_ARG), + restoreInfo.name); + continue; } BJsonEntityCaps::BundleInfo info = {.name = (*it).name, .versionCode = (*it).versionCode, + .versionName = (*it).versionName, .spaceOccupied = (*it).spaceOccupied, .allToBackup = (*it).allToBackup, .extensionName = restoreInfo.extensionName, @@ -256,8 +275,7 @@ static bool GetRestoreBundleNames(const vector &bun .restoreDeps = (*it).restoreDeps}; restoreBundleInfos.emplace_back(info); } - restoreBundleNames = SvcRestoreDepsManager::GetInstance().GetRestoreBundleNames(restoreBundleInfos, restoreType); - return true; + return restoreBundleInfos; } ErrCode Service::AppendBundlesRestoreSession(UniqueFd fd, @@ -265,52 +283,52 @@ ErrCode Service::AppendBundlesRestoreSession(UniqueFd fd, RestoreTypeEnum restoreType, int32_t userId) { - HILOGI("Begin"); - session_->SetIsBusy(true); // BundleMgrAdapter::GetBundleInfos可能耗时 - if (userId != DEFAULT_INVAL_VALUE) { /* multi user scenario */ - session_->SetSessionUserId(userId); - } - VerifyCaller(IServiceReverse::Scenario::RESTORE); - auto restoreInfos = BundleMgrAdapter::GetBundleInfos(bundleNames, session_->GetSessionUserId()); - BJsonCachedEntity cachedEntity(move(fd)); - auto cache = cachedEntity.Structuralize(); - auto bundleInfos = cache.GetBundleInfos(); - if (!bundleInfos.size()) { - throw BError(BError::Codes::SA_INVAL_ARG, "Json entity caps is empty"); - } - vector restoreBundleNames {}; - if (!GetRestoreBundleNames(bundleInfos, restoreBundleNames, restoreType, restoreInfos)) { - return BError(BError::Codes::SA_INVAL_ARG); - } - if (restoreBundleNames.empty()) { - return BError(BError::Codes::OK); - } - session_->AppendBundles(restoreBundleNames); - for (auto bundleName : restoreBundleNames) { - HILOGD("bundleName: %{public}s", bundleName.c_str()); - for (auto &&bundleInfo : bundleInfos) { - if (bundleInfo.name != bundleName) { + try { + HILOGI("Begin"); + session_->SetIsBusy(true); + if (userId != DEFAULT_INVAL_VALUE) { /* multi user scenario */ + session_->SetSessionUserId(userId); + } + VerifyCaller(IServiceReverse::Scenario::RESTORE); + auto restoreInfos = GetRestoreBundleNames(move(fd), session_, bundleNames); + auto restoreBundleNames = SvcRestoreDepsManager::GetInstance().GetRestoreBundleNames(restoreInfos, restoreType); + if (restoreBundleNames.empty()) { + session_->SetIsBusy(false); + return BError(BError::Codes::OK); + } + session_->AppendBundles(restoreBundleNames); + for (auto restoreInfo : restoreInfos) { + auto it = find_if(restoreBundleNames.begin(), restoreBundleNames.end(), + [&restoreInfo](auto &bundleName) { return bundleName == restoreInfo.name; }); + if (it == restoreBundleNames.end()) { + throw BError(BError::Codes::SA_INVAL_ARG, "Can't find bundle name"); + } + HILOGD("bundleName: %{public}s, extensionName: %{public}s", restoreInfo.name.c_str(), + restoreInfo.extensionName.c_str()); + if (restoreInfo.allToBackup == false && !SpeicalVersion(restoreInfo.versionName, restoreInfo.versionCode)) { + session_->GetServiceReverseProxy()->RestoreOnBundleStarted(BError(BError::Codes::SA_REFUSED_ACT), + restoreInfo.name); + session_->RemoveExtInfo(restoreInfo.name); continue; } - session_->SetNeedToInstall(bundleInfo.name, bundleInfo.needToInstall); - session_->SetBundleRestoreType(bundleInfo.name, restoreType); - session_->SetBundleVersionCode(bundleInfo.name, bundleInfo.versionCode); - session_->SetBundleVersionName(bundleInfo.name, bundleInfo.versionName); - session_->SetBundleDataSize(bundleInfo.name, bundleInfo.spaceOccupied); - } - } - for (auto info : restoreInfos) { - session_->SetBackupExtName(info.name, info.extensionName); - HILOGD("bundleName: %{public}s, extensionName: %{public}s", info.name.c_str(), info.extensionName.c_str()); - if (info.allToBackup == false) { - session_->GetServiceReverseProxy()->RestoreOnBundleStarted(BError(BError::Codes::SA_REFUSED_ACT), - info.name); - session_->RemoveExtInfo(info.name); + session_->SetNeedToInstall(restoreInfo.name, restoreInfo.needToInstall); + session_->SetBundleRestoreType(restoreInfo.name, restoreType); + session_->SetBundleVersionCode(restoreInfo.name, restoreInfo.versionCode); + session_->SetBundleVersionName(restoreInfo.name, restoreInfo.versionName); + session_->SetBundleDataSize(restoreInfo.name, restoreInfo.spaceOccupied); + session_->SetBackupExtName(restoreInfo.name, restoreInfo.extensionName); } + OnStartSched(); + session_->SetIsBusy(false); + return BError(BError::Codes::OK); + } catch (const BError &e) { + session_->SetIsBusy(false); + return e.GetCode(); + } catch (...) { + session_->SetIsBusy(false); + HILOGI("Unexpected exception"); + return EPERM; } - OnStartSched(); - session_->SetIsBusy(false); - return BError(BError::Codes::OK); } ErrCode Service::AppendBundlesBackupSession(const vector &bundleNames) diff --git a/utils/include/b_resources/b_constants.h b/utils/include/b_resources/b_constants.h index 183f69098534381330fd8717a0de54754928868d..8d486942eb3b5e313e84143dae631a5ca3d01422 100644 --- a/utils/include/b_resources/b_constants.h +++ b/utils/include/b_resources/b_constants.h @@ -129,6 +129,7 @@ static inline std::string_view DEFAULT_VERSION_NAME_CLONE_3 = "99.99.99.997"; static inline std::string_view DEFAULT_VERSION_NAME_PC = "99.99.99.996"; static inline std::string_view DEFAULT_VERSION_NAME_CLOUD = "99.99.99.995"; static inline std::vector DEFAULT_VERSION_NAMES_VEC = { + DEFAULT_VERSION_NAME, DEFAULT_VERSION_NAME_CLONE, DEFAULT_VERSION_NAME_CLONE_2, DEFAULT_VERSION_NAME_CLONE_3,