From 709fc26f85e6913981e7ad3c210ff78f6169ec80 Mon Sep 17 00:00:00 2001 From: "yaoruozi1@huawei.com" Date: Sun, 24 Mar 2024 19:41:55 +0800 Subject: [PATCH 01/11] Added event delivery function to the backup and restore fwk Signed-off-by: yaoruozi1@huawei.com --- bundle.json | 10 +- .../src/b_incremental_restore_session.cpp | 5 +- .../b_incremental_session_restore_async.cpp | 3 +- .../src/b_session_restore.cpp | 5 +- .../src/b_session_restore_async.cpp | 3 +- .../backup_kit_inner/src/service_proxy.cpp | 4 + .../impl/b_incremental_restore_session.h | 4 +- .../b_incremental_session_restore_async.h | 2 + .../backup_kit_inner/impl/b_session_restore.h | 4 +- .../impl/b_session_restore_async.h | 2 + .../native/backup_kit_inner/impl/i_service.h | 1 + .../backup_kit_inner/impl/service_proxy.h | 1 + .../js/backup/session_restore_n_exporter.cpp | 19 ++- services/backup_sa/BUILD.gn | 7 +- .../backup_sa/include/module_ipc/service.h | 15 ++ .../module_notify/notify_work_service.h | 32 +++++ services/backup_sa/src/module_ipc/service.cpp | 69 ++++++--- .../backup_sa/src/module_ipc/service_stub.cpp | 7 +- .../src/module_notify/notify_work_service.cpp | 49 +++++++ .../b_session_restore_async_mock.cpp | 1 + .../b_session_restore_mock.cpp | 3 +- .../backup_kit_inner/service_proxy_mock.cpp | 1 + .../b_session_restore_async_test.cpp | 5 +- .../b_session_restore_test.cpp | 5 +- .../src/tools_op_incremental_restore.cpp | 6 +- .../tools_op_incremental_restore_async.cpp | 4 +- tools/backup_tool/src/tools_op_restore.cpp | 6 +- .../src/tools_op_restore_async.cpp | 4 +- utils/BUILD.gn | 2 + utils/include/b_jsonutil/b_jsonutil.h | 78 +++++++++++ utils/src/b_jsonutil/b_jsonutil.cpp | 132 ++++++++++++++++++ 31 files changed, 439 insertions(+), 50 deletions(-) create mode 100644 services/backup_sa/include/module_notify/notify_work_service.h create mode 100644 services/backup_sa/src/module_notify/notify_work_service.cpp create mode 100644 utils/include/b_jsonutil/b_jsonutil.h create mode 100644 utils/src/b_jsonutil/b_jsonutil.cpp diff --git a/bundle.json b/bundle.json index 482d41985..22d8e9aa3 100644 --- a/bundle.json +++ b/bundle.json @@ -44,10 +44,14 @@ "third_party": [ "bounds_checking_function", "jsoncpp", - "openssl" + "openssl", + "cJSON" ] }, - "adapted_system_type": [ "small", "standard" ], + "adapted_system_type": [ + "small", + "standard" + ], "rom": "1024KB", "ram": "1024KB", "build": { @@ -153,4 +157,4 @@ ] } } -} +} \ No newline at end of file diff --git a/frameworks/native/backup_kit_inner/src/b_incremental_restore_session.cpp b/frameworks/native/backup_kit_inner/src/b_incremental_restore_session.cpp index faab1b70b..56913f216 100644 --- a/frameworks/native/backup_kit_inner/src/b_incremental_restore_session.cpp +++ b/frameworks/native/backup_kit_inner/src/b_incremental_restore_session.cpp @@ -83,14 +83,15 @@ ErrCode BIncrementalRestoreSession::GetFileHandle(const string &bundleName, cons return proxy->GetIncrementalFileHandle(bundleName, fileName); } -ErrCode BIncrementalRestoreSession::AppendBundles(UniqueFd remoteCap, vector bundlesToRestore) +ErrCode BIncrementalRestoreSession::AppendBundles(UniqueFd remoteCap, vector bundlesToRestore, + std::vector detailInfos) { 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); + return proxy->AppendBundlesRestoreSession(move(remoteCap), bundlesToRestore, detailInfos); } ErrCode BIncrementalRestoreSession::Release() diff --git a/frameworks/native/backup_kit_inner/src/b_incremental_session_restore_async.cpp b/frameworks/native/backup_kit_inner/src/b_incremental_session_restore_async.cpp index c531d37d9..ef87967e0 100644 --- a/frameworks/native/backup_kit_inner/src/b_incremental_session_restore_async.cpp +++ b/frameworks/native/backup_kit_inner/src/b_incremental_session_restore_async.cpp @@ -92,6 +92,7 @@ ErrCode BIncrementalSessionRestoreAsync::GetFileHandle(const string &bundleName, ErrCode BIncrementalSessionRestoreAsync::AppendBundles(UniqueFd remoteCap, vector bundlesToRestore, + std::vector detailInfos, RestoreTypeEnum restoreType, int32_t userId) { @@ -100,7 +101,7 @@ ErrCode BIncrementalSessionRestoreAsync::AppendBundles(UniqueFd remoteCap, return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode(); } - return proxy->AppendBundlesRestoreSession(move(remoteCap), bundlesToRestore, restoreType, userId); + return proxy->AppendBundlesRestoreSession(move(remoteCap), bundlesToRestore, detailInfos, restoreType, userId); } ErrCode BIncrementalSessionRestoreAsync::Release() 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 37139fae6..b98fb8f75 100644 --- a/frameworks/native/backup_kit_inner/src/b_session_restore.cpp +++ b/frameworks/native/backup_kit_inner/src/b_session_restore.cpp @@ -93,14 +93,15 @@ ErrCode BSessionRestore::GetFileHandle(const string &bundleName, const string &f return proxy->GetFileHandle(bundleName, fileName); } -ErrCode BSessionRestore::AppendBundles(UniqueFd remoteCap, vector bundlesToRestore) +ErrCode BSessionRestore::AppendBundles(UniqueFd remoteCap, vector bundlesToRestore, + std::vector detailInfos) { 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); + return proxy->AppendBundlesRestoreSession(move(remoteCap), bundlesToRestore, detailInfos); } ErrCode BSessionRestore::Finish() diff --git a/frameworks/native/backup_kit_inner/src/b_session_restore_async.cpp b/frameworks/native/backup_kit_inner/src/b_session_restore_async.cpp index 9336f3d95..310be12d8 100644 --- a/frameworks/native/backup_kit_inner/src/b_session_restore_async.cpp +++ b/frameworks/native/backup_kit_inner/src/b_session_restore_async.cpp @@ -93,6 +93,7 @@ ErrCode BSessionRestoreAsync::GetFileHandle(const string &bundleName, const stri ErrCode BSessionRestoreAsync::AppendBundles(UniqueFd remoteCap, vector bundlesToRestore, + std::vector detailInfos, RestoreTypeEnum restoreType, int32_t userId) { @@ -101,7 +102,7 @@ ErrCode BSessionRestoreAsync::AppendBundles(UniqueFd remoteCap, return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode(); } - return proxy->AppendBundlesRestoreSession(move(remoteCap), bundlesToRestore, restoreType, userId); + return proxy->AppendBundlesRestoreSession(move(remoteCap), bundlesToRestore, detailInfos, restoreType, userId); } ErrCode BSessionRestoreAsync::Release() diff --git a/frameworks/native/backup_kit_inner/src/service_proxy.cpp b/frameworks/native/backup_kit_inner/src/service_proxy.cpp index 0e03d48ef..ff634260c 100644 --- a/frameworks/native/backup_kit_inner/src/service_proxy.cpp +++ b/frameworks/native/backup_kit_inner/src/service_proxy.cpp @@ -239,6 +239,7 @@ ErrCode ServiceProxy::GetFileHandle(const string &bundleName, const string &file ErrCode ServiceProxy::AppendBundlesRestoreSession(UniqueFd fd, const vector &bundleNames, + const std::vector &detailInfos, RestoreTypeEnum restoreType, int32_t userId) { @@ -259,6 +260,9 @@ ErrCode ServiceProxy::AppendBundlesRestoreSession(UniqueFd fd, if (!data.WriteStringVector(bundleNames)) { return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send bundleNames").GetCode(); } + if (!detailInfos.empty() && !data.WriteStringVector(detailInfos)) { + return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send detailInfos").GetCode(); + } if (!data.WriteInt32(static_cast(restoreType))) { return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send restoreType").GetCode(); } diff --git a/interfaces/inner_api/native/backup_kit_inner/impl/b_incremental_restore_session.h b/interfaces/inner_api/native/backup_kit_inner/impl/b_incremental_restore_session.h index 020b80db2..d2c2d4d6d 100644 --- a/interfaces/inner_api/native/backup_kit_inner/impl/b_incremental_restore_session.h +++ b/interfaces/inner_api/native/backup_kit_inner/impl/b_incremental_restore_session.h @@ -69,9 +69,11 @@ public: * * @param remoteCap 已打开的保存远端设备能力的Json文件。可使用GetLocalCapabilities方法获取 * @param bundlesToRestore 待恢复的应用清单 + * @param detailInfos bundle对应的单双映射关系json串 * @return ErrCode 规范错误码 */ - ErrCode AppendBundles(UniqueFd remoteCap, std::vector bundlesToRestore); + ErrCode AppendBundles(UniqueFd remoteCap, std::vector bundlesToRestore, + std::vector detailInfos); /** * @brief 用于结束服务 diff --git a/interfaces/inner_api/native/backup_kit_inner/impl/b_incremental_session_restore_async.h b/interfaces/inner_api/native/backup_kit_inner/impl/b_incremental_session_restore_async.h index 742c3c096..03d8d2c3d 100644 --- a/interfaces/inner_api/native/backup_kit_inner/impl/b_incremental_session_restore_async.h +++ b/interfaces/inner_api/native/backup_kit_inner/impl/b_incremental_session_restore_async.h @@ -78,11 +78,13 @@ public: * * @param remoteCap 已打开的保存远端设备能力的Json文件。可使用GetLocalCapabilities方法获取 * @param bundlesToRestore 待恢复的应用清单 + * @param detailInfos bundle的单双映射关系json串 * @param userId 用户ID * @return ErrCode 规范错误码 */ ErrCode AppendBundles(UniqueFd remoteCap, std::vector bundlesToRestore, + std::vector detailInfos, RestoreTypeEnum restoreType = RestoreTypeEnum::RESTORE_DATA_WAIT_SEND, int32_t userId = DEFAULT_INVAL_VALUE); 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 59fe34360..0928bfd69 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 @@ -67,9 +67,11 @@ public: * * @param remoteCap 已打开的保存远端设备能力的Json文件。可使用GetLocalCapabilities方法获取 * @param bundlesToRestore 待恢复的应用清单 + * @param detailInfos bundle对应的单双映射关系json * @return ErrCode 规范错误码 */ - ErrCode AppendBundles(UniqueFd remoteCap, std::vector bundlesToRestore); + ErrCode AppendBundles(UniqueFd remoteCap, std::vector bundlesToRestore, + std::vector detailInfos); /** * @brief 用于结束追加应用,结束后不可在调用AppendBundles diff --git a/interfaces/inner_api/native/backup_kit_inner/impl/b_session_restore_async.h b/interfaces/inner_api/native/backup_kit_inner/impl/b_session_restore_async.h index 9b7853706..db961b6ac 100644 --- a/interfaces/inner_api/native/backup_kit_inner/impl/b_session_restore_async.h +++ b/interfaces/inner_api/native/backup_kit_inner/impl/b_session_restore_async.h @@ -78,11 +78,13 @@ public: * * @param remoteCap 已打开的保存远端设备能力的Json文件。可使用GetLocalCapabilities方法获取 * @param bundlesToRestore 待恢复的应用清单 + * @param detailInfos bundle对应的单双映射的json串 * @param userId 用户ID * @return ErrCode 规范错误码 */ ErrCode AppendBundles(UniqueFd remoteCap, std::vector bundlesToRestore, + std::vector detailInfos, RestoreTypeEnum restoreType = RestoreTypeEnum::RESTORE_DATA_WAIT_SEND, int32_t userId = DEFAULT_INVAL_VALUE); 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 4e58bbd5e..7878a5391 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 @@ -47,6 +47,7 @@ public: virtual ErrCode GetFileHandle(const std::string &bundleName, const std::string &fileName) = 0; virtual ErrCode AppendBundlesRestoreSession(UniqueFd fd, const std::vector &bundleNames, + const std::vector &detailInfos, RestoreTypeEnum restoreType = RestoreTypeEnum::RESTORE_DATA_WAIT_SEND, int32_t userId = DEFAULT_INVAL_VALUE) = 0; virtual ErrCode AppendBundlesBackupSession(const std::vector &bundleNames) = 0; 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 6204832a8..711b0550a 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,7 @@ public: ErrCode GetFileHandle(const std::string &bundleName, const std::string &fileName) override; ErrCode AppendBundlesRestoreSession(UniqueFd fd, const std::vector &bundleNames, + const std::vector &detailInfos, RestoreTypeEnum restoreType = RestoreTypeEnum::RESTORE_DATA_WAIT_SEND, int32_t userId = DEFAULT_INVAL_VALUE) override; ErrCode AppendBundlesBackupSession(const std::vector &bundleNames) override; diff --git a/interfaces/kits/js/backup/session_restore_n_exporter.cpp b/interfaces/kits/js/backup/session_restore_n_exporter.cpp index 34afc9697..b3b2ce53c 100644 --- a/interfaces/kits/js/backup/session_restore_n_exporter.cpp +++ b/interfaces/kits/js/backup/session_restore_n_exporter.cpp @@ -327,6 +327,19 @@ napi_value SessionRestoreNExporter::AppendBundles(napi_env env, napi_callback_in return nullptr; } + NVal jsDetails(env, funcArg[NARG_POS::THIRD]); + std::vector bundleDetails; + if (jsDetails.TypeIs(napi_undefined) || jsDetails.TypeIs(napi_null)) { + HILOGW("Third param is not exist"); + } else { + auto [deSuc, bundleDetails, deIgnore] = jsDetails.ToStringArray(); + if (!deSuc) { + HILOGE("Third argument is not bundles array."); + NError(BError(BError::Codes::SDK_INVAL_ARG, "Third argument is not bundles array.").GetCode()) + .ThrowErr(env); + return nullptr; + } + } auto restoreEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); if (!(restoreEntity && (restoreEntity->sessionWhole || restoreEntity->sessionSheet))) { HILOGE("Failed to get RestoreSession entity."); @@ -334,14 +347,14 @@ napi_value SessionRestoreNExporter::AppendBundles(napi_env env, napi_callback_in return nullptr; } - auto cbExec = [entity {restoreEntity}, fd {fd}, bundles {bundles}]() -> NError { + auto cbExec = [entity {restoreEntity}, fd {fd}, bundles {bundles}, bundleDetails {bundleDetails}]() -> NError { if (!(entity && (entity->sessionWhole || entity->sessionSheet))) { return NError(BError(BError::Codes::SDK_INVAL_ARG, "restore session is nullptr").GetCode()); } if (entity->sessionWhole) { - return NError(entity->sessionWhole->AppendBundles(UniqueFd(fd), bundles)); + return NError(entity->sessionWhole->AppendBundles(UniqueFd(fd), bundles, bundleDetails)); } - return NError(entity->sessionSheet->AppendBundles(UniqueFd(fd), bundles)); + return NError(entity->sessionSheet->AppendBundles(UniqueFd(fd), bundles, bundleDetails)); }; auto cbCompl = [](napi_env env, NError err) -> NVal { return err ? NVal {env, err.GetNapiErr(env)} : NVal::CreateUndefined(env); diff --git a/services/backup_sa/BUILD.gn b/services/backup_sa/BUILD.gn index 53ff7a4f5..d1088712c 100644 --- a/services/backup_sa/BUILD.gn +++ b/services/backup_sa/BUILD.gn @@ -40,6 +40,7 @@ ohos_shared_library("backup_sa") { "src/module_ipc/svc_restore_deps_manager.cpp", "src/module_ipc/svc_session_manager.cpp", "src/module_sched/sched_scheduler.cpp", + "src/module_notify/notify_work_service.cpp", ] defines = [ @@ -47,7 +48,10 @@ ohos_shared_library("backup_sa") { "LOG_TAG=\"BackupSA\"", ] - include_dirs = [ "include" ] + include_dirs = [ + "include", + "include/module_notify", + ] deps = [ "${path_backup}/interfaces/inner_api/native/backup_kit_inner:backup_kit_inner", @@ -69,6 +73,7 @@ ohos_shared_library("backup_sa") { "safwk:system_ability_fwk", "samgr:samgr_proxy", "storage_service:storage_manager_sa_proxy", + "common_event_service:cesfwk_innerkits", ] use_exceptions = true diff --git a/services/backup_sa/include/module_ipc/service.h b/services/backup_sa/include/module_ipc/service.h index 66dc61a54..aafddd3b0 100644 --- a/services/backup_sa/include/module_ipc/service.h +++ b/services/backup_sa/include/module_ipc/service.h @@ -19,6 +19,8 @@ #include #include +#include "b_jsonutil/b_jsonutil.h" +#include "b_json/b_json_entity_caps.h" #include "i_service_reverse.h" #include "iremote_stub.h" #include "module_sched/sched_scheduler.h" @@ -42,6 +44,7 @@ public: ErrCode GetFileHandle(const std::string &bundleName, const std::string &fileName) override; ErrCode AppendBundlesRestoreSession(UniqueFd fd, const std::vector &bundleNames, + const std::vector &detailInfos, RestoreTypeEnum restoreType = RestoreTypeEnum::RESTORE_DATA_WAIT_SEND, int32_t userId = DEFAULT_INVAL_VALUE) override; ErrCode AppendBundlesBackupSession(const std::vector &bundleNames) override; @@ -194,6 +197,18 @@ private: */ void ExtConnectDied(const std::string &bundleName); + /** + * @brief 设置当前session的关键信息 + * + * @param restoreBundleInfos 待恢复的应用 + * @param restoreBundleNames 待恢复的应用包信息 + * @param bundleNameDetailMap bundle和detaild的对应关系 + * @param restoreType 任务类型 + */ + void SetCurrentSessProperties(std::vector &restoreBundleInfos, + std::vector &restoreBundleNames, + std::map &bundleNameDetailMap, RestoreTypeEnum restoreType); + private: static sptr instance_; static std::mutex instanceLock_; diff --git a/services/backup_sa/include/module_notify/notify_work_service.h b/services/backup_sa/include/module_notify/notify_work_service.h new file mode 100644 index 000000000..c4bb9b749 --- /dev/null +++ b/services/backup_sa/include/module_notify/notify_work_service.h @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2022-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_FILEMGMT_BACKUP_NOTIFY_WORK_SERVICE_H +#define OHOS_FILEMGMT_BACKUP_NOTIFY_WORK_SERVICE_H + +#include +#include + +#include "b_jsonutil/b_jsonutil.h" + +namespace OHOS::FileManagement::Backup { +class NotifyWorkService final : public NoCopyable { + DECLARE_DELAYED_SINGLETON(NotifyWorkService); +public: + bool NotifyBundleDetail(BJsonUtil::BundleDetailInfo bundleDetailInfo); +}; +} // namespace OHOS::FileManagement::Backup + +#endif // OHOS_FILEMGMT_BACKUP_NOTIFY_WORK_SERVICE_H \ No newline at end of file diff --git a/services/backup_sa/src/module_ipc/service.cpp b/services/backup_sa/src/module_ipc/service.cpp index 7043f60e7..bd4d670ae 100644 --- a/services/backup_sa/src/module_ipc/service.cpp +++ b/services/backup_sa/src/module_ipc/service.cpp @@ -41,7 +41,7 @@ #include "b_error/b_excep_utils.h" #include "b_file_info.h" #include "b_json/b_json_cached_entity.h" -#include "b_json/b_json_entity_caps.h" +#include "b_jsonutil/b_jsonutil.h" #include "b_ohos/startup/backup_para.h" #include "b_process/b_multiuser.h" #include "b_resources/b_constants.h" @@ -53,6 +53,7 @@ #include "module_external/sms_adapter.h" #include "module_ipc/svc_backup_connection.h" #include "module_ipc/svc_restore_deps_manager.h" +#include "notify_work_service.h" #include "parameter.h" #include "system_ability_definition.h" #include "hitrace_meter.h" @@ -310,6 +311,7 @@ static vector GetRestoreBundleNames(UniqueFd fd, ErrCode Service::AppendBundlesRestoreSession(UniqueFd fd, const vector &bundleNames, + const std::vector &detailInfos, RestoreTypeEnum restoreType, int32_t userId) { @@ -321,34 +323,19 @@ ErrCode Service::AppendBundlesRestoreSession(UniqueFd fd, session_->SetSessionUserId(userId); } VerifyCaller(IServiceReverse::Scenario::RESTORE); - auto restoreInfos = GetRestoreBundleNames(move(fd), session_, bundleNames); + std::vector realBundleNames; + std::vector bundleDetails = + BJsonUtil::ConvertBundleDetailInfos(bundleNames, detailInfos, ":", realBundleNames); + std::map bundleNameDetailMap; + BJsonUtil::RecordBundleDetailRelation(bundleNameDetailMap, bundleDetails); + auto restoreInfos = GetRestoreBundleNames(move(fd), session_, realBundleNames); auto restoreBundleNames = SvcRestoreDepsManager::GetInstance().GetRestoreBundleNames(restoreInfos, restoreType); if (restoreBundleNames.empty()) { session_->DecreaseSessionCnt(); return BError(BError::Codes::OK); } session_->AppendBundles(restoreBundleNames); - for (auto restoreInfo : restoreInfos) { - auto it = find_if(restoreBundleNames.begin(), restoreBundleNames.end(), - [&restoreInfo](const auto &bundleName) { return bundleName == restoreInfo.name; }); - if (it == restoreBundleNames.end()) { - throw BError(BError::Codes::SA_BUNDLE_INFO_EMPTY, "Can't find bundle name"); - } - HILOGD("bundleName: %{public}s, extensionName: %{public}s", restoreInfo.name.c_str(), - restoreInfo.extensionName.c_str()); - if ((restoreInfo.allToBackup == false && - !SpeicalVersion(restoreInfo.versionName, restoreInfo.versionCode)) || - restoreInfo.extensionName.empty()) { - OnBundleStarted(BError(BError::Codes::SA_FORBID_BACKUP_RESTORE), session_, restoreInfo.name); - session_->RemoveExtInfo(restoreInfo.name); - continue; - } - session_->SetBundleRestoreType(restoreInfo.name, restoreType); - session_->SetBundleVersionCode(restoreInfo.name, restoreInfo.versionCode); - session_->SetBundleVersionName(restoreInfo.name, restoreInfo.versionName); - session_->SetBundleDataSize(restoreInfo.name, restoreInfo.spaceOccupied); - session_->SetBackupExtName(restoreInfo.name, restoreInfo.extensionName); - } + SetCurrentSessProperties(restoreInfos, restoreBundleNames, bundleNameDetailMap, restoreType); OnStartSched(); session_->DecreaseSessionCnt(); return BError(BError::Codes::OK); @@ -362,6 +349,42 @@ ErrCode Service::AppendBundlesRestoreSession(UniqueFd fd, } } +void Service::SetCurrentSessProperties(std::vector &restoreBundleInfos, + std::vector &restoreBundleNames, + std::map &bundleNameDetailMap, RestoreTypeEnum restoreType) +{ + HILOGI("Start"); + for (auto restoreInfo : restoreBundleInfos) { + auto it = find_if(restoreBundleNames.begin(), restoreBundleNames.end(), + [&restoreInfo](const auto &bundleName) { return bundleName == restoreInfo.name; }); + if (it == restoreBundleNames.end()) { + throw BError(BError::Codes::SA_BUNDLE_INFO_EMPTY, "Can't find bundle name"); + } + HILOGD("bundleName: %{public}s, extensionName: %{public}s", restoreInfo.name.c_str(), + restoreInfo.extensionName.c_str()); + if ((restoreInfo.allToBackup == false && !SpeicalVersion(restoreInfo.versionName, restoreInfo.versionCode)) || + restoreInfo.extensionName.empty()) { + OnBundleStarted(BError(BError::Codes::SA_FORBID_BACKUP_RESTORE), session_, restoreInfo.name); + session_->RemoveExtInfo(restoreInfo.name); + continue; + } + if (restoreType == TypeRestoreTypeEnum::RESTORE_DATA_READDY || + SpeicalVersion(restoreInfo.versionName, restoreInfo.versionCode)) { + BJsonUtil::BundleDetailInfo bundleDetailInfo = bundleNameDetailMap[restoreInfo.name]; + if (bundleDetailInfo.type == "broadcast") { + DelayedSingleton::GetInstance()->NotifyBundleDetail(bundleDetailInfo); + } + + } + session_->SetBundleRestoreType(restoreInfo.name, restoreType); + session_->SetBundleVersionCode(restoreInfo.name, restoreInfo.versionCode); + session_->SetBundleVersionName(restoreInfo.name, restoreInfo.versionName); + session_->SetBundleDataSize(restoreInfo.name, restoreInfo.spaceOccupied); + session_->SetBackupExtName(restoreInfo.name, restoreInfo.extensionName); + } + HILOGI("End"); +} + ErrCode Service::AppendBundlesBackupSession(const vector &bundleNames) { HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); diff --git a/services/backup_sa/src/module_ipc/service_stub.cpp b/services/backup_sa/src/module_ipc/service_stub.cpp index d55334d30..7e1702c0e 100644 --- a/services/backup_sa/src/module_ipc/service_stub.cpp +++ b/services/backup_sa/src/module_ipc/service_stub.cpp @@ -234,6 +234,11 @@ int32_t ServiceStub::CmdAppendBundlesRestoreSession(MessageParcel &data, Message if (!data.ReadStringVector(&bundleNames)) { return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive bundleNames"); } + vector detailInfos; + if (!data.ReadStringVector(&detailInfos)) { + HILOGE("Failed to receive bundle detailInfos"); + return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive detailInfos"); + } int32_t type; if (!data.ReadInt32(type)) { return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive restoreType"); @@ -244,7 +249,7 @@ int32_t ServiceStub::CmdAppendBundlesRestoreSession(MessageParcel &data, Message return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive userId"); } - int res = AppendBundlesRestoreSession(move(fd), bundleNames, restoreType, userId); + int res = AppendBundlesRestoreSession(move(fd), bundleNames, detailInfos, restoreType, userId); if (!reply.WriteInt32(res)) { return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to send the result ") + to_string(res)); } diff --git a/services/backup_sa/src/module_notify/notify_work_service.cpp b/services/backup_sa/src/module_notify/notify_work_service.cpp new file mode 100644 index 000000000..67df0eac6 --- /dev/null +++ b/services/backup_sa/src/module_notify/notify_work_service.cpp @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2022-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "notify_work_service.h" + +#include "common_event_data.h" +#include "common_event_manager.h" +#include "common_event_support.h" +#include "filemgmt_libhilog.h" +#include "int_wrapper.h" +#include "string_wrapper.h" +#include "want.h" +#include "want_params.h" + +namespace OHOS::FileManagement::Backup { +namespace { + const static std::string EVENT_NAME = "COMMON_EVENT_RESTORE_START"; +} +NotifyWorkService::NotifyWorkService() {} +NotifyWorkService::~NotifyWorkService() {} + +bool NotifyWorkService::NotifyBundleDetail(BJsonUtil::BundleDetailInfo bundleDetailInfo) +{ + AAFwk::Want want; + std::string bundleName = bundleDetailInfo.bundleName; + HILOGI("Start publish event, bundleName is: %{public}s", bundleName.c_str()); + want.SetParam("bundleName", bundleName); + want.SetParam("type", bundleDetailInfo.type); + want.SetParam("index", bundleDetailInfo.bundleIndex); + want.SetParam("detail", bundleDetailInfo.detail); + want.SetAction(EVENT_NAME); + EventFwk::CommonEventData commonData {want}; + bool publishRet = EventFwk::CommonEventManager::PublishCommonEvent(commonData); + HILOGI("End publish event, bundleName is: %{public}s", bundleName.c_str()); + return publishRet; +} +} \ No newline at end of file diff --git a/tests/mock/backup_kit_inner/b_session_restore_async_mock.cpp b/tests/mock/backup_kit_inner/b_session_restore_async_mock.cpp index a80632f9b..f8165d184 100644 --- a/tests/mock/backup_kit_inner/b_session_restore_async_mock.cpp +++ b/tests/mock/backup_kit_inner/b_session_restore_async_mock.cpp @@ -60,6 +60,7 @@ ErrCode BSessionRestoreAsync::GetFileHandle(const string &bundleName, const stri ErrCode BSessionRestoreAsync::AppendBundles(UniqueFd remoteCap, vector bundlesToRestore, + vector detailInfos, RestoreTypeEnum restoreType, int32_t userId) { diff --git a/tests/mock/backup_kit_inner/b_session_restore_mock.cpp b/tests/mock/backup_kit_inner/b_session_restore_mock.cpp index bb68b01a2..6707975bc 100644 --- a/tests/mock/backup_kit_inner/b_session_restore_mock.cpp +++ b/tests/mock/backup_kit_inner/b_session_restore_mock.cpp @@ -85,7 +85,8 @@ ErrCode BSessionRestore::GetFileHandle(const string &bundleName, const string &f return BError(BError::Codes::OK); } -ErrCode BSessionRestore::AppendBundles(UniqueFd remoteCap, vector bundlesToRestore) +ErrCode BSessionRestore::AppendBundles(UniqueFd remoteCap, vector bundlesToRestore, + vector detailInfos) { Start(); return BError(BError::Codes::OK); diff --git a/tests/mock/backup_kit_inner/service_proxy_mock.cpp b/tests/mock/backup_kit_inner/service_proxy_mock.cpp index 1d9217548..1553910c8 100644 --- a/tests/mock/backup_kit_inner/service_proxy_mock.cpp +++ b/tests/mock/backup_kit_inner/service_proxy_mock.cpp @@ -82,6 +82,7 @@ ErrCode ServiceProxy::GetFileHandle(const string &bundleName, const string &file ErrCode ServiceProxy::AppendBundlesRestoreSession(UniqueFd fd, const vector &bundleNames, + const verctor &detailInfos, RestoreTypeEnum restoreType, int32_t userId) { diff --git a/tests/moduletests/backup_kit_inner/b_session_restore_async_test.cpp b/tests/moduletests/backup_kit_inner/b_session_restore_async_test.cpp index e9badac6b..f3c22f9d8 100644 --- a/tests/moduletests/backup_kit_inner/b_session_restore_async_test.cpp +++ b/tests/moduletests/backup_kit_inner/b_session_restore_async_test.cpp @@ -213,9 +213,10 @@ HWTEST_F(BSessionRestoreAsyncTest, SUB_backup_b_session_restore_async_0500, test SetMockGetInstance(true); SetMockLoadSystemAbility(true); vector bundleNames; - ErrCode ret = restorePtr_->AppendBundles(UniqueFd(-1), bundleNames); + vector detailInfos; + ErrCode ret = restorePtr_->AppendBundles(UniqueFd(-1), bundleNames, detailInfos); EXPECT_EQ(ret, ErrCode(BError::Codes::OK)); - ret = restorePtr_->AppendBundles(UniqueFd(-1), bundleNames); + ret = restorePtr_->AppendBundles(UniqueFd(-1), bundleNames, detailInfos); EXPECT_EQ(ret, ErrCode(BError::Codes::OK)); restorePtr_ = nullptr; } catch (...) { diff --git a/tests/moduletests/backup_kit_inner/b_session_restore_test.cpp b/tests/moduletests/backup_kit_inner/b_session_restore_test.cpp index 77bded075..1495ed414 100644 --- a/tests/moduletests/backup_kit_inner/b_session_restore_test.cpp +++ b/tests/moduletests/backup_kit_inner/b_session_restore_test.cpp @@ -258,16 +258,17 @@ HWTEST_F(BSessionRestoreTest, SUB_backup_b_session_restore_0600, testing::ext::T UniqueFd remoteCap(open(filePath.data(), O_RDONLY | O_CREAT, S_IRUSR | S_IWUSR)); string bundleName = ""; vector bundlesToRestore; + vector detailInfos; bundlesToRestore.emplace_back(bundleName); GTEST_LOG_(INFO) << "GetInstance is false"; SetMockGetInstance(false); - auto ret = restorePtr_->AppendBundles(move(remoteCap), bundlesToRestore); + auto ret = restorePtr_->AppendBundles(move(remoteCap), bundlesToRestore, detailInfos); EXPECT_NE(ret, ErrCode(BError::Codes::OK)); GTEST_LOG_(INFO) << "GetInstance is true"; SetMockGetInstance(true); - ret = restorePtr_->AppendBundles(move(remoteCap), bundlesToRestore); + ret = restorePtr_->AppendBundles(move(remoteCap), bundlesToRestore, detailInfos); EXPECT_EQ(ret, ErrCode(BError::Codes::OK)); } catch (...) { EXPECT_TRUE(false); diff --git a/tools/backup_tool/src/tools_op_incremental_restore.cpp b/tools/backup_tool/src/tools_op_incremental_restore.cpp index ab635a44d..03567faef 100644 --- a/tools/backup_tool/src/tools_op_incremental_restore.cpp +++ b/tools/backup_tool/src/tools_op_incremental_restore.cpp @@ -292,14 +292,16 @@ static int32_t Init(const string &pathCapFile, vector bundleNames, bool FinishTrace(HITRACE_TAG_FILEMANAGEMENT); return -errno; } - int result = ctx->session_->AppendBundles(move(fileFd), {bundleName}); + std::string detailInfo = ""; + int result = ctx->session_->AppendBundles(move(fileFd), {bundleName}, {detailInfo}); if (result != 0) { printf("restore append bundles error: %d\n", result); return -result; } } } else { - ret = ctx->session_->AppendBundles(move(fd), bundleNames); + vector detailInfos; + ret = ctx->session_->AppendBundles(move(fd), bundleNames, detailInfos); if (ret != 0) { printf("restore append bundles error: %d\n", ret); return -ret; diff --git a/tools/backup_tool/src/tools_op_incremental_restore_async.cpp b/tools/backup_tool/src/tools_op_incremental_restore_async.cpp index fe9628ac9..fe0eee6e4 100644 --- a/tools/backup_tool/src/tools_op_incremental_restore_async.cpp +++ b/tools/backup_tool/src/tools_op_incremental_restore_async.cpp @@ -314,7 +314,9 @@ static int32_t AppendBundles(shared_ptr restore, restoreType = RestoreTypeEnum::RESTORE_DATA_READDY; } try { - int ret = restore->session_->AppendBundles(move(fd), bundleNames, restoreType, atoi(userId.data())); + std::vector detailInfos; + int ret = restore->session_->AppendBundles(move(fd), bundleNames, detailInfos, restoreType, + atoi(userId.data())); if (ret != 0) { printf("restore append bundles error: %d\n", ret); return -ret; diff --git a/tools/backup_tool/src/tools_op_restore.cpp b/tools/backup_tool/src/tools_op_restore.cpp index 5e7916c07..d2d6e8750 100644 --- a/tools/backup_tool/src/tools_op_restore.cpp +++ b/tools/backup_tool/src/tools_op_restore.cpp @@ -263,14 +263,16 @@ static int32_t InitPathCapFile(const string &pathCapFile, vector bundleN FinishTrace(HITRACE_TAG_FILEMANAGEMENT); return -errno; } - int result = ctx->session_->AppendBundles(move(fileFd), {bundleName}); + std::string bundleDetail = ""; + int result = ctx->session_->AppendBundles(move(fileFd), {bundleName}, {bundleDetail}); if (result != 0) { printf("restore append bundles error: %d\n", result); return -result; } } } else { - ret = ctx->session_->AppendBundles(move(fd), bundleNames); + std::vector detailInfos; + ret = ctx->session_->AppendBundles(move(fd), bundleNames, detailInfos); if (ret != 0) { printf("restore append bundles error: %d\n", ret); return -ret; diff --git a/tools/backup_tool/src/tools_op_restore_async.cpp b/tools/backup_tool/src/tools_op_restore_async.cpp index 47ab6b8d4..72bd84c5c 100644 --- a/tools/backup_tool/src/tools_op_restore_async.cpp +++ b/tools/backup_tool/src/tools_op_restore_async.cpp @@ -298,7 +298,9 @@ static int32_t AppendBundles(shared_ptr restore, restoreType = RestoreTypeEnum::RESTORE_DATA_READDY; } try { - int ret = restore->session_->AppendBundles(move(fd), bundleNames, restoreType, atoi(userId.data())); + std::vector detailInfos; + int ret = restore->session_->AppendBundles(move(fd), bundleNames, detailInfos, + restoreType, atoi(userId.data())); if (ret != 0) { printf("restore append bundles error: %d\n", ret); return -ret; diff --git a/utils/BUILD.gn b/utils/BUILD.gn index b095efa50..dd6275403 100644 --- a/utils/BUILD.gn +++ b/utils/BUILD.gn @@ -93,6 +93,7 @@ ohos_shared_library("backup_utils") { "src/b_process/b_process.cpp", "src/b_tarball/b_tarball_cmdline.cpp", "src/b_tarball/b_tarball_factory.cpp", + "src/b_jsonutil/b_jsonutil.cpp", ] sources += get_target_outputs(":backup_cxx_gen") @@ -120,6 +121,7 @@ ohos_shared_library("backup_utils") { ":backup_cxx_rust", "${path_jsoncpp}:jsoncpp", "//third_party/openssl:libcrypto_shared", + "//third_party/cJSON:cjson", ] use_exceptions = true diff --git a/utils/include/b_jsonutil/b_jsonutil.h b/utils/include/b_jsonutil/b_jsonutil.h new file mode 100644 index 000000000..e0eb00de4 --- /dev/null +++ b/utils/include/b_jsonutil/b_jsonutil.h @@ -0,0 +1,78 @@ +/* + * Copyright (c) 2022-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_FILEMGMT_BACKUP_JSON_UTIL_H +#define OHOS_FILEMGMT_BACKUP_JSON_UTIL_H + +#include +#include + +namespace OHOS::FileManagement::Backup { +class BJsonUtil { +public: + typedef struct BundleDetailInfo { + std::string bundleName; + std::string type; + std::string detail; + int bundleIndex; + }BundleDetailInfo; + /** + * @brief 带有拼接字符的bundleName按照拼接字符进行分割 + * + * @param bundleNameStr bundleName拼接index的字符串 + * @param patternInfo 拼接字符串 + * + * @return 分割好的结果赋值给结构体 + */ + static BundleDetailInfo ParseBundleNameIndexStr(const std::string &bundleNameStr, const std::string &patternInfo); + + /** + * @brief 将传进来的bundleNames的集合进行按照拼接字符分割处理 + * + * @param bundleNames bundleName拼接index的字符串集合 + * @param details infos的集合 + * @param patternInfo 拼接的字符 + * @param realBundleNames 分割后真正的bundleNames + * + * @return 结构体集合 + * + */ + static std::vector ConvertBundleDetailInfos(const std::vector &bundleNames, + const std::vector &details, const std::string &patternInfo, + std::vector &realBundleNames); + + /** + * @brief 解析单个bundle对应的json串 + * + * @param bundleDetailInfo json串 + * @param bundleDetail 结构体对象 + * + */ + static void ParseBundleDetailInfo(const std::string &bundleDetailInfo, BundleDetailInfo &bundleDetail); + + + /** + * @brief 根据bundleName获取对应的detailInfo + * + * @param bundleNameDetailMap bundleName和detail的对应关系 + * @param bundleDetailInfo 结构体对象 + * + */ + static void RecordBundleDetailRelation(std::map &bundleNameDetailMap, + std::vector &bundleDetailInfo); +}; +} // namespace OHOS::FileManagement::Backup + +#endif // OHOS_FILEMGMT_BACKUP_BACKUP_PARA_H \ No newline at end of file diff --git a/utils/src/b_jsonutil/b_jsonutil.cpp b/utils/src/b_jsonutil/b_jsonutil.cpp new file mode 100644 index 000000000..7fe85bcf7 --- /dev/null +++ b/utils/src/b_jsonutil/b_jsonutil.cpp @@ -0,0 +1,132 @@ +/* + * Copyright (c) 2022-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "b_jsonutil/b_jsonutil.h" + +#include +#include "cJSON.h" + +#include "b_error/b_error.h" +#include "filemgmt_libhilog.h" + +namespace OHOS::FileManagement::Backup { +using namespace std; +namespace { + const static int BUNDLE_INDEX_DEFAULT_VAL = 0; + const static string COMMON_EVENT_TYPE = "broadcast"; +} + +BJsonUtil::BundleDetailInfo BJsonUtil::ParseBundleNameIndexStr(const std::string &bundleNameStr, + const std::string &patternInfo) +{ + HILOGI("Start parse bundle name and index"); + size_t hasPos = bundleNameStr.find(patternInfo); + BundleDetailInfo bundleDetailInfo; + if (hasPos == std::string::npos) { + bundleDetailInfo.bundleName = bundleNameStr; + bundleDetailInfo.bundleIndex = BUNDLE_INDEX_DEFAULT_VAL; + return bundleDetailInfo; + } + std::string bundleName = bundleNameStr.substr(0, hasPos); + std::string indexStr = bundleNameStr.substr(hasPos + 1); + int index = std::stoi(indexStr); + bundleDetailInfo.bundleName = bundleName; + bundleDetailInfo.bundleIndex = index; + HILOGI("End parse bundle name and index"); + return bundleDetailInfo; +} + +std::vector BJsonUtil::ConvertBundleDetailInfos( + const std::vector &bundleNameIndexStrs, + const std::vector &details, const std::string &patternInfo, std::vector &realBundleNames) +{ + std::vector bundleDetailInfoList; + for (size_t pos = 0; pos < bundleNameIndexStrs.size(); pos++) + { + std::string bundleNameIndexStr = bundleNameIndexStrs[pos]; + BundleDetailInfo bundleDetailInfo; + size_t hasPos = bundleNameIndexStr.find(patternInfo); + if (hasPos == std::string::npos) { + bundleDetailInfo.bundleName = bundleNameIndexStr; + bundleDetailInfo.bundleIndex = BUNDLE_INDEX_DEFAULT_VAL; + realBundleNames.push_back(bundleNameIndexStr); + } else { + std::string bundleName = bundleNameIndexStr.substr(0, hasPos); + std::string indexStr = bundleNameIndexStr.substr(hasPos + 1); + int index = std::stoi(indexStr); + bundleDetailInfo.bundleName = bundleName; + bundleDetailInfo.bundleIndex = index; + realBundleNames.push_back(bundleNameIndexStr); + } + if (pos < details.size()) { + std::string bundleDetailStr = details[pos]; + ParseBundleDetailInfo(bundleDetailStr, bundleDetailInfo); + } + bundleDetailInfoList.push_back(bundleDetailInfo); + } + return bundleDetailInfoList; +} + +void BJsonUtil::ParseBundleDetailInfo(const std::string &bundleDetailInfo, BundleDetailInfo &bundleDetail) +{ + cJSON *root = cJSON_Parse(bundleDetailInfo.c_str()); + if (root == nullptr) { + HILOGE("Parse json error,root is null"); + return; + } + cJSON *infos = cJSON_GetObjectItem(root, "infos"); + if (!cJSON_IsArray(infos)) { + HILOGE("Parse json error, infos is not array"); + cJSON_Delete(root); + return; + } + int infosCount = cJSON_GetArraySize(infos); + for (int i = 0; i < infosCount; i++) + { + cJSON *infoItem = cJSON_GetArrayItem(infos, i); + if (!cJSON_IsObject(infoItem)) { + HILOGE("Parse json error, info item is not an object"); + cJSON_Delete(root); + return; + } + cJSON *type = cJSON_GetObjectItem(infoItem, "type"); + if (type && cJSON_IsString(type)) { + if (type->valuestring == COMMON_EVENT_TYPE) { + bundleDetail.type = type->valuestring; + } + } + cJSON *details = cJSON_GetObjectItem(infoItem, "details"); + if (details && cJSON_IsArray(details)) { + char *detailInfos = cJSON_Print(details); + bundleDetail.detail = std::string(detailInfos); + free(detailInfos); + } + } + cJSON_Delete(root); +} + +void BJsonUtil::RecordBundleDetailRelation(std::map &bundleNameDetailMap, + std::vector &bundleDetailInfos) +{ + for (auto &bundleDetail : bundleDetailInfos) + { + std::string bundleName = bundleDetail.bundleName; + bundleNameDetailMap[bundleName] = bundleDetail; + } +} +} + + + -- Gitee From 39bdf87fa665202e38daea065284e8802151a4fd Mon Sep 17 00:00:00 2001 From: "yaoruozi1@huawei.com" Date: Mon, 25 Mar 2024 09:24:39 +0800 Subject: [PATCH 02/11] Added event delivery function to the backup and restore fwk Signed-off-by: yaoruozi1@huawei.com --- services/backup_sa/src/module_ipc/service.cpp | 2 +- .../backup_sa/src/module_notify/notify_work_service.cpp | 2 +- tests/mock/module_ipc/service_mock.cpp | 1 + tests/mock/module_ipc/service_stub_mock.cpp | 5 +++-- .../backup_api/backup_impl/include/i_service_mock.h | 1 + .../backup_api/backup_impl/service_proxy_test.cpp | 6 +++--- tests/unittests/backup_sa/module_ipc/BUILD.gn | 1 + tests/unittests/backup_sa/module_ipc/service_stub_test.cpp | 7 ++++--- tests/unittests/backup_sa/module_ipc/service_test.cpp | 3 ++- utils/include/b_jsonutil/b_jsonutil.h | 2 +- 10 files changed, 18 insertions(+), 12 deletions(-) diff --git a/services/backup_sa/src/module_ipc/service.cpp b/services/backup_sa/src/module_ipc/service.cpp index bd4d670ae..d5fd69f34 100644 --- a/services/backup_sa/src/module_ipc/service.cpp +++ b/services/backup_sa/src/module_ipc/service.cpp @@ -53,7 +53,7 @@ #include "module_external/sms_adapter.h" #include "module_ipc/svc_backup_connection.h" #include "module_ipc/svc_restore_deps_manager.h" -#include "notify_work_service.h" +#include "module_notify/notify_work_service.h" #include "parameter.h" #include "system_ability_definition.h" #include "hitrace_meter.h" diff --git a/services/backup_sa/src/module_notify/notify_work_service.cpp b/services/backup_sa/src/module_notify/notify_work_service.cpp index 67df0eac6..07d5eb161 100644 --- a/services/backup_sa/src/module_notify/notify_work_service.cpp +++ b/services/backup_sa/src/module_notify/notify_work_service.cpp @@ -13,7 +13,7 @@ * limitations under the License. */ -#include "notify_work_service.h" +#include "module_notify/notify_work_service.h" #include "common_event_data.h" #include "common_event_manager.h" diff --git a/tests/mock/module_ipc/service_mock.cpp b/tests/mock/module_ipc/service_mock.cpp index cb41e78f0..7dd2e6b05 100644 --- a/tests/mock/module_ipc/service_mock.cpp +++ b/tests/mock/module_ipc/service_mock.cpp @@ -74,6 +74,7 @@ ErrCode Service::AppDone(ErrCode errCode) ErrCode Service::AppendBundlesRestoreSession(UniqueFd fd, const std::vector &bundleNames, + const std::vector &detailInfos, RestoreTypeEnum restoreType, int32_t userId) { diff --git a/tests/mock/module_ipc/service_stub_mock.cpp b/tests/mock/module_ipc/service_stub_mock.cpp index 7ae9cced2..c5f1cfcf2 100644 --- a/tests/mock/module_ipc/service_stub_mock.cpp +++ b/tests/mock/module_ipc/service_stub_mock.cpp @@ -152,8 +152,9 @@ int32_t ServiceStub::CmdAppendBundlesRestoreSession(MessageParcel &data, Message UniqueFd fd(data.ReadFileDescriptor()); std::vector bundleNames; data.ReadStringVector(&bundleNames); - - int res = AppendBundlesRestoreSession(move(fd), bundleNames); + std::vector detailInfos; + data.ReadStringVector(&detailInfos); + int res = AppendBundlesRestoreSession(move(fd), bundleNames, detailInfos;); reply.WriteInt32(res); 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 9c4a84cc7..bacadb204 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 @@ -95,6 +95,7 @@ public: ErrCode AppendBundlesRestoreSession(UniqueFd fd, const std::vector &bundleNames, + const std::vector &detailInfos, RestoreTypeEnum restoreType, int32_t userId) override { diff --git a/tests/unittests/backup_api/backup_impl/service_proxy_test.cpp b/tests/unittests/backup_api/backup_impl/service_proxy_test.cpp index 72f7167a7..e6e009209 100644 --- a/tests/unittests/backup_api/backup_impl/service_proxy_test.cpp +++ b/tests/unittests/backup_api/backup_impl/service_proxy_test.cpp @@ -288,10 +288,10 @@ HWTEST_F(ServiceProxyTest, SUB_Service_proxy_AppendBundlesRestoreSession_0100, t TestManager tm("BackupSession_GetFd_0100"); std::string filePath = tm.GetRootDirCurTest().append(FILE_NAME); UniqueFd fd(open(filePath.data(), O_RDONLY | O_CREAT, S_IRUSR | S_IWUSR)); - - int32_t result = proxy_->AppendBundlesRestoreSession(move(fd), bundleNames); + std::vector detailInfos; + int32_t result = proxy_->AppendBundlesRestoreSession(move(fd), bundleNames, detailInfos); EXPECT_EQ(result, BError(BError::Codes::OK)); - result = proxy_->AppendBundlesRestoreSession(UniqueFd(-1), bundleNames); + result = proxy_->AppendBundlesRestoreSession(UniqueFd(-1), bundleNames, detailInfos); EXPECT_NE(result, BError(BError::Codes::OK)); GTEST_LOG_(INFO) << "ServiceProxyTest-end SUB_Service_proxy_AppendBundlesRestoreSession_0100"; } diff --git a/tests/unittests/backup_sa/module_ipc/BUILD.gn b/tests/unittests/backup_sa/module_ipc/BUILD.gn index 9d8782e08..6f3ac0674 100644 --- a/tests/unittests/backup_sa/module_ipc/BUILD.gn +++ b/tests/unittests/backup_sa/module_ipc/BUILD.gn @@ -248,6 +248,7 @@ ohos_unittest("backup_restore_deps_manager_test") { "${path_backup}/services/backup_sa/src/module_ipc/service.cpp", "${path_backup}/services/backup_sa/src/module_ipc/service_incremental.cpp", "${path_backup}/services/backup_sa/src/module_ipc/svc_restore_deps_manager.cpp", + "${path_backup}/services/backup_sa/src/module_notify/notify_work_service.cpp", "svc_restore_deps_manager_test.cpp", ] sources += backup_mock_src 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 00723373e..59e30575d 100644 --- a/tests/unittests/backup_sa/module_ipc/service_stub_test.cpp +++ b/tests/unittests/backup_sa/module_ipc/service_stub_test.cpp @@ -48,9 +48,10 @@ public: MOCK_METHOD2(AppFileReady, ErrCode(const string &fileName, UniqueFd fd)); MOCK_METHOD1(AppDone, ErrCode(ErrCode errCode)); MOCK_METHOD2(GetFileHandle, ErrCode(const string &bundleName, const string &fileName)); - MOCK_METHOD4( + MOCK_METHOD5( AppendBundlesRestoreSession, - ErrCode(UniqueFd fd, const std::vector &bundleNames, RestoreTypeEnum restoreType, int32_t userId)); + ErrCode(UniqueFd fd, const std::vector &bundleNames, const std::vector &detailInfos, + RestoreTypeEnum restoreType, int32_t userId)); MOCK_METHOD1(AppendBundlesBackupSession, ErrCode(const std::vector &bundleNames)); MOCK_METHOD0(Finish, ErrCode()); MOCK_METHOD0(Release, ErrCode()); @@ -384,7 +385,7 @@ HWTEST_F(ServiceStubTest, SUB_backup_sa_ServiceStub_AppendBundlesRestoreSession_ GTEST_LOG_(INFO) << "ServiceStubTest-begin SUB_backup_sa_ServiceStub_AppendBundlesRestoreSession_0100"; try { MockService service; - EXPECT_CALL(service, AppendBundlesRestoreSession(_, _, _, _)).WillOnce(Return(BError(BError::Codes::OK))); + EXPECT_CALL(service, AppendBundlesRestoreSession(_, _, _,_, _)).WillOnce(Return(BError(BError::Codes::OK))); MessageParcel data; MessageParcel reply; MessageOption option; diff --git a/tests/unittests/backup_sa/module_ipc/service_test.cpp b/tests/unittests/backup_sa/module_ipc/service_test.cpp index 7189421ef..c33929df3 100644 --- a/tests/unittests/backup_sa/module_ipc/service_test.cpp +++ b/tests/unittests/backup_sa/module_ipc/service_test.cpp @@ -64,6 +64,7 @@ void ServiceTest::TearDownTestCase() ErrCode ServiceTest::Init(IServiceReverse::Scenario scenario) { vector bundleNames; + vector detailInfos; bundleNames.emplace_back(BUNDLE_NAME); ErrCode ret = 0; if (scenario == IServiceReverse::Scenario::RESTORE) { @@ -71,7 +72,7 @@ ErrCode ServiceTest::Init(IServiceReverse::Scenario scenario) EXPECT_GE(fd, BError(BError::Codes::OK)); ret = servicePtr_->InitRestoreSession(remote_); EXPECT_EQ(ret, BError(BError::Codes::OK)); - ret = servicePtr_->AppendBundlesRestoreSession(move(fd), bundleNames); + ret = servicePtr_->AppendBundlesRestoreSession(move(fd), bundleNames, detailInfos); EXPECT_EQ(ret, BError(BError::Codes::OK)); ret = servicePtr_->Finish(); EXPECT_EQ(ret, BError(BError::Codes::OK)); diff --git a/utils/include/b_jsonutil/b_jsonutil.h b/utils/include/b_jsonutil/b_jsonutil.h index e0eb00de4..b936fcb80 100644 --- a/utils/include/b_jsonutil/b_jsonutil.h +++ b/utils/include/b_jsonutil/b_jsonutil.h @@ -75,4 +75,4 @@ public: }; } // namespace OHOS::FileManagement::Backup -#endif // OHOS_FILEMGMT_BACKUP_BACKUP_PARA_H \ No newline at end of file +#endif // OHOS_FILEMGMT_BACKUP_JSON_UTIL_H \ No newline at end of file -- Gitee From 5d0bcfad5143e56ebdae48036dd05ab880f670fa Mon Sep 17 00:00:00 2001 From: "yaoruozi1@huawei.com" Date: Thu, 28 Mar 2024 18:20:24 +0800 Subject: [PATCH 03/11] Added event delivery function to the backup and restore fwk Signed-off-by: yaoruozi1@huawei.com --- tests/mock/backup_kit_inner/service_proxy_mock.cpp | 2 +- tests/unittests/backup_sa/module_ipc/BUILD.gn | 3 +++ utils/include/b_json/b_json_cached_entity.h | 3 ++- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/tests/mock/backup_kit_inner/service_proxy_mock.cpp b/tests/mock/backup_kit_inner/service_proxy_mock.cpp index 1553910c8..8d98e5c82 100644 --- a/tests/mock/backup_kit_inner/service_proxy_mock.cpp +++ b/tests/mock/backup_kit_inner/service_proxy_mock.cpp @@ -82,7 +82,7 @@ ErrCode ServiceProxy::GetFileHandle(const string &bundleName, const string &file ErrCode ServiceProxy::AppendBundlesRestoreSession(UniqueFd fd, const vector &bundleNames, - const verctor &detailInfos, + const vector &detailInfos, RestoreTypeEnum restoreType, int32_t userId) { diff --git a/tests/unittests/backup_sa/module_ipc/BUILD.gn b/tests/unittests/backup_sa/module_ipc/BUILD.gn index 6f3ac0674..cdfd8ab01 100644 --- a/tests/unittests/backup_sa/module_ipc/BUILD.gn +++ b/tests/unittests/backup_sa/module_ipc/BUILD.gn @@ -77,6 +77,7 @@ ohos_unittest("backup_service_test") { "${path_backup}/services/backup_sa/src/module_ipc/service.cpp", "${path_backup}/services/backup_sa/src/module_ipc/service_incremental.cpp", "${path_backup}/services/backup_sa/src/module_ipc/svc_restore_deps_manager.cpp", + "${path_backup}/services/backup_sa/src/module_notify/notify_work_service.cpp", "service_test.cpp", ] sources += backup_mock_src @@ -112,6 +113,7 @@ ohos_unittest("backup_service_test") { "safwk:system_ability_fwk", "samgr:samgr_proxy", "storage_service:storage_manager_sa_proxy", + "common_event_service:cesfwk_innerkits", ] sanitize = { @@ -284,6 +286,7 @@ ohos_unittest("backup_restore_deps_manager_test") { "safwk:system_ability_fwk", "samgr:samgr_proxy", "storage_service:storage_manager_sa_proxy", + "common_event_service:cesfwk_innerkits", ] use_exceptions = true diff --git a/utils/include/b_json/b_json_cached_entity.h b/utils/include/b_json/b_json_cached_entity.h index 8413c5615..aeb13f2d8 100644 --- a/utils/include/b_json/b_json_cached_entity.h +++ b/utils/include/b_json/b_json_cached_entity.h @@ -105,7 +105,8 @@ public: bool res = jsonReader->parse(sv.data(), sv.data() + sv.length(), &jValue, &errs); if (!res || !errs.empty()) { - throw BError(BError::Codes::UTILS_INVAL_JSON_ENTITY, errs); + HILOGE("Json utils operated on an invalid file"); + return; } obj_ = std::move(jValue); -- Gitee From 6447b67f96c835a8ab645d74ebbd17e0045f5856 Mon Sep 17 00:00:00 2001 From: "yaoruozi1@huawei.com" Date: Fri, 29 Mar 2024 11:44:19 +0800 Subject: [PATCH 04/11] Added event delivery function to the backup and restore fwk Signed-off-by: yaoruozi1@huawei.com --- services/backup_sa/src/module_ipc/service.cpp | 25 +++++++++++-------- .../src/module_notify/notify_work_service.cpp | 2 +- utils/include/b_jsonutil/b_jsonutil.h | 4 ++- utils/src/b_jsonutil/b_jsonutil.cpp | 11 ++++---- 4 files changed, 24 insertions(+), 18 deletions(-) diff --git a/services/backup_sa/src/module_ipc/service.cpp b/services/backup_sa/src/module_ipc/service.cpp index d5fd69f34..f7dce98ae 100644 --- a/services/backup_sa/src/module_ipc/service.cpp +++ b/services/backup_sa/src/module_ipc/service.cpp @@ -65,6 +65,7 @@ REGISTER_SYSTEM_ABILITY_BY_ID(Service, FILEMANAGEMENT_BACKUP_SERVICE_SA_ID, fals namespace { constexpr int32_t DEBUG_ID = 100; +const static string COMMON_EVENT_TYPE = "broadcast"; } // namespace /* Shell/Xts user id equal to 0/1, we need set default 100 */ @@ -323,12 +324,12 @@ ErrCode Service::AppendBundlesRestoreSession(UniqueFd fd, session_->SetSessionUserId(userId); } VerifyCaller(IServiceReverse::Scenario::RESTORE); - std::vector realBundleNames; + std::vector hosBundleNames; std::vector bundleDetails = - BJsonUtil::ConvertBundleDetailInfos(bundleNames, detailInfos, ":", realBundleNames); + BJsonUtil::ConvertBundleDetailInfos(bundleNames, detailInfos, ":", hosBundleNames, userId); std::map bundleNameDetailMap; BJsonUtil::RecordBundleDetailRelation(bundleNameDetailMap, bundleDetails); - auto restoreInfos = GetRestoreBundleNames(move(fd), session_, realBundleNames); + auto restoreInfos = GetRestoreBundleNames(move(fd), session_, hosBundleNames); auto restoreBundleNames = SvcRestoreDepsManager::GetInstance().GetRestoreBundleNames(restoreInfos, restoreType); if (restoreBundleNames.empty()) { session_->DecreaseSessionCnt(); @@ -368,19 +369,21 @@ void Service::SetCurrentSessProperties(std::vector session_->RemoveExtInfo(restoreInfo.name); continue; } - if (restoreType == TypeRestoreTypeEnum::RESTORE_DATA_READDY || - SpeicalVersion(restoreInfo.versionName, restoreInfo.versionCode)) { - BJsonUtil::BundleDetailInfo bundleDetailInfo = bundleNameDetailMap[restoreInfo.name]; - if (bundleDetailInfo.type == "broadcast") { - DelayedSingleton::GetInstance()->NotifyBundleDetail(bundleDetailInfo); - } - - } session_->SetBundleRestoreType(restoreInfo.name, restoreType); session_->SetBundleVersionCode(restoreInfo.name, restoreInfo.versionCode); session_->SetBundleVersionName(restoreInfo.name, restoreInfo.versionName); session_->SetBundleDataSize(restoreInfo.name, restoreInfo.spaceOccupied); session_->SetBackupExtName(restoreInfo.name, restoreInfo.extensionName); + if (restoreType == TypeRestoreTypeEnum::RESTORE_DATA_READDY || + SpeicalVersion(restoreInfo.versionName, restoreInfo.versionCode)) { + auto iter = bundleNameDetailMap.find(restoreInfo.name); + if (iter != bundleNameDetailMap.end()) { + BJsonUtil::BundleDetailInfo bundetailInfo = iter->second; + if (bundleDetailInfo.type == COMMON_EVENT_TYPE) { + DelayedSingleton::GetInstance()->NotifyBundleDetail(bundleDetailInfo); + } + } + } } HILOGI("End"); } diff --git a/services/backup_sa/src/module_notify/notify_work_service.cpp b/services/backup_sa/src/module_notify/notify_work_service.cpp index 07d5eb161..c148545e3 100644 --- a/services/backup_sa/src/module_notify/notify_work_service.cpp +++ b/services/backup_sa/src/module_notify/notify_work_service.cpp @@ -37,7 +37,7 @@ bool NotifyWorkService::NotifyBundleDetail(BJsonUtil::BundleDetailInfo bundleDet std::string bundleName = bundleDetailInfo.bundleName; HILOGI("Start publish event, bundleName is: %{public}s", bundleName.c_str()); want.SetParam("bundleName", bundleName); - want.SetParam("type", bundleDetailInfo.type); + want.SetParam("userId", bundleDetailInfo.userId); want.SetParam("index", bundleDetailInfo.bundleIndex); want.SetParam("detail", bundleDetailInfo.detail); want.SetAction(EVENT_NAME); diff --git a/utils/include/b_jsonutil/b_jsonutil.h b/utils/include/b_jsonutil/b_jsonutil.h index b936fcb80..af669a3a4 100644 --- a/utils/include/b_jsonutil/b_jsonutil.h +++ b/utils/include/b_jsonutil/b_jsonutil.h @@ -27,6 +27,7 @@ public: std::string type; std::string detail; int bundleIndex; + int32_t userId; }BundleDetailInfo; /** * @brief 带有拼接字符的bundleName按照拼接字符进行分割 @@ -45,13 +46,14 @@ public: * @param details infos的集合 * @param patternInfo 拼接的字符 * @param realBundleNames 分割后真正的bundleNames + * @param userId userId * * @return 结构体集合 * */ static std::vector ConvertBundleDetailInfos(const std::vector &bundleNames, const std::vector &details, const std::string &patternInfo, - std::vector &realBundleNames); + std::vector &realBundleNames, int32_t userId); /** * @brief 解析单个bundle对应的json串 diff --git a/utils/src/b_jsonutil/b_jsonutil.cpp b/utils/src/b_jsonutil/b_jsonutil.cpp index 7fe85bcf7..3ad76578b 100644 --- a/utils/src/b_jsonutil/b_jsonutil.cpp +++ b/utils/src/b_jsonutil/b_jsonutil.cpp @@ -49,32 +49,33 @@ BJsonUtil::BundleDetailInfo BJsonUtil::ParseBundleNameIndexStr(const std::string } std::vector BJsonUtil::ConvertBundleDetailInfos( - const std::vector &bundleNameIndexStrs, - const std::vector &details, const std::string &patternInfo, std::vector &realBundleNames) + const std::vector &bundleNameIndexStrs, const std::vector &details, + const std::string &patternInfo, std::vector &realBundleNames, int32_t userId) { std::vector bundleDetailInfoList; for (size_t pos = 0; pos < bundleNameIndexStrs.size(); pos++) { std::string bundleNameIndexStr = bundleNameIndexStrs[pos]; BundleDetailInfo bundleDetailInfo; + bundleDetailInfo.userId = userId; size_t hasPos = bundleNameIndexStr.find(patternInfo); if (hasPos == std::string::npos) { bundleDetailInfo.bundleName = bundleNameIndexStr; bundleDetailInfo.bundleIndex = BUNDLE_INDEX_DEFAULT_VAL; - realBundleNames.push_back(bundleNameIndexStr); + realBundleNames.emplace_back(bundleNameIndexStr); } else { std::string bundleName = bundleNameIndexStr.substr(0, hasPos); std::string indexStr = bundleNameIndexStr.substr(hasPos + 1); int index = std::stoi(indexStr); bundleDetailInfo.bundleName = bundleName; bundleDetailInfo.bundleIndex = index; - realBundleNames.push_back(bundleNameIndexStr); + realBundleNames.emplace_back(bundleName); } if (pos < details.size()) { std::string bundleDetailStr = details[pos]; ParseBundleDetailInfo(bundleDetailStr, bundleDetailInfo); } - bundleDetailInfoList.push_back(bundleDetailInfo); + bundleDetailInfoList.emplace_back(bundleDetailInfo); } return bundleDetailInfoList; } -- Gitee From 46e150cc5c14ff55fba7ea3b361b22185493759e Mon Sep 17 00:00:00 2001 From: "yaoruozi1@huawei.com" Date: Fri, 29 Mar 2024 20:41:25 +0800 Subject: [PATCH 05/11] =?UTF-8?q?=E5=A4=87=E4=BB=BD=E6=81=A2=E5=A4=8D?= =?UTF-8?q?=E6=A1=86=E6=9E=B6=E6=96=B0=E5=A2=9E=E6=81=A2=E5=A4=8D=E4=BA=8B?= =?UTF-8?q?=E4=BB=B6=E4=B8=8B=E5=8F=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yaoruozi1@huawei.com --- .../js/backup/session_restore_n_exporter.cpp | 36 +++++++++++-------- .../js/backup/session_restore_n_exporter.h | 3 ++ tests/mock/module_ipc/service_stub_mock.cpp | 2 +- 3 files changed, 26 insertions(+), 15 deletions(-) diff --git a/interfaces/kits/js/backup/session_restore_n_exporter.cpp b/interfaces/kits/js/backup/session_restore_n_exporter.cpp index b3b2ce53c..b184224b5 100644 --- a/interfaces/kits/js/backup/session_restore_n_exporter.cpp +++ b/interfaces/kits/js/backup/session_restore_n_exporter.cpp @@ -300,35 +300,32 @@ napi_value SessionRestoreNExporter::Constructor(napi_env env, napi_callback_info return funcArg.GetThisVar(); } -napi_value SessionRestoreNExporter::AppendBundles(napi_env env, napi_callback_info cbinfo) +bool SessionRestoreNExporter::DealArgs(NFuncArg funcArg, + int32 &fd, std::vector &bundles, std::vector &bundleDetails) { - HILOGI("called SessionRestore::AppendBundles begin"); - NFuncArg funcArg(env, cbinfo); if (!funcArg.InitArgs(NARG_CNT::TWO, NARG_CNT::THREE)) { HILOGE("Number of arguments unmatched."); NError(BError(BError::Codes::SDK_INVAL_ARG, "Number of arguments unmatched.").GetCode()).ThrowErr(env); - return nullptr; + return false; } - NVal remoteCap(env, funcArg[NARG_POS::FIRST]); auto [err, fd] = remoteCap.ToInt32(); + fd = fd; if (!err) { HILOGE("First argument is not remote capabilitily file number."); NError(BError(BError::Codes::SDK_INVAL_ARG, "First argument is not remote capabilitily file number.").GetCode()) .ThrowErr(env); - return nullptr; + return false; } - NVal jsBundles(env, funcArg[NARG_POS::SECOND]); auto [succ, bundles, ignore] = jsBundles.ToStringArray(); if (!succ) { HILOGE("First argument is not bundles array."); NError(BError(BError::Codes::SDK_INVAL_ARG, "First argument is not bundles array.").GetCode()).ThrowErr(env); - return nullptr; + return false; } - + bundles = bundles; NVal jsDetails(env, funcArg[NARG_POS::THIRD]); - std::vector bundleDetails; if (jsDetails.TypeIs(napi_undefined) || jsDetails.TypeIs(napi_null)) { HILOGW("Third param is not exist"); } else { @@ -337,16 +334,29 @@ napi_value SessionRestoreNExporter::AppendBundles(napi_env env, napi_callback_in HILOGE("Third argument is not bundles array."); NError(BError(BError::Codes::SDK_INVAL_ARG, "Third argument is not bundles array.").GetCode()) .ThrowErr(env); - return nullptr; + return false; } } + bundleDetails = bundleDetails; + return true; +} + +napi_value SessionRestoreNExporter::AppendBundles(napi_env env, napi_callback_info cbinfo) +{ + HILOGI("called SessionRestore::AppendBundles begin"); + int32 fd = -1; + std::vector bundles; + std::vector bundleDetails; + NFuncArg funcArg(env, cbinfo); + if (!DealArgs(funcArg, fd, bundles, bundleDetails)) { + return nullptr; + } auto restoreEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); if (!(restoreEntity && (restoreEntity->sessionWhole || restoreEntity->sessionSheet))) { HILOGE("Failed to get RestoreSession entity."); NError(BError(BError::Codes::SDK_INVAL_ARG, "Failed to get RestoreSession entity.").GetCode()).ThrowErr(env); return nullptr; } - auto cbExec = [entity {restoreEntity}, fd {fd}, bundles {bundles}, bundleDetails {bundleDetails}]() -> NError { if (!(entity && (entity->sessionWhole || entity->sessionSheet))) { return NError(BError(BError::Codes::SDK_INVAL_ARG, "restore session is nullptr").GetCode()); @@ -359,9 +369,7 @@ napi_value SessionRestoreNExporter::AppendBundles(napi_env env, napi_callback_in auto cbCompl = [](napi_env env, NError err) -> NVal { return err ? NVal {env, err.GetNapiErr(env)} : NVal::CreateUndefined(env); }; - HILOGI("Called SessionRestore::AppendBundles end."); - NVal thisVar(env, funcArg.GetThisVar()); if (funcArg.GetArgc() == NARG_CNT::TWO) { return NAsyncWorkPromise(env, thisVar).Schedule(className, cbExec, cbCompl).val_; diff --git a/interfaces/kits/js/backup/session_restore_n_exporter.h b/interfaces/kits/js/backup/session_restore_n_exporter.h index 49e1f6388..300606f63 100644 --- a/interfaces/kits/js/backup/session_restore_n_exporter.h +++ b/interfaces/kits/js/backup/session_restore_n_exporter.h @@ -36,6 +36,9 @@ public: SessionRestoreNExporter(napi_env env, napi_value exports); ~SessionRestoreNExporter() override; +private: + bool DealArgs(NFuncArg funcArg, int32 &fd, + std::vector &bundles, std::vector &bundleDetails); }; } // namespace OHOS::FileManagement::Backup #endif // INTERFACES_KITS_JS_SRC_MOD_BACKUP_PROPERTIES_SESSION_RESTORE_N_EXPORTER_H \ No newline at end of file diff --git a/tests/mock/module_ipc/service_stub_mock.cpp b/tests/mock/module_ipc/service_stub_mock.cpp index c5f1cfcf2..16414f248 100644 --- a/tests/mock/module_ipc/service_stub_mock.cpp +++ b/tests/mock/module_ipc/service_stub_mock.cpp @@ -154,7 +154,7 @@ int32_t ServiceStub::CmdAppendBundlesRestoreSession(MessageParcel &data, Message data.ReadStringVector(&bundleNames); std::vector detailInfos; data.ReadStringVector(&detailInfos); - int res = AppendBundlesRestoreSession(move(fd), bundleNames, detailInfos;); + int res = AppendBundlesRestoreSession(move(fd), bundleNames, detailInfos); reply.WriteInt32(res); return BError(BError::Codes::OK); } -- Gitee From ee8053e51a999a5b4010e2802e3084c5a1e7e639 Mon Sep 17 00:00:00 2001 From: "yaoruozi1@huawei.com" Date: Sun, 31 Mar 2024 10:27:56 +0800 Subject: [PATCH 06/11] =?UTF-8?q?=E5=A4=87=E4=BB=BD=E6=81=A2=E5=A4=8D?= =?UTF-8?q?=E6=A1=86=E6=9E=B6=E5=A2=9E=E5=8A=A0=E6=81=A2=E5=A4=8D=E4=BA=8B?= =?UTF-8?q?=E4=BB=B6=E4=B8=8B=E5=8F=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yaoruozi1@huawei.com --- tests/unittests/backup_utils/BUILD.gn | 30 ++ .../b_jsonutil/b_jsonutil_test.cpp | 320 ++++++++++++++++++ 2 files changed, 350 insertions(+) create mode 100644 tests/unittests/backup_utils/b_jsonutil/b_jsonutil_test.cpp diff --git a/tests/unittests/backup_utils/BUILD.gn b/tests/unittests/backup_utils/BUILD.gn index f50e4d759..61f050541 100644 --- a/tests/unittests/backup_utils/BUILD.gn +++ b/tests/unittests/backup_utils/BUILD.gn @@ -68,6 +68,36 @@ ohos_unittest("b_file_test") { use_exceptions = true } +ohos_unittest("b_jsonutil_test") { + branch_protector_ret = "pac_ret" + sanitize = { + integer_overflow = true + cfi = true + cfi_cross_dso = true + debug = false + } + + module_out_path = path_module_out_tests + + sources = [ + "b_jsonutil/b_jsonutil_test.cpp", + ] + + deps = [ + "${path_backup}/tests/utils:backup_test_utils", + "${path_backup}/utils/:backup_utils", + "${path_jsoncpp}:jsoncpp", + "//third_party/cJSON:cjson", + ] + + external_deps = [ + "c_utils:utils", + "hilog:libhilog", + ] + + use_exceptions = true +} + ohos_unittest("b_json_test") { branch_protector_ret = "pac_ret" diff --git a/tests/unittests/backup_utils/b_jsonutil/b_jsonutil_test.cpp b/tests/unittests/backup_utils/b_jsonutil/b_jsonutil_test.cpp new file mode 100644 index 000000000..9d4a6d7d2 --- /dev/null +++ b/tests/unittests/backup_utils/b_jsonutil/b_jsonutil_test.cpp @@ -0,0 +1,320 @@ +/* + * Copyright (c) 2022-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include + +#include +#include + +#include +#include +#include + +#include "b_jsonutil/b_jsonutil.h" +#include "b_process/b_process.h" +#include "test_manager.h" + +namespace OHOS::FileManagement::Backup { +using namespace std; + +class BJsonUtilTest : public testing::Test { +public: + static void SetUpTestCase(void) {}; + static void TearDownTestCase() {}; + void SetUp() {}; + void TearDown() {}; +}; + +/** + * @tc.number: SUB_backup_b_dir_GetDirFiles_0100 + * @tc.name: b_dir_GetDirFiles_0100 + * @tc.desc: Test function of GetDirFiles interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 0 + * @tc.require: I6F3GV + */ +HWTEST_F(BJsonUtilTest, b_jsonutil_ParseBundleNameIndexStr_0100, testing::ext::TestSize.Level0) +{ + GTEST_LOG_(INFO) << "BJsonUtilTest-begin b_dir_GetDirFiles_0100"; + try { + std::string bundleName = "com.hos.app01:1"; + std::string pattern = ":"; + BJsonUtil::BundleDetailInfo detailInfo = BJsonUtil::ParseBundleNameIndexStr(bundleName, pattern); + EXPECT_EQ("com.hos.app01", detailInfo.bundleName); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "BDirTest-an exception occurred."; + } + GTEST_LOG_(INFO) << "BDirTest-end b_dir_GetDirFiles_0100"; +} + +/** + * @tc.number: SUB_backup_b_dir_GetDirFiles_0104 + * @tc.name: b_dir_GetDirFiles_0104 + * @tc.desc: Test function of GetDirFiles interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 0 + * @tc.require: I6F3GV + */ +HWTEST_F(BJsonUtilTest, b_jsonutil_ParseBundleNameIndexStr_0100, testing::ext::TestSize.Level0) +{ + GTEST_LOG_(INFO) << "BJsonUtilTest-begin b_dir_GetDirFiles_0104"; + try { + std::vector bundleNames; + std::string bundleName = "com.hos.app01:1"; + std::string bundleName1 = "com.hos.app02"; + bundleNames.push_back(bundleName); + bundleNames.push_back(bundleName1); + std::string pattern = ":"; + std::vector detailInfos; + std::string detail01 = "{ + "infos" : [ + { + "details" : [ + { + "detail" : [ + { + "source" : "com.ohos.app01", + "target" : "com.hos.app01" + } + ], + "type" : "app_mapping_relation" + } + ], + "type" : "broadcast" + } + ] + }"; + detailInfos.push_back(detail01); + int32_t userId = 100; + std::vector realBundleNames; + std::vector detailInfos = BJsonUtil::ConvertBundleDetailInfos(bundleNames, detailInfos, pattern, + realBundleNames, userId); + EXPECT_EQ("com.hos.app01", detailInfos.at(0)); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "BDirTest-an exception occurred."; + } + GTEST_LOG_(INFO) << "BDirTest-end b_dir_GetDirFiles_0104"; +} + + +/** + * @tc.number: SUB_backup_b_dir_GetBigFiles_0100 + * @tc.name: b_dir_GetBigFiles_0100 + * @tc.desc: 测试GetBigFiles接口是否能成功获取大文件 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I6F3GV + */ +HWTEST_F(BDirTest, b_dir_GetBigFiles_0100, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "BDirTest-begin b_dir_GetBigFiles_0100"; + try { + TestManager tm("b_dir_GetBigFiles_0100"); + string rootDir = tm.GetRootDirCurTest(); + string filePath1 = rootDir + "a.txt"; + string filePath2 = rootDir + "b.txt"; + // 文件大小大于等于1GB(1024MB)的文件属于大文件,因此这里创建大小为1025MB和1026MB的大文件 + auto [bFatalErr, ret] = + BProcess::ExecuteCmd({"dd", "if=/dev/urandom", ("of=" + filePath1).c_str(), "bs=1M", "count=1025"}); + EXPECT_FALSE(bFatalErr); + EXPECT_EQ(ret, 0); + tie(bFatalErr, ret) = + BProcess::ExecuteCmd({"dd", "if=/dev/urandom", ("of=" + filePath2).c_str(), "bs=1M", "count=1026"}); + EXPECT_FALSE(bFatalErr); + EXPECT_EQ(ret, 0); + vector includes = {rootDir}; + vector excludes = {filePath2}; + auto [errCode, mpNameToStat, smallFiles] = BDir::GetBigFiles(includes, excludes); + EXPECT_EQ(errCode, ERR_OK); + EXPECT_EQ(mpNameToStat.at(filePath1).st_size, 1024 * 1024 * 1025); + EXPECT_EQ(mpNameToStat.find(filePath2), mpNameToStat.end()); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "BDirTest-an exception occurred."; + } + GTEST_LOG_(INFO) << "BDirTest-end b_dir_GetBigFiles_0100"; +} + +/** + * @tc.number: SUB_backup_b_dir_GetBigFiles_0200 + * @tc.name: b_dir_GetBigFiles_0200 + * @tc.desc: 测试GetBigFiles接口 分支逻辑 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I6F3GV + */ +HWTEST_F(BDirTest, b_dir_GetBigFiles_0200, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "BDirTest-begin b_dir_GetBigFiles_0200"; + try { + vector includes = {{}, {}}; + vector excludes = {{}}; + auto [errCode, mpNameToStat, smallFiles] = BDir::GetBigFiles(includes, excludes); + EXPECT_EQ(errCode, ERR_OK); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "BDirTest-an exception occurred."; + } + GTEST_LOG_(INFO) << "BDirTest-end b_dir_GetBigFiles_0200"; +} + +/** + * @tc.number: SUB_backup_b_dir_GetBigFiles_0201 + * @tc.name: b_dir_GetBigFiles_0201 + * @tc.desc: 测试GetBigFiles接口 分支逻辑 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I6F3GV + */ +HWTEST_F(BDirTest, b_dir_GetBigFiles_0201, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "BDirTest-begin b_dir_GetBigFiles_0201"; + try { + vector includes = {"/data/"}; + vector excludes; + auto [errCode, mpNameToStat, smallFiles] = BDir::GetBigFiles(includes, excludes); + EXPECT_EQ(errCode, ERR_OK); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "BDirTest-an exception occurred."; + } + GTEST_LOG_(INFO) << "BDirTest-end b_dir_GetBigFiles_0201"; +} + +/** + * @tc.number: SUB_backup_b_dir_GetBigFiles_0202 + * @tc.name: b_dir_GetBigFiles_0202 + * @tc.desc: 测试GetBigFiles接口 分支逻辑 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I6F3GV + */ +HWTEST_F(BDirTest, b_dir_GetBigFiles_0202, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "BDirTest-begin b_dir_GetBigFiles_0202"; + try { + vector includes = {"/data/app/"}; + vector excludes; + auto [errCode, mpNameToStat, smallFiles] = BDir::GetBigFiles(includes, excludes); + EXPECT_EQ(errCode, ERR_OK); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "BDirTest-an exception occurred."; + } + GTEST_LOG_(INFO) << "BDirTest-end b_dir_GetBigFiles_0202"; +} + +/** + * @tc.number: SUB_backup_b_dir_GetBigFiles_0203 + * @tc.name: b_dir_GetBigFiles_0203 + * @tc.desc: 测试GetBigFiles接口 分支逻辑 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I6F3GV + */ +HWTEST_F(BDirTest, b_dir_GetBigFiles_0203, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "BDirTest-begin b_dir_GetBigFiles_0203"; + try { + vector includes; + vector excludes; + const string str = ""; + auto [errCode, mpNameToStat, smallFiles] = BDir::GetBigFiles(includes, excludes); + EXPECT_EQ(errCode, ERR_OK); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "BDirTest-an exception occurred."; + } + GTEST_LOG_(INFO) << "BDirTest-end b_dir_GetBigFiles_0203"; +} + +/** + * @tc.number: SUB_backup_b_dir_GetBigFiles_0300 + * @tc.name: b_dir_GetBigFiles_0300 + * @tc.desc: 测试GetBigFiles接口 分支逻辑 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I6F3GV + */ +HWTEST_F(BDirTest, b_dir_GetBigFiles_0300, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "BDirTest-begin b_dir_GetBigFiles_0300"; + try { + TestManager tm("b_dir_GetBigFiles_0300"); + string preparedDir = tm.GetRootDirCurTest(); + string cmdMkdir = string("mkdir -p ") + preparedDir + string("test/test1/test2"); + system(cmdMkdir.c_str()); + string touchFilePrefix = string("touch ") + preparedDir; + system(touchFilePrefix.append("a.txt").c_str()); + system(touchFilePrefix.append("b.txt").c_str()); + system(touchFilePrefix.append("c.txt").c_str()); + + touchFilePrefix = string("touch ") + preparedDir + string("test/"); + system(touchFilePrefix.append("a.txt").c_str()); + system(touchFilePrefix.append("b.txt").c_str()); + system(touchFilePrefix.append("c.txt").c_str()); + touchFilePrefix = string("touch ") + preparedDir + string("test/test1/test2"); + system(touchFilePrefix.append("a.txt").c_str()); + system(touchFilePrefix.append("b.txt").c_str()); + system(touchFilePrefix.append("c.txt").c_str()); + vector includes = {preparedDir + string("/*"), preparedDir + string("test")}; + vector excludes = {preparedDir + string("/test/test1/test2"), {}}; + auto [errCode, mpNameToStat, smallFiles] = BDir::GetBigFiles(includes, excludes); + EXPECT_EQ(errCode, ERR_OK); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "BDirTest-an exception occurred."; + } + GTEST_LOG_(INFO) << "BDirTest-end b_dir_GetBigFiles_0300"; +} + +/** + * @tc.number: SUB_backup_b_dir_GetDirs_0100 + * @tc.name: b_dir_GetDirs_0100 + * @tc.desc: Test function of GetDirs interface for SUCCESS + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I6F3GV + */ +HWTEST_F(BDirTest, b_dir_GetDirs_0100, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "BDirTest-begin b_dir_GetDirs_0100"; + try { + TestManager tm("b_dir_GetDirs_0100"); + vector paths; + vector res = BDir::GetDirs(paths); + set inc(paths.begin(), paths.end()); + bool result = equal(inc.begin(), inc.end(), res.begin(), res.end()); + EXPECT_EQ(1, result); + EXPECT_EQ(inc.size(), res.size()); + } catch (...) { + GTEST_LOG_(INFO) << "BDirTest-an exception occurred."; + } + GTEST_LOG_(INFO) << "BDirTest-end b_dir_GetDirs_0100"; +} + +} // namespace OHOS::FileManagement::Backup \ No newline at end of file -- Gitee From e9281bb6d807d6069c97727c1f234022d4ffa703 Mon Sep 17 00:00:00 2001 From: "yaoruozi1@huawei.com" Date: Sun, 31 Mar 2024 10:47:45 +0800 Subject: [PATCH 07/11] =?UTF-8?q?=E5=A4=87=E4=BB=BD=E6=81=A2=E5=A4=8D?= =?UTF-8?q?=E6=A1=86=E6=9E=B6=E5=A2=9E=E5=8A=A0=E6=81=A2=E5=A4=8D=E4=BA=8B?= =?UTF-8?q?=E4=BB=B6=E4=B8=8B=E5=8F=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yaoruozi1@huawei.com --- .../b_jsonutil/b_jsonutil_test.cpp | 228 +----------------- tools/backup_tool/src/tools_op_restore.cpp | 7 + 2 files changed, 19 insertions(+), 216 deletions(-) diff --git a/tests/unittests/backup_utils/b_jsonutil/b_jsonutil_test.cpp b/tests/unittests/backup_utils/b_jsonutil/b_jsonutil_test.cpp index 9d4a6d7d2..862250925 100644 --- a/tests/unittests/backup_utils/b_jsonutil/b_jsonutil_test.cpp +++ b/tests/unittests/backup_utils/b_jsonutil/b_jsonutil_test.cpp @@ -39,9 +39,9 @@ public: }; /** - * @tc.number: SUB_backup_b_dir_GetDirFiles_0100 - * @tc.name: b_dir_GetDirFiles_0100 - * @tc.desc: Test function of GetDirFiles interface for SUCCESS. + * @tc.number: b_jsonutil_ParseBundleNameIndexStr_0100 + * @tc.name: b_jsonutil_ParseBundleNameIndexStr_0100 + * @tc.desc: Test function of ParseBundleNameIndexStr interface for SUCCESS. * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 0 @@ -63,17 +63,17 @@ HWTEST_F(BJsonUtilTest, b_jsonutil_ParseBundleNameIndexStr_0100, testing::ext::T } /** - * @tc.number: SUB_backup_b_dir_GetDirFiles_0104 - * @tc.name: b_dir_GetDirFiles_0104 - * @tc.desc: Test function of GetDirFiles interface for SUCCESS. + * @tc.number: b_jsonutil_ConvertBundleDetailInfos_0100 + * @tc.name: b_jsonutil_ConvertBundleDetailInfos_0100 + * @tc.desc: Test function of ConvertBundleDetailInfos interface for SUCCESS. * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 0 * @tc.require: I6F3GV */ -HWTEST_F(BJsonUtilTest, b_jsonutil_ParseBundleNameIndexStr_0100, testing::ext::TestSize.Level0) +HWTEST_F(BJsonUtilTest, b_jsonutil_ConvertBundleDetailInfos_0100, testing::ext::TestSize.Level0) { - GTEST_LOG_(INFO) << "BJsonUtilTest-begin b_dir_GetDirFiles_0104"; + GTEST_LOG_(INFO) << "BJsonUtilTest-begin ConvertBundleDetailInfoss_0100"; try { std::vector bundleNames; std::string bundleName = "com.hos.app01:1"; @@ -103,218 +103,14 @@ HWTEST_F(BJsonUtilTest, b_jsonutil_ParseBundleNameIndexStr_0100, testing::ext::T detailInfos.push_back(detail01); int32_t userId = 100; std::vector realBundleNames; - std::vector detailInfos = BJsonUtil::ConvertBundleDetailInfos(bundleNames, detailInfos, pattern, + std::vector detailInfos = + BJsonUtil::ConvertBundleDetailInfos(bundleNames, detailInfos, pattern, realBundleNames, userId); EXPECT_EQ("com.hos.app01", detailInfos.at(0)); } catch (...) { EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "BDirTest-an exception occurred."; - } - GTEST_LOG_(INFO) << "BDirTest-end b_dir_GetDirFiles_0104"; -} - - -/** - * @tc.number: SUB_backup_b_dir_GetBigFiles_0100 - * @tc.name: b_dir_GetBigFiles_0100 - * @tc.desc: 测试GetBigFiles接口是否能成功获取大文件 - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - * @tc.require: I6F3GV - */ -HWTEST_F(BDirTest, b_dir_GetBigFiles_0100, testing::ext::TestSize.Level1) -{ - GTEST_LOG_(INFO) << "BDirTest-begin b_dir_GetBigFiles_0100"; - try { - TestManager tm("b_dir_GetBigFiles_0100"); - string rootDir = tm.GetRootDirCurTest(); - string filePath1 = rootDir + "a.txt"; - string filePath2 = rootDir + "b.txt"; - // 文件大小大于等于1GB(1024MB)的文件属于大文件,因此这里创建大小为1025MB和1026MB的大文件 - auto [bFatalErr, ret] = - BProcess::ExecuteCmd({"dd", "if=/dev/urandom", ("of=" + filePath1).c_str(), "bs=1M", "count=1025"}); - EXPECT_FALSE(bFatalErr); - EXPECT_EQ(ret, 0); - tie(bFatalErr, ret) = - BProcess::ExecuteCmd({"dd", "if=/dev/urandom", ("of=" + filePath2).c_str(), "bs=1M", "count=1026"}); - EXPECT_FALSE(bFatalErr); - EXPECT_EQ(ret, 0); - vector includes = {rootDir}; - vector excludes = {filePath2}; - auto [errCode, mpNameToStat, smallFiles] = BDir::GetBigFiles(includes, excludes); - EXPECT_EQ(errCode, ERR_OK); - EXPECT_EQ(mpNameToStat.at(filePath1).st_size, 1024 * 1024 * 1025); - EXPECT_EQ(mpNameToStat.find(filePath2), mpNameToStat.end()); - } catch (...) { - EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "BDirTest-an exception occurred."; - } - GTEST_LOG_(INFO) << "BDirTest-end b_dir_GetBigFiles_0100"; -} - -/** - * @tc.number: SUB_backup_b_dir_GetBigFiles_0200 - * @tc.name: b_dir_GetBigFiles_0200 - * @tc.desc: 测试GetBigFiles接口 分支逻辑 - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - * @tc.require: I6F3GV - */ -HWTEST_F(BDirTest, b_dir_GetBigFiles_0200, testing::ext::TestSize.Level1) -{ - GTEST_LOG_(INFO) << "BDirTest-begin b_dir_GetBigFiles_0200"; - try { - vector includes = {{}, {}}; - vector excludes = {{}}; - auto [errCode, mpNameToStat, smallFiles] = BDir::GetBigFiles(includes, excludes); - EXPECT_EQ(errCode, ERR_OK); - } catch (...) { - EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "BDirTest-an exception occurred."; - } - GTEST_LOG_(INFO) << "BDirTest-end b_dir_GetBigFiles_0200"; -} - -/** - * @tc.number: SUB_backup_b_dir_GetBigFiles_0201 - * @tc.name: b_dir_GetBigFiles_0201 - * @tc.desc: 测试GetBigFiles接口 分支逻辑 - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - * @tc.require: I6F3GV - */ -HWTEST_F(BDirTest, b_dir_GetBigFiles_0201, testing::ext::TestSize.Level1) -{ - GTEST_LOG_(INFO) << "BDirTest-begin b_dir_GetBigFiles_0201"; - try { - vector includes = {"/data/"}; - vector excludes; - auto [errCode, mpNameToStat, smallFiles] = BDir::GetBigFiles(includes, excludes); - EXPECT_EQ(errCode, ERR_OK); - } catch (...) { - EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "BDirTest-an exception occurred."; + GTEST_LOG_(INFO) << "BJsonUtilTest-an exception occurred."; } - GTEST_LOG_(INFO) << "BDirTest-end b_dir_GetBigFiles_0201"; + GTEST_LOG_(INFO) << "BJsonUtilTest-end b_dir_ConvertBundleDetailInfos_0100"; } - -/** - * @tc.number: SUB_backup_b_dir_GetBigFiles_0202 - * @tc.name: b_dir_GetBigFiles_0202 - * @tc.desc: 测试GetBigFiles接口 分支逻辑 - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - * @tc.require: I6F3GV - */ -HWTEST_F(BDirTest, b_dir_GetBigFiles_0202, testing::ext::TestSize.Level1) -{ - GTEST_LOG_(INFO) << "BDirTest-begin b_dir_GetBigFiles_0202"; - try { - vector includes = {"/data/app/"}; - vector excludes; - auto [errCode, mpNameToStat, smallFiles] = BDir::GetBigFiles(includes, excludes); - EXPECT_EQ(errCode, ERR_OK); - } catch (...) { - EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "BDirTest-an exception occurred."; - } - GTEST_LOG_(INFO) << "BDirTest-end b_dir_GetBigFiles_0202"; -} - -/** - * @tc.number: SUB_backup_b_dir_GetBigFiles_0203 - * @tc.name: b_dir_GetBigFiles_0203 - * @tc.desc: 测试GetBigFiles接口 分支逻辑 - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - * @tc.require: I6F3GV - */ -HWTEST_F(BDirTest, b_dir_GetBigFiles_0203, testing::ext::TestSize.Level1) -{ - GTEST_LOG_(INFO) << "BDirTest-begin b_dir_GetBigFiles_0203"; - try { - vector includes; - vector excludes; - const string str = ""; - auto [errCode, mpNameToStat, smallFiles] = BDir::GetBigFiles(includes, excludes); - EXPECT_EQ(errCode, ERR_OK); - } catch (...) { - EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "BDirTest-an exception occurred."; - } - GTEST_LOG_(INFO) << "BDirTest-end b_dir_GetBigFiles_0203"; -} - -/** - * @tc.number: SUB_backup_b_dir_GetBigFiles_0300 - * @tc.name: b_dir_GetBigFiles_0300 - * @tc.desc: 测试GetBigFiles接口 分支逻辑 - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - * @tc.require: I6F3GV - */ -HWTEST_F(BDirTest, b_dir_GetBigFiles_0300, testing::ext::TestSize.Level1) -{ - GTEST_LOG_(INFO) << "BDirTest-begin b_dir_GetBigFiles_0300"; - try { - TestManager tm("b_dir_GetBigFiles_0300"); - string preparedDir = tm.GetRootDirCurTest(); - string cmdMkdir = string("mkdir -p ") + preparedDir + string("test/test1/test2"); - system(cmdMkdir.c_str()); - string touchFilePrefix = string("touch ") + preparedDir; - system(touchFilePrefix.append("a.txt").c_str()); - system(touchFilePrefix.append("b.txt").c_str()); - system(touchFilePrefix.append("c.txt").c_str()); - - touchFilePrefix = string("touch ") + preparedDir + string("test/"); - system(touchFilePrefix.append("a.txt").c_str()); - system(touchFilePrefix.append("b.txt").c_str()); - system(touchFilePrefix.append("c.txt").c_str()); - touchFilePrefix = string("touch ") + preparedDir + string("test/test1/test2"); - system(touchFilePrefix.append("a.txt").c_str()); - system(touchFilePrefix.append("b.txt").c_str()); - system(touchFilePrefix.append("c.txt").c_str()); - vector includes = {preparedDir + string("/*"), preparedDir + string("test")}; - vector excludes = {preparedDir + string("/test/test1/test2"), {}}; - auto [errCode, mpNameToStat, smallFiles] = BDir::GetBigFiles(includes, excludes); - EXPECT_EQ(errCode, ERR_OK); - } catch (...) { - EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "BDirTest-an exception occurred."; - } - GTEST_LOG_(INFO) << "BDirTest-end b_dir_GetBigFiles_0300"; -} - -/** - * @tc.number: SUB_backup_b_dir_GetDirs_0100 - * @tc.name: b_dir_GetDirs_0100 - * @tc.desc: Test function of GetDirs interface for SUCCESS - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - * @tc.require: I6F3GV - */ -HWTEST_F(BDirTest, b_dir_GetDirs_0100, testing::ext::TestSize.Level1) -{ - GTEST_LOG_(INFO) << "BDirTest-begin b_dir_GetDirs_0100"; - try { - TestManager tm("b_dir_GetDirs_0100"); - vector paths; - vector res = BDir::GetDirs(paths); - set inc(paths.begin(), paths.end()); - bool result = equal(inc.begin(), inc.end(), res.begin(), res.end()); - EXPECT_EQ(1, result); - EXPECT_EQ(inc.size(), res.size()); - } catch (...) { - GTEST_LOG_(INFO) << "BDirTest-an exception occurred."; - } - GTEST_LOG_(INFO) << "BDirTest-end b_dir_GetDirs_0100"; -} - } // namespace OHOS::FileManagement::Backup \ No newline at end of file diff --git a/tools/backup_tool/src/tools_op_restore.cpp b/tools/backup_tool/src/tools_op_restore.cpp index 32e5632eb..4648d0b2d 100644 --- a/tools/backup_tool/src/tools_op_restore.cpp +++ b/tools/backup_tool/src/tools_op_restore.cpp @@ -158,6 +158,12 @@ static void OnBundleFinished(shared_ptr ctx, ErrCode err, const BundleN ctx->TryNotify(); } +static void OnResultReport(shared_ptr ctx, ErrCode err, const std::string resultInfo) +{ + printf("BundleFinished errCode = %d, detailInfo = %s\n", err, resultInfo.c_str()); + ctx->TryNotify(true); +} + static void OnAllBundlesFinished(shared_ptr ctx, ErrCode err) { ctx->isAllBundelsFinished.store(true); @@ -235,6 +241,7 @@ static int32_t InitRestoreSession(shared_ptr ctx) BSessionRestore::Callbacks {.onFileReady = bind(OnFileReady, ctx, placeholders::_1, placeholders::_2), .onBundleStarted = bind(OnBundleStarted, ctx, placeholders::_1, placeholders::_2), .onBundleFinished = bind(OnBundleFinished, ctx, placeholders::_1, placeholders::_2), + .onResultReport = bind(OnResultReport, cxt, placeholders::_1, placeholders::_2); .onAllBundlesFinished = bind(OnAllBundlesFinished, ctx, placeholders::_1), .onBackupServiceDied = bind(OnBackupServiceDied, ctx)}); if (ctx->session_ == nullptr) { -- Gitee From 9d752441b5db479e0f3bff50cc7f6bbe87c1047b Mon Sep 17 00:00:00 2001 From: "yaoruozi1@huawei.com" Date: Sun, 31 Mar 2024 13:27:02 +0800 Subject: [PATCH 08/11] =?UTF-8?q?=E5=A4=87=E4=BB=BD=E6=81=A2=E5=A4=8D?= =?UTF-8?q?=E6=A1=86=E6=9E=B6=E5=A2=9E=E5=8A=A0=E6=81=A2=E5=A4=8D=E4=BA=8B?= =?UTF-8?q?=E4=BB=B6=E4=B8=8B=E5=8F=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yaoruozi1@huawei.com --- .../js/backup/session_restore_n_exporter.cpp | 86 +++++++++---------- .../js/backup/session_restore_n_exporter.h | 3 - services/backup_sa/BUILD.gn | 4 +- .../backup_sa/include/module_ipc/service.h | 13 ++- services/backup_sa/src/module_ipc/service.cpp | 29 ++++--- .../module_ipc/service_stub_test.cpp | 2 +- tools/backup_tool/src/tools_op_restore.cpp | 6 +- utils/include/b_jsonutil/b_jsonutil.h | 4 +- utils/src/b_jsonutil/b_jsonutil.cpp | 6 +- 9 files changed, 83 insertions(+), 70 deletions(-) diff --git a/interfaces/kits/js/backup/session_restore_n_exporter.cpp b/interfaces/kits/js/backup/session_restore_n_exporter.cpp index 8b65337ee..eeafe3a11 100644 --- a/interfaces/kits/js/backup/session_restore_n_exporter.cpp +++ b/interfaces/kits/js/backup/session_restore_n_exporter.cpp @@ -278,6 +278,47 @@ static void onResultReport(weak_ptr pCallbacks, const std::str callbacks->onResultReport.ThreadSafeSchedule(cbCompl); } +static bool DealArgs(NFuncArg &funcArg, int32_t fd, std::vector &bundles, + std::vector &bundleDetails, napi_env env) +{ + if (!funcArg.InitArgs(NARG_CNT::TWO, NARG_CNT::THREE)) { + HILOGE("Number of arguments unmatched."); + NError(BError(BError::Codes::SDK_INVAL_ARG, "Number of arguments unmatched.").GetCode()).ThrowErr(env); + return false; + } + NVal remoteCap(env, funcArg[NARG_POS::FIRST]); + auto [err, jsFd] = remoteCap.ToInt32(); + fd = jsFd; + if (!err) { + HILOGE("First argument is not remote capabilitily file number."); + NError(BError(BError::Codes::SDK_INVAL_ARG, "First argument is not remote capabilitily file number.").GetCode()) + .ThrowErr(env); + return false; + } + NVal jsBundles(env, funcArg[NARG_POS::SECOND]); + auto [succ, jsBundlesParam, ignore] = jsBundles.ToStringArray(); + if (!succ) { + HILOGE("First argument is not bundles array."); + NError(BError(BError::Codes::SDK_INVAL_ARG, "First argument is not bundles array.").GetCode()).ThrowErr(env); + return false; + } + bundles = jsBundlesParam; + NVal jsDetails(env, funcArg[NARG_POS::THIRD]); + if (jsDetails.TypeIs(napi_undefined) || jsDetails.TypeIs(napi_null)) { + HILOGW("Third param is not exist"); + } else { + auto [deSuc, jsBundleDetails, deIgnore] = jsDetails.ToStringArray(); + if (!deSuc) { + HILOGE("Third argument is not bundles array."); + NError(BError(BError::Codes::SDK_INVAL_ARG, "Third argument is not bundles array.").GetCode()) + .ThrowErr(env); + return false; + } + bundleDetails = jsBundleDetails; + } + return true; +} + napi_value SessionRestoreNExporter::Constructor(napi_env env, napi_callback_info cbinfo) { HILOGI("called SessionRestore::Constructor begin"); @@ -332,55 +373,14 @@ napi_value SessionRestoreNExporter::Constructor(napi_env env, napi_callback_info return funcArg.GetThisVar(); } -bool SessionRestoreNExporter::DealArgs(NFuncArg funcArg, - int32 &fd, std::vector &bundles, std::vector &bundleDetails) -{ - if (!funcArg.InitArgs(NARG_CNT::TWO, NARG_CNT::THREE)) { - HILOGE("Number of arguments unmatched."); - NError(BError(BError::Codes::SDK_INVAL_ARG, "Number of arguments unmatched.").GetCode()).ThrowErr(env); - return false; - } - NVal remoteCap(env, funcArg[NARG_POS::FIRST]); - auto [err, fd] = remoteCap.ToInt32(); - fd = fd; - if (!err) { - HILOGE("First argument is not remote capabilitily file number."); - NError(BError(BError::Codes::SDK_INVAL_ARG, "First argument is not remote capabilitily file number.").GetCode()) - .ThrowErr(env); - return false; - } - NVal jsBundles(env, funcArg[NARG_POS::SECOND]); - auto [succ, bundles, ignore] = jsBundles.ToStringArray(); - if (!succ) { - HILOGE("First argument is not bundles array."); - NError(BError(BError::Codes::SDK_INVAL_ARG, "First argument is not bundles array.").GetCode()).ThrowErr(env); - return false; - } - bundles = bundles; - NVal jsDetails(env, funcArg[NARG_POS::THIRD]); - if (jsDetails.TypeIs(napi_undefined) || jsDetails.TypeIs(napi_null)) { - HILOGW("Third param is not exist"); - } else { - auto [deSuc, bundleDetails, deIgnore] = jsDetails.ToStringArray(); - if (!deSuc) { - HILOGE("Third argument is not bundles array."); - NError(BError(BError::Codes::SDK_INVAL_ARG, "Third argument is not bundles array.").GetCode()) - .ThrowErr(env); - return false; - } - } - bundleDetails = bundleDetails; - return true; -} - napi_value SessionRestoreNExporter::AppendBundles(napi_env env, napi_callback_info cbinfo) { HILOGI("called SessionRestore::AppendBundles begin"); - int32 fd = -1; + int32_t fd = -1; std::vector bundles; std::vector bundleDetails; NFuncArg funcArg(env, cbinfo); - if (!DealArgs(funcArg, fd, bundles, bundleDetails)) { + if (!DealArgs(funcArg, fd, bundles, bundleDetails, env)) { return nullptr; } auto restoreEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); diff --git a/interfaces/kits/js/backup/session_restore_n_exporter.h b/interfaces/kits/js/backup/session_restore_n_exporter.h index 300606f63..49e1f6388 100644 --- a/interfaces/kits/js/backup/session_restore_n_exporter.h +++ b/interfaces/kits/js/backup/session_restore_n_exporter.h @@ -36,9 +36,6 @@ public: SessionRestoreNExporter(napi_env env, napi_value exports); ~SessionRestoreNExporter() override; -private: - bool DealArgs(NFuncArg funcArg, int32 &fd, - std::vector &bundles, std::vector &bundleDetails); }; } // namespace OHOS::FileManagement::Backup #endif // INTERFACES_KITS_JS_SRC_MOD_BACKUP_PROPERTIES_SESSION_RESTORE_N_EXPORTER_H \ No newline at end of file diff --git a/services/backup_sa/BUILD.gn b/services/backup_sa/BUILD.gn index d1088712c..d4d08d4d3 100644 --- a/services/backup_sa/BUILD.gn +++ b/services/backup_sa/BUILD.gn @@ -39,8 +39,8 @@ ohos_shared_library("backup_sa") { "src/module_ipc/svc_extension_proxy.cpp", "src/module_ipc/svc_restore_deps_manager.cpp", "src/module_ipc/svc_session_manager.cpp", - "src/module_sched/sched_scheduler.cpp", "src/module_notify/notify_work_service.cpp", + "src/module_sched/sched_scheduler.cpp", ] defines = [ @@ -66,6 +66,7 @@ ohos_shared_library("backup_sa") { "bundle_framework:appexecfwk_base", "bundle_framework:appexecfwk_core", "c_utils:utils", + "common_event_service:cesfwk_innerkits", "hilog:libhilog", "hitrace:hitrace_meter", "init:libbegetutil", @@ -73,7 +74,6 @@ ohos_shared_library("backup_sa") { "safwk:system_ability_fwk", "samgr:samgr_proxy", "storage_service:storage_manager_sa_proxy", - "common_event_service:cesfwk_innerkits", ] use_exceptions = true diff --git a/services/backup_sa/include/module_ipc/service.h b/services/backup_sa/include/module_ipc/service.h index f9de2128f..5222f0b7b 100644 --- a/services/backup_sa/include/module_ipc/service.h +++ b/services/backup_sa/include/module_ipc/service.h @@ -211,13 +211,24 @@ private: * * @param restoreBundleInfos 待恢复的应用 * @param restoreBundleNames 待恢复的应用包信息 - * @param bundleNameDetailMap bundle和detaild的对应关系 + * @param bundleNameDetailMap bundle和detail的对应关系 * @param restoreType 任务类型 */ void SetCurrentSessProperties(std::vector &restoreBundleInfos, std::vector &restoreBundleNames, std::map &bundleNameDetailMap, RestoreTypeEnum restoreType); + /** + * @brief 通知权限模块 + * + * @param bundleNameDetailMap bundle和detail的对应关系 + * @param restoreInfo 待恢复的应用 + * @param restoreType 任务类型 + * + */ + void NotifyBundleInfos(std::map &bundleNameDetailMap, + BJsonEntityCaps::BundleInfo restoreInfo, RestoreTypeEnum restoreType); + private: static sptr instance_; static std::mutex instanceLock_; diff --git a/services/backup_sa/src/module_ipc/service.cpp b/services/backup_sa/src/module_ipc/service.cpp index eaf7fc2a0..669282bd9 100644 --- a/services/backup_sa/src/module_ipc/service.cpp +++ b/services/backup_sa/src/module_ipc/service.cpp @@ -352,8 +352,8 @@ ErrCode Service::AppendBundlesRestoreSession(UniqueFd fd, } void Service::SetCurrentSessProperties(std::vector &restoreBundleInfos, - std::vector &restoreBundleNames, - std::map &bundleNameDetailMap, RestoreTypeEnum restoreType) + std::vector &restoreBundleNames, + std::map &bundleNameDetailMap, RestoreTypeEnum restoreType) { HILOGI("Start"); for (auto restoreInfo : restoreBundleInfos) { @@ -375,20 +375,27 @@ void Service::SetCurrentSessProperties(std::vector session_->SetBundleVersionName(restoreInfo.name, restoreInfo.versionName); session_->SetBundleDataSize(restoreInfo.name, restoreInfo.spaceOccupied); session_->SetBackupExtName(restoreInfo.name, restoreInfo.extensionName); - if (restoreType == TypeRestoreTypeEnum::RESTORE_DATA_READDY || - SpeicalVersion(restoreInfo.versionName, restoreInfo.versionCode)) { - auto iter = bundleNameDetailMap.find(restoreInfo.name); - if (iter != bundleNameDetailMap.end()) { - BJsonUtil::BundleDetailInfo bundetailInfo = iter->second; - if (bundleDetailInfo.type == COMMON_EVENT_TYPE) { - DelayedSingleton::GetInstance()->NotifyBundleDetail(bundleDetailInfo); - } + NotifyBundleInfos(bundleNameDetailMap, restoreInfo, restoreType); + } + HILOGI("End"); +} + +void Service::NotifyBundleInfos(std::map &bundleNameDetailMap, + BJsonEntityCaps::BundleInfo restoreInfo, RestoreTypeEnum restoreType) +{ + if (restoreType == TypeRestoreTypeEnum::RESTORE_DATA_READDY || + SpeicalVersion(restoreInfo.versionName, restoreInfo.versionCode)) { + auto iter = bundleNameDetailMap.find(restoreInfo.name); + if (iter != bundleNameDetailMap.end()) { + BJsonUtil::BundleDetailInfo bundetailInfo = iter->second; + if (bundleDetailInfo.type == COMMON_EVENT_TYPE) { + DelayedSingleton::GetInstance()->NotifyBundleDetail(bundleDetailInfo); } } } - HILOGI("End"); } + ErrCode Service::AppendBundlesBackupSession(const vector &bundleNames) { HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); 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 a52820e2d..53e4c800d 100644 --- a/tests/unittests/backup_sa/module_ipc/service_stub_test.cpp +++ b/tests/unittests/backup_sa/module_ipc/service_stub_test.cpp @@ -387,7 +387,7 @@ HWTEST_F(ServiceStubTest, SUB_backup_sa_ServiceStub_AppendBundlesRestoreSession_ GTEST_LOG_(INFO) << "ServiceStubTest-begin SUB_backup_sa_ServiceStub_AppendBundlesRestoreSession_0100"; try { MockService service; - EXPECT_CALL(service, AppendBundlesRestoreSession(_, _, _,_, _)).WillOnce(Return(BError(BError::Codes::OK))); + EXPECT_CALL(service, AppendBundlesRestoreSession(_, _, _, _, _)).WillOnce(Return(BError(BError::Codes::OK))); MessageParcel data; MessageParcel reply; MessageOption option; diff --git a/tools/backup_tool/src/tools_op_restore.cpp b/tools/backup_tool/src/tools_op_restore.cpp index 4648d0b2d..8f5694195 100644 --- a/tools/backup_tool/src/tools_op_restore.cpp +++ b/tools/backup_tool/src/tools_op_restore.cpp @@ -158,9 +158,9 @@ static void OnBundleFinished(shared_ptr ctx, ErrCode err, const BundleN ctx->TryNotify(); } -static void OnResultReport(shared_ptr ctx, ErrCode err, const std::string resultInfo) +static void OnResultReport(shared_ptr ctx, const std::string resultInfo) { - printf("BundleFinished errCode = %d, detailInfo = %s\n", err, resultInfo.c_str()); + printf("OnResultReport, detailInfo = %s\n", resultInfo.c_str()); ctx->TryNotify(true); } @@ -241,8 +241,8 @@ static int32_t InitRestoreSession(shared_ptr ctx) BSessionRestore::Callbacks {.onFileReady = bind(OnFileReady, ctx, placeholders::_1, placeholders::_2), .onBundleStarted = bind(OnBundleStarted, ctx, placeholders::_1, placeholders::_2), .onBundleFinished = bind(OnBundleFinished, ctx, placeholders::_1, placeholders::_2), - .onResultReport = bind(OnResultReport, cxt, placeholders::_1, placeholders::_2); .onAllBundlesFinished = bind(OnAllBundlesFinished, ctx, placeholders::_1), + .onResultReport = bind(OnResultReport, ctx, placeholders::_1); .onBackupServiceDied = bind(OnBackupServiceDied, ctx)}); if (ctx->session_ == nullptr) { printf("Failed to init restore\n"); diff --git a/utils/include/b_jsonutil/b_jsonutil.h b/utils/include/b_jsonutil/b_jsonutil.h index af669a3a4..66ff58e9e 100644 --- a/utils/include/b_jsonutil/b_jsonutil.h +++ b/utils/include/b_jsonutil/b_jsonutil.h @@ -29,6 +29,7 @@ public: int bundleIndex; int32_t userId; }BundleDetailInfo; + /** * @brief 带有拼接字符的bundleName按照拼接字符进行分割 * @@ -37,7 +38,7 @@ public: * * @return 分割好的结果赋值给结构体 */ - static BundleDetailInfo ParseBundleNameIndexStr(const std::string &bundleNameStr, const std::string &patternInfo); + static BundleDetailInfo ParseBundleNameIndexStr (const std::string &bundleNameStr, const std::string &patternInfo); /** * @brief 将传进来的bundleNames的集合进行按照拼接字符分割处理 @@ -64,7 +65,6 @@ public: */ static void ParseBundleDetailInfo(const std::string &bundleDetailInfo, BundleDetailInfo &bundleDetail); - /** * @brief 根据bundleName获取对应的detailInfo * diff --git a/utils/src/b_jsonutil/b_jsonutil.cpp b/utils/src/b_jsonutil/b_jsonutil.cpp index 3ad76578b..bafcbb825 100644 --- a/utils/src/b_jsonutil/b_jsonutil.cpp +++ b/utils/src/b_jsonutil/b_jsonutil.cpp @@ -94,8 +94,7 @@ void BJsonUtil::ParseBundleDetailInfo(const std::string &bundleDetailInfo, Bundl return; } int infosCount = cJSON_GetArraySize(infos); - for (int i = 0; i < infosCount; i++) - { + for (int i = 0; i < infosCount; i++) { cJSON *infoItem = cJSON_GetArrayItem(infos, i); if (!cJSON_IsObject(infoItem)) { HILOGE("Parse json error, info item is not an object"); @@ -121,8 +120,7 @@ void BJsonUtil::ParseBundleDetailInfo(const std::string &bundleDetailInfo, Bundl void BJsonUtil::RecordBundleDetailRelation(std::map &bundleNameDetailMap, std::vector &bundleDetailInfos) { - for (auto &bundleDetail : bundleDetailInfos) - { + for (auto &bundleDetail : bundleDetailInfos) { std::string bundleName = bundleDetail.bundleName; bundleNameDetailMap[bundleName] = bundleDetail; } -- Gitee From 343bbc3b08537a9e1fa6b3b5a32ae9b89927b926 Mon Sep 17 00:00:00 2001 From: "yaoruozi1@huawei.com" Date: Sun, 31 Mar 2024 14:12:40 +0800 Subject: [PATCH 09/11] =?UTF-8?q?=E5=A4=87=E4=BB=BD=E6=81=A2=E5=A4=8D?= =?UTF-8?q?=E6=A1=86=E6=9E=B6=E5=A2=9E=E5=8A=A0=E6=81=A2=E5=A4=8D=E4=BA=8B?= =?UTF-8?q?=E4=BB=B6=E4=B8=8B=E5=8F=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yaoruozi1@huawei.com --- tests/unittests/backup_sa/module_ipc/BUILD.gn | 4 ++-- .../b_jsonutil/b_jsonutil_test.cpp | 21 +++++-------------- utils/BUILD.gn | 5 +++-- utils/src/b_jsonutil/b_jsonutil.cpp | 10 +++------ 4 files changed, 13 insertions(+), 27 deletions(-) diff --git a/tests/unittests/backup_sa/module_ipc/BUILD.gn b/tests/unittests/backup_sa/module_ipc/BUILD.gn index cdfd8ab01..071568250 100644 --- a/tests/unittests/backup_sa/module_ipc/BUILD.gn +++ b/tests/unittests/backup_sa/module_ipc/BUILD.gn @@ -106,6 +106,7 @@ ohos_unittest("backup_service_test") { "ability_runtime:ability_manager", "bundle_framework:appexecfwk_core", "c_utils:utils", + "common_event_service:cesfwk_innerkits", "hilog:libhilog", "hitrace:hitrace_meter", "init:libbegetutil", @@ -113,7 +114,6 @@ ohos_unittest("backup_service_test") { "safwk:system_ability_fwk", "samgr:samgr_proxy", "storage_service:storage_manager_sa_proxy", - "common_event_service:cesfwk_innerkits", ] sanitize = { @@ -279,6 +279,7 @@ ohos_unittest("backup_restore_deps_manager_test") { "ability_runtime:ability_manager", "bundle_framework:appexecfwk_core", "c_utils:utils", + "common_event_service:cesfwk_innerkits", "hilog:libhilog", "hitrace:hitrace_meter", "init:libbegetutil", @@ -286,7 +287,6 @@ ohos_unittest("backup_restore_deps_manager_test") { "safwk:system_ability_fwk", "samgr:samgr_proxy", "storage_service:storage_manager_sa_proxy", - "common_event_service:cesfwk_innerkits", ] use_exceptions = true diff --git a/tests/unittests/backup_utils/b_jsonutil/b_jsonutil_test.cpp b/tests/unittests/backup_utils/b_jsonutil/b_jsonutil_test.cpp index 862250925..77ef3170b 100644 --- a/tests/unittests/backup_utils/b_jsonutil/b_jsonutil_test.cpp +++ b/tests/unittests/backup_utils/b_jsonutil/b_jsonutil_test.cpp @@ -83,22 +83,11 @@ HWTEST_F(BJsonUtilTest, b_jsonutil_ConvertBundleDetailInfos_0100, testing::ext:: std::string pattern = ":"; std::vector detailInfos; std::string detail01 = "{ - "infos" : [ - { - "details" : [ - { - "detail" : [ - { - "source" : "com.ohos.app01", - "target" : "com.hos.app01" - } - ], - "type" : "app_mapping_relation" - } - ], - "type" : "broadcast" - } - ] + "infos" : [ { + "details" : [ { + "detail" : [ { "source" : "com.ohos.app01", "target" : "com.hos.app01" } ], + "type" : "app_mapping_relation" } ], + "type" : "broadcast" } ] }"; detailInfos.push_back(detail01); int32_t userId = 100; diff --git a/utils/BUILD.gn b/utils/BUILD.gn index dd6275403..50b6d300f 100644 --- a/utils/BUILD.gn +++ b/utils/BUILD.gn @@ -87,13 +87,13 @@ ohos_shared_library("backup_utils") { "src/b_json/b_json_entity_ext_manage.cpp", "src/b_json/b_json_entity_extension_config.cpp", "src/b_json/b_report_entity.cpp", + "src/b_jsonutil/b_jsonutil.cpp", "src/b_ohos/startup/backup_para.cpp", "src/b_process/b_guard_cwd.cpp", "src/b_process/b_guard_signal.cpp", "src/b_process/b_process.cpp", "src/b_tarball/b_tarball_cmdline.cpp", "src/b_tarball/b_tarball_factory.cpp", - "src/b_jsonutil/b_jsonutil.cpp", ] sources += get_target_outputs(":backup_cxx_gen") @@ -113,6 +113,7 @@ ohos_shared_library("backup_utils") { "${path_backup}/interfaces/inner_api/native/backup_kit_inner/impl", "${path_rust}/crates/cxx/include", "${target_gen_dir}/rust/src", + "//third_party/cJSON:cjson", ] deps = [ @@ -120,8 +121,8 @@ ohos_shared_library("backup_utils") { ":backup_cxx_gen", ":backup_cxx_rust", "${path_jsoncpp}:jsoncpp", - "//third_party/openssl:libcrypto_shared", "//third_party/cJSON:cjson", + "//third_party/openssl:libcrypto_shared", ] use_exceptions = true diff --git a/utils/src/b_jsonutil/b_jsonutil.cpp b/utils/src/b_jsonutil/b_jsonutil.cpp index bafcbb825..bb631ff5e 100644 --- a/utils/src/b_jsonutil/b_jsonutil.cpp +++ b/utils/src/b_jsonutil/b_jsonutil.cpp @@ -53,8 +53,7 @@ std::vector BJsonUtil::ConvertBundleDetailInfos( const std::string &patternInfo, std::vector &realBundleNames, int32_t userId) { std::vector bundleDetailInfoList; - for (size_t pos = 0; pos < bundleNameIndexStrs.size(); pos++) - { + for (size_t pos = 0; pos < bundleNameIndexStrs.size(); pos++) { std::string bundleNameIndexStr = bundleNameIndexStrs[pos]; BundleDetailInfo bundleDetailInfo; bundleDetailInfo.userId = userId; @@ -117,7 +116,7 @@ void BJsonUtil::ParseBundleDetailInfo(const std::string &bundleDetailInfo, Bundl cJSON_Delete(root); } -void BJsonUtil::RecordBundleDetailRelation(std::map &bundleNameDetailMap, +void BJsonUtil::RecordBundleDetailRelation(std::map &bundleNameDetailMap, std::vector &bundleDetailInfos) { for (auto &bundleDetail : bundleDetailInfos) { @@ -125,7 +124,4 @@ void BJsonUtil::RecordBundleDetailRelation(std::map Date: Sun, 31 Mar 2024 14:24:28 +0800 Subject: [PATCH 10/11] =?UTF-8?q?=E5=A4=87=E4=BB=BD=E6=81=A2=E5=A4=8D?= =?UTF-8?q?=E6=A1=86=E6=9E=B6=E5=A2=9E=E5=8A=A0=E6=81=A2=E5=A4=8D=E4=BA=8B?= =?UTF-8?q?=E4=BB=B6=E4=B8=8B=E5=8F=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yaoruozi1@huawei.com --- tests/unittests/backup_utils/BUILD.gn | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/unittests/backup_utils/BUILD.gn b/tests/unittests/backup_utils/BUILD.gn index 61f050541..e71ece848 100644 --- a/tests/unittests/backup_utils/BUILD.gn +++ b/tests/unittests/backup_utils/BUILD.gn @@ -79,9 +79,7 @@ ohos_unittest("b_jsonutil_test") { module_out_path = path_module_out_tests - sources = [ - "b_jsonutil/b_jsonutil_test.cpp", - ] + sources = [ "b_jsonutil/b_jsonutil_test.cpp" ] deps = [ "${path_backup}/tests/utils:backup_test_utils", -- Gitee From e3fce035785eb7b890f0cf567028b093c3df23c0 Mon Sep 17 00:00:00 2001 From: "yaoruozi1@huawei.com" Date: Sun, 31 Mar 2024 14:49:33 +0800 Subject: [PATCH 11/11] =?UTF-8?q?=E5=A4=87=E4=BB=BD=E6=81=A2=E5=A4=8D?= =?UTF-8?q?=E6=A1=86=E6=9E=B6=E5=A2=9E=E5=8A=A0=E6=81=A2=E5=A4=8D=E4=BA=8B?= =?UTF-8?q?=E4=BB=B6=E4=B8=8B=E5=8F=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yaoruozi1@huawei.com --- bundle.json | 4 ++-- services/backup_sa/src/module_ipc/service.cpp | 2 +- tests/unittests/backup_utils/BUILD.gn | 2 +- tools/backup_tool/src/tools_op_restore.cpp | 2 +- utils/BUILD.gn | 3 +-- 5 files changed, 6 insertions(+), 7 deletions(-) diff --git a/bundle.json b/bundle.json index 22d8e9aa3..ad8744a25 100644 --- a/bundle.json +++ b/bundle.json @@ -26,6 +26,7 @@ "bundle_framework", "build_framework", "common_event_service", + "cJSON", "c_utils", "data_share", "device_manager", @@ -44,8 +45,7 @@ "third_party": [ "bounds_checking_function", "jsoncpp", - "openssl", - "cJSON" + "openssl" ] }, "adapted_system_type": [ diff --git a/services/backup_sa/src/module_ipc/service.cpp b/services/backup_sa/src/module_ipc/service.cpp index 669282bd9..c4d7587c4 100644 --- a/services/backup_sa/src/module_ipc/service.cpp +++ b/services/backup_sa/src/module_ipc/service.cpp @@ -387,7 +387,7 @@ void Service::NotifyBundleInfos(std::mapsecond; + BJsonUtil::BundleDetailInfo bundleDetailInfo = iter->second; if (bundleDetailInfo.type == COMMON_EVENT_TYPE) { DelayedSingleton::GetInstance()->NotifyBundleDetail(bundleDetailInfo); } diff --git a/tests/unittests/backup_utils/BUILD.gn b/tests/unittests/backup_utils/BUILD.gn index e71ece848..b67e7a51e 100644 --- a/tests/unittests/backup_utils/BUILD.gn +++ b/tests/unittests/backup_utils/BUILD.gn @@ -85,10 +85,10 @@ ohos_unittest("b_jsonutil_test") { "${path_backup}/tests/utils:backup_test_utils", "${path_backup}/utils/:backup_utils", "${path_jsoncpp}:jsoncpp", - "//third_party/cJSON:cjson", ] external_deps = [ + "cJSON:cjson", "c_utils:utils", "hilog:libhilog", ] diff --git a/tools/backup_tool/src/tools_op_restore.cpp b/tools/backup_tool/src/tools_op_restore.cpp index 8f5694195..06cd546f2 100644 --- a/tools/backup_tool/src/tools_op_restore.cpp +++ b/tools/backup_tool/src/tools_op_restore.cpp @@ -242,7 +242,7 @@ static int32_t InitRestoreSession(shared_ptr ctx) .onBundleStarted = bind(OnBundleStarted, ctx, placeholders::_1, placeholders::_2), .onBundleFinished = bind(OnBundleFinished, ctx, placeholders::_1, placeholders::_2), .onAllBundlesFinished = bind(OnAllBundlesFinished, ctx, placeholders::_1), - .onResultReport = bind(OnResultReport, ctx, placeholders::_1); + .onResultReport = bind(OnResultReport, ctx, placeholders::_1), .onBackupServiceDied = bind(OnBackupServiceDied, ctx)}); if (ctx->session_ == nullptr) { printf("Failed to init restore\n"); diff --git a/utils/BUILD.gn b/utils/BUILD.gn index 50b6d300f..5893b725e 100644 --- a/utils/BUILD.gn +++ b/utils/BUILD.gn @@ -101,6 +101,7 @@ ohos_shared_library("backup_utils") { public_configs = [ ":utils_public_config" ] external_deps = [ + "cJSON:cjson", "c_utils:utils", "faultloggerd:libdfx_dumpcatcher", "hilog:libhilog", @@ -113,7 +114,6 @@ ohos_shared_library("backup_utils") { "${path_backup}/interfaces/inner_api/native/backup_kit_inner/impl", "${path_rust}/crates/cxx/include", "${target_gen_dir}/rust/src", - "//third_party/cJSON:cjson", ] deps = [ @@ -121,7 +121,6 @@ ohos_shared_library("backup_utils") { ":backup_cxx_gen", ":backup_cxx_rust", "${path_jsoncpp}:jsoncpp", - "//third_party/cJSON:cjson", "//third_party/openssl:libcrypto_shared", ] -- Gitee