diff --git a/frameworks/native/backup_kit_inner/src/b_incremental_backup_session.cpp b/frameworks/native/backup_kit_inner/src/b_incremental_backup_session.cpp index 60648c65b7df2b4fda05859d89a0e6833dd1ff4f..9550ffe5e293b3fa03f84df0fb39c646b228d943 100644 --- a/frameworks/native/backup_kit_inner/src/b_incremental_backup_session.cpp +++ b/frameworks/native/backup_kit_inner/src/b_incremental_backup_session.cpp @@ -70,6 +70,35 @@ unique_ptr BIncrementalBackupSession::Init(Callbacks return nullptr; } +unique_ptr BIncrementalBackupSession::Init(Callbacks callbacks, + std::string &errMsg, ErrCode &errCode) +{ + try { + HILOGI("Init IncrementalBackupSession Begin"); + auto backup = make_unique(); + ServiceProxy::InvaildInstance(); + auto proxy = ServiceProxy::GetInstance(); + if (proxy == nullptr) { + HILOGI("Failed to get backup service"); + return nullptr; + } + errCode = proxy->InitIncrementalBackupSession(sptr(new ServiceReverse(callbacks)), errMsg); + if (errCode != ERR_OK) { + HILOGE("Failed to Backup because of %{public}d", errCode); + AppRadar::Info info("", "", ""); + AppRadar::GetInstance().RecordBackupFuncRes(info, "BIncrementalBackupSession::Init", + AppRadar::GetInstance().GetUserId(), BizStageBackup::BIZ_STAGE_CREATE_BACKUP_SESSION_FAIL, errCode); + return nullptr; + } + backup->RegisterBackupServiceDied(callbacks.onBackupServiceDied); + return backup; + } catch (const exception &e) { + HILOGE("Failed to Backup because of %{public}s", e.what()); + errCode = BError(BError::Codes::SDK_INVAL_ARG); + } + return nullptr; +} + void BIncrementalBackupSession::RegisterBackupServiceDied(function functor) { auto proxy = ServiceProxy::GetInstance(); diff --git a/frameworks/native/backup_kit_inner/src/b_session_backup.cpp b/frameworks/native/backup_kit_inner/src/b_session_backup.cpp index 48ec7869f331bbb9462349f37d92961da5ef79c6..dfc5a682140fd495cf5bc44ada59ba0f8dccc6c9 100644 --- a/frameworks/native/backup_kit_inner/src/b_session_backup.cpp +++ b/frameworks/native/backup_kit_inner/src/b_session_backup.cpp @@ -70,6 +70,35 @@ unique_ptr BSessionBackup::Init(Callbacks callbacks) return nullptr; } +unique_ptr BSessionBackup::Init(Callbacks callbacks, + std::string &errMsg, ErrCode &errCode) +{ + try { + HILOGI("Init BackupSession Begin"); + auto backup = make_unique(); + ServiceProxy::InvaildInstance(); + auto proxy = ServiceProxy::GetInstance(); + if (proxy == nullptr) { + HILOGI("Failed to get backup service"); + return nullptr; + } + errCode = proxy->InitBackupSession(sptr(new ServiceReverse(callbacks)), errMsg); + if (errCode != ERR_OK) { + HILOGE("Failed to Backup because of %{public}d", errCode); + AppRadar::Info info("", "", ""); + AppRadar::GetInstance().RecordBackupFuncRes(info, "BSessionBackup::Init", + AppRadar::GetInstance().GetUserId(), BizStageBackup::BIZ_STAGE_CREATE_BACKUP_SESSION_FAIL, errCode); + return nullptr; + } + backup->RegisterBackupServiceDied(callbacks.onBackupServiceDied); + return backup; + } catch (const exception &e) { + HILOGE("Failed to Backup because of %{public}s", e.what()); + errCode = BError(BError::Codes::SDK_INVAL_ARG); + } + return nullptr; +} + void BSessionBackup::RegisterBackupServiceDied(std::function functor) { auto proxy = ServiceProxy::GetInstance(); diff --git a/frameworks/native/backup_kit_inner/src/service_incremental_proxy.cpp b/frameworks/native/backup_kit_inner/src/service_incremental_proxy.cpp index e534448503f503dc8e540287c4e75d9954b69a8a..bbb503bfaad495965cb31a54d586d65a4ae00131 100644 --- a/frameworks/native/backup_kit_inner/src/service_incremental_proxy.cpp +++ b/frameworks/native/backup_kit_inner/src/service_incremental_proxy.cpp @@ -132,6 +132,38 @@ ErrCode ServiceProxy::InitIncrementalBackupSession(sptr remote) return reply.ReadInt32(); } +ErrCode ServiceProxy::InitIncrementalBackupSession(sptr remote, std::string &errMsg) +{ + HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); + BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "remote is nullptr"); + MessageParcel data; + if (!data.WriteInterfaceToken(GetDescriptor())) { + HILOGE("Failed to write descriptor"); + return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write descriptor").GetCode(); + } + MessageParcel reply; + MessageOption option; + if (!remote) { + HILOGE("Empty reverse stub"); + return BError(BError::Codes::SDK_INVAL_ARG, "Empty reverse stub").GetCode(); + } + if (!data.WriteRemoteObject(remote->AsObject().GetRefPtr())) { + HILOGE("Failed to send the reverse stub"); + return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send the reverse stub").GetCode(); + } + int32_t ret = Remote()->SendRequest( + static_cast(IServiceInterfaceCode::SERVICE_CMD_INIT_INCREMENTAL_BACKUP_SESSION_MSG), + data, reply, option); + if (ret != NO_ERROR) { + HILOGE("Received error %{public}d when doing IPC", ret); + return BError(BError::Codes::SDK_INVAL_ARG, "Received error when doing IPC").GetCode(); + } + if (!reply.ReadString(errMsg)) { + return BError(BError::Codes::SDK_INVAL_ARG, "Failed to receive the errMsg").GetCode(); + } + return reply.ReadInt32(); +} + ErrCode ServiceProxy::AppendBundlesIncrementalBackupSession(const vector &bundlesToBackup) { HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); diff --git a/frameworks/native/backup_kit_inner/src/service_proxy.cpp b/frameworks/native/backup_kit_inner/src/service_proxy.cpp index e367757bc9582cf4a80a869fd418456241ce1e71..216e79fad1b22c39f273efabc582fc15609132ee 100644 --- a/frameworks/native/backup_kit_inner/src/service_proxy.cpp +++ b/frameworks/native/backup_kit_inner/src/service_proxy.cpp @@ -113,6 +113,35 @@ ErrCode ServiceProxy::InitBackupSession(sptr remote) return reply.ReadInt32(); } +ErrCode ServiceProxy::InitBackupSession(sptr remote, std::string &errMsg) +{ + HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); + BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr"); + MessageParcel data; + if (!data.WriteInterfaceToken(GetDescriptor())) { + return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write descriptor").GetCode(); + } + MessageParcel reply; + MessageOption option; + if (!remote) { + return BError(BError::Codes::SDK_INVAL_ARG, "Empty reverse stub").GetCode(); + } + if (!data.WriteRemoteObject(remote->AsObject().GetRefPtr())) { + return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send the reverse stub").GetCode(); + } + + int32_t ret = Remote()->SendRequest( + static_cast(IServiceInterfaceCode::SERVICE_CMD_INIT_BACKUP_SESSION_MSG), data, reply, option); + if (ret != NO_ERROR) { + string str = "Failed to send out the request because of " + to_string(ret); + return BError(BError::Codes::SDK_INVAL_ARG, str.data()).GetCode(); + } + if (!reply.ReadString(errMsg)) { + return BError(BError::Codes::SDK_INVAL_ARG, "Failed to receive the errMsg").GetCode(); + } + return reply.ReadInt32(); +} + ErrCode ServiceProxy::Start() { HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); diff --git a/interfaces/inner_api/native/backup_kit_inner/impl/b_incremental_backup_session.h b/interfaces/inner_api/native/backup_kit_inner/impl/b_incremental_backup_session.h index 0ab71f6fd4c3ab26cbff31114803b241e6843858..87e0f0313cb59945666d89cb9ea44e686da35d78 100644 --- a/interfaces/inner_api/native/backup_kit_inner/impl/b_incremental_backup_session.h +++ b/interfaces/inner_api/native/backup_kit_inner/impl/b_incremental_backup_session.h @@ -49,6 +49,17 @@ public: */ static std::unique_ptr Init(Callbacks callbacks); + /** + * @brief 获取一个用于控制备份流程的会话 + * + * @param callbacks 注册回调 + * @param errMsg 失败信息 + * @param errCode 错误码 + * @return std::unique_ptr 指向会话的智能指针。失败时为空指针 + */ + static std::unique_ptr Init(Callbacks callbacks, + std::string &errMsg, ErrCode &errCode); + /** * @brief 用于追加应用,现阶段仅支持在Start之前调用 * diff --git a/interfaces/inner_api/native/backup_kit_inner/impl/b_session_backup.h b/interfaces/inner_api/native/backup_kit_inner/impl/b_session_backup.h index 619c0afc2c2c4db543ba5a10c3c1f80ca559417a..3228ca18555b6fdbed9845689d6652aab37afd9c 100644 --- a/interfaces/inner_api/native/backup_kit_inner/impl/b_session_backup.h +++ b/interfaces/inner_api/native/backup_kit_inner/impl/b_session_backup.h @@ -47,6 +47,16 @@ public: */ static std::unique_ptr Init(Callbacks callbacks); + /** + * @brief 获取一个用于控制备份流程的会话 + * + * @param callbacks 注册回调 + * @param errMsg 失败信息 + * @param errCode 错误码 + * @return std::unique_ptr 指向会话的智能指针。失败时为空指针 + */ + static std::unique_ptr Init(Callbacks callbacks, std::string &errMsg, ErrCode &errCode); + /** * @brief 用于追加应用,现阶段仅支持在Start之前调用 * diff --git a/interfaces/inner_api/native/backup_kit_inner/impl/i_service.h b/interfaces/inner_api/native/backup_kit_inner/impl/i_service.h index ec94562938692da901f120a2861ae209107c9c56..4f84ca68d90297ff12b27302e273ce462168efb0 100644 --- a/interfaces/inner_api/native/backup_kit_inner/impl/i_service.h +++ b/interfaces/inner_api/native/backup_kit_inner/impl/i_service.h @@ -48,6 +48,7 @@ public: virtual ErrCode InitRestoreSession(sptr remote) = 0; virtual ErrCode InitRestoreSession(sptr remote, std::string &errMsg) = 0; virtual ErrCode InitBackupSession(sptr remote) = 0; + virtual ErrCode InitBackupSession(sptr remote, std::string &errMsg) = 0; virtual ErrCode Start() = 0; virtual UniqueFd GetLocalCapabilities() = 0; virtual ErrCode PublishFile(const BFileInfo &fileInfo) = 0; @@ -74,6 +75,7 @@ public: virtual UniqueFd GetLocalCapabilitiesIncremental(const std::vector &bundleNames) = 0; virtual ErrCode GetAppLocalListAndDoIncrementalBackup() = 0; virtual ErrCode InitIncrementalBackupSession(sptr remotse) = 0; + virtual ErrCode InitIncrementalBackupSession(sptr remotse, std::string &errMsg) = 0; virtual ErrCode AppendBundlesIncrementalBackupSession(const std::vector &bundlesToBackup) = 0; virtual ErrCode AppendBundlesIncrementalBackupSession(const std::vector &bundlesToBackup, const std::vector &bundleInfos) = 0; diff --git a/interfaces/inner_api/native/backup_kit_inner/impl/i_service_ipc_interface_code.h b/interfaces/inner_api/native/backup_kit_inner/impl/i_service_ipc_interface_code.h index 2431573bcd93f94533b5a4c8d2e88ba0b35e6070..0b858c8af19288ec6a617cdd6d939cd116579234 100644 --- a/interfaces/inner_api/native/backup_kit_inner/impl/i_service_ipc_interface_code.h +++ b/interfaces/inner_api/native/backup_kit_inner/impl/i_service_ipc_interface_code.h @@ -22,6 +22,7 @@ enum class IServiceInterfaceCode { SERVICE_CMD_INIT_RESTORE_SESSION, SERVICE_CMD_INIT_RESTORE_SESSION_MSG, SERVICE_CMD_INIT_BACKUP_SESSION, + SERVICE_CMD_INIT_BACKUP_SESSION_MSG, SERVICE_CMD_GET_LOCAL_CAPABILITIES, SERVICE_CMD_PUBLISH_FILE, SERVICE_CMD_APP_FILE_READY, @@ -37,6 +38,7 @@ enum class IServiceInterfaceCode { SERVICE_CMD_RELSEASE_SESSION, SERVICE_CMD_GET_LOCAL_CAPABILITIES_INCREMENTAL, SERVICE_CMD_INIT_INCREMENTAL_BACKUP_SESSION, + SERVICE_CMD_INIT_INCREMENTAL_BACKUP_SESSION_MSG, SERVICE_CMD_APPEND_BUNDLES_INCREMENTAL_BACKUP_SESSION, SERVICE_CMD_APPEND_BUNDLES_INCREMENTAL_BACKUP_SESSION_DETAILS, SERVICE_CMD_PUBLISH_INCREMENTAL_FILE, diff --git a/interfaces/inner_api/native/backup_kit_inner/impl/service_proxy.h b/interfaces/inner_api/native/backup_kit_inner/impl/service_proxy.h index e2b849c625df350a8f6127eaa10f8cd19bfdeedd..7f8d548e08f26061866fa13b1f58dbd465254af6 100644 --- a/interfaces/inner_api/native/backup_kit_inner/impl/service_proxy.h +++ b/interfaces/inner_api/native/backup_kit_inner/impl/service_proxy.h @@ -31,6 +31,7 @@ public: ErrCode InitRestoreSession(sptr remote) override; ErrCode InitRestoreSession(sptr remote, std::string &errMsg) override; ErrCode InitBackupSession(sptr remote) override; + ErrCode InitBackupSession(sptr remote, std::string &errMsg) override; ErrCode Start() override; UniqueFd GetLocalCapabilities() override; ErrCode PublishFile(const BFileInfo &fileInfo) override; @@ -57,6 +58,7 @@ public: UniqueFd GetLocalCapabilitiesIncremental(const std::vector &bundleNames) override; ErrCode GetAppLocalListAndDoIncrementalBackup() override; ErrCode InitIncrementalBackupSession(sptr remote) override; + ErrCode InitIncrementalBackupSession(sptr remote, std::string &errMsg) override; ErrCode AppendBundlesIncrementalBackupSession(const std::vector &bundlesToBackup) override; ErrCode AppendBundlesIncrementalBackupSession(const std::vector &bundlesToBackup, const std::vector &infos) override; diff --git a/interfaces/kits/js/backup/session_backup_n_exporter.cpp b/interfaces/kits/js/backup/session_backup_n_exporter.cpp index c39d3196a61736db29f79715c798fa5e9a894663..674f7d11c69eeb5d105e035887847e393899cc8d 100644 --- a/interfaces/kits/js/backup/session_backup_n_exporter.cpp +++ b/interfaces/kits/js/backup/session_backup_n_exporter.cpp @@ -317,12 +317,10 @@ napi_value SessionBackupNExporter::Constructor(napi_env env, napi_callback_info { HILOGD("called SessionBackup::Constructor begin"); if (!SAUtils::CheckBackupPermission()) { - HILOGE("Has not permission!"); NError(E_PERMISSION).ThrowErr(env); return nullptr; } if (!SAUtils::IsSystemApp()) { - HILOGE("System App check fail!"); NError(E_PERMISSION_SYS).ThrowErr(env); return nullptr; } @@ -332,17 +330,17 @@ napi_value SessionBackupNExporter::Constructor(napi_env env, napi_callback_info NError(BError(BError::Codes::SDK_INVAL_ARG, "Number of arguments unmatched.").GetCode()).ThrowErr(env); return nullptr; } - NVal callbacks(env, funcArg[NARG_POS::FIRST]); if (!callbacks.TypeIs(napi_object)) { HILOGE("First argument is not an object."); NError(BError(BError::Codes::SDK_INVAL_ARG, "First argument is not an object.").GetCode()).ThrowErr(env); return nullptr; } - NVal ptr(env, funcArg.GetThisVar()); auto backupEntity = std::make_unique(); backupEntity->callbacks = make_shared(env, ptr, callbacks); + ErrCode errCode; + std::string errMsg; backupEntity->session = BSessionBackup::Init(BSessionBackup::Callbacks { .onFileReady = bind(OnFileReady, backupEntity->callbacks, placeholders::_1, placeholders::_2, placeholders::_3), .onBundleStarted = bind(onBundleBegin, backupEntity->callbacks, placeholders::_1, placeholders::_2), @@ -350,9 +348,12 @@ napi_value SessionBackupNExporter::Constructor(napi_env env, napi_callback_info .onAllBundlesFinished = bind(onAllBundlesEnd, backupEntity->callbacks, placeholders::_1), .onResultReport = bind(OnResultReport, backupEntity->callbacks, placeholders::_1, placeholders::_2), .onBackupServiceDied = bind(OnBackupServiceDied, backupEntity->callbacks), - .onProcess = bind(OnProcess, backupEntity->callbacks, placeholders::_1, placeholders::_2)}); + .onProcess = bind(OnProcess, backupEntity->callbacks, placeholders::_1, placeholders::_2)}, errMsg, errCode); if (!backupEntity->session) { - NError(BError(BError::Codes::SDK_INVAL_ARG, "Failed to init backup").GetCode()).ThrowErr(env); + std::tuple errInfo = + std::make_tuple(errCode, BError::GetBackupMsgByErrno(errCode) + ", " + errMsg); + ErrParam errorParam = [ errInfo ]() { return errInfo;}; + NError(errorParam).ThrowErr(env); return nullptr; } if (!SetSessionBackupEntity(env, funcArg, std::move(backupEntity))) { diff --git a/interfaces/kits/js/backup/session_incremental_backup_n_exporter.cpp b/interfaces/kits/js/backup/session_incremental_backup_n_exporter.cpp index b54fa973f97437563e532d4701e4817dd3bf7528..3f88afec2eb4448df39f6ce72bb2bae815f6b0d3 100644 --- a/interfaces/kits/js/backup/session_incremental_backup_n_exporter.cpp +++ b/interfaces/kits/js/backup/session_incremental_backup_n_exporter.cpp @@ -321,12 +321,10 @@ napi_value SessionIncrementalBackupNExporter::Constructor(napi_env env, napi_cal { HILOGD("called SessionIncrementalBackup::Constructor begin"); if (!SAUtils::CheckBackupPermission()) { - HILOGE("Has not permission!"); NError(E_PERMISSION).ThrowErr(env); return nullptr; } if (!SAUtils::IsSystemApp()) { - HILOGE("System App check fail!"); NError(E_PERMISSION_SYS).ThrowErr(env); return nullptr; } @@ -336,17 +334,17 @@ napi_value SessionIncrementalBackupNExporter::Constructor(napi_env env, napi_cal NError(BError(BError::Codes::SDK_INVAL_ARG, "Number of arguments unmatched.").GetCode()).ThrowErr(env); return nullptr; } - NVal callbacks(env, funcArg[NARG_POS::FIRST]); if (!callbacks.TypeIs(napi_object)) { HILOGE("First argument is not an object."); NError(BError(BError::Codes::SDK_INVAL_ARG, "First argument is not an object.").GetCode()).ThrowErr(env); return nullptr; } - NVal ptr(env, funcArg.GetThisVar()); auto backupEntity = std::make_unique(); backupEntity->callbacks = make_shared(env, ptr, callbacks); + ErrCode errCode; + std::string errMsg; backupEntity->session = BIncrementalBackupSession::Init(BIncrementalBackupSession::Callbacks { .onFileReady = bind(OnFileReady, backupEntity->callbacks, placeholders::_1, placeholders::_2, placeholders::_3, placeholders::_4), @@ -355,9 +353,12 @@ napi_value SessionIncrementalBackupNExporter::Constructor(napi_env env, napi_cal .onAllBundlesFinished = bind(onAllBundlesEnd, backupEntity->callbacks, placeholders::_1), .onResultReport = bind(OnResultReport, backupEntity->callbacks, placeholders::_1, placeholders::_2), .onBackupServiceDied = bind(OnBackupServiceDied, backupEntity->callbacks), - .onProcess = bind(OnProcess, backupEntity->callbacks, placeholders::_1, placeholders::_2)}); + .onProcess = bind(OnProcess, backupEntity->callbacks, placeholders::_1, placeholders::_2)}, errMsg, errCode); if (!backupEntity->session) { - NError(BError(BError::Codes::SDK_INVAL_ARG, "Failed to init backup").GetCode()).ThrowErr(env); + std::tuple errInfo = + std::make_tuple(errCode, BError::GetBackupMsgByErrno(errCode) + ", " + errMsg); + ErrParam errorParam = [ errInfo ]() { return errInfo;}; + NError(errorParam).ThrowErr(env); return nullptr; } if (!SetIncrementalBackupEntity(env, funcArg, std::move(backupEntity))) { diff --git a/services/backup_sa/include/module_ipc/service.h b/services/backup_sa/include/module_ipc/service.h index 4d257bbce116d0bdcc571bdef51085eec5d16f68..a9ed6ca5c0de5319d9591365f3740209aad24e8a 100644 --- a/services/backup_sa/include/module_ipc/service.h +++ b/services/backup_sa/include/module_ipc/service.h @@ -51,6 +51,7 @@ public: ErrCode InitRestoreSession(sptr remote) override; ErrCode InitRestoreSession(sptr remote, std::string &errMsg) override; ErrCode InitBackupSession(sptr remote) override; + ErrCode InitBackupSession(sptr remote, std::string &errMsg) override; ErrCode Start() override; UniqueFd GetLocalCapabilities() override; ErrCode PublishFile(const BFileInfo &fileInfo) override; @@ -78,6 +79,7 @@ public: UniqueFd GetLocalCapabilitiesIncremental(const std::vector &bundleNames) override; ErrCode GetAppLocalListAndDoIncrementalBackup() override; ErrCode InitIncrementalBackupSession(sptr remote) override; + ErrCode InitIncrementalBackupSession(sptr remote, std::string &errMsg) override; ErrCode AppendBundlesIncrementalBackupSession(const std::vector &bundlesToBackup) override; ErrCode AppendBundlesIncrementalBackupSession(const std::vector &bundlesToBackup, const std::vector &infos) override; diff --git a/services/backup_sa/include/module_ipc/service_stub.h b/services/backup_sa/include/module_ipc/service_stub.h index 45acead705fc166c7ec52f35acb9c67ab807e7f9..5c631e35cfbe15e8025da070d16b9e613aa2c0b7 100644 --- a/services/backup_sa/include/module_ipc/service_stub.h +++ b/services/backup_sa/include/module_ipc/service_stub.h @@ -37,6 +37,7 @@ private: int32_t CmdInitRestoreSession(MessageParcel &data, MessageParcel &reply); int32_t CmdInitRestoreSessionMsg(MessageParcel &data, MessageParcel &reply); int32_t CmdInitBackupSession(MessageParcel &data, MessageParcel &reply); + int32_t CmdInitBackupSessionMsg(MessageParcel &data, MessageParcel &reply); int32_t CmdStart(MessageParcel &data, MessageParcel &reply); int32_t CmdGetLocalCapabilities(MessageParcel &data, MessageParcel &reply); int32_t CmdPublishFile(MessageParcel &data, MessageParcel &reply); @@ -54,6 +55,7 @@ private: int32_t CmdGetLocalCapabilitiesIncremental(MessageParcel &data, MessageParcel &reply); int32_t CmdGetAppLocalListAndDoIncrementalBackup(MessageParcel &data, MessageParcel &reply); int32_t CmdInitIncrementalBackupSession(MessageParcel &data, MessageParcel &reply); + int32_t CmdInitIncrementalBackupSessionMsg(MessageParcel &data, MessageParcel &reply); int32_t CmdAppendBundlesIncrementalBackupSession(MessageParcel &data, MessageParcel &reply); int32_t CmdAppendBundlesDetailsIncrementalBackupSession(MessageParcel &data, MessageParcel &reply); int32_t CmdPublishIncrementalFile(MessageParcel &data, MessageParcel &reply); diff --git a/services/backup_sa/src/module_ipc/service.cpp b/services/backup_sa/src/module_ipc/service.cpp index 86f5810cb70ad2e7c33b5e1685385e9729f179d7..f49a37103cd0c6a10d3d3066a089e633ae3ed740 100644 --- a/services/backup_sa/src/module_ipc/service.cpp +++ b/services/backup_sa/src/module_ipc/service.cpp @@ -458,14 +458,18 @@ ErrCode Service::InitRestoreSession(sptr remote) .callerName = GetCallerName(), .activeTime = TimeUtils::GetCurrentTime(), }); - if (ret != ERR_OK) { + if (ret == ERR_OK) { + ClearFailedBundles(); + successBundlesNum_ = 0; + return ret; + } + if (ret == BError(BError::Codes::SA_SESSION_CONFLICT)) { HILOGE("Active restore session error, Already have a session"); - StopAll(nullptr, true); return ret; } - ClearFailedBundles(); - successBundlesNum_ = 0; - return BError(BError::Codes::OK); + HILOGE("Active restore session error"); + StopAll(nullptr, true); + return ret; } ErrCode Service::InitBackupSession(sptr remote) @@ -487,14 +491,18 @@ ErrCode Service::InitBackupSession(sptr remote) .callerName = GetCallerName(), .activeTime = TimeUtils::GetCurrentTime(), }); - if (ret != ERR_OK) { + if (ret == ERR_OK) { + ClearFailedBundles(); + successBundlesNum_ = 0; + return ret; + } + if (ret == BError(BError::Codes::SA_SESSION_CONFLICT)) { HILOGE("Active backup session error, Already have a session"); - StopAll(nullptr, true); return ret; } - ClearFailedBundles(); - successBundlesNum_ = 0; - return BError(BError::Codes::OK); + HILOGE("Active backup session error"); + StopAll(nullptr, true); + return ret; } ErrCode Service::Start() diff --git a/services/backup_sa/src/module_ipc/service_incremental.cpp b/services/backup_sa/src/module_ipc/service_incremental.cpp index 49616ee8a75693c0a4c510ad7fe9644fb35b0626..fd49d77753ec077de211ed655843a00952056dfe 100644 --- a/services/backup_sa/src/module_ipc/service_incremental.cpp +++ b/services/backup_sa/src/module_ipc/service_incremental.cpp @@ -311,14 +311,54 @@ ErrCode Service::InitIncrementalBackupSession(sptr remote) .isIncrementalBackup = true, .callerName = GetCallerName(), .activeTime = TimeUtils::GetCurrentTime()}); + if (errCode == ERR_OK) { + ClearFailedBundles(); + successBundlesNum_ = 0; + return errCode; + } + if (errCode == BError(BError::Codes::SA_SESSION_CONFLICT)) { + HILOGE("Active restore session error, Already have a session"); + return errCode; + } + HILOGE("Active restore session error"); + StopAll(nullptr, true); + return errCode; +} + +ErrCode Service::InitIncrementalBackupSession(sptr remote, std::string &errMsg) +{ + HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); + ErrCode errCode = VerifyCaller(); if (errCode != ERR_OK) { + HILOGE("Init incremental backup session fail, Verify caller failed, errCode:%{public}d", errCode); + return errCode; + } + if (session_ == nullptr) { + HILOGE("Init Incremental backup session error, session is empty"); + return BError(BError::Codes::SA_INVAL_ARG); + } + errCode = session_->Active({.clientToken = IPCSkeleton::GetCallingTokenID(), + .scenario = IServiceReverse::Scenario::BACKUP, + .clientProxy = remote, + .userId = GetUserIdDefault(), + .isIncrementalBackup = true, + .callerName = GetCallerName(), + .activeTime = TimeUtils::GetCurrentTime()}); + if (errCode == ERR_OK) { + ClearFailedBundles(); + successBundlesNum_ = 0; + return errCode; + } + if (errCode == BError(BError::Codes::SA_SESSION_CONFLICT)) { + errMsg = BJsonUtil::BuildInitSessionErrInfo(session_->GetSessionUserId(), + session_->GetSessionCallerName(), + session_->GetSessionActiveTime()); HILOGE("Active incremental backup session error, Already have a session"); - StopAll(nullptr, true); return errCode; } - ClearFailedBundles(); - successBundlesNum_ = 0; - return BError(BError::Codes::OK); + HILOGE("Active incremental backup session error"); + StopAll(nullptr, true); + return errCode; } vector Service::GetBundleNameByDetails(const std::vector &bundlesToBackup) diff --git a/services/backup_sa/src/module_ipc/service_stub.cpp b/services/backup_sa/src/module_ipc/service_stub.cpp index ff3d5b2190be36c4c3c364b9b99eec3aafb481cf..3a8998c01717e10c9374ce1505539a852fd2f07a 100644 --- a/services/backup_sa/src/module_ipc/service_stub.cpp +++ b/services/backup_sa/src/module_ipc/service_stub.cpp @@ -52,6 +52,10 @@ void ServiceStub::ServiceStubSupplement() &ServiceStub::CmdStartFwkTimer; opToInterfaceMap_[static_cast(IServiceInterfaceCode::SERVICE_CMD_CANCEL_BUNDLE)] = &ServiceStub::CmdCancel; + opToInterfaceMap_[static_cast(IServiceInterfaceCode::SERVICE_CMD_INIT_BACKUP_SESSION_MSG)] = + &ServiceStub::CmdInitBackupSessionMsg; + opToInterfaceMap_[static_cast(IServiceInterfaceCode::SERVICE_CMD_INIT_INCREMENTAL_BACKUP_SESSION_MSG)] = + &ServiceStub::CmdInitIncrementalBackupSessionMsg; opToInterfaceMap_[static_cast(IServiceInterfaceCode::SERVICE_CMD_INIT_RESTORE_SESSION_MSG)] = &ServiceStub::CmdInitRestoreSessionMsg; opToInterfaceMap_[static_cast(IServiceInterfaceCode::SERVICE_CMD_STOP_EXT_TIMER)] = @@ -201,6 +205,29 @@ int32_t ServiceStub::CmdInitBackupSession(MessageParcel &data, MessageParcel &re return BError(BError::Codes::OK); } +int32_t ServiceStub::CmdInitBackupSessionMsg(MessageParcel &data, MessageParcel &reply) +{ + auto remote = data.ReadRemoteObject(); + std::string errMsg; + if (!remote) { + return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive the reverse stub"); + } + auto iRemote = iface_cast(remote); + if (!iRemote) { + return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive the reverse stub"); + } + int res = InitBackupSession(iRemote, errMsg); + if (!reply.WriteString(errMsg)) { + return BError(BError::Codes::SA_BROKEN_IPC, "Failed to send the errMsg"); + } + if (!reply.WriteInt32(res)) { + stringstream ss; + ss << "Failed to send the result " << res; + return BError(BError::Codes::SA_BROKEN_IPC, ss.str()); + } + return BError(BError::Codes::OK); +} + int32_t ServiceStub::CmdStart(MessageParcel &data, MessageParcel &reply) { int res = Start(); @@ -571,6 +598,29 @@ int32_t ServiceStub::CmdInitIncrementalBackupSession(MessageParcel &data, Messag return BError(BError::Codes::OK); } +int32_t ServiceStub::CmdInitIncrementalBackupSessionMsg(MessageParcel &data, MessageParcel &reply) +{ + auto remote = data.ReadRemoteObject(); + std::string errMsg; + if (!remote) { + return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive the reverse stub"); + } + auto iRemote = iface_cast(remote); + if (!iRemote) { + return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive the reverse stub"); + } + int32_t res = InitIncrementalBackupSession(iRemote, errMsg); + if (!reply.WriteString(errMsg)) { + return BError(BError::Codes::SA_BROKEN_IPC, "Failed to send the errMsg"); + } + if (!reply.WriteInt32(res)) { + stringstream ss; + ss << "Failed to send the result " << res; + return BError(BError::Codes::SA_BROKEN_IPC, ss.str()); + } + return BError(BError::Codes::OK); +} + int32_t ServiceStub::CmdAppendBundlesIncrementalBackupSession(MessageParcel &data, MessageParcel &reply) { vector bundlesToBackup; diff --git a/services/backup_sa/src/module_ipc/sub_service.cpp b/services/backup_sa/src/module_ipc/sub_service.cpp index 87da0624068e6cc7706c8836bd9dd8842219c2a8..e6d88a92c3ad9207dc03cad36dcb0f63addbdb57 100644 --- a/services/backup_sa/src/module_ipc/sub_service.cpp +++ b/services/backup_sa/src/module_ipc/sub_service.cpp @@ -680,11 +680,46 @@ ErrCode Service::InitRestoreSession(sptr remote, std::string &e session_->GetSessionCallerName(), session_->GetSessionActiveTime()); HILOGE("Active restore session error, Already have a session"); - StopAll(nullptr, true); return ret; } HILOGE("Active restore session error"); StopAll(nullptr, true); return ret; } + +ErrCode Service::InitBackupSession(sptr remote, std::string &errMsg) +{ + HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); + ErrCode ret = VerifyCaller(); + if (ret != ERR_OK) { + HILOGE("Init full backup session fail, verify caller failed"); + return ret; + } + int32_t oldSize = StorageMgrAdapter::UpdateMemPara(BConstants::BACKUP_VFS_CACHE_PRESSURE); + HILOGI("InitBackupSession oldSize %{public}d", oldSize); + session_->SetMemParaCurSize(oldSize); + ret = session_->Active({ + .clientToken = IPCSkeleton::GetCallingTokenID(), + .scenario = IServiceReverse::Scenario::BACKUP, + .clientProxy = remote, + .userId = GetUserIdDefault(), + .callerName = GetCallerName(), + .activeTime = TimeUtils::GetCurrentTime(), + }); + if (ret == ERR_OK) { + ClearFailedBundles(); + successBundlesNum_ = 0; + return ret; + } + if (ret == BError(BError::Codes::SA_SESSION_CONFLICT)) { + errMsg = BJsonUtil::BuildInitSessionErrInfo(session_->GetSessionUserId(), + session_->GetSessionCallerName(), + session_->GetSessionActiveTime()); + HILOGE("Active backup session error, Already have a session"); + return ret; + } + HILOGE("Active backup session error"); + StopAll(nullptr, true); + return ret; +} } \ No newline at end of file diff --git a/tests/mock/backup_kit_inner/service_proxy_mock.cpp b/tests/mock/backup_kit_inner/service_proxy_mock.cpp index 15c53566cd9059a292e8543ea1327466139669ba..158245f93e92a4e4062868024b1fb5b554bf9211 100644 --- a/tests/mock/backup_kit_inner/service_proxy_mock.cpp +++ b/tests/mock/backup_kit_inner/service_proxy_mock.cpp @@ -55,6 +55,14 @@ int32_t ServiceProxy::InitBackupSession(sptr remote) return 0; } +int32_t ServiceProxy::InitBackupSession(sptr remote, std::string &errMsg) +{ + if (!GetMockInitBackupOrRestoreSession()) { + return 1; + } + return 0; +} + ErrCode ServiceProxy::Start() { return BError(BError::Codes::OK); @@ -153,6 +161,11 @@ ErrCode ServiceProxy::InitIncrementalBackupSession(sptr remote) return BError(BError::Codes::OK); } +ErrCode ServiceProxy::InitIncrementalBackupSession(sptr remote, std::string &errMsg) +{ + return BError(BError::Codes::OK); +} + ErrCode ServiceProxy::AppendBundlesIncrementalBackupSession(const vector &bundlesToBackup) { return BError(BError::Codes::OK); diff --git a/tests/mock/module_ipc/service_mock.cpp b/tests/mock/module_ipc/service_mock.cpp index d197448539fb3aa471260060d82d50c73c9670d2..6218bb1376e0ea12c6e06af8d81446e4ff5e3ec4 100644 --- a/tests/mock/module_ipc/service_mock.cpp +++ b/tests/mock/module_ipc/service_mock.cpp @@ -62,6 +62,11 @@ ErrCode Service::InitBackupSession(sptr remote) return BError(BError::Codes::OK); } +ErrCode Service::InitBackupSession(sptr remote, std::string &errMsg) +{ + return BError(BError::Codes::OK); +} + ErrCode Service::Start() { return BError(BError::Codes::OK); @@ -197,6 +202,11 @@ ErrCode Service::InitIncrementalBackupSession(sptr remote) return BError(BError::Codes::OK); } +ErrCode Service::InitIncrementalBackupSession(sptr remote, std::string &errMsg) +{ + return BError(BError::Codes::OK); +} + ErrCode Service::AppendBundlesIncrementalBackupSession(const std::vector &bundlesToBackup) { return BError(BError::Codes::OK); diff --git a/tests/mock/module_ipc/service_stub_mock.cpp b/tests/mock/module_ipc/service_stub_mock.cpp index c429636896b4da04ce848de39524935298f82b68..a58ee7f8543aaee29463119fbe89386401b70cd8 100644 --- a/tests/mock/module_ipc/service_stub_mock.cpp +++ b/tests/mock/module_ipc/service_stub_mock.cpp @@ -87,6 +87,10 @@ void ServiceStub::ServiceStubSupplement() &ServiceStub::CmdReportAppProcessInfo; opToInterfaceMap_[static_cast(IServiceInterfaceCode::SERVICE_CMD_INIT_RESTORE_SESSION_MSG)] = &ServiceStub::CmdInitRestoreSessionMsg; + opToInterfaceMap_[static_cast(IServiceInterfaceCode::SERVICE_CMD_INIT_BACKUP_SESSION_MSG)] = + &ServiceStub::CmdInitBackupSessionMsg; + opToInterfaceMap_[static_cast(IServiceInterfaceCode::SERVICE_CMD_INIT_INCREMENTAL_BACKUP_SESSION_MSG)] = + &ServiceStub::CmdInitIncrementalBackupSessionMsg; opToInterfaceMap_[static_cast(IServiceInterfaceCode::SERVICE_CMD_CANCEL_BUNDLE)] = &ServiceStub::CmdCancel; } @@ -137,6 +141,17 @@ int32_t ServiceStub::CmdInitBackupSession(MessageParcel &data, MessageParcel &re return BError(BError::Codes::OK); } +int32_t ServiceStub::CmdInitBackupSessionMsg(MessageParcel &data, MessageParcel &reply) +{ + auto remote = data.ReadRemoteObject(); + auto iremote = iface_cast(remote); + std::string errMsg; + int res = InitBackupSession(iremote, errMsg); + reply.WriteString(errMsg); + reply.WriteInt32(res); + return BError(BError::Codes::OK); +} + int32_t ServiceStub::CmdStart(MessageParcel &data, MessageParcel &reply) { int res = Start(); @@ -319,6 +334,11 @@ int32_t ServiceStub::CmdInitIncrementalBackupSession(MessageParcel &data, Messag return BError(BError::Codes::OK); } +int32_t ServiceStub::CmdInitIncrementalBackupSessionMsg(MessageParcel &data, MessageParcel &reply) +{ + return BError(BError::Codes::OK); +} + int32_t ServiceStub::CmdAppendBundlesIncrementalBackupSession(MessageParcel &data, MessageParcel &reply) { return BError(BError::Codes::OK); diff --git a/tests/unittests/backup_api/backup_impl/include/i_service_mock.h b/tests/unittests/backup_api/backup_impl/include/i_service_mock.h index f923e04ae3e227fb93894f8a2f4c73f1fd7ad750..28afe7f10945cf1e08fe1951b7802af545e1656d 100644 --- a/tests/unittests/backup_api/backup_impl/include/i_service_mock.h +++ b/tests/unittests/backup_api/backup_impl/include/i_service_mock.h @@ -77,6 +77,11 @@ public: return BError(BError::Codes::OK); } + ErrCode InitBackupSession(sptr remote, std::string &errMsg) override + { + return BError(BError::Codes::OK); + } + ErrCode Start() override { return BError(BError::Codes::OK); @@ -207,6 +212,11 @@ public: return BError(BError::Codes::OK); } + ErrCode InitIncrementalBackupSession(sptr remote, std::string &errMsg) override + { + return BError(BError::Codes::OK); + } + ErrCode AppendBundlesIncrementalBackupSession(const std::vector &bundlesToBackup) override { return BError(BError::Codes::OK); diff --git a/tests/unittests/backup_sa/module_ipc/service_incremental_test.cpp b/tests/unittests/backup_sa/module_ipc/service_incremental_test.cpp index c78dd646994fe17e1191af0d8ae5fc75b8df3fa1..b087868c1b24acba066937c728ced59763f3a4ae 100644 --- a/tests/unittests/backup_sa/module_ipc/service_incremental_test.cpp +++ b/tests/unittests/backup_sa/module_ipc/service_incremental_test.cpp @@ -62,6 +62,11 @@ ErrCode Service::InitBackupSession(sptr remote) return BError(BError::Codes::OK); } +ErrCode Service::InitBackupSession(sptr remote, std::string &errMsg) +{ + return BError(BError::Codes::OK); +} + ErrCode Service::Start() { return BError(BError::Codes::OK); diff --git a/tests/unittests/backup_sa/module_ipc/service_other_test.cpp b/tests/unittests/backup_sa/module_ipc/service_other_test.cpp index f686a5b3483f738aa1e7511073eed714711d06a9..e4e235dcc4813354f48e44b1f8b5a105b6515eaa 100644 --- a/tests/unittests/backup_sa/module_ipc/service_other_test.cpp +++ b/tests/unittests/backup_sa/module_ipc/service_other_test.cpp @@ -67,6 +67,11 @@ ErrCode Service::InitIncrementalBackupSession(sptr) return BError(BError::Codes::OK); } +ErrCode Service::InitIncrementalBackupSession(sptr, std::string &) +{ + return BError(BError::Codes::OK); +} + vector Service::GetBundleNameByDetails(const std::vector&) { return {}; diff --git a/tests/unittests/backup_sa/module_ipc/service_stub_test.cpp b/tests/unittests/backup_sa/module_ipc/service_stub_test.cpp index 353ed10841ef71a3cccb8d0e21f1da7bf2543da5..ce27e56c4a4acba2a9f754e77be48d2c3f15e4de 100644 --- a/tests/unittests/backup_sa/module_ipc/service_stub_test.cpp +++ b/tests/unittests/backup_sa/module_ipc/service_stub_test.cpp @@ -46,6 +46,7 @@ public: MOCK_METHOD1(InitRestoreSession, ErrCode(sptr remote)); MOCK_METHOD2(InitRestoreSession, ErrCode(sptr remote, std::string &errMsg)); MOCK_METHOD1(InitBackupSession, ErrCode(sptr remote)); + MOCK_METHOD2(InitBackupSession, ErrCode(sptr remote, std::string &errMsg)); MOCK_METHOD0(Start, ErrCode()); MOCK_METHOD0(GetLocalCapabilities, UniqueFd()); MOCK_METHOD1(PublishFile, ErrCode(const BFileInfo &fileInfo)); @@ -70,6 +71,7 @@ public: MOCK_METHOD1(GetLocalCapabilitiesIncremental, UniqueFd(const std::vector &bundleNames)); MOCK_METHOD0(GetAppLocalListAndDoIncrementalBackup, ErrCode()); MOCK_METHOD1(InitIncrementalBackupSession, ErrCode(sptr remote)); + MOCK_METHOD2(InitIncrementalBackupSession, ErrCode(sptr remote, std::string &errMsg)); MOCK_METHOD1(AppendBundlesIncrementalBackupSession, ErrCode(const std::vector &bundlesToBackup)); MOCK_METHOD2(AppendBundlesIncrementalBackupSession, ErrCode(const std::vector &bundlesToBackup, const std::vector &infos)); diff --git a/tests/unittests/backup_sa/session/service_proxy_mock.cpp b/tests/unittests/backup_sa/session/service_proxy_mock.cpp index d75c3196a8cf6ab5c58835646f0b8f8669515a36..ebeddd7aa1487b4006061918915cc33e021085a6 100644 --- a/tests/unittests/backup_sa/session/service_proxy_mock.cpp +++ b/tests/unittests/backup_sa/session/service_proxy_mock.cpp @@ -35,6 +35,11 @@ int32_t ServiceProxy::InitBackupSession(sptr remote) return 0; } +int32_t ServiceProxy::InitBackupSession(sptr remote, std::string &errMsg) +{ + return 0; +} + ErrCode ServiceProxy::Start() { return BError(BError::Codes::OK); @@ -127,6 +132,11 @@ ErrCode ServiceProxy::InitIncrementalBackupSession(sptr remote) return BError(BError::Codes::OK); } +ErrCode ServiceProxy::InitIncrementalBackupSession(sptr remote, std::string &errMsg) +{ + return BError(BError::Codes::OK); +} + ErrCode ServiceProxy::AppendBundlesIncrementalBackupSession(const vector &bundlesToBackup) { return BError(BError::Codes::OK);