From 9da5a9433b5f90d07d20cf88b53aef77dc73a2f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E4=B8=96=E4=BC=9F1?= Date: Wed, 14 May 2025 16:22:27 +0800 Subject: [PATCH] =?UTF-8?q?=E2=80=9C=E5=A4=87=E4=BB=BD=E6=81=A2=E5=A4=8D?= =?UTF-8?q?=E6=A1=86=E6=9E=B6=E6=94=AF=E6=8C=81=E5=B7=A5=E5=85=B7=E5=BA=94?= =?UTF-8?q?=E7=94=A8=E4=B8=BB=E5=8A=A8=E6=B8=85=E7=90=86=E4=B8=B4=E6=97=B6?= =?UTF-8?q?=E7=9B=AE=E5=BD=95=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 李世伟1 --- .../native/backup_ext/include/ext_extension.h | 2 + .../native/backup_ext/src/ext_extension.cpp | 24 +++++++ .../src/b_incremental_backup_session.cpp | 10 +++ .../src/b_incremental_restore_session.cpp | 10 +++ .../backup_kit_inner/src/b_session_backup.cpp | 10 +++ .../src/b_session_restore.cpp | 10 +++ .../impl/b_incremental_backup_session.h | 8 +++ .../impl/b_incremental_restore_session.h | 8 +++ .../backup_kit_inner/impl/b_session_backup.h | 8 +++ .../backup_kit_inner/impl/b_session_restore.h | 8 +++ .../js/backup/session_backup_n_exporter.cpp | 59 ++++++++++++++++++ .../js/backup/session_backup_n_exporter.h | 1 + .../session_incremental_backup_n_exporter.cpp | 59 ++++++++++++++++++ .../session_incremental_backup_n_exporter.h | 1 + .../js/backup/session_restore_n_exporter.cpp | 62 +++++++++++++++++++ .../js/backup/session_restore_n_exporter.h | 1 + services/backup_sa/IExtension.idl | 1 + services/backup_sa/IService.idl | 1 + .../backup_sa/include/module_ipc/service.h | 1 + services/backup_sa/src/module_ipc/service.cpp | 47 ++++++++++++++ .../src/module_ipc/svc_extension_proxy.cpp | 20 ++++++ 21 files changed, 351 insertions(+) diff --git a/frameworks/native/backup_ext/include/ext_extension.h b/frameworks/native/backup_ext/include/ext_extension.h index 9d7ab287b..0cb3d575d 100644 --- a/frameworks/native/backup_ext/include/ext_extension.h +++ b/frameworks/native/backup_ext/include/ext_extension.h @@ -63,6 +63,8 @@ public: void AsyncTaskIncrementalRestoreForUpgrade(void); ErrCode User0OnBackup() override; ErrCode UpdateDfxInfo(int64_t uniqId, uint32_t extConnectSpend, const std::string &bundleName) override; + ErrCode CleanBundleTempDir() override; + public: explicit BackupExtExtension(const std::shared_ptr &extension, const std::string &bundleName) : extension_(extension) diff --git a/frameworks/native/backup_ext/src/ext_extension.cpp b/frameworks/native/backup_ext/src/ext_extension.cpp index 849fb2b59..6b2280aa5 100644 --- a/frameworks/native/backup_ext/src/ext_extension.cpp +++ b/frameworks/native/backup_ext/src/ext_extension.cpp @@ -2207,4 +2207,28 @@ ErrCode BackupExtExtension::IncrementalAllFileReady(const TarMap &pkgInfo, } return ret; } + +ErrCode BackupExtExtension::CleanBundleTempDir() +{ + HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); + try { + HILOGI("BackupExtExtension::CleanBundleTempDir begin"); + if (extension_ == nullptr) { + HILOGE("Failed to CleanBundleTempDir, extension is nullptr"); + return BError(BError::Codes::EXT_INVAL_ARG, "Extension is nullptr").GetCode(); + } + if (extension_->GetExtensionAction() == BConstants::ExtensionAction::INVALID) { + return BError(BError::Codes::EXT_INVAL_ARG, "Action is invalid").GetCode(); + } + VerifyCaller(); + bool isClearFlag = isClearData_; + isClearData_ = true; + DoClear(); + isClearData_ = isClearFlag; + return ERR_OK; + } catch (...) { + HILOGE("Failed to CleanBundleTempDir"); + return BError(BError::Codes::EXT_BROKEN_IPC).GetCode(); + } +} } // namespace OHOS::FileManagement::Backup diff --git a/frameworks/native/backup_kit_inner/src/b_incremental_backup_session.cpp b/frameworks/native/backup_kit_inner/src/b_incremental_backup_session.cpp index 731e23cdf..12f5894d4 100644 --- a/frameworks/native/backup_kit_inner/src/b_incremental_backup_session.cpp +++ b/frameworks/native/backup_kit_inner/src/b_incremental_backup_session.cpp @@ -209,4 +209,14 @@ ErrCode BIncrementalBackupSession::Cancel(std::string bundleName) } return result; } + +ErrCode BIncrementalBackupSession::CleanBundleTempDir(const std::string &bundleName) +{ + HILOGI("CleanBundleTempDir"); + auto proxy = ServiceClient::GetInstance(); + if (proxy == nullptr) { + return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode(); + } + return proxy->CleanBundleTempDir(bundleName); +} } // namespace OHOS::FileManagement::Backup \ 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 25bcef813..6b25a1878 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 @@ -231,4 +231,14 @@ ErrCode BIncrementalRestoreSession::Cancel(std::string bundleName) } return result; } + +ErrCode BIncrementalRestoreSession::CleanBundleTempDir(const std::string &bundleName) +{ + HILOGI("CleanBundleTempDir."); + auto proxy = ServiceClient::GetInstance(); + if (proxy == nullptr) { + return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode(); + } + return proxy->CleanBundleTempDir(bundleName); +} } // namespace OHOS::FileManagement::Backup \ No newline at end of file diff --git a/frameworks/native/backup_kit_inner/src/b_session_backup.cpp b/frameworks/native/backup_kit_inner/src/b_session_backup.cpp index 863d696d5..38693ecd8 100644 --- a/frameworks/native/backup_kit_inner/src/b_session_backup.cpp +++ b/frameworks/native/backup_kit_inner/src/b_session_backup.cpp @@ -228,4 +228,14 @@ ErrCode BSessionBackup::Cancel(std::string bundleName) } return result; } + +ErrCode BSessionBackup::CleanBundleTempDir(const std::string &bundleName) +{ + HILOGI("CleanBundleTempDir."); + auto proxy = ServiceClient::GetInstance(); + if (proxy == nullptr) { + return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode(); + } + return proxy->CleanBundleTempDir(bundleName); +} } // namespace OHOS::FileManagement::Backup \ No newline at end of file diff --git a/frameworks/native/backup_kit_inner/src/b_session_restore.cpp b/frameworks/native/backup_kit_inner/src/b_session_restore.cpp index 3daf19df3..f6ecfe044 100644 --- a/frameworks/native/backup_kit_inner/src/b_session_restore.cpp +++ b/frameworks/native/backup_kit_inner/src/b_session_restore.cpp @@ -209,4 +209,14 @@ ErrCode BSessionRestore::Cancel(std::string bundleName) } return result; } + +ErrCode BSessionRestore::CleanBundleTempDir(const std::string &bundleName) +{ + HILOGI("CleanBundleTempDir."); + auto proxy = ServiceClient::GetInstance(); + if (proxy == nullptr) { + return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode(); + } + return proxy->CleanBundleTempDir(bundleName); +} } // namespace OHOS::FileManagement::Backup \ No newline at end of file diff --git a/interfaces/inner_api/native/backup_kit_inner/impl/b_incremental_backup_session.h b/interfaces/inner_api/native/backup_kit_inner/impl/b_incremental_backup_session.h index 934cb5593..b32113789 100644 --- a/interfaces/inner_api/native/backup_kit_inner/impl/b_incremental_backup_session.h +++ b/interfaces/inner_api/native/backup_kit_inner/impl/b_incremental_backup_session.h @@ -115,6 +115,14 @@ public: */ void RegisterBackupServiceDied(std::function functor); + /** + * @brief 备份或者恢复任务结束后,用于清理当前应用的临时目录(./backup目录下的backup和restore目录)的数据 + * + * @param bundleName 应用名称 + * @return ErrCode 规范错误码 + */ + ErrCode CleanBundleTempDir(const std::string &bundleName); + public: ~BIncrementalBackupSession(); 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 3b0e46a9c..feaadb654 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 @@ -137,6 +137,14 @@ public: */ void RegisterBackupServiceDied(std::function functor); + /** + * @brief 备份或者恢复任务结束后,用于清理当前应用的临时目录(./backup目录下的backup和restore目录)的数据 + * + * @param bundleName 应用名称 + * @return ErrCode 规范错误码 + */ + ErrCode CleanBundleTempDir(const std::string &bundleName); + public: ~BIncrementalRestoreSession(); diff --git a/interfaces/inner_api/native/backup_kit_inner/impl/b_session_backup.h b/interfaces/inner_api/native/backup_kit_inner/impl/b_session_backup.h index 0fbcb4dc2..7f5cd583d 100644 --- a/interfaces/inner_api/native/backup_kit_inner/impl/b_session_backup.h +++ b/interfaces/inner_api/native/backup_kit_inner/impl/b_session_backup.h @@ -130,6 +130,14 @@ public: */ void RegisterBackupServiceDied(std::function functor); + /** + * @brief 备份或者恢复任务结束后,用于清理当前应用的临时目录(./backup目录下的backup和restore目录)的数据 + * + * @param bundleName 应用名称 + * @return ErrCode 规范错误码 + */ + ErrCode CleanBundleTempDir(const std::string &bundleName); + public: ~BSessionBackup(); 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 7aa5fdcd8..4dd41dc36 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 @@ -137,6 +137,14 @@ public: */ void RegisterBackupServiceDied(std::function functor); + /** + * @brief 备份或者恢复任务结束后,用于清理当前应用的临时目录(./backup目录下的backup和restore目录)的数据 + * + * @param bundleName 应用名称 + * @return ErrCode 规范错误码 + */ + ErrCode CleanBundleTempDir(const std::string &bundleName); + public: ~BSessionRestore(); diff --git a/interfaces/kits/js/backup/session_backup_n_exporter.cpp b/interfaces/kits/js/backup/session_backup_n_exporter.cpp index c04ef6664..bf6d7cadb 100644 --- a/interfaces/kits/js/backup/session_backup_n_exporter.cpp +++ b/interfaces/kits/js/backup/session_backup_n_exporter.cpp @@ -655,6 +655,64 @@ napi_value SessionBackupNExporter::Cancel(napi_env env, napi_callback_info info) return nResult; } +static NContextCBExec CleanBundleTempDirCBExec(napi_env env, const NFuncArg &funcArg, std::unique_ptr bundleName) +{ + auto backupEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); + if (!(backupEntity && (backupEntity->session))) { + HILOGE("Failed to get BackupSession entity."); + NError(BError(BError::Codes::SDK_INVAL_ARG, "Failed to get BackupSession entity.").GetCode()).ThrowErr(env); + return nullptr; + } + return [entity {backupEntity}, bundleName {std::string(bundleName.get())}]() -> NError { + if (!(entity && (entity->session))) { + return NError(BError(BError::Codes::SDK_INVAL_ARG, "Backup session is nullptr").GetCode()); + } + return NError(entity->session->CleanBundleTempDir(bundleName)); + }; +} + +napi_value SessionBackupNExporter::CleanBundleTempDir(napi_env env, napi_callback_info cbinfo) +{ + HILOGI("Called SessionBackupNExporter::CleanBundleTempDir begin."); + if (!SAUtils::CheckBackupPermission()) { + HILOGE("Has not permission!"); + NError(E_PERMISSION).ThrowErr(env); + return nullptr; + } + if (!SAUtils::IsSystemApp()) { + HILOGE("System App check fail!"); + NError(E_PERMISSION_SYS).ThrowErr(env); + return nullptr; + } + NFuncArg funcArg(env, cbinfo); + //校验参数格式是否是1 + if (!funcArg.InitArgs(NARG_CNT::ONE)) { + HILOGE("Number of arguments unmatched"); + NError(E_PARAMS).ThrowErr(env); + return nullptr; + } + NVal jsBundleStr(env, funcArg[NARG_POS::FIRST]); + auto [succ, bundleName, sizeStr] = jsBundleStr.ToUTF8String(); + if (!succ) { + HILOGE("First arguments is not string."); + NError(E_PARAMS).ThrowErr(env); + return nullptr; + } + + auto cbExec = CleanBundleTempDirCBExec(env, funcArg, std::move(bundleName)); + if (cbExec == nullptr) { + HILOGE("CleanBundleTempDirCBExec fail!"); + return nullptr; + } + auto cbCompl = [](napi_env env, NError err) -> NVal { + return err? NVal::CreateBool(env, false) : NVal::CreateBool(env, false); + }; + HILOGD("Called SessionBackupNExporter::CleanBundleTempDir end."); + + NVal thisVar(env, funcArg.GetThisVar()); + return NAsyncWorkPromise(env, thisVar).Schedule(className, cbExec, cbCompl).val_; +} + bool SessionBackupNExporter::Export() { HILOGD("called SessionBackupNExporter::Export begin"); @@ -664,6 +722,7 @@ bool SessionBackupNExporter::Export() NVal::DeclareNapiFunction("appendBundles", AppendBundles), NVal::DeclareNapiFunction("release", Release), NVal::DeclareNapiFunction("cancel", Cancel), + NVal::DeclareNapiFunction("cleanBundleTempDir", CleanBundleTempDir), }; auto [succ, classValue] = NClass::DefineClass(exports_.env_, className, Constructor, std::move(props)); diff --git a/interfaces/kits/js/backup/session_backup_n_exporter.h b/interfaces/kits/js/backup/session_backup_n_exporter.h index 5fc20d610..708f3b9b7 100644 --- a/interfaces/kits/js/backup/session_backup_n_exporter.h +++ b/interfaces/kits/js/backup/session_backup_n_exporter.h @@ -31,6 +31,7 @@ public: static napi_value AppendBundles(napi_env env, napi_callback_info cbinfo); static napi_value Release(napi_env env, napi_callback_info cbinfo); static napi_value Cancel(napi_env env, napi_callback_info cbinfo); + static napi_value CleanBundleTempDir(napi_env env, napi_callback_info cbinfo); SessionBackupNExporter(napi_env env, napi_value exports); ~SessionBackupNExporter() override; diff --git a/interfaces/kits/js/backup/session_incremental_backup_n_exporter.cpp b/interfaces/kits/js/backup/session_incremental_backup_n_exporter.cpp index 05912440e..eee4891be 100644 --- a/interfaces/kits/js/backup/session_incremental_backup_n_exporter.cpp +++ b/interfaces/kits/js/backup/session_incremental_backup_n_exporter.cpp @@ -654,6 +654,64 @@ napi_value SessionIncrementalBackupNExporter::Cancel(napi_env env, napi_callback return nResult; } +static NContextCBExec CleanBundleTempDirCBExec(napi_env env, const NFuncArg &funcArg, std::unique_ptr bundleName) +{ + auto backupEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); + if (!(backupEntity && (backupEntity->session))) { + HILOGE("Failed to get BackupSession entity."); + NError(BError(BError::Codes::SDK_INVAL_ARG, "Failed to get BackupSession entity.").GetCode()).ThrowErr(env); + return nullptr; + } + return [entity {backupEntity}, bundleName {std::string(bundleName.get())}]() -> NError { + if (!(entity && (entity->session))) { + return NError(BError(BError::Codes::SDK_INVAL_ARG, "Backup session is nullptr").GetCode()); + } + return NError(entity->session->CleanBundleTempDir(bundleName)); + }; +} + +napi_value SessionIncrementalBackupNExporter::CleanBundleTempDir(napi_env env, napi_callback_info cbinfo) +{ + HILOGI("Called SessionIncrementalBackupNExporter::CleanBundleTempDir begin."); + if (!SAUtils::CheckBackupPermission()) { + HILOGE("Has not permission!"); + NError(E_PERMISSION).ThrowErr(env); + return nullptr; + } + if (!SAUtils::IsSystemApp()) { + HILOGE("System App check fail!"); + NError(E_PERMISSION_SYS).ThrowErr(env); + return nullptr; + } + NFuncArg funcArg(env, cbinfo); + //校验参数格式是否是1 + if (!funcArg.InitArgs(NARG_CNT::ONE)) { + HILOGE("Number of arguments unmatched"); + NError(E_PARAMS).ThrowErr(env); + return nullptr; + } + NVal jsBundleStr(env, funcArg[NARG_POS::FIRST]); + auto [succ, bundleName, sizeStr] = jsBundleStr.ToUTF8String(); + if (!succ) { + HILOGE("First arguments is not string."); + NError(E_PARAMS).ThrowErr(env); + return nullptr; + } + + auto cbExec = CleanBundleTempDirCBExec(env, funcArg, std::move(bundleName)); + if (cbExec == nullptr) { + HILOGE("CleanBundleTempDirCBExec fail!"); + return nullptr; + } + auto cbCompl = [](napi_env env, NError err) -> NVal { + return err? NVal::CreateBool(env, false) : NVal::CreateBool(env, false); + }; + HILOGD("Called SessionIncrementalBackupNExporter::CleanBundleTempDir end."); + + NVal thisVar(env, funcArg.GetThisVar()); + return NAsyncWorkPromise(env, thisVar).Schedule(className, cbExec, cbCompl).val_; +} + bool SessionIncrementalBackupNExporter::Export() { HILOGD("called SessionIncrementalBackupNExporter::Export begin"); @@ -663,6 +721,7 @@ bool SessionIncrementalBackupNExporter::Export() NVal::DeclareNapiFunction("appendBundles", AppendBundles), NVal::DeclareNapiFunction("release", Release), NVal::DeclareNapiFunction("cancel", Cancel), + NVal::DeclareNapiFunction("cleanBundleTempDir", CleanBundleTempDir), }; auto [succ, classValue] = NClass::DefineClass(exports_.env_, className, Constructor, std::move(props)); diff --git a/interfaces/kits/js/backup/session_incremental_backup_n_exporter.h b/interfaces/kits/js/backup/session_incremental_backup_n_exporter.h index d2d5ba475..7f9547232 100644 --- a/interfaces/kits/js/backup/session_incremental_backup_n_exporter.h +++ b/interfaces/kits/js/backup/session_incremental_backup_n_exporter.h @@ -31,6 +31,7 @@ public: static napi_value AppendBundles(napi_env env, napi_callback_info cbinfo); static napi_value Release(napi_env env, napi_callback_info cbinfo); static napi_value Cancel(napi_env env, napi_callback_info cbinfo); + static napi_value CleanBundleTempDir(napi_env env, napi_callback_info cbinfo); SessionIncrementalBackupNExporter(napi_env env, napi_value exports); ~SessionIncrementalBackupNExporter() override; diff --git a/interfaces/kits/js/backup/session_restore_n_exporter.cpp b/interfaces/kits/js/backup/session_restore_n_exporter.cpp index ef6ddf6df..e30fd9beb 100644 --- a/interfaces/kits/js/backup/session_restore_n_exporter.cpp +++ b/interfaces/kits/js/backup/session_restore_n_exporter.cpp @@ -841,6 +841,67 @@ napi_value SessionRestoreNExporter::Cancel(napi_env env, napi_callback_info info return nResult; } +static NContextCBExec CleanBundleTempDirCBExec(napi_env env, const NFuncArg &funcArg, std::unique_ptr bundleName) +{ + 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; + } + return [entity {restoreEntity}, bundleName {string(bundleName.get())}]() -> 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->CleanBundleTempDir(bundleName)); + } + return NError(entity->sessionSheet->CleanBundleTempDir(bundleName)); + }; +} + +napi_value SessionRestoreNExporter::CleanBundleTempDir(napi_env env, napi_callback_info cbinfo) +{ + HILOGI("Called SessionRestore::CleanBundleTempDir begin."); + if (!SAUtils::CheckBackupPermission()) { + HILOGE("Has not permission!"); + NError(E_PERMISSION).ThrowErr(env); + return nullptr; + } + if (!SAUtils::IsSystemApp()) { + HILOGE("System App check fail!"); + NError(E_PERMISSION_SYS).ThrowErr(env); + return nullptr; + } + NFuncArg funcArg(env, cbinfo); + //校验参数格式是否是1 + if (!funcArg.InitArgs(NARG_CNT::ONE)) { + HILOGE("Number of arguments unmatched"); + NError(E_PARAMS).ThrowErr(env); + return nullptr; + } + NVal jsBundleStr(env, funcArg[NARG_POS::FIRST]); + auto [succ, bundleName, sizeStr] = jsBundleStr.ToUTF8String(); + if (!succ) { + HILOGE("First arguments is not string."); + NError(E_PARAMS).ThrowErr(env); + return nullptr; + } + + auto cbExec = CleanBundleTempDirCBExec(env, funcArg, std::move(bundleName)); + if (cbExec == nullptr) { + HILOGE("CleanBundleTempDirCBExec fail!"); + return nullptr; + } + auto cbCompl = [](napi_env env, NError err) -> NVal { + return err? NVal::CreateBool(env, false) : NVal::CreateBool(env, false); + }; + HILOGD("Called SessionRestore::CleanBundleTempDir end."); + + NVal thisVar(env, funcArg.GetThisVar()); + return NAsyncWorkPromise(env, thisVar).Schedule(className, cbExec, cbCompl).val_; +} + bool SessionRestoreNExporter::Export() { HILOGD("called SessionRestoreNExporter::Export begin"); @@ -851,6 +912,7 @@ bool SessionRestoreNExporter::Export() NVal::DeclareNapiFunction("getFileHandle", GetFileHandle), NVal::DeclareNapiFunction("release", Release), NVal::DeclareNapiFunction("cancel", Cancel), + NVal::DeclareNapiFunction("cleanBundleTempDir", CleanBundleTempDir), }; auto [succ, classValue] = NClass::DefineClass(exports_.env_, className, Constructor, std::move(props)); diff --git a/interfaces/kits/js/backup/session_restore_n_exporter.h b/interfaces/kits/js/backup/session_restore_n_exporter.h index 157f34b4a..12a40fca8 100644 --- a/interfaces/kits/js/backup/session_restore_n_exporter.h +++ b/interfaces/kits/js/backup/session_restore_n_exporter.h @@ -35,6 +35,7 @@ public: static napi_value GetFileHandle(napi_env env, napi_callback_info cbinfo); static napi_value Release(napi_env env, napi_callback_info cbinfo); static napi_value Cancel(napi_env env, napi_callback_info cbinfo); + static napi_value CleanBundleTempDir(napi_env env, napi_callback_info cbinfo); SessionRestoreNExporter(napi_env env, napi_value exports); ~SessionRestoreNExporter() override; diff --git a/services/backup_sa/IExtension.idl b/services/backup_sa/IExtension.idl index d6d37d3d1..4dcff5076 100644 --- a/services/backup_sa/IExtension.idl +++ b/services/backup_sa/IExtension.idl @@ -29,4 +29,5 @@ interface OHOS.FileManagement.Backup.IExtension{ [ipccode 12] void UpdateFdSendRate([in] String bundleName, [in] int sendRate); [ipccode 13] void User0OnBackup(); [ipccode 14] void UpdateDfxInfo([in] long uniqId, [in] unsigned int extConnectSpend, [in] String bundleName); + [ipccode 15] void CleanBundleTempDir(); } \ No newline at end of file diff --git a/services/backup_sa/IService.idl b/services/backup_sa/IService.idl index 5ecb32295..d5cc82be5 100644 --- a/services/backup_sa/IService.idl +++ b/services/backup_sa/IService.idl @@ -69,4 +69,5 @@ interface OHOS.FileManagement.Backup.IService{ [ipccode 37] void ServiceResultReport([in]String restoreRetInfo, [in] BackupRestoreScenario sennario, [in] int serviceResultReportErrCode); [ipccode 38] void GetBackupDataSize([in] boolean isPreciseScan,[in] BIncrementalData[] bundleNameList); + [ipccode 40] void CleanBundleTempDir([in] String bundleName); } \ No newline at end of file diff --git a/services/backup_sa/include/module_ipc/service.h b/services/backup_sa/include/module_ipc/service.h index f68dd45d3..53d3180f7 100644 --- a/services/backup_sa/include/module_ipc/service.h +++ b/services/backup_sa/include/module_ipc/service.h @@ -110,6 +110,7 @@ public: void StartGetFdTask(std::string bundleName, wptr ptr); ErrCode GetBackupDataSize(bool isPreciseScan, const std::vector& bundleNameList) override; + ErrCode CleanBundleTempDir(const std::string &bundleName) override; // 以下都是非IPC接口 public: diff --git a/services/backup_sa/src/module_ipc/service.cpp b/services/backup_sa/src/module_ipc/service.cpp index 6e4218602..62b37704a 100644 --- a/services/backup_sa/src/module_ipc/service.cpp +++ b/services/backup_sa/src/module_ipc/service.cpp @@ -2080,4 +2080,51 @@ void Service::SetUserIdAndRestoreType(RestoreTypeEnum restoreType, int32_t userI session_->SetSessionUserId(GetUserIdDefault()); } } + +ErrCode Service::CleanBundleTempDir(const string &callerName) +{ + HILOGI("CleanBundleTempDir."); + HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); + + if (session_ == nullptr) { + HILOGE("Get BackupInfo error, session is empty."); + return BError(BError::Codes::SA_INVAL_ARG); + } + std::string bundleName = callerName; + auto backupConnection = session_->CreateBackupConnection(bundleName); + if (backupConnection == nullptr) { + HILOGE("backupConnection is null. bundleName: %{public}s", bundleName.c_str()); + return BError(BError::Codes::SA_INVAL_ARG); + } + auto callConnected = GetBackupInfoConnectDone(wptr(this), bundleName); + auto callDied = GetBackupInfoConnectDied(wptr(this), bundleName); + backupConnection->SetCallback(callConnected); + backupConnection->SetCallDied(callDied); + AAFwk::Want want = CreateConnectWant(bundleName); + auto ret = backupConnection->ConnectBackupExtAbility(want, GetUserIdDefault(), false); + if (ret) { + HILOGE("CleanBundleTempDir error, ConnectBackupExtAbility failed, bundleName:%{public}s, ret:%{public}d", bundleName.c_str(), ret); + return BError(BError::Codes::SA_BOOT_EXT_FAIL); + } + std::unique_lock lock(getBackupInfoSyncLock_); + getBackupInfoCondition_.wait_for(lock, std::chrono::seconds(CONNECT_WAIT_TIME_S)); + if (isConnectDied_.load()) { + HILOGE("CleanBundleTempDir error, GetBackupInfoConnectDied, please check bundleName: %{public}s", bundleName.c_str()); + isConnectDied_.store(false); + return BError(BError::Codes::EXT_ABILITY_DIED); + } + + session_->IncreaseSessionCnt(__PRETTY_FUNCTION__); + auto proxy = backupConnection->GetBackupExtProxy(); + if (!proxy) { + HILOGE("CleanBundleTempDir error, Extension backup Proxy is empty."); + backupConnection->DisconnectBackupExtAbility(); + session_->DecreaseSessionCnt(__PRETTY_FUNCTION__); + return BError(BError::Codes::SA_INVAL_ARG); + } + proxy->CleanBundleTempDir(); + backupConnection->DisconnectBackupExtAbility(); + session_->DecreaseSessionCnt(__PRETTY_FUNCTION__); + return BError(BError::Codes::OK); +} } // namespace OHOS::FileManagement::Backup diff --git a/services/backup_sa/src/module_ipc/svc_extension_proxy.cpp b/services/backup_sa/src/module_ipc/svc_extension_proxy.cpp index bd0d8e8a0..6882f4c37 100644 --- a/services/backup_sa/src/module_ipc/svc_extension_proxy.cpp +++ b/services/backup_sa/src/module_ipc/svc_extension_proxy.cpp @@ -212,4 +212,24 @@ ErrCode SvcExtensionProxy::UpdateFdSendRate(std::string &bundleName, int32_t sen HILOGI("SvcExtensionProxy::UpdateFdSendRate end."); return ret; } + +ErrCode SvcExtensionProxy::CleanBundleTempDir() +{ + HILOGD("CleanBundleTempDir start"); + HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); + BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr"); + MessageParcel data; + data.WriteInterfaceToken(GetDescriptor()); + + MessageParcel reply; + MessageOption option; + int32_t ret = + Remote()->SendRequest(static_cast(IExtensionInterfaceCode::CMD_CLEAN_BUNDLE_TEMP_DIR), data, reply, option); + if (ret != NO_ERROR) { + HILOGE("Received error %{public}d when doing IPC", ret); + return ErrCode(ret); + } + HILOGD("CleanBundleTempDir end"); + return reply.ReadInt32(); +} } // namespace OHOS::FileManagement::Backup -- Gitee