From fa7da4fb9a8b7021d304e6870b1442f5f3b2e2fb Mon Sep 17 00:00:00 2001 From: huaqingsimeng <1004904143@qq.com> Date: Fri, 24 Mar 2023 06:13:24 +0000 Subject: [PATCH] =?UTF-8?q?=E5=A4=87=E4=BB=BD=E6=81=A2=E5=A4=8D=E6=8F=90?= =?UTF-8?q?=E4=BE=9Bjs=E6=8E=A5=E5=8F=A3=E8=83=BD=E5=8A=9B-=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E7=AB=AF=E6=96=B0=E5=A2=9Eipc=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: huaqingsimeng --- .../backup_kit_inner/src/b_session_backup.cpp | 22 ++- .../src/b_session_restore.cpp | 28 ++- .../backup_kit_inner/src/service_proxy.cpp | 176 +++++++++++++----- .../backup_kit_inner/impl/b_session_backup.h | 15 ++ .../backup_kit_inner/impl/b_session_restore.h | 16 ++ .../native/backup_kit_inner/impl/i_service.h | 9 +- .../backup_kit_inner/impl/service_proxy.h | 3 + .../backup_sa/include/module_ipc/service.h | 3 + .../include/module_ipc/service_stub.h | 3 + .../include/module_ipc/svc_session_manager.h | 13 +- services/backup_sa/src/module_ipc/service.cpp | 30 ++- .../backup_sa/src/module_ipc/service_stub.cpp | 54 +++++- .../src/module_ipc/svc_session_manager.cpp | 10 + .../backup_kit_inner/service_proxy_mock.cpp | 15 ++ tests/mock/module_ipc/service_mock.cpp | 15 ++ tests/mock/module_ipc/service_stub_mock.cpp | 47 +++++ .../module_ipc/svc_session_manager_mock.cpp | 2 + .../backup_impl/include/i_service_mock.h | 15 ++ .../module_ipc/service_stub_test.cpp | 3 + 19 files changed, 417 insertions(+), 62 deletions(-) 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 7da9c9716..976336618 100644 --- a/frameworks/native/backup_kit_inner/src/b_session_backup.cpp +++ b/frameworks/native/backup_kit_inner/src/b_session_backup.cpp @@ -89,9 +89,29 @@ ErrCode BSessionBackup::Start() { auto proxy = ServiceProxy::GetInstance(); if (proxy == nullptr) { - return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to proxy because of is empty").GetCode(); + return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode(); } return proxy->Start(); } + +ErrCode BSessionBackup::AppendBundles(vector bundlesToBackup) +{ + auto proxy = ServiceProxy::GetInstance(); + if (proxy == nullptr) { + return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode(); + } + + return proxy->AppendBundlesBackupSession(bundlesToBackup); +} + +ErrCode BSessionBackup::Finish() +{ + auto proxy = ServiceProxy::GetInstance(); + if (proxy == nullptr) { + return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode(); + } + + return proxy->Finish(); +} } // namespace OHOS::FileManagement::Backup \ No newline at end of file diff --git a/frameworks/native/backup_kit_inner/src/b_session_restore.cpp b/frameworks/native/backup_kit_inner/src/b_session_restore.cpp index 34ef872a5..6fc1db385 100644 --- a/frameworks/native/backup_kit_inner/src/b_session_restore.cpp +++ b/frameworks/native/backup_kit_inner/src/b_session_restore.cpp @@ -67,7 +67,7 @@ UniqueFd BSessionRestore::GetLocalCapabilities() { auto proxy = ServiceProxy::GetInstance(); if (proxy == nullptr) { - HILOGI("Failed to proxy because of is empty"); + HILOGI("Failed to get backup service"); return UniqueFd(-1); } return UniqueFd(proxy->GetLocalCapabilities()); @@ -77,7 +77,7 @@ ErrCode BSessionRestore::PublishFile(BFileInfo fileInfo) { auto proxy = ServiceProxy::GetInstance(); if (proxy == nullptr) { - return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to proxy because of is empty").GetCode(); + return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode(); } return proxy->PublishFile(fileInfo); } @@ -86,7 +86,7 @@ ErrCode BSessionRestore::Start() { auto proxy = ServiceProxy::GetInstance(); if (proxy == nullptr) { - return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to proxy because of is empty").GetCode(); + return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode(); } return proxy->Start(); @@ -96,12 +96,32 @@ ErrCode BSessionRestore::GetExtFileName(string &bundleName, string &fileName) { auto proxy = ServiceProxy::GetInstance(); if (proxy == nullptr) { - return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to proxy because of is empty").GetCode(); + return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode(); } return proxy->GetExtFileName(bundleName, fileName); } +ErrCode BSessionRestore::AppendBundles(UniqueFd remoteCap, vector bundlesToRestore) +{ + auto proxy = ServiceProxy::GetInstance(); + if (proxy == nullptr) { + return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode(); + } + + return proxy->AppendBundlesRestoreSession(move(remoteCap), bundlesToRestore); +} + +ErrCode BSessionRestore::Finish() +{ + auto proxy = ServiceProxy::GetInstance(); + if (proxy == nullptr) { + return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode(); + } + + return proxy->Finish(); +} + void BSessionRestore::RegisterBackupServiceDied(std::function functor) { auto proxy = ServiceProxy::GetInstance(); diff --git a/frameworks/native/backup_kit_inner/src/service_proxy.cpp b/frameworks/native/backup_kit_inner/src/service_proxy.cpp index 1bc9826b1..e35abfd43 100644 --- a/frameworks/native/backup_kit_inner/src/service_proxy.cpp +++ b/frameworks/native/backup_kit_inner/src/service_proxy.cpp @@ -15,10 +15,8 @@ #include "service_proxy.h" -#include -#include - #include "b_error/b_error.h" +#include "b_error/b_excep_utils.h" #include "b_resources/b_constants.h" #include "filemgmt_libhilog.h" #include "iservice_registry.h" @@ -26,12 +24,16 @@ namespace OHOS::FileManagement::Backup { using namespace std; +using namespace BExcepUltils; int32_t ServiceProxy::InitRestoreSession(sptr remote, const std::vector &bundleNames) { - HILOGI("Start"); + HILOGI("Begin"); + BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr"); MessageParcel data; - data.WriteInterfaceToken(GetDescriptor()); + if (!data.WriteInterfaceToken(GetDescriptor())) { + return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write descriptor").GetCode(); + } MessageParcel reply; MessageOption option; @@ -48,11 +50,9 @@ int32_t ServiceProxy::InitRestoreSession(sptr remote, const std int32_t ret = Remote()->SendRequest(IService::SERVICE_CMD_INIT_RESTORE_SESSION, data, reply, option); if (ret != NO_ERROR) { - stringstream ss; - ss << "Failed to send out the request for " << ret; - return BError(BError::Codes::SDK_INVAL_ARG, ss.str()).GetCode(); + string str = "Failed to send out the request because of " + to_string(ret); + return BError(BError::Codes::SDK_INVAL_ARG, str.data()).GetCode(); } - HILOGI("Successful"); return reply.ReadInt32(); } @@ -60,9 +60,12 @@ int32_t ServiceProxy::InitBackupSession(sptr remote, UniqueFd fd, const std::vector &bundleNames) { - HILOGI("Start"); + HILOGI("Begin"); + BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr"); MessageParcel data; - data.WriteInterfaceToken(GetDescriptor()); + if (!data.WriteInterfaceToken(GetDescriptor())) { + return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write descriptor").GetCode(); + } MessageParcel reply; MessageOption option; @@ -82,38 +85,40 @@ int32_t ServiceProxy::InitBackupSession(sptr remote, int32_t ret = Remote()->SendRequest(IService::SERVICE_CMD_INIT_BACKUP_SESSION, data, reply, option); if (ret != NO_ERROR) { - stringstream ss; - ss << "Failed to send out the request because of " << ret; - return BError(BError::Codes::SDK_INVAL_ARG, ss.str()).GetCode(); + string str = "Failed to send out the request because of " + to_string(ret); + return BError(BError::Codes::SDK_INVAL_ARG, str.data()).GetCode(); } - HILOGI("Successful"); return reply.ReadInt32(); } ErrCode ServiceProxy::Start() { - HILOGI("Start"); + HILOGI("Begin"); + BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr"); MessageParcel data; - data.WriteInterfaceToken(GetDescriptor()); + if (!data.WriteInterfaceToken(GetDescriptor())) { + return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write descriptor").GetCode(); + } MessageParcel reply; MessageOption option; int32_t ret = Remote()->SendRequest(IService::SERVICE_CMD_START, data, reply, option); if (ret != NO_ERROR) { - stringstream ss; - ss << "Failed to send out the request because of " << ret; - return BError(BError::Codes::SDK_INVAL_ARG, ss.str()).GetCode(); + string str = "Failed to send out the request because of " + to_string(ret); + return BError(BError::Codes::SDK_INVAL_ARG, str.data()).GetCode(); } - - HILOGI("Successful"); return reply.ReadInt32(); } UniqueFd ServiceProxy::GetLocalCapabilities() { - HILOGI("Start"); + HILOGI("Begin"); + BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr"); MessageParcel data; - data.WriteInterfaceToken(GetDescriptor()); + if (!data.WriteInterfaceToken(GetDescriptor())) { + HILOGE("Failed to write descriptor"); + return UniqueFd(-EPERM); + } MessageParcel reply; MessageOption option; @@ -122,17 +127,18 @@ UniqueFd ServiceProxy::GetLocalCapabilities() HILOGE("Received error %{public}d when doing IPC", ret); return UniqueFd(-ret); } - - HILOGI("Successful"); UniqueFd fd(reply.ReadFileDescriptor()); return UniqueFd(fd.Release()); } ErrCode ServiceProxy::PublishFile(const BFileInfo &fileInfo) { - HILOGI("Start"); + HILOGI("Begin"); + BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr"); MessageParcel data; - data.WriteInterfaceToken(GetDescriptor()); + if (!data.WriteInterfaceToken(GetDescriptor())) { + return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write descriptor").GetCode(); + } if (!data.WriteParcelable(&fileInfo)) { HILOGE("Failed to send the fileInfo"); @@ -143,20 +149,20 @@ ErrCode ServiceProxy::PublishFile(const BFileInfo &fileInfo) MessageOption option; int32_t ret = Remote()->SendRequest(IService::SERVICE_CMD_PUBLISH_FILE, data, reply, option); if (ret != NO_ERROR) { - stringstream ss; - ss << "Failed to send out the request because of " << ret; - return BError(BError::Codes::SDK_INVAL_ARG, ss.str()).GetCode(); + string str = "Failed to send out the request because of " + to_string(ret); + return BError(BError::Codes::SDK_INVAL_ARG, str.data()).GetCode(); } - - HILOGI("Successful"); return reply.ReadInt32(); } ErrCode ServiceProxy::AppFileReady(const string &fileName, UniqueFd fd) { - HILOGI("Start"); + HILOGI("Begin"); + BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr"); MessageParcel data; - data.WriteInterfaceToken(GetDescriptor()); + if (!data.WriteInterfaceToken(GetDescriptor())) { + return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write descriptor").GetCode(); + } if (!data.WriteString(fileName)) { return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send the filename").GetCode(); @@ -169,19 +175,20 @@ ErrCode ServiceProxy::AppFileReady(const string &fileName, UniqueFd fd) MessageOption option; int32_t ret = Remote()->SendRequest(IService::SERVICE_CMD_APP_FILE_READY, data, reply, option); if (ret != NO_ERROR) { - stringstream ss; - ss << "Failed to send out the request because of " << ret; - return BError(BError::Codes::SDK_INVAL_ARG, ss.str()).GetCode(); + string str = "Failed to send out the request because of " + to_string(ret); + return BError(BError::Codes::SDK_INVAL_ARG, str.data()).GetCode(); } - HILOGI("Successful"); return reply.ReadInt32(); } ErrCode ServiceProxy::AppDone(ErrCode errCode) { - HILOGI("Start"); + HILOGI("Begin"); + BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr"); MessageParcel data; - data.WriteInterfaceToken(GetDescriptor()); + if (!data.WriteInterfaceToken(GetDescriptor())) { + return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write descriptor").GetCode(); + } if (!data.WriteInt32(errCode)) { return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send the errCode").GetCode(); @@ -191,19 +198,20 @@ ErrCode ServiceProxy::AppDone(ErrCode errCode) MessageOption option; int32_t ret = Remote()->SendRequest(IService::SERVICE_CMD_APP_DONE, data, reply, option); if (ret != NO_ERROR) { - stringstream ss; - ss << "Failed to send out the request because of " << ret; - return BError(BError::Codes::SDK_INVAL_ARG, ss.str()).GetCode(); + string str = "Failed to send out the request because of " + to_string(ret); + return BError(BError::Codes::SDK_INVAL_ARG, str.data()).GetCode(); } - HILOGI("Successful"); return reply.ReadInt32(); } ErrCode ServiceProxy::GetExtFileName(string &bundleName, string &fileName) { - HILOGI("Start"); + HILOGI("Begin"); + BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr"); MessageParcel data; - data.WriteInterfaceToken(GetDescriptor()); + if (!data.WriteInterfaceToken(GetDescriptor())) { + return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write descriptor").GetCode(); + } if (!data.WriteString(bundleName)) { return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send the bundleName").GetCode(); @@ -217,14 +225,80 @@ ErrCode ServiceProxy::GetExtFileName(string &bundleName, string &fileName) option.SetFlags(MessageOption::TF_ASYNC); int32_t ret = Remote()->SendRequest(IService::SERVICE_CMD_GET_EXT_FILE_NAME, data, reply, option); if (ret != NO_ERROR) { - stringstream ss; - ss << "Failed to send out the request because of " << ret; - BError(BError::Codes::SDK_INVAL_ARG, ss.str()); + string str = "Failed to send out the request because of " + to_string(ret); + return BError(BError::Codes::SDK_INVAL_ARG, str.data()).GetCode(); } - HILOGI("Successful"); return ret; } +ErrCode ServiceProxy::AppendBundlesRestoreSession(UniqueFd fd, const vector &bundleNames) +{ + HILOGI("Begin"); + 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 (!data.WriteFileDescriptor(fd)) { + return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send the fd").GetCode(); + } + if (!data.WriteStringVector(bundleNames)) { + return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send bundleNames").GetCode(); + } + + int32_t ret = Remote()->SendRequest(IService::SERVICE_CMD_APPEND_BUNDLES_RESTORE_SESSION, 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(); + } + return reply.ReadInt32(); +} + +ErrCode ServiceProxy::AppendBundlesBackupSession(const vector &bundleNames) +{ + HILOGI("Begin"); + 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 (!data.WriteStringVector(bundleNames)) { + return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send bundleNames").GetCode(); + } + + int32_t ret = Remote()->SendRequest(IService::SERVICE_CMD_APPEND_BUNDLES_BACKUP_SESSION, 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(); + } + return reply.ReadInt32(); +} + +ErrCode ServiceProxy::Finish() +{ + HILOGI("Begin"); + 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; + int32_t ret = Remote()->SendRequest(IService::SERVICE_CMD_FINISH, 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(); + } + return reply.ReadInt32(); +} + sptr ServiceProxy::GetInstance() { unique_lock lock(proxyMutex_); 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 d5e40d47d..7ab3e2519 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 @@ -48,6 +48,21 @@ public: std::vector bundlesToBackup, Callbacks callbacks); + /** + * @brief 用于追加应用,现阶段仅支持在Start之前调用 + * + * @param bundlesToBackup 待备份的应用清单 + * @return ErrCode 规范错误码 + */ + ErrCode AppendBundles(std::vector bundlesToBackup); + + /** + * @brief 用于结束追加应用,结束后不可在调用AppendBundles + * + * @return ErrCode 规范错误码 + */ + ErrCode Finish(); + /** * @brief 用于启动备份流程 * diff --git a/interfaces/inner_api/native/backup_kit_inner/impl/b_session_restore.h b/interfaces/inner_api/native/backup_kit_inner/impl/b_session_restore.h index 33b38274e..308084eae 100644 --- a/interfaces/inner_api/native/backup_kit_inner/impl/b_session_restore.h +++ b/interfaces/inner_api/native/backup_kit_inner/impl/b_session_restore.h @@ -70,6 +70,22 @@ public: */ ErrCode GetExtFileName(std::string &bundleName, std::string &fileName); + /** + * @brief 用于追加应用,现阶段仅支持在Start之前调用 + * + * @param remoteCap 已打开的保存远端设备能力的Json文件。可使用GetLocalCapabilities方法获取 + * @param bundlesToRestore 待恢复的应用清单 + * @return ErrCode 规范错误码 + */ + ErrCode AppendBundles(UniqueFd remoteCap, std::vector bundlesToRestore); + + /** + * @brief 用于结束追加应用,结束后不可在调用AppendBundles + * + * @return ErrCode 规范错误码 + */ + ErrCode Finish(); + /** * @brief 用于启动恢复流程 * 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 bc240c71b..3dbe01b78 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 @@ -19,10 +19,11 @@ #include #include +#include + #include "b_file_info.h" #include "i_service_reverse.h" #include "iremote_broker.h" -#include "unique_fd.h" namespace OHOS::FileManagement::Backup { class IService : public IRemoteBroker { @@ -36,6 +37,9 @@ public: SERVICE_CMD_APP_DONE, SERVICE_CMD_START, SERVICE_CMD_GET_EXT_FILE_NAME, + SERVICE_CMD_APPEND_BUNDLES_RESTORE_SESSION, + SERVICE_CMD_APPEND_BUNDLES_BACKUP_SESSION, + SERVICE_CMD_FINISH, }; virtual ErrCode InitRestoreSession(sptr remote, const std::vector &bundleNames) = 0; @@ -48,6 +52,9 @@ public: virtual ErrCode AppFileReady(const std::string &fileName, UniqueFd fd) = 0; virtual ErrCode AppDone(ErrCode errCode) = 0; virtual ErrCode GetExtFileName(std::string &bundleName, std::string &fileName) = 0; + virtual ErrCode AppendBundlesRestoreSession(UniqueFd fd, const std::vector &bundleNames) = 0; + virtual ErrCode AppendBundlesBackupSession(const std::vector &bundleNames) = 0; + virtual ErrCode Finish() = 0; DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.Filemanagement.Backup.IService") }; 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 9b3c8688c..18ef4a0f3 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 @@ -38,6 +38,9 @@ public: ErrCode AppFileReady(const std::string &fileName, UniqueFd fd) override; ErrCode AppDone(ErrCode errCode) override; ErrCode GetExtFileName(std::string &bundleName, std::string &fileName) override; + ErrCode AppendBundlesRestoreSession(UniqueFd fd, const std::vector &bundleNames) override; + ErrCode AppendBundlesBackupSession(const std::vector &bundleNames) override; + ErrCode Finish() override; public: explicit ServiceProxy(const sptr &impl) : IRemoteProxy(impl) {} diff --git a/services/backup_sa/include/module_ipc/service.h b/services/backup_sa/include/module_ipc/service.h index 1a1ba2dec..5b771775c 100644 --- a/services/backup_sa/include/module_ipc/service.h +++ b/services/backup_sa/include/module_ipc/service.h @@ -42,6 +42,9 @@ public: ErrCode AppFileReady(const std::string &fileName, UniqueFd fd) override; ErrCode AppDone(ErrCode errCode) override; ErrCode GetExtFileName(std::string &bundleName, std::string &fileName) override; + ErrCode AppendBundlesRestoreSession(UniqueFd fd, const std::vector &bundleNames) override; + ErrCode AppendBundlesBackupSession(const std::vector &bundleNames) override; + ErrCode Finish() override; // 以下都是非IPC接口 public: diff --git a/services/backup_sa/include/module_ipc/service_stub.h b/services/backup_sa/include/module_ipc/service_stub.h index fb2e0782b..fb0fc4eb8 100644 --- a/services/backup_sa/include/module_ipc/service_stub.h +++ b/services/backup_sa/include/module_ipc/service_stub.h @@ -42,6 +42,9 @@ private: int32_t CmdAppFileReady(MessageParcel &data, MessageParcel &reply); int32_t CmdAppDone(MessageParcel &data, MessageParcel &reply); int32_t CmdGetExtFileName(MessageParcel &data, MessageParcel &reply); + int32_t CmdAppendBundlesRestoreSession(MessageParcel &data, MessageParcel &reply); + int32_t CmdAppendBundlesBackupSession(MessageParcel &data, MessageParcel &reply); + int32_t CmdFinish(MessageParcel &data, MessageParcel &reply); }; } // namespace OHOS::FileManagement::Backup 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 48c2b3fba..0ec0f863c 100644 --- a/services/backup_sa/include/module_ipc/svc_session_manager.h +++ b/services/backup_sa/include/module_ipc/svc_session_manager.h @@ -24,10 +24,11 @@ #include #include -#include #include #include +#include + #include "b_file_info.h" #include "b_resources/b_constants.h" #include "bundlemgr/bundle_mgr_interface.h" @@ -53,6 +54,7 @@ public: IServiceReverse::Scenario scenario {IServiceReverse::Scenario::UNDEFINED}; std::map backupExtNameMap; sptr clientProxy; + bool isAppendFinish {false}; }; public: @@ -197,6 +199,13 @@ public: */ std::string GetBackupExtName(const std::string &bundleName); + /** + * @brief 结束追加应用 + * + * @return ErrCode + */ + void Finish(); + private: /** * @brief 校验BundleName和ability type 并补全Impl.backupExtNameMap信息 @@ -240,7 +249,7 @@ public: private: sptr GetBundleManager(); - + mutable std::shared_mutex lock_; wptr reversePtr_; sptr deathRecipient_; diff --git a/services/backup_sa/src/module_ipc/service.cpp b/services/backup_sa/src/module_ipc/service.cpp index b6debaa69..5869f9611 100644 --- a/services/backup_sa/src/module_ipc/service.cpp +++ b/services/backup_sa/src/module_ipc/service.cpp @@ -245,6 +245,34 @@ ErrCode Service::Start() } } +ErrCode Service::AppendBundlesRestoreSession(UniqueFd fd, const vector &bundleNames) +{ + HILOGI("Begin"); + VerifyCaller(session_->GetScenario()); + BJsonCachedEntity cachedEntity(move(fd)); + auto cache = cachedEntity.Structuralize(); + uint64_t size = cache.GetFreeDiskSpace(); + if (size == 0) { + throw BError(BError::Codes::SA_INVAL_ARG, "Invalid field FreeDiskSpace or unsufficient space"); + } + return BError(BError::Codes::OK); +} + +ErrCode Service::AppendBundlesBackupSession(const vector &bundleNames) +{ + HILOGI("Begin"); + VerifyCaller(session_->GetScenario()); + return BError(BError::Codes::OK); +} + +ErrCode Service::Finish() +{ + HILOGI("Begin"); + VerifyCaller(session_->GetScenario()); + session_->Finish(); + return BError(BError::Codes::OK); +} + ErrCode Service::PublishFile(const BFileInfo &fileInfo) { try { @@ -281,7 +309,7 @@ ErrCode Service::PublishFile(const BFileInfo &fileInfo) ErrCode Service::AppFileReady(const string &fileName, UniqueFd fd) { try { - HILOGE("begin %{public}s", fileName.data()); + HILOGI("Begin"); string callerName = VerifyCallerAndGetCallerName(); if (!regex_match(fileName, regex("^[0-9a-zA-Z_.]+$"))) { throw BError(BError::Codes::SA_INVAL_ARG, "Filename is not alphanumeric"); diff --git a/services/backup_sa/src/module_ipc/service_stub.cpp b/services/backup_sa/src/module_ipc/service_stub.cpp index 21f25220f..4c7b48bf3 100644 --- a/services/backup_sa/src/module_ipc/service_stub.cpp +++ b/services/backup_sa/src/module_ipc/service_stub.cpp @@ -24,11 +24,13 @@ #include #include "b_error/b_error.h" +#include "b_error/b_excep_utils.h" #include "filemgmt_libhilog.h" #include "module_ipc/service_reverse_proxy.h" namespace OHOS::FileManagement::Backup { using namespace std; +using namespace BExcepUltils; ServiceStub::ServiceStub() { @@ -40,6 +42,9 @@ ServiceStub::ServiceStub() opToInterfaceMap_[SERVICE_CMD_APP_DONE] = &ServiceStub::CmdAppDone; opToInterfaceMap_[SERVICE_CMD_START] = &ServiceStub::CmdStart; opToInterfaceMap_[SERVICE_CMD_GET_EXT_FILE_NAME] = &ServiceStub::CmdGetExtFileName; + opToInterfaceMap_[SERVICE_CMD_APPEND_BUNDLES_RESTORE_SESSION] = &ServiceStub::CmdAppendBundlesRestoreSession; + opToInterfaceMap_[SERVICE_CMD_APPEND_BUNDLES_BACKUP_SESSION] = &ServiceStub::CmdAppendBundlesBackupSession; + opToInterfaceMap_[SERVICE_CMD_FINISH] = &ServiceStub::CmdFinish; } int32_t ServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) @@ -57,7 +62,7 @@ int32_t ServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, Message return IPCObjectStub::OnRemoteRequest(code, data, reply, option); } - return (this->*(interfaceIndex->second))(data, reply); + return ExceptionCatcherLocked([&]() { return ErrCode((this->*(interfaceIndex->second))(data, reply)); }); } int32_t ServiceStub::CmdInitRestoreSession(MessageParcel &data, MessageParcel &reply) @@ -94,7 +99,7 @@ int32_t ServiceStub::CmdInitBackupSession(MessageParcel &data, MessageParcel &re return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive the reverse stub"); } auto iremote = iface_cast(remote); - if (!remote) { + if (!iremote) { return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive the reverse stub"); } @@ -205,4 +210,49 @@ int32_t ServiceStub::CmdGetExtFileName(MessageParcel &data, MessageParcel &reply return GetExtFileName(bundleName, fileName); } + +int32_t ServiceStub::CmdAppendBundlesRestoreSession(MessageParcel &data, MessageParcel &reply) +{ + HILOGI("Begin"); + UniqueFd fd(data.ReadFileDescriptor()); + if (fd < 0) { + return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive fd"); + } + + vector bundleNames; + if (!data.ReadStringVector(&bundleNames)) { + return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive bundleNames"); + } + + int res = AppendBundlesRestoreSession(move(fd), bundleNames); + if (!reply.WriteInt32(res)) { + return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to send the result ") + to_string(res)); + } + return BError(BError::Codes::OK); +} + +int32_t ServiceStub::CmdAppendBundlesBackupSession(MessageParcel &data, MessageParcel &reply) +{ + HILOGI("Begin"); + vector bundleNames; + if (!data.ReadStringVector(&bundleNames)) { + return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive bundleNames"); + } + + int32_t res = AppendBundlesBackupSession(bundleNames); + if (!reply.WriteInt32(res)) { + return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to send the result ") + to_string(res)); + } + return BError(BError::Codes::OK); +} + +int32_t ServiceStub::CmdFinish(MessageParcel &data, MessageParcel &reply) +{ + HILOGI("Begin"); + int res = Finish(); + if (!reply.WriteInt32(res)) { + return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to send the result ") + to_string(res)); + } + return BError(BError::Codes::OK); +} } // namespace OHOS::FileManagement::Backup 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 c508315a4..db963091f 100644 --- a/services/backup_sa/src/module_ipc/svc_session_manager.cpp +++ b/services/backup_sa/src/module_ipc/svc_session_manager.cpp @@ -421,4 +421,14 @@ sptr SvcSessionManager::GetBundleManager() return iface_cast(bundleObj); } + +void SvcSessionManager::Finish() +{ + HILOGI("Begin"); + unique_lock lock(lock_); + if (!impl_.clientToken) { + throw BError(BError::Codes::SA_INVAL_ARG, "No caller token was specified"); + } + impl_.isAppendFinish = true; +} } // namespace OHOS::FileManagement::Backup \ 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 5c4bdc226..7ce08cae2 100644 --- a/tests/mock/backup_kit_inner/service_proxy_mock.cpp +++ b/tests/mock/backup_kit_inner/service_proxy_mock.cpp @@ -82,6 +82,21 @@ ErrCode ServiceProxy::GetExtFileName(string &bundleName, string &fileName) return BError(BError::Codes::OK); } +ErrCode ServiceProxy::AppendBundlesRestoreSession(UniqueFd fd, const vector &bundleNames) +{ + return BError(BError::Codes::OK); +} + +ErrCode ServiceProxy::AppendBundlesBackupSession(const vector &bundleNames) +{ + return BError(BError::Codes::OK); +} + +ErrCode ServiceProxy::Finish() +{ + return BError(BError::Codes::OK); +} + sptr ServiceProxy::GetInstance() { if (!GetMockGetInstance()) { diff --git a/tests/mock/module_ipc/service_mock.cpp b/tests/mock/module_ipc/service_mock.cpp index c324bd3fe..f3f124e8b 100644 --- a/tests/mock/module_ipc/service_mock.cpp +++ b/tests/mock/module_ipc/service_mock.cpp @@ -72,6 +72,21 @@ ErrCode Service::AppDone(ErrCode errCode) return BError(BError::Codes::OK); } +ErrCode Service::AppendBundlesRestoreSession(UniqueFd fd, const std::vector &bundleNames) +{ + return BError(BError::Codes::OK); +} + +ErrCode Service::AppendBundlesBackupSession(const std::vector &bundleNames) +{ + return BError(BError::Codes::OK); +} + +ErrCode Service::Finish() +{ + return BError(BError::Codes::OK); +} + ErrCode Service::LaunchBackupExtension(const BundleName &bundleName) { 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 313a9d2e2..aa843d04c 100644 --- a/tests/mock/module_ipc/service_stub_mock.cpp +++ b/tests/mock/module_ipc/service_stub_mock.cpp @@ -33,6 +33,9 @@ ServiceStub::ServiceStub() opToInterfaceMap_[SERVICE_CMD_APP_DONE] = &ServiceStub::CmdAppDone; opToInterfaceMap_[SERVICE_CMD_START] = &ServiceStub::CmdStart; opToInterfaceMap_[SERVICE_CMD_GET_EXT_FILE_NAME] = &ServiceStub::CmdGetExtFileName; + opToInterfaceMap_[SERVICE_CMD_APPEND_BUNDLES_RESTORE_SESSION] = &ServiceStub::CmdAppendBundlesRestoreSession; + opToInterfaceMap_[SERVICE_CMD_APPEND_BUNDLES_BACKUP_SESSION] = &ServiceStub::CmdAppendBundlesBackupSession; + opToInterfaceMap_[SERVICE_CMD_FINISH] = &ServiceStub::CmdFinish; } int32_t ServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) @@ -127,4 +130,48 @@ int32_t ServiceStub::CmdGetExtFileName(MessageParcel &data, MessageParcel &reply return GetExtFileName(bundleName, fileName); } + +int32_t ServiceStub::CmdAppendBundlesRestoreSession(MessageParcel &data, MessageParcel &reply) +{ + UniqueFd fd(data.ReadFileDescriptor()); + if (fd < 0) { + return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive fd"); + } + std::vector bundleNames; + if (!data.ReadStringVector(&bundleNames)) { + return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive bundleNames"); + } + + int res = AppendBundlesRestoreSession(move(fd), bundleNames); + if (!reply.WriteInt32(res)) { + string str = "Failed to send the result " + to_string(res); + return BError(BError::Codes::SA_INVAL_ARG, str.data()).GetCode(); + } + return BError(BError::Codes::OK); +} + +int32_t ServiceStub::CmdAppendBundlesBackupSession(MessageParcel &data, MessageParcel &reply) +{ + std::vector bundleNames; + if (!data.ReadStringVector(&bundleNames)) { + return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive bundleNames"); + } + + int res = AppendBundlesBackupSession(bundleNames); + if (!reply.WriteInt32(res)) { + string str = "Failed to send the result " + to_string(res); + return BError(BError::Codes::SA_INVAL_ARG, str.data()).GetCode(); + } + return BError(BError::Codes::OK); +} + +int32_t ServiceStub::CmdFinish(MessageParcel &data, MessageParcel &reply) +{ + int res = Finish(); + if (!reply.WriteInt32(res)) { + string str = "Failed to send the result " + to_string(res); + return BError(BError::Codes::SA_INVAL_ARG, str.data()).GetCode(); + } + return BError(BError::Codes::OK); +} } // namespace OHOS::FileManagement::Backup diff --git a/tests/mock/module_ipc/svc_session_manager_mock.cpp b/tests/mock/module_ipc/svc_session_manager_mock.cpp index e5105627c..4e6f460f0 100644 --- a/tests/mock/module_ipc/svc_session_manager_mock.cpp +++ b/tests/mock/module_ipc/svc_session_manager_mock.cpp @@ -196,4 +196,6 @@ string SvcSessionManager::GetBackupExtName(const string &bundleName) GTEST_LOG_(INFO) << "GetBackupExtName " << bundleName; return ""; } + +void SvcSessionManager::Finish() {} } // namespace OHOS::FileManagement::Backup \ No newline at end of file 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 5fe4f5057..2e834b074 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 @@ -94,6 +94,21 @@ public: { return BError(BError::Codes::OK); } + + ErrCode AppendBundlesRestoreSession(UniqueFd fd, const std::vector &bundleNames) override + { + return BError(BError::Codes::OK); + } + + ErrCode AppendBundlesBackupSession(const std::vector &bundleNames) override + { + return BError(BError::Codes::OK); + } + + ErrCode Finish() override + { + return BError(BError::Codes::OK); + } }; } // namespace OHOS::FileManagement::Backup #endif // MOCK_I_SERVICE_MOCK_H \ No newline at end of file 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 8a982e086..b3a1ba52e 100644 --- a/tests/unittests/backup_sa/module_ipc/service_stub_test.cpp +++ b/tests/unittests/backup_sa/module_ipc/service_stub_test.cpp @@ -49,6 +49,9 @@ public: MOCK_METHOD2(AppFileReady, ErrCode(const string &fileName, UniqueFd fd)); MOCK_METHOD1(AppDone, ErrCode(ErrCode errCode)); MOCK_METHOD2(GetExtFileName, ErrCode(string &bundleName, string &fileName)); + MOCK_METHOD2(AppendBundlesRestoreSession, ErrCode(UniqueFd fd, const std::vector &bundleNames)); + MOCK_METHOD1(AppendBundlesBackupSession, ErrCode(const std::vector &bundleNames)); + MOCK_METHOD0(Finish, ErrCode()); UniqueFd InvokeGetLocalCapabilities() { if (bCapabilities_) { -- Gitee