From 099e50f4f55920e4bf1b03b059fa20e8e987c37f Mon Sep 17 00:00:00 2001 From: dinglei Date: Fri, 14 Jul 2023 02:14:15 +0000 Subject: [PATCH] =?UTF-8?q?=E5=A4=87=E4=BB=BD=E6=81=A2=E5=A4=8D=E6=A1=86?= =?UTF-8?q?=E6=9E=B6=E6=94=AF=E6=8C=81=E5=A4=9A=E7=94=A8=E6=88=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/remote_file_share.cpp | 6 +- .../include/module_external/bms_adapter.h | 10 ++- .../module_ipc/svc_backup_connection.h | 2 +- .../include/module_ipc/svc_session_manager.h | 74 ++++++++++++++++++ .../src/module_external/bms_adapter.cpp | 14 ++-- services/backup_sa/src/module_ipc/service.cpp | 46 ++++++++--- .../src/module_ipc/svc_backup_connection.cpp | 4 +- .../src/module_ipc/svc_session_manager.cpp | 78 ++++++++++++++++++- .../src/module_sched/sched_scheduler.cpp | 6 +- .../mock/module_external/bms_adapter_mock.cpp | 6 +- .../module_ipc/svc_backup_connection_mock.cpp | 2 +- .../module_ipc/svc_session_manager_mock.cpp | 65 ++++++++++++++++ .../module_ipc/svc_backup_connection_test.cpp | 3 +- .../backup_tool/tools_op_backup_test.cpp | 2 +- .../backup_tool/tools_op_restore_test.cpp | 4 +- .../b_json_entity_extension_config_test.cpp | 2 +- tools/backup_tool/src/tools_op_backup.cpp | 7 +- utils/include/b_resources/b_constants.h | 37 ++++++++- .../b_json/b_json_entity_extension_config.cpp | 3 +- 19 files changed, 321 insertions(+), 50 deletions(-) diff --git a/interfaces/innerkits/native/remote_file_share/src/remote_file_share.cpp b/interfaces/innerkits/native/remote_file_share/src/remote_file_share.cpp index bfcc1446b..4d272b21a 100644 --- a/interfaces/innerkits/native/remote_file_share/src/remote_file_share.cpp +++ b/interfaces/innerkits/native/remote_file_share/src/remote_file_share.cpp @@ -320,10 +320,8 @@ int32_t RemoteFileShare::GetDfsUriFromLocal(const std::string &uriStr, const int } struct HmdfsDstInfo hdi; - std::string bundleName = uri.GetAuthority(); - LOGD("PhysicalPath: %{public}s DistributedPath: %{public}s BundleName: %{public}s", - physicalPath.c_str(), distributedPath.c_str(), bundleName.c_str()); - InitHmdfsInfo(hdi, physicalPath, distributedPath, bundleName); + LOGD("PhysicalPath: %{public}s DistributedPath: %{public}s", physicalPath.c_str(), distributedPath.c_str()); + InitHmdfsInfo(hdi, physicalPath, distributedPath, uri.GetAuthority()); std::string ioctlDir = SHAER_PATH_HEAD + std::to_string(userId) + SHAER_PATH_MID; int32_t dirFd = open(ioctlDir.c_str(), O_RDONLY); diff --git a/services/backup_sa/include/module_external/bms_adapter.h b/services/backup_sa/include/module_external/bms_adapter.h index 3671396d9..49b69ac2d 100644 --- a/services/backup_sa/include/module_external/bms_adapter.h +++ b/services/backup_sa/include/module_external/bms_adapter.h @@ -30,25 +30,29 @@ public: /** * @brief Get the Bundle Infos object * + * @param userId User ID * @return std::vector */ - static std::vector GetBundleInfos(); + static std::vector GetBundleInfos(int32_t userId); /** * @brief Get the bundle infos object * * @param bundleNames bundle names + * @param userId User ID * @return std::vector */ - static std::vector GetBundleInfos(const std::vector &bundleNames); + static std::vector GetBundleInfos( + const std::vector&bundleNames, int32_t userId); /** * @brief Install bundle * * @param bundleName bundle name * @param bundleFilePath bundle file path + * @param userId User ID */ - static ErrCode Install(wptr statusReceiver, const std::string &bundleFilePath); + static ErrCode Install(wptr statusReceiver, const std::string &bundleFilePath, int32_t userId); }; } // namespace OHOS::FileManagement::Backup #endif // OHOS_FILEMGMT_BACKUP_BUNDLE_MGR_ADAPTER_H \ No newline at end of file diff --git a/services/backup_sa/include/module_ipc/svc_backup_connection.h b/services/backup_sa/include/module_ipc/svc_backup_connection.h index 91d46b562..142738561 100644 --- a/services/backup_sa/include/module_ipc/svc_backup_connection.h +++ b/services/backup_sa/include/module_ipc/svc_backup_connection.h @@ -49,7 +49,7 @@ public: /** * @brief connect remote ability of ExtBackup. */ - ErrCode ConnectBackupExtAbility(AAFwk::Want &want); + ErrCode ConnectBackupExtAbility(AAFwk::Want &want, int32_t userId); /** * @brief disconnect remote ability of ExtBackup. diff --git a/services/backup_sa/include/module_ipc/svc_session_manager.h b/services/backup_sa/include/module_ipc/svc_session_manager.h index 1c0dc1d6c..b7a91090d 100644 --- a/services/backup_sa/include/module_ipc/svc_session_manager.h +++ b/services/backup_sa/include/module_ipc/svc_session_manager.h @@ -32,6 +32,7 @@ #include "b_file_info.h" #include "b_resources/b_constants.h" #include "i_service_reverse.h" +#include "i_service.h" #include "module_ipc/svc_backup_connection.h" #include "svc_death_recipient.h" @@ -46,6 +47,12 @@ struct BackupExtInfo { BConstants::ServiceSchedAction schedAction {BConstants::ServiceSchedAction::WAIT}; bool bNeedToInstall {false}; std::string installState; + /* [RESTORE] Record whether data backup is required during the app exec restore proceess. */ + RestoreTpyeEnum restoreType; + /* Clone App: old device app versionCode */ + uint32_t versionCode; + /* Clone App: old device app versionCode */ + std::string versionName; }; class Service; @@ -58,6 +65,11 @@ public: sptr clientProxy; bool isBackupStart {false}; bool isAppendFinish {false}; + /* Note: Multi user scenario: + Caller must complete all processes before next user tigger. + [RESTORE] Support for multiple users, incoming during restore process. + */ + int32_t userId; }; public: @@ -110,6 +122,20 @@ public: */ IServiceReverse::Scenario GetScenario(); + /** + * @brief 获取当前处理事务会话对应的userId + * + * @return int32_t + */ + int32_t GetSessionUserId(); + + /** + * @brief 设置当前处理事务会话对应的userId + * + * @param userid 相关会话对应的userId + */ + void SetSessionUserId(int32_t userId); + /** * @brief 更新backupExtNameMap并判断是否完成分发 * @@ -280,6 +306,54 @@ public: */ bool NeedToUnloadService(); +/** + * @brief Set the bundle restore type object + * + * @param bundleName + * @param restoreType + */ + void SetBundleRestoreType(const std::string &bundleName, RestoreTpyeEnum restoreType); + + /** + * @brief Get the bundle restore type object + * + * @param bundleName + * @return restoreType + */ + RestoreTpyeEnum GetBundleRestoreType(const std::string &bundleName); + + /** + * @brief Set the bundle version code object + * + * @param bundleName + * @param versionCode + */ + void SetBundleVersionCode(const std::string &bundleName, uint32_t versionCode); + + /** + * @brief Get the bundle version code object + * + * @param bundleName + * @return versionCode + */ + uint32_t GetBundleVersionCode(const std::string &bundleName); + + /** + * @brief Set the bundle version name object + * + * @param bundleName + * @param versionName + */ + void SetBundleVersionName(const std::string &bundleName, std::string versionName); + + /** + * @brief Get the bundle version name object + * + * @param bundleName + * @return versionName + */ + std::string GetBundleVersionName(const std::string &bundleName); + private: /** * @brief 获取backup extension ability diff --git a/services/backup_sa/src/module_external/bms_adapter.cpp b/services/backup_sa/src/module_external/bms_adapter.cpp index 2f57d4141..f98be9b46 100644 --- a/services/backup_sa/src/module_external/bms_adapter.cpp +++ b/services/backup_sa/src/module_external/bms_adapter.cpp @@ -69,13 +69,12 @@ static tuple GetAllowAndExtName(const vector BundleMgrAdapter::GetBundleInfos() +vector BundleMgrAdapter::GetBundleInfos(int32_t userId) { vector bundleInfos; vector installedBundles; auto bms = GetBundleManager(); - if (!bms->GetBundleInfos(AppExecFwk::GET_BUNDLE_WITH_EXTENSION_INFO, installedBundles, - AppExecFwk::Constants::START_USERID)) { + if (!bms->GetBundleInfos(AppExecFwk::GET_BUNDLE_WITH_EXTENSION_INFO, installedBundles, userId)) { throw BError(BError::Codes::SA_BROKEN_IPC, "Failed to get bundle infos"); } for (auto const &installedBundle : installedBundles) { @@ -88,14 +87,13 @@ vector BundleMgrAdapter::GetBundleInfos() return bundleInfos; } -vector BundleMgrAdapter::GetBundleInfos(const vector &bundleNames) +vector BundleMgrAdapter::GetBundleInfos(const vector &bundleNames, int32_t userId) { vector bundleInfos; auto bms = GetBundleManager(); for (auto const &bundleName : bundleNames) { AppExecFwk::BundleInfo installedBundle; - if (!bms->GetBundleInfo(bundleName, AppExecFwk::GET_BUNDLE_WITH_EXTENSION_INFO, installedBundle, - AppExecFwk::Constants::START_USERID)) { + if (!bms->GetBundleInfo(bundleName, AppExecFwk::GET_BUNDLE_WITH_EXTENSION_INFO, installedBundle, userId)) { throw BError(BError::Codes::SA_BROKEN_IPC, "Failed to get bundle info"); } auto [allToBackup, extName] = GetAllowAndExtName(installedBundle.extensionInfos); @@ -107,7 +105,7 @@ vector BundleMgrAdapter::GetBundleInfos(const vecto return bundleInfos; } -ErrCode BundleMgrAdapter::Install(wptr statusReceiver, const string &bundleFilePath) +ErrCode BundleMgrAdapter::Install(wptr statusReceiver, const string &bundleFilePath, int32_t userId) { HILOGI("Begin"); auto bms = GetBundleManager(); @@ -131,7 +129,7 @@ ErrCode BundleMgrAdapter::Install(wptr statusReceiver, const AppExecFwk::InstallParam installParam; installParam.installFlag = AppExecFwk::InstallFlag::REPLACE_EXISTING; - installParam.userId = AppExecFwk::Constants::START_USERID; + installParam.userId = userId; return install->StreamInstall({bundleFilePath}, installParam, receiver); } } // namespace OHOS::FileManagement::Backup diff --git a/services/backup_sa/src/module_ipc/service.cpp b/services/backup_sa/src/module_ipc/service.cpp index 9d1097f7f..f63595c5d 100644 --- a/services/backup_sa/src/module_ipc/service.cpp +++ b/services/backup_sa/src/module_ipc/service.cpp @@ -56,6 +56,10 @@ using namespace std; REGISTER_SYSTEM_ABILITY_BY_ID(Service, FILEMANAGEMENT_BACKUP_SERVICE_SA_ID, false); +/* Shell/Xts user id equal to 0/1, we need set default 100 */ +#define INIT_USERID(userId) (((userId == BConstants::SYSTEM_UID) || \ + (userId == BConstants::XTS_UID)) ? (BConstants::DEFAULT_USER_ID) : (userId)) + void Service::OnStart() { bool res = SystemAbility::Publish(sptr(this)); @@ -75,14 +79,21 @@ UniqueFd Service::GetLocalCapabilities() { try { HILOGI("Begin"); + /* + Only called by restore app before InitBackupSession, + so there must be set init userId. + */ + session_->SetSessionUserId(INIT_USERID(IPCSkeleton::GetCallingUid())); VerifyCaller(); BJsonCachedEntity cachedEntity( - UniqueFd(open(BConstants::SA_BUNDLE_BACKUP_ROOT_DIR.data(), O_TMPFILE | O_RDWR, 0600))); + UniqueFd(open(BConstants::GetSaBundleBackupRootDir( + session_->GetSessionUserId()).data(), O_TMPFILE | O_RDWR, 0600))); + auto cache = cachedEntity.Structuralize(); cache.SetSystemFullName(GetOSFullName()); cache.SetDeviceType(GetDeviceType()); - auto bundleInfos = BundleMgrAdapter::GetBundleInfos(); + auto bundleInfos = BundleMgrAdapter::GetBundleInfos(session_->GetSessionUserId()); cache.SetBundleInfos(bundleInfos); cachedEntity.Persist(); @@ -124,12 +135,8 @@ void Service::VerifyCaller() uint32_t tokenCaller = IPCSkeleton::GetCallingTokenID(); int tokenType = Security::AccessToken::AccessTokenKit::GetTokenType(tokenCaller); switch (tokenType) { + case Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE: /* Update Service */ case Security::AccessToken::ATokenTypeEnum::TOKEN_HAP: { - auto multiuser = BMultiuser::ParseUid(IPCSkeleton::GetCallingUid()); - if ((multiuser.userId != BConstants::DEFAULT_USER_ID) && (multiuser.userId != BConstants::XTS_UID)) { - throw BError(BError::Codes::SA_INVAL_ARG, string("Calling user is ").append( - to_string(multiuser.userId)).append(", which is currently not supported")); - } const string permission = "ohos.permission.BACKUP"; if (Security::AccessToken::AccessTokenKit::VerifyAccessToken(tokenCaller, permission) == Security::AccessToken::TypePermissionState::PERMISSION_DENIED) { @@ -163,6 +170,7 @@ ErrCode Service::InitRestoreSession(sptr remote) .clientToken = IPCSkeleton::GetCallingTokenID(), .scenario = IServiceReverse::Scenario::RESTORE, .clientProxy = remote, + .userId = INIT_USERID(IPCSkeleton::GetCallingUid()), }); return BError(BError::Codes::OK); } catch (const BError &e) { @@ -185,6 +193,7 @@ ErrCode Service::InitBackupSession(sptr remote) .clientToken = IPCSkeleton::GetCallingTokenID(), .scenario = IServiceReverse::Scenario::BACKUP, .clientProxy = remote, + .userId = INIT_USERID(IPCSkeleton::GetCallingUid()), }); return BError(BError::Codes::OK); } catch (const BError &e) { @@ -208,7 +217,10 @@ ErrCode Service::AppendBundlesRestoreSession(UniqueFd fd, int32_t userId) { HILOGI("Begin"); - VerifyCaller(session_->GetScenario()); + if (userId != DEFAULT_INVAL_VALUE) { /* multi user scenario */ + session_->SetSessionUserId(userId); + } + VerifyCaller(IServiceReverse::Scenario::RESTORE); BJsonCachedEntity cachedEntity(move(fd)); auto cache = cachedEntity.Structuralize(); auto bundleInfos = cache.GetBundleInfos(); @@ -222,6 +234,9 @@ ErrCode Service::AppendBundlesRestoreSession(UniqueFd fd, continue; } session_->SetNeedToInstall(bundleInfo.name, bundleInfo.needToInstall); + session_->SetBundleRestoreType(bundleInfo.name, restoreType); + session_->SetBundleVersionCode(bundleInfo.name, bundleInfo.versionCode); + session_->SetBundleVersionName(bundleInfo.name, bundleInfo.versionName); } } Start(); @@ -233,7 +248,7 @@ ErrCode Service::AppendBundlesRestoreSession(UniqueFd fd, ErrCode Service::AppendBundlesBackupSession(const vector &bundleNames) { HILOGI("Begin"); - VerifyCaller(session_->GetScenario()); + VerifyCaller(IServiceReverse::Scenario::BACKUP); session_->AppendBundles(bundleNames); Start(); Finish(); @@ -378,12 +393,21 @@ ErrCode Service::LaunchBackupExtension(const BundleName &bundleName) } AAFwk::Want want; - string backupExtName = session_->GetBackupExtName(bundleName); + string backupExtName = session_->GetBackupExtName(bundleName); /* new device app ext name */ + string versionName = session_->GetBundleVersionName(bundleName); /* old device app version name */ + uint32_t versionCode = session_->GetBundleVersionCode(bundleName); /* old device app version code */ + int32_t userId = session_->GetSessionUserId(); /* current session user id */ + RestoreTpyeEnum restoreType = session_->GetBundleRestoreType(bundleName); /* app restore type */ + want.SetElementName(bundleName, backupExtName); want.SetParam(BConstants::EXTENSION_ACTION_PARA, static_cast(action)); + want.SetParam(BConstants::VERSION_CODE_PARA, static_cast(versionCode)); + want.SetParam(BConstants::RESTORE_TYPE_PARA, static_cast(restoreType)); + want.SetParam(BConstants::USER_ID_PARA, static_cast(userId)); + want.SetParam(BConstants::VERSION_NAME_PARA, versionName); auto backUpConnection = session_->GetExtConnection(bundleName); - ErrCode ret = backUpConnection->ConnectBackupExtAbility(want); + ErrCode ret = backUpConnection->ConnectBackupExtAbility(want, session_->GetSessionUserId()); return ret; } catch (const BError &e) { return e.GetCode(); diff --git a/services/backup_sa/src/module_ipc/svc_backup_connection.cpp b/services/backup_sa/src/module_ipc/svc_backup_connection.cpp index 18265e6ac..4da0013a6 100644 --- a/services/backup_sa/src/module_ipc/svc_backup_connection.cpp +++ b/services/backup_sa/src/module_ipc/svc_backup_connection.cpp @@ -58,12 +58,12 @@ void SvcBackupConnection::OnAbilityDisconnectDone(const AppExecFwk::ElementName HILOGI("called end"); } -ErrCode SvcBackupConnection::ConnectBackupExtAbility(AAFwk::Want &want) +ErrCode SvcBackupConnection::ConnectBackupExtAbility(AAFwk::Want &want, int32_t userId) { HILOGI("called begin"); std::unique_lock lock(mutex_); ErrCode ret = - AAFwk::AbilityManagerClient::GetInstance()->ConnectAbility(want, this, AppExecFwk::Constants::START_USERID); + AAFwk::AbilityManagerClient::GetInstance()->ConnectAbility(want, this, userId); HILOGI("called end, ret=%{public}d", ret); return ret; } diff --git a/services/backup_sa/src/module_ipc/svc_session_manager.cpp b/services/backup_sa/src/module_ipc/svc_session_manager.cpp index 030e0ddff..743605417 100644 --- a/services/backup_sa/src/module_ipc/svc_session_manager.cpp +++ b/services/backup_sa/src/module_ipc/svc_session_manager.cpp @@ -119,6 +119,16 @@ IServiceReverse::Scenario SvcSessionManager::GetScenario() return impl_.scenario; } +int32_t SvcSessionManager::GetSessionUserId() +{ + return impl_.userId; +} + +void SvcSessionManager::SetSessionUserId(int32_t userId) +{ + impl_.userId = userId; +} + bool SvcSessionManager::OnBunleFileReady(const string &bundleName, const string &fileName) { unique_lock lock(lock_); @@ -356,7 +366,7 @@ string SvcSessionManager::GetBackupExtName(const string &bundleName) } string name = bundleName; VerifyBundleName(name); - auto bundleInfos = BundleMgrAdapter::GetBundleInfos({bundleName}); + auto bundleInfos = BundleMgrAdapter::GetBundleInfos({bundleName}, impl_.userId); for (auto &&bundleInfo : bundleInfos) { if (!bundleInfo.allToBackup || bundleInfo.extensionName.empty()) { throw BError(BError::Codes::SA_INVAL_ARG, @@ -487,4 +497,70 @@ bool SvcSessionManager::NeedToUnloadService() } return false; } + +void SvcSessionManager::SetBundleRestoreType(const std::string &bundleName, RestoreTpyeEnum restoreType) +{ + unique_lock lock(lock_); + if (!impl_.clientToken) { + throw BError(BError::Codes::SA_INVAL_ARG, "No caller token was specified"); + } + + auto it = GetBackupExtNameMap(bundleName); + it->second.restoreType = restoreType; +} + +RestoreTpyeEnum SvcSessionManager::GetBundleRestoreType(const std::string &bundleName) +{ + unique_lock lock(lock_); + if (!impl_.clientToken) { + throw BError(BError::Codes::SA_INVAL_ARG, "No caller token was specified"); + } + + auto it = GetBackupExtNameMap(bundleName); + return it->second.restoreType; +} + +void SvcSessionManager::SetBundleVersionCode(const std::string &bundleName, uint32_t versionCode) +{ + unique_lock lock(lock_); + if (!impl_.clientToken) { + throw BError(BError::Codes::SA_INVAL_ARG, "No caller token was specified"); + } + + auto it = GetBackupExtNameMap(bundleName); + it->second.versionCode = versionCode; +} + +uint32_t SvcSessionManager::GetBundleVersionCode(const std::string &bundleName) +{ + unique_lock lock(lock_); + if (!impl_.clientToken) { + throw BError(BError::Codes::SA_INVAL_ARG, "No caller token was specified"); + } + + auto it = GetBackupExtNameMap(bundleName); + return it->second.versionCode; +} + +void SvcSessionManager::SetBundleVersionName(const std::string &bundleName, std::string versionName) +{ + unique_lock lock(lock_); + if (!impl_.clientToken) { + throw BError(BError::Codes::SA_INVAL_ARG, "No caller token was specified"); + } + + auto it = GetBackupExtNameMap(bundleName); + it->second.versionName = versionName; +} + +std::string SvcSessionManager::GetBundleVersionName(const std::string &bundleName) +{ + unique_lock lock(lock_); + if (!impl_.clientToken) { + throw BError(BError::Codes::SA_INVAL_ARG, "No caller token was specified"); + } + + auto it = GetBackupExtNameMap(bundleName); + return it->second.versionName; +} } // namespace OHOS::FileManagement::Backup \ No newline at end of file diff --git a/services/backup_sa/src/module_sched/sched_scheduler.cpp b/services/backup_sa/src/module_sched/sched_scheduler.cpp index 89e3d427b..696d3539e 100644 --- a/services/backup_sa/src/module_sched/sched_scheduler.cpp +++ b/services/backup_sa/src/module_sched/sched_scheduler.cpp @@ -139,7 +139,7 @@ void SchedScheduler::InstallingState(const string &bundleName) return; } string state = sessionPtr_->GetInstallState(bundleName); - string path = string(BConstants::SA_BUNDLE_BACKUP_ROOT_DIR).append(bundleName); + string path = BConstants::GetSaBundleBackupRootDir(sessionPtr_->GetSessionUserId()).append(bundleName); string filePath = path + "/bundle.hap"; if (state == BConstants::RESTORE_INSTALL_PATH) { @@ -159,7 +159,7 @@ void SchedScheduler::InstallingState(const string &bundleName) throw BError(BError::Codes::SA_INVAL_ARG, string("File already exists")); } sptr statusReceiver = sptr(new InnerReceiverImpl(bundleName, wptr(this))); - ErrCode err = BundleMgrAdapter::Install(statusReceiver, filePath); + ErrCode err = BundleMgrAdapter::Install(statusReceiver, filePath, sessionPtr_->GetSessionUserId()); if (err != ERR_OK) { InstallSuccess(bundleName, err); } @@ -219,7 +219,7 @@ void SchedScheduler::InstallSuccess(const std::string &bundleName, const int32_t sessionPtr_->GetServiceReverseProxy()->RestoreOnBundleStarted(resultCode, bundleName); sessionPtr_->RemoveExtInfo(bundleName); } - string path = string(BConstants::SA_BUNDLE_BACKUP_ROOT_DIR).append(bundleName); + string path = BConstants::GetSaBundleBackupRootDir(sessionPtr_->GetSessionUserId()).append(bundleName); if (!ForceRemoveDirectory(path)) { HILOGE("RemoveDirectory failed"); } diff --git a/tests/mock/module_external/bms_adapter_mock.cpp b/tests/mock/module_external/bms_adapter_mock.cpp index edf7f3612..d6f295373 100644 --- a/tests/mock/module_external/bms_adapter_mock.cpp +++ b/tests/mock/module_external/bms_adapter_mock.cpp @@ -29,7 +29,7 @@ namespace OHOS::FileManagement::Backup { using namespace std; -vector BundleMgrAdapter::GetBundleInfos() +vector BundleMgrAdapter::GetBundleInfos(int32_t userId) { vector bundleInfos; bundleInfos.emplace_back( @@ -37,7 +37,7 @@ vector BundleMgrAdapter::GetBundleInfos() return bundleInfos; } -vector BundleMgrAdapter::GetBundleInfos(const vector &bundleNames) +vector BundleMgrAdapter::GetBundleInfos(const vector &bundleNames, int32_t userId) { vector bundleInfos; bundleInfos.emplace_back( @@ -45,7 +45,7 @@ vector BundleMgrAdapter::GetBundleInfos(const vecto return bundleInfos; } -ErrCode BundleMgrAdapter::Install(wptr statusReceiver, const string &bundleFilePath) +ErrCode BundleMgrAdapter::Install(wptr statusReceiver, const string &bundleFilePath, int32_t userId) { return 0; } diff --git a/tests/mock/module_ipc/svc_backup_connection_mock.cpp b/tests/mock/module_ipc/svc_backup_connection_mock.cpp index f08dd4960..ead43d40d 100644 --- a/tests/mock/module_ipc/svc_backup_connection_mock.cpp +++ b/tests/mock/module_ipc/svc_backup_connection_mock.cpp @@ -45,7 +45,7 @@ void SvcBackupConnection::OnAbilityDisconnectDone(const AppExecFwk::ElementName callDied_(move(bundleName)); } -ErrCode SvcBackupConnection::ConnectBackupExtAbility(AAFwk::Want &want) +ErrCode SvcBackupConnection::ConnectBackupExtAbility(AAFwk::Want &want, int32_t userId) { return 0; } diff --git a/tests/mock/module_ipc/svc_session_manager_mock.cpp b/tests/mock/module_ipc/svc_session_manager_mock.cpp index 79eb5933f..6740f4612 100644 --- a/tests/mock/module_ipc/svc_session_manager_mock.cpp +++ b/tests/mock/module_ipc/svc_session_manager_mock.cpp @@ -241,4 +241,69 @@ bool SvcSessionManager::NeedToUnloadService() { return false; } + +int32_t SvcSessionManager::GetSessionUserId() +{ + return impl_.userId; +} + +void SvcSessionManager::SetSessionUserId(int32_t userId) +{ + impl_.userId = userId; +} + +void SvcSessionManager::SetBundleRestoreType(const std::string &bundleName, RestoreTpyeEnum restoreType) +{ + auto it = impl_.backupExtNameMap.find(bundleName); + if (it == impl_.backupExtNameMap.end()) { + return; + } + it->second.restoreType = restoreType; +} + +RestoreTpyeEnum SvcSessionManager::GetBundleRestoreType(const std::string &bundleName) +{ + auto it = impl_.backupExtNameMap.find(bundleName); + if (it == impl_.backupExtNameMap.end()) { + return RestoreTpyeEnum::RESTORE_DATA_READDY; + } + return it->second.restoreType; +} + +void SvcSessionManager::SetBundleVersionCode(const std::string &bundleName, uint32_t versionCode) +{ + auto it = impl_.backupExtNameMap.find(bundleName); + if (it == impl_.backupExtNameMap.end()) { + return; + } + it->second.versionCode = versionCode; +} + +uint32_t SvcSessionManager::GetBundleVersionCode(const std::string &bundleName) +{ + auto it = impl_.backupExtNameMap.find(bundleName); + if (it == impl_.backupExtNameMap.end()) { + return 0; + } + return it->second.versionCode; +} + +void SvcSessionManager::SetBundleVersionName(const std::string &bundleName, std::string versionName) +{ + auto it = impl_.backupExtNameMap.find(bundleName); + if (it == impl_.backupExtNameMap.end()) { + return; + } + it->second.versionName = versionName; +} + +std::string SvcSessionManager::GetBundleVersionName(const std::string &bundleName) +{ + auto it = impl_.backupExtNameMap.find(bundleName); + if (it == impl_.backupExtNameMap.end()) { + return ""; + } + return it->second.versionName; +} + } // namespace OHOS::FileManagement::Backup \ No newline at end of file diff --git a/tests/unittests/backup_sa/module_ipc/svc_backup_connection_test.cpp b/tests/unittests/backup_sa/module_ipc/svc_backup_connection_test.cpp index b2cb332e8..45ecec2b1 100644 --- a/tests/unittests/backup_sa/module_ipc/svc_backup_connection_test.cpp +++ b/tests/unittests/backup_sa/module_ipc/svc_backup_connection_test.cpp @@ -31,6 +31,7 @@ using namespace testing; namespace { constexpr int32_t WAIT_TIME = 1; +constexpr int DEFAULT_USER_ID = 100; } // namespace class SvcBackupConnectionTest : public testing::Test { @@ -138,7 +139,7 @@ HWTEST_F(SvcBackupConnectionTest, SUB_BackupConnection_ConnectBackupExtAbility_0 { GTEST_LOG_(INFO) << "SvcBackupConnectionTest-begin SUB_BackupConnection_ConnectBackupExtAbility_0100"; AAFwk::Want want; - ErrCode ret = backupCon_->ConnectBackupExtAbility(want); + ErrCode ret = backupCon_->ConnectBackupExtAbility(want, DEFAULT_USER_ID); EXPECT_NE(ret, BError(BError::Codes::OK)); GTEST_LOG_(INFO) << "SvcBackupConnectionTest-end SUB_BackupConnection_ConnectBackupExtAbility_0100"; } diff --git a/tests/unittests/backup_tools/backup_tool/tools_op_backup_test.cpp b/tests/unittests/backup_tools/backup_tool/tools_op_backup_test.cpp index 1eed3a4a9..fd759abc1 100644 --- a/tests/unittests/backup_tools/backup_tool/tools_op_backup_test.cpp +++ b/tests/unittests/backup_tools/backup_tool/tools_op_backup_test.cpp @@ -49,7 +49,7 @@ HWTEST_F(ToolsOpBackupTest, SUB_backup_tools_op_backup_0100, testing::ext::TestS SetMockGetInstance(true); GTEST_LOG_(INFO) << "ToolsOpBackupTest-info"; map> mapArgToVal; - string localCap = string(BConstants::SA_BUNDLE_BACKUP_TOOL_DIR.data()) + "/tmp"; + string localCap = string(BConstants::GetSaBundleBackupToolDir(BConstants::DEFAULT_USER_ID).data()) + "/tmp"; vector path = {localCap.data()}; mapArgToVal.insert(make_pair("pathCapFile", path)); vector bundles = {"com.example.app2backup"}; diff --git a/tests/unittests/backup_tools/backup_tool/tools_op_restore_test.cpp b/tests/unittests/backup_tools/backup_tool/tools_op_restore_test.cpp index b71715f78..c8d9ee169 100644 --- a/tests/unittests/backup_tools/backup_tool/tools_op_restore_test.cpp +++ b/tests/unittests/backup_tools/backup_tool/tools_op_restore_test.cpp @@ -54,7 +54,7 @@ HWTEST_F(ToolsOpRestoreTest, SUB_backup_tools_op_restore_0100, testing::ext::Tes try { GTEST_LOG_(INFO) << "ToolsOpRestoreTest-info"; map> mapArgToVal; - string localCap = string(BConstants::SA_BUNDLE_BACKUP_TOOL_DIR.data()) + "/tmp"; + string localCap = string(BConstants::GetSaBundleBackupToolDir(BConstants::DEFAULT_USER_ID).data()) + "/tmp"; vector path = {localCap.data()}; mapArgToVal.insert(make_pair("pathCapFile", path)); vector bundles = {"com.example.app2backup"}; @@ -63,7 +63,7 @@ HWTEST_F(ToolsOpRestoreTest, SUB_backup_tools_op_restore_0100, testing::ext::Tes // 创建测试路径以及测试环境 string cmdMkdir = string("mkdir -p ") + BConstants::BACKUP_TOOL_RECEIVE_DIR.data() + BUNDLE_NAME; system(cmdMkdir.c_str()); - string cmdTool = string("mkdir -p ") + BConstants::SA_BUNDLE_BACKUP_TOOL_DIR.data(); + string cmdTool = string("mkdir -p ") + BConstants::GetSaBundleBackupToolDir(BConstants::DEFAULT_USER_ID).data(); system(cmdTool.c_str()); string touchTar = string("touch ") + BConstants::BACKUP_TOOL_RECEIVE_DIR.data() + BUNDLE_NAME + FILE_NAME; system(touchTar.c_str()); diff --git a/tests/unittests/backup_utils/b_json/b_json_entity_extension_config_test.cpp b/tests/unittests/backup_utils/b_json/b_json_entity_extension_config_test.cpp index 122f6424f..6467d433f 100644 --- a/tests/unittests/backup_utils/b_json/b_json_entity_extension_config_test.cpp +++ b/tests/unittests/backup_utils/b_json/b_json_entity_extension_config_test.cpp @@ -360,7 +360,7 @@ HWTEST_F(BJsonEntityExtensionConfigTest, b_json_entity_extension_config_1000, te GTEST_LOG_(INFO) << "BJsonEntityExtensionConfigTest-begin b_json_entity_extension_config_1000"; try { string bundleName = "com.example.app2backup"; - string jsonFileDir = string(BConstants::SA_BUNDLE_BACKUP_ROOT_DIR).append(bundleName); + string jsonFileDir = BConstants::GetSaBundleBackupRootDir(BConstants::DEFAULT_USER_ID).append(bundleName); string jsonContent = R"({"allowToBackupRestore":true})"; auto [bFatalErr, ret] = BProcess::ExecuteCmd({"mkdir", "-p", jsonFileDir}); EXPECT_FALSE(bFatalErr); diff --git a/tools/backup_tool/src/tools_op_backup.cpp b/tools/backup_tool/src/tools_op_backup.cpp index d1f819240..c5ca37b0d 100644 --- a/tools/backup_tool/src/tools_op_backup.cpp +++ b/tools/backup_tool/src/tools_op_backup.cpp @@ -201,11 +201,12 @@ static void BackupToolDirSoftlinkToBackupDir() } } - if (access(BConstants::SA_BUNDLE_BACKUP_TOOL_DIR.data(), F_OK) != 0 && - mkdir(BConstants::SA_BUNDLE_BACKUP_TOOL_DIR.data(), S_IRWXU) != 0) { + if (access(BConstants::GetSaBundleBackupToolDir(BConstants::DEFAULT_USER_ID).data(), F_OK) != 0 && + mkdir(BConstants::GetSaBundleBackupToolDir(BConstants::DEFAULT_USER_ID).data(), S_IRWXU) != 0) { throw BError(BError::Codes::TOOL_INVAL_ARG, generic_category().message(errno)); } - if (symlink(BConstants::SA_BUNDLE_BACKUP_TOOL_DIR.data(), BConstants::BACKUP_TOOL_LINK_DIR.data()) == -1) { + if (symlink(BConstants::GetSaBundleBackupToolDir(BConstants::DEFAULT_USER_ID).data(), + BConstants::BACKUP_TOOL_LINK_DIR.data()) == -1) { HILOGE("failed to create soft link file %{public}s errno : %{public}d", BConstants::BACKUP_TOOL_LINK_DIR.data(), errno); throw BError(BError::Codes::TOOL_INVAL_ARG, generic_category().message(errno)); diff --git a/utils/include/b_resources/b_constants.h b/utils/include/b_resources/b_constants.h index 58a4a2358..94815bdef 100644 --- a/utils/include/b_resources/b_constants.h +++ b/utils/include/b_resources/b_constants.h @@ -24,6 +24,10 @@ namespace OHOS::FileManagement::Backup::BConstants { static inline const char *EXTENSION_ACTION_PARA = "extensionAction"; +static inline const char *RESTORE_TYPE_PARA = "restoreType"; +static inline const char *VERSION_CODE_PARA = "versionCode"; +static inline const char *VERSION_NAME_PARA = "versionName"; +static inline const char *USER_ID_PARA = "userId"; enum class ExtensionAction { INVALID = 0, BACKUP = 1, @@ -102,18 +106,43 @@ static inline std::string ENTRY_NAME_SIZE = "size"; // backup.para内配置项的名称,改配置项值为true时可在不更新hap包的情况下,可以读取包管理元数据配置文件的内容 static inline std::string BACKUP_DEBUG_OVERRIDE_EXTENSION_CONFIG_KEY = "backup.debug.overrideExtensionConfig"; -// 应用备份数据暂存路径。 -static inline std::string_view SA_BUNDLE_BACKUP_DIR = "/data/service/el2/100/backup/bundles/"; +// 应用备份数据暂存路径 static inline std::string_view SA_BUNDLE_BACKUP_BACKUP = "/backup/"; static inline std::string_view SA_BUNDLE_BACKUP_RESTORE = "/restore/"; -static inline std::string_view SA_BUNDLE_BACKUP_ROOT_DIR = "/data/service/el2/100/backup/backup_sa/"; static inline std::string_view SA_BUNDLE_BACKUP_TMP_DIR = "/tmp/"; static inline std::string_view BACKUP_TOOL_RECEIVE_DIR = "/data/backup/received/"; static inline std::string_view PATH_BUNDLE_BACKUP_HOME = "/data/storage/el2/backup"; -static inline std::string_view SA_BUNDLE_BACKUP_TOOL_DIR = "/data/service/el2/100/backup/backup_tool/"; static inline std::string_view BACKUP_TOOL_LINK_DIR = "/data/backup"; static inline std::string_view BACKUP_TOOL_INSTALL_DIR = "/data/backup/install/"; +// 多用户场景应用备份数据路径 +static inline std::string GetSaBundleBackupDir(int32_t userId) +{ + std::string str; + str.append("/data/service/el2/"); + str.append(std::to_string(userId)); + str.append("/backup/bundles/"); + return str; +} + +static inline std::string GetSaBundleBackupRootDir(int32_t userId) +{ + std::string str; + str.append("/data/service/el2/"); + str.append(std::to_string(userId)); + str.append("/backup/backup_sa/"); + return str; +} + +static inline std::string GetSaBundleBackupToolDir(int32_t userId) +{ + std::string str; + str.append("/data/service/el2/"); + str.append(std::to_string(userId)); + str.append("/backup/backup_tool/"); + return str; +} + // 备份恢复配置文件暂存路径 static inline std::string_view BACKUP_CONFIG_EXTENSION_PATH = "/data/storage/el2/base/temp/"; diff --git a/utils/src/b_json/b_json_entity_extension_config.cpp b/utils/src/b_json/b_json_entity_extension_config.cpp index b0c11a081..39fed9282 100644 --- a/utils/src/b_json/b_json_entity_extension_config.cpp +++ b/utils/src/b_json/b_json_entity_extension_config.cpp @@ -102,6 +102,7 @@ string BJsonEntityExtensionConfig::GetJSonSource(string_view jsonFromRealWorld, return string(jsonFromRealWorld); } + /* Server for test case, the user function does not execute the following code. */ if (!option.has_value()) { if (getuid() == static_cast(BConstants::BACKUP_UID)) { throw BError(BError::Codes::SA_INVAL_ARG, "Current process is not extension process"); @@ -119,7 +120,7 @@ string BJsonEntityExtensionConfig::GetJSonSource(string_view jsonFromRealWorld, } catch (const bad_any_cast &e) { throw BError(BError::Codes::SA_INVAL_ARG, e.what()); } - string jsonFilePath = string(BConstants::SA_BUNDLE_BACKUP_ROOT_DIR). + string jsonFilePath = string(BConstants::GetSaBundleBackupRootDir(BConstants::DEFAULT_USER_ID)). append(bundleName). append("/"). append(BConstants::BACKUP_CONFIG_JSON); -- Gitee