From f647cc06b9f99c1001517488a633ef4288309e75 Mon Sep 17 00:00:00 2001 From: nieben Date: Mon, 25 Dec 2023 20:20:56 +0800 Subject: [PATCH] incre restore Signed-off-by: nieben Change-Id: Ida54337c11a51a665689b53d4960a890500ca3a2 --- bundle.json | 3 + .../native/backup_ext/include/ext_extension.h | 24 ++ .../backup_ext/include/ext_extension_stub.h | 3 + .../native/backup_ext/include/untar_file.h | 19 + .../native/backup_ext/src/ext_extension.cpp | 325 ++++++++++++++++++ .../backup_ext/src/ext_extension_stub.cpp | 52 +++ .../native/backup_ext/src/untar_file.cpp | 107 ++++++ .../include/service_reverse.h | 16 + .../include/service_reverse_stub.h | 10 + .../src/b_incremental_backup_session.cpp | 107 ++++++ .../src/b_incremental_data.cpp | 61 ++++ .../src/b_incremental_restore_session.cpp | 125 +++++++ .../backup_kit_inner/src/b_session_backup.cpp | 10 + .../src/b_session_restore.cpp | 10 + .../src/b_session_restore_async.cpp | 10 + .../src/service_incremental_proxy.cpp | 263 ++++++++++++++ .../src/service_incremental_reverse.cpp | 115 +++++++ .../src/service_reverse_stub.cpp | 90 +++++ .../native/backup_kit_inner/BUILD.gn | 5 + .../backup_kit_inner/backup_kit_inner.h | 2 + .../impl/b_incremental_backup_session.h | 79 +++++ .../impl/b_incremental_data.h | 47 +++ .../impl/b_incremental_restore_session.h | 97 ++++++ .../backup_kit_inner/impl/b_session_backup.h | 7 + .../backup_kit_inner/impl/b_session_restore.h | 7 + .../impl/b_session_restore_async.h | 25 +- .../backup_kit_inner/impl/i_extension.h | 3 + .../impl/i_extension_ipc_interface_code.h | 3 + .../native/backup_kit_inner/impl/i_service.h | 10 + .../impl/i_service_ipc_interface_code.h | 8 + .../backup_kit_inner/impl/i_service_reverse.h | 13 + .../i_service_reverse_ipc_interface_code.h | 8 + .../backup_kit_inner/impl/service_proxy.h | 11 + services/backup.para | 5 +- services/backup_sa/BUILD.gn | 2 + .../backup_sa/include/module_ipc/service.h | 19 + .../module_ipc/service_reverse_proxy.h | 10 + .../include/module_ipc/service_stub.h | 12 + .../include/module_ipc/svc_extension_proxy.h | 4 + .../include/module_ipc/svc_session_manager.h | 29 ++ .../include/module_sched/sched_scheduler.h | 6 +- services/backup_sa/src/module_ipc/service.cpp | 31 +- .../src/module_ipc/service_incremental.cpp | 321 +++++++++++++++++ .../service_incremental_reverse_proxy.cpp | 183 ++++++++++ .../backup_sa/src/module_ipc/service_stub.cpp | 175 ++++++++++ .../src/module_ipc/svc_extension_proxy.cpp | 73 ++++ .../src/module_ipc/svc_session_manager.cpp | 37 ++ .../src/module_sched/sched_scheduler.cpp | 9 + .../b_session_backup_mock.cpp | 5 + .../b_session_restore_async_mock.cpp | 5 + .../b_session_restore_mock.cpp | 5 + .../backup_kit_inner/service_proxy_mock.cpp | 40 +++ tests/mock/module_ipc/service_mock.cpp | 40 +++ .../module_ipc/service_reverse_proxy_mock.cpp | 16 + tests/mock/module_ipc/service_stub_mock.cpp | 52 +++ .../module_ipc/svc_extension_proxy_mock.cpp | 15 + .../module_ipc/svc_session_manager_mock.cpp | 12 + .../module_sched/sched_scheduler_mock.cpp | 2 + tests/moduletests/backup_kit_inner/BUILD.gn | 1 + .../backup_impl/include/ext_extension_mock.h | 15 + .../backup_impl/include/i_service_mock.h | 40 +++ .../include/service_reverse_mock.h | 10 + .../backup_impl/service_reverse_stub_test.cpp | 8 + tests/unittests/backup_sa/module_ipc/BUILD.gn | 2 + .../module_ipc/service_stub_test.cpp | 39 +++ tools/backup_tool/src/tools_op_backup.cpp | 1 + tools/backup_tool/src/tools_op_restore.cpp | 3 +- utils/include/b_json/b_report_entity.h | 193 +++++++++++ utils/include/b_ohos/startup/backup_para.h | 14 + utils/include/b_resources/b_constants.h | 10 + utils/src/b_ohos/startup/backup_para.cpp | 20 ++ 71 files changed, 3110 insertions(+), 29 deletions(-) create mode 100644 frameworks/native/backup_kit_inner/src/b_incremental_backup_session.cpp create mode 100644 frameworks/native/backup_kit_inner/src/b_incremental_data.cpp create mode 100644 frameworks/native/backup_kit_inner/src/b_incremental_restore_session.cpp create mode 100644 frameworks/native/backup_kit_inner/src/service_incremental_proxy.cpp create mode 100644 frameworks/native/backup_kit_inner/src/service_incremental_reverse.cpp create mode 100644 interfaces/inner_api/native/backup_kit_inner/impl/b_incremental_backup_session.h create mode 100644 interfaces/inner_api/native/backup_kit_inner/impl/b_incremental_data.h create mode 100644 interfaces/inner_api/native/backup_kit_inner/impl/b_incremental_restore_session.h create mode 100644 services/backup_sa/src/module_ipc/service_incremental.cpp create mode 100644 services/backup_sa/src/module_ipc/service_incremental_reverse_proxy.cpp create mode 100644 utils/include/b_json/b_report_entity.h diff --git a/bundle.json b/bundle.json index 0d0c382c7..db8920b9d 100644 --- a/bundle.json +++ b/bundle.json @@ -108,6 +108,9 @@ "header_base": "//foundation/filemanagement/app_file_service/interfaces/inner_api/native/backup_kit_inner", "header_files": [ "backup_kit_inner.h", + "impl/b_incremental_backup_session.h", + "impl/b_incremental_data.h", + "impl/b_incremental_restore_session.h", "impl/b_session_restore.h", "impl/b_session_restore_async.h", "impl/b_file_info.h", diff --git a/frameworks/native/backup_ext/include/ext_extension.h b/frameworks/native/backup_ext/include/ext_extension.h index c39e05fcd..997c05374 100644 --- a/frameworks/native/backup_ext/include/ext_extension.h +++ b/frameworks/native/backup_ext/include/ext_extension.h @@ -34,8 +34,12 @@ public: ErrCode PublishFile(const std::string &fileName) override; ErrCode HandleBackup() override; ErrCode HandleRestore() override; + ErrCode GetIncrementalFileHandle(const std::string &fileName) override; + ErrCode PublishIncrementalFile(const std::string &fileName) override; + ErrCode HandleIncrementalBackup(UniqueFd incrementalFd, UniqueFd manifestFd) override; void AsyncTaskRestoreForUpgrade(void); + void AsyncTaskIncrementalRestoreForUpgrade(void); public: explicit BackupExtExtension(const std::shared_ptr &extension) : extension_(extension) @@ -68,6 +72,13 @@ private: */ int DoRestore(const string &fileName); + /** + * @brief incremental restore + * + * @param fileName name of the file that to be untar + */ + int DoIncrementalRestore(const string &fileName); + /** @brief clear backup restore data */ void DoClear(); @@ -78,6 +89,13 @@ private: */ void AppDone(ErrCode errCode); + /** + * @brief extension backup incremental restore is done + * + * @param errCode + */ + void AppIncrementalDone(ErrCode errCode); + /** * @brief Executing Backup Tasks Asynchronously * @@ -93,6 +111,12 @@ private: */ void AsyncTaskRestore(); + /** + * @brief Executing Incremental Restoration Tasks Asynchronously + * + */ + void AsyncTaskIncrementalRestore(); + void AsyncTaskOnBackup(); private: diff --git a/frameworks/native/backup_ext/include/ext_extension_stub.h b/frameworks/native/backup_ext/include/ext_extension_stub.h index 806bf8595..08ccc2a88 100644 --- a/frameworks/native/backup_ext/include/ext_extension_stub.h +++ b/frameworks/native/backup_ext/include/ext_extension_stub.h @@ -36,6 +36,9 @@ private: ErrCode CmdHandleBackup(MessageParcel &data, MessageParcel &reply); ErrCode CmdPublishFile(MessageParcel &data, MessageParcel &reply); ErrCode CmdHandleRestore(MessageParcel &data, MessageParcel &reply); + ErrCode CmdGetIncrementalFileHandle(MessageParcel &data, MessageParcel &reply); + ErrCode CmdPublishIncrementalFile(MessageParcel &data, MessageParcel &reply); + ErrCode CmdHandleIncrementalBackup(MessageParcel &data, MessageParcel &reply); private: using ExtensionInterface = int32_t (ExtExtensionStub::*)(MessageParcel &data, MessageParcel &reply); diff --git a/frameworks/native/backup_ext/include/untar_file.h b/frameworks/native/backup_ext/include/untar_file.h index 1f92f88a2..fe0b12714 100644 --- a/frameworks/native/backup_ext/include/untar_file.h +++ b/frameworks/native/backup_ext/include/untar_file.h @@ -32,6 +32,8 @@ public: typedef enum { ERR_FORMAT = -1 } ErrorCode; static UntarFile &GetInstance(); int UnPacket(const std::string &tarFile, const std::string &rootPath); + int IncrementalUnPacket(const std::string &tarFile, const std::string &rootPath, + const std::unordered_map &includes); private: UntarFile() = default; @@ -46,6 +48,13 @@ private: */ int ParseTarFile(const std::string &rootPath); + /** + * @brief parse incremental tar file + * + * @param rootpath 解包的目标路径 + */ + int ParseIncrementalTarFile(const std::string &rootPath); + /** * @brief verfy check sum * @@ -112,6 +121,15 @@ private: */ void ParseFileByTypeFlag(char typeFlag, bool &isSkip, FileStatInfo &info); + /** + * @brief parse incremental file by typeFlag + * + * @param typeFlag 文件类型标志 + * @param isSkip 是否跳过当前文件 + * @param info 文件属性结构体 + */ + void ParseIncrementalFileByTypeFlag(char typeFlag, bool &isSkip, FileStatInfo &info); + /** * @brief Handle file ownership groups * @@ -128,6 +146,7 @@ private: off_t tarFileBlockCnt_ {0}; off_t pos_ {0}; size_t readCnt_ {0}; + std::unordered_map includes_; }; } // namespace OHOS::FileManagement::Backup diff --git a/frameworks/native/backup_ext/src/ext_extension.cpp b/frameworks/native/backup_ext/src/ext_extension.cpp index 083e4d7d4..d9886436d 100644 --- a/frameworks/native/backup_ext/src/ext_extension.cpp +++ b/frameworks/native/backup_ext/src/ext_extension.cpp @@ -38,6 +38,7 @@ #include "b_filesystem/b_file.h" #include "b_json/b_json_cached_entity.h" #include "b_json/b_json_entity_ext_manage.h" +#include "b_json/b_report_entity.h" #include "b_resources/b_constants.h" #include "b_tarball/b_tarball_factory.h" #include "filemgmt_libhilog.h" @@ -126,6 +127,78 @@ UniqueFd BackupExtExtension::GetFileHandle(const string &fileName) } } +static string ChangeFileExtension(const string &fileName, const string &fileExt) +{ + auto n = fileName.rfind("."); + if (n == string::npos) { + throw BError(BError::Codes::EXT_INVAL_ARG, "filename has no extension"); + } + + return string(fileName).replace(n, fileName.length()-n, "." + fileExt); +} + +static string GetReportFileName(const string &fileName) +{ + string reportName; + if (fileName.rfind(".") != string::npos) { + reportName = ChangeFileExtension(fileName, string(BConstants::REPORT_FILE_EXT)); + } else { + reportName = fileName + "." + string(BConstants::REPORT_FILE_EXT); + } + + return reportName; +} + +ErrCode BackupExtExtension::GetIncrementalFileHandle(const string &fileName) +{ + try { + if (extension_->GetExtensionAction() != BConstants::ExtensionAction::RESTORE) { + HILOGI("Failed to get file handle, because action is %{public}d invalid", extension_->GetExtensionAction()); + throw BError(BError::Codes::EXT_INVAL_ARG, "Action is invalid"); + } + + VerifyCaller(); + + string path = string(BConstants::PATH_BUNDLE_BACKUP_HOME).append(BConstants::SA_BUNDLE_BACKUP_RESTORE); + if (mkdir(path.data(), S_IRWXU) && errno != EEXIST) { + string str = string("Failed to create restore folder. ").append(std::generic_category().message(errno)); + throw BError(BError::Codes::EXT_INVAL_ARG, str); + } + + string tarName = path + fileName; + if (fileName.find('/') != string::npos) { + HILOGD("GetFileHandle: fileName include path symbol, need to make hash."); + tarName = path + GenerateHashForFileName(fileName); + if (CheckIfTarSuffix(fileName)) { + tarName += ".tar"; + } + } + if (access(tarName.c_str(), F_OK) == 0) { + throw BError(BError::Codes::EXT_INVAL_ARG, string("The file already exists")); + } + UniqueFd fd = UniqueFd(open(tarName.data(), O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR)); + + // 对应的简报文件 + string reportName = GetReportFileName(tarName); + if (access(reportName.c_str(), F_OK) == 0) { + throw BError(BError::Codes::EXT_INVAL_ARG, string("The report file already exists")); + } + UniqueFd reportFd = UniqueFd(open(reportName.data(), O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR)); + + auto proxy = ServiceProxy::GetInstance(); + auto ret = proxy->AppIncrementalFileReady(fileName, move(fd), move(reportFd)); + if (ret != ERR_OK) { + HILOGI("Failed to AppIncrementalFileReady %{public}d", ret); + } + + return ERR_OK; + } catch (...) { + HILOGE("Failed to get incremental file handle"); + DoClear(); + return BError(BError::Codes::EXT_INVAL_ARG).GetCode(); + } +} + ErrCode BackupExtExtension::HandleClear() { HILOGI("begin clear"); @@ -263,6 +336,57 @@ ErrCode BackupExtExtension::PublishFile(const string &fileName) } } +ErrCode BackupExtExtension::PublishIncrementalFile(const string &fileName) +{ + HILOGE("begin publish incremental file. fileName is %{public}s", fileName.data()); + try { + if (extension_->GetExtensionAction() != BConstants::ExtensionAction::RESTORE) { + throw BError(BError::Codes::EXT_INVAL_ARG, "Action is invalid"); + } + VerifyCaller(); + + string path = string(BConstants::PATH_BUNDLE_BACKUP_HOME).append(BConstants::SA_BUNDLE_BACKUP_RESTORE); + string tarName = path + fileName; + // 带路径的文件名在这里转换为hash值 + if (fileName.find('/') != string::npos) { + HILOGD("PublishFile: fileName include path symbol, need to make hash."); + tarName = path + GenerateHashForFileName(fileName); + if (CheckIfTarSuffix(fileName)) { + tarName += ".tar"; + } + } + { + BExcepUltils::VerifyPath(tarName, true); + unique_lock lock(lock_); + if (find(tars_.begin(), tars_.end(), fileName) != tars_.end() || access(tarName.data(), F_OK) != 0) { + throw BError(BError::Codes::EXT_INVAL_ARG, "The file does not exist"); + } + tars_.push_back(fileName); + if (!IsAllFileReceived(tars_)) { + return ERR_OK; + } + } + + // 异步执行解压操作 + if (extension_->AllowToBackupRestore()) { + AsyncTaskIncrementalRestore(); + } + + return ERR_OK; + } catch (const BError &e) { + DoClear(); + return e.GetCode(); + } catch (const exception &e) { + HILOGE("Catched an unexpected low-level exception %{public}s", e.what()); + DoClear(); + return BError(BError::Codes::EXT_BROKEN_FRAMEWORK).GetCode(); + } catch (...) { + HILOGE("Unexpected exception"); + DoClear(); + return BError(BError::Codes::EXT_BROKEN_FRAMEWORK).GetCode(); + } +} + ErrCode BackupExtExtension::HandleBackup() { string usrConfig = extension_->GetUsrConfig(); @@ -414,6 +538,65 @@ int BackupExtExtension::DoRestore(const string &fileName) return ERR_OK; } +static unordered_map GetTarIncludes(const string &tarName) +{ + // 根据简报文件获取待解压的文件列表,如果简报文件内容为空,则进行全量解压,返回空 + unordered_map includes; + + // 获取简报文件内容 + string reportName = GetReportFileName(tarName); + + // 获取简报内容 + BReportEntity rp(UniqueFd(open(reportName.data(), O_RDONLY))); + vector infos = rp.GetReportFileInfos(); + for (auto iter : infos) { + includes.emplace(iter.filePath, true); + } + + return includes; +} + +int BackupExtExtension::DoIncrementalRestore(const string &fileName) +{ + HILOGI("Do incremental restore"); + if (extension_->GetExtensionAction() != BConstants::ExtensionAction::RESTORE) { + return EPERM; + } + // REM: 给定version + // REM: 解压启动Extension时即挂载好的备份目录中的数据 + string path = string(BConstants::PATH_BUNDLE_BACKUP_HOME).append(BConstants::SA_BUNDLE_BACKUP_RESTORE); + string tarName = path + fileName; + + // 带路径的恢复需要找到hash后的待解压文件 + if (fileName.find('/') != string::npos) { + HILOGD("DoIncrementalRestore: fileName include path symbol, need to make hash."); + string filePath = path + GenerateHashForFileName(fileName); + size_t pos = filePath.rfind('/'); + if (pos == string::npos) { + return EPERM; + } + string folderPath = filePath.substr(0, pos); + if (!ForceCreateDirectory(folderPath.data())) { + HILOGE("Failed to create directory"); + return EPERM; + } + tarName = filePath; + if (CheckIfTarSuffix(fileName)) { + tarName += ".tar"; + } + } + + // 当用户指定fullBackupOnly字段或指定版本的恢复,解压目录当前在/backup/restore + if (extension_->SpeicalVersionForCloneAndCloud() || extension_->UseFullBackupOnly()) { + UntarFile::GetInstance().IncrementalUnPacket(tarName, path, GetTarIncludes(tarName)); + } else { + UntarFile::GetInstance().IncrementalUnPacket(tarName, "/", GetTarIncludes(tarName)); + } + HILOGI("Application recovered successfully, package path is %{public}s", tarName.c_str()); + + return ERR_OK; +} + void BackupExtExtension::AsyncTaskBackup(const string config) { auto task = [obj {wptr(this)}, config]() { @@ -611,6 +794,36 @@ static void DeleteBackupTars() } } +static void DeleteBackupIncrementalTars() +{ + // The directory include tars and manage.json which would be deleted + BJsonCachedEntity cachedEntity(UniqueFd(open(INDEX_FILE_RESTORE.data(), O_RDONLY))); + auto cache = cachedEntity.Structuralize(); + auto info = cache.GetExtManage(); + auto path = string(BConstants::PATH_BUNDLE_BACKUP_HOME).append(BConstants::SA_BUNDLE_BACKUP_RESTORE); + for (auto &item : info) { + if (ExtractFileExt(item) != "tar" || IsUserTar(item, INDEX_FILE_RESTORE)) { + continue; + } + string tarPath = path + item; + if (!RemoveFile(tarPath)) { + HILOGE("Failed to delete the backup tar %{public}s", tarPath.c_str()); + } + // 删除简报文件 + string reportPath = GetReportFileName(tarPath); + if (!RemoveFile(reportPath)) { + HILOGE("Failed to delete the backup report"); + } + } + if (!RemoveFile(INDEX_FILE_RESTORE)) { + HILOGE("Failed to delete the backup index %{public}s", INDEX_FILE_RESTORE.c_str()); + } + string reportManagePath = GetReportFileName(INDEX_FILE_RESTORE); // GetIncrementalFileHandle创建的空fd + if (!RemoveFile(reportManagePath)) { + HILOGE("Failed to delete the backup report index %{public}s", reportManagePath.c_str()); + } +} + void BackupExtExtension::AsyncTaskRestore() { auto task = [obj {wptr(this)}, tars {tars_}]() { @@ -664,6 +877,59 @@ void BackupExtExtension::AsyncTaskRestore() }); } +void BackupExtExtension::AsyncTaskIncrementalRestore() +{ + auto task = [obj {wptr(this)}, tars {tars_}]() { + auto ptr = obj.promote(); + BExcepUltils::BAssert(ptr, BError::Codes::EXT_BROKEN_FRAMEWORK, + "Ext extension handle have been already released"); + try { + // 解压 + int ret = ERR_OK; + for (auto item : tars) { // 处理要解压的tar文件 + if (ExtractFileExt(item) == "tar" && !IsUserTar(item, INDEX_FILE_RESTORE)) { + ret = ptr->DoIncrementalRestore(item); + } + } + // 恢复用户tar包以及大文件 + // 目的地址是否需要拼接path(临时目录),FullBackupOnly为true并且非特殊场景 + bool appendTargetPath = ptr->extension_->UseFullBackupOnly() && + !ptr->extension_->SpeicalVersionForCloneAndCloud(); + RestoreBigFiles(appendTargetPath); + + // delete 1.tar/manage.json + DeleteBackupIncrementalTars(); + + if (ret == ERR_OK) { + HILOGI("after extra, do incremental restore."); + ptr->AsyncTaskIncrementalRestoreForUpgrade(); + } else { + ptr->AppIncrementalDone(ret); + ptr->DoClear(); + } + } catch (const BError &e) { + ptr->AppIncrementalDone(e.GetCode()); + } catch (const exception &e) { + HILOGE("Catched an unexpected low-level exception %{public}s", e.what()); + ptr->AppIncrementalDone(BError(BError::Codes::EXT_INVAL_ARG).GetCode()); + } catch (...) { + HILOGE("Failed to restore the ext bundle"); + ptr->AppIncrementalDone(BError(BError::Codes::EXT_INVAL_ARG).GetCode()); + } + }; + + // REM: 这里异步化了,需要做并发控制 + // 在往线程池中投入任务之前将需要的数据拷贝副本到参数中,保证不发生读写竞争, + // 由于拷贝参数时尚运行在主线程中,故在参数拷贝过程中是线程安全的。 + threadPool_.AddTask([task]() { + try { + task(); + } catch (...) { + HILOGE("Failed to add task to thread pool"); + } + }); +} + void BackupExtExtension::AsyncTaskRestoreForUpgrade() { auto task = [obj {wptr(this)}]() { @@ -707,6 +973,49 @@ void BackupExtExtension::AsyncTaskRestoreForUpgrade() }); } +void BackupExtExtension::AsyncTaskIncrementalRestoreForUpgrade() +{ + auto task = [obj {wptr(this)}]() { + auto ptr = obj.promote(); + try { + BExcepUltils::BAssert(ptr, BError::Codes::EXT_BROKEN_FRAMEWORK, + "Ext extension handle have been already released"); + BExcepUltils::BAssert(ptr->extension_, BError::Codes::EXT_INVAL_ARG, + "extension handle have been already released"); + + auto callBackup = [obj]() { + HILOGI("begin call restore"); + auto extensionPtr = obj.promote(); + BExcepUltils::BAssert(extensionPtr, BError::Codes::EXT_BROKEN_FRAMEWORK, + "Ext extension handle have been already released"); + extensionPtr->AppIncrementalDone(BError(BError::Codes::OK)); + // 清空恢复目录 + extensionPtr->DoClear(); + }; + ptr->extension_->OnRestore(callBackup); + } catch (const BError &e) { + ptr->AppIncrementalDone(e.GetCode()); + } catch (const exception &e) { + HILOGE("Catched an unexpected low-level exception %{public}s", e.what()); + ptr->AppIncrementalDone(BError(BError::Codes::EXT_INVAL_ARG).GetCode()); + } catch (...) { + HILOGE("Failed to restore the ext bundle"); + ptr->AppIncrementalDone(BError(BError::Codes::EXT_INVAL_ARG).GetCode()); + } + }; + + // REM: 这里异步化了,需要做并发控制 + // 在往线程池中投入任务之前将需要的数据拷贝副本到参数中,保证不发生读写竞争, + // 由于拷贝参数时尚运行在主线程中,故在参数拷贝过程中是线程安全的。 + threadPool_.AddTask([task]() { + try { + task(); + } catch (...) { + HILOGE("Failed to add task to thread pool"); + } + }); +} + void BackupExtExtension::DoClear() { try { @@ -742,6 +1051,16 @@ void BackupExtExtension::AppDone(ErrCode errCode) } } +void BackupExtExtension::AppIncrementalDone(ErrCode errCode) +{ + auto proxy = ServiceProxy::GetInstance(); + BExcepUltils::BAssert(proxy, BError::Codes::EXT_BROKEN_IPC, "Failed to obtain the ServiceProxy handle"); + auto ret = proxy->AppIncrementalDone(errCode); + if (ret != ERR_OK) { + HILOGE("Failed to notify the app done. err = %{public}d", ret); + } +} + void BackupExtExtension::AsyncTaskOnBackup() { auto task = [obj {wptr(this)}]() { @@ -805,4 +1124,10 @@ ErrCode BackupExtExtension::HandleRestore() return 0; } + +ErrCode BackupExtExtension::HandleIncrementalBackup(UniqueFd incrementalFd, UniqueFd manifestFd) +{ + HILOGI("begin"); + return BError(BError::Codes::OK); +} } // namespace OHOS::FileManagement::Backup diff --git a/frameworks/native/backup_ext/src/ext_extension_stub.cpp b/frameworks/native/backup_ext/src/ext_extension_stub.cpp index c98380a37..373caeefa 100644 --- a/frameworks/native/backup_ext/src/ext_extension_stub.cpp +++ b/frameworks/native/backup_ext/src/ext_extension_stub.cpp @@ -37,6 +37,12 @@ ExtExtensionStub::ExtExtensionStub() &ExtExtensionStub::CmdPublishFile; opToInterfaceMap_[static_cast(IExtensionInterfaceCode::CMD_HANDLE_RESTORE)] = &ExtExtensionStub::CmdHandleRestore; + opToInterfaceMap_[static_cast(IExtensionInterfaceCode::CMD_GET_INCREMENTAL_FILE_HANDLE)] = + &ExtExtensionStub::CmdGetIncrementalFileHandle; + opToInterfaceMap_[static_cast(IExtensionInterfaceCode::CMD_PUBLISH_INCREMENTAL_FILE)] = + &ExtExtensionStub::CmdPublishIncrementalFile; + opToInterfaceMap_[static_cast(IExtensionInterfaceCode::CMD_HANDLE_INCREMENTAL_BACKUP)] = + &ExtExtensionStub::CmdHandleIncrementalBackup; } int32_t ExtExtensionStub::OnRemoteRequest(uint32_t code, @@ -128,4 +134,50 @@ ErrCode ExtExtensionStub::CmdHandleRestore(MessageParcel &data, MessageParcel &r } return BError(BError::Codes::OK); } + +ErrCode ExtExtensionStub::CmdGetIncrementalFileHandle(MessageParcel &data, MessageParcel &reply) +{ + HILOGI("Begin"); + string fileName; + if (!data.ReadString(fileName)) { + return BError(BError::Codes::EXT_INVAL_ARG, "Failed to receive fileName").GetCode(); + } + + ErrCode res = GetIncrementalFileHandle(fileName); + if (!reply.WriteInt32(res)) { + return BError(BError::Codes::EXT_BROKEN_IPC, "Failed to send out the file").GetCode(); + } + return BError(BError::Codes::OK); +} + +ErrCode ExtExtensionStub::CmdPublishIncrementalFile(MessageParcel &data, MessageParcel &reply) +{ + HILOGI("Begin"); + string fileName; + if (!data.ReadString(fileName)) { + return BError(BError::Codes::EXT_INVAL_ARG, "Failed to receive fileName"); + } + + ErrCode res = PublishIncrementalFile(fileName); + if (!reply.WriteInt32(res)) { + stringstream ss; + ss << "Failed to send the result " << res; + return BError(BError::Codes::EXT_BROKEN_IPC, ss.str()).GetCode(); + } + return BError(BError::Codes::OK); +} + +ErrCode ExtExtensionStub::CmdHandleIncrementalBackup(MessageParcel &data, MessageParcel &reply) +{ + HILOGI("Begin"); + UniqueFd incrementalFd(data.ReadFileDescriptor()); + UniqueFd manifestFd(data.ReadFileDescriptor()); + ErrCode res = HandleIncrementalBackup(move(incrementalFd), move(manifestFd)); + if (!reply.WriteInt32(res)) { + stringstream ss; + ss << "Failed to send the result " << res; + return BError(BError::Codes::EXT_BROKEN_IPC, ss.str()).GetCode(); + } + return BError(BError::Codes::OK); +} } // namespace OHOS::FileManagement::Backup \ No newline at end of file diff --git a/frameworks/native/backup_ext/src/untar_file.cpp b/frameworks/native/backup_ext/src/untar_file.cpp index 6e8ff73f2..1a3a6d9de 100644 --- a/frameworks/native/backup_ext/src/untar_file.cpp +++ b/frameworks/native/backup_ext/src/untar_file.cpp @@ -79,6 +79,26 @@ int UntarFile::UnPacket(const string &tarFile, const string &rootPath) return 0; } +int UntarFile::IncrementalUnPacket(const string &tarFile, const string &rootPath, + const unordered_map &includes) +{ + includes_ = includes; + tarFilePtr_ = fopen(tarFile.c_str(), "rb"); + if (tarFilePtr_ == nullptr) { + HILOGE("Failed to open tar file %{public}s, err = %{public}d", tarFile.c_str(), errno); + return errno; + } + + if (ParseIncrementalTarFile(rootPath) != 0) { + HILOGE("Failed to parse tar file"); + } + + fclose(tarFilePtr_); + tarFilePtr_ = nullptr; + + return 0; +} + void UntarFile::HandleTarBuffer(const string &buff, const string &name, FileStatInfo &info) { info.mode = static_cast(ParseOctalStr(&buff[0] + TMODE_BASE, TMODE_LEN)); @@ -147,6 +167,56 @@ int UntarFile::ParseTarFile(const string &rootPath) return ret; } +int UntarFile::ParseIncrementalTarFile(const string &rootPath) +{ + // re-parse tar header + rootPath_ = rootPath; + char buff[BLOCK_SIZE] = {0}; + bool isSkip = false; + FileStatInfo info {}; + + // tarFileSize + int ret = fseeko(tarFilePtr_, 0L, SEEK_END); + tarFileSize_ = ftello(tarFilePtr_); + // reback file to begin + ret = fseeko(tarFilePtr_, 0L, SEEK_SET); + + while (1) { + readCnt_ = fread(buff, 1, BLOCK_SIZE, tarFilePtr_); + if (readCnt_ < BLOCK_SIZE) { + HILOGE("Parsing tar file completed, read data count is less then block size."); + return 0; + } + // two empty continuous block indicate end of file + if (IsEmptyBlock(buff)) { + char tailBuff[BLOCK_SIZE] = {0}; + size_t tailRead = fread(tailBuff, 1, BLOCK_SIZE, tarFilePtr_); + if (tailRead == BLOCK_SIZE && IsEmptyBlock(tailBuff)) { + HILOGE("Parsing tar file completed, tailBuff is empty."); + return 0; + } + } + // check header + TarHeader *header = reinterpret_cast(buff); + if (!IsValidTarBlock(*header)) { + // when split unpack, ftell size is over than file really size [0,READ_BUFF_SIZE] + if (ftello(tarFilePtr_) > (tarFileSize_ + READ_BUFF_SIZE) || !IsEmptyBlock(buff)) { + HILOGE("Invalid tar file format"); + ret = ERR_FORMAT; + } + return ret; + } + HandleTarBuffer(string(buff, BLOCK_SIZE), header->name, info); + ParseIncrementalFileByTypeFlag(header->typeFlag, isSkip, info); + ret = HandleFileProperties(isSkip, info); + if (ret != 0) { + HILOGE("Failed to handle file property"); + } + } + + return ret; +} + void UntarFile::ParseFileByTypeFlag(char typeFlag, bool &isSkip, FileStatInfo &info) { switch (typeFlag) { @@ -179,6 +249,43 @@ void UntarFile::ParseFileByTypeFlag(char typeFlag, bool &isSkip, FileStatInfo &i } } +void UntarFile::ParseIncrementalFileByTypeFlag(char typeFlag, bool &isSkip, FileStatInfo &info) +{ + switch (typeFlag) { + case REGTYPE: + case AREGTYPE: + if (!includes_.empty() && includes_.find(info.fullPath) == includes_.end()) { // not in includes + isSkip = true; + fseeko(tarFilePtr_, pos_ + tarFileBlockCnt_ * BLOCK_SIZE, SEEK_SET); + break; + } + ParseRegularFile(info, typeFlag, isSkip); + break; + case SYMTYPE: + isSkip = false; + break; + case DIRTYPE: + CreateDir(info.fullPath, info.mode); + isSkip = false; + break; + case GNUTYPE_LONGNAME: { + size_t nameLen = static_cast(tarFileSize_); + if (nameLen < PATH_MAX_LEN) { + fread(&(info.longName[0]), sizeof(char), nameLen, tarFilePtr_); + } + isSkip = true; + fseeko(tarFilePtr_, pos_ + tarFileBlockCnt_ * BLOCK_SIZE, SEEK_SET); + break; + } + default: { + // Ignoring, skip + isSkip = true; + fseeko(tarFilePtr_, tarFileBlockCnt_ * BLOCK_SIZE, SEEK_CUR); + break; + } + } +} + void UntarFile::ParseRegularFile(FileStatInfo &info, char typeFlag, bool &isSkip) { FILE *destFile = CreateFile(info.fullPath, info.mode, typeFlag); diff --git a/frameworks/native/backup_kit_inner/include/service_reverse.h b/frameworks/native/backup_kit_inner/include/service_reverse.h index f0810e14a..92b22c47e 100644 --- a/frameworks/native/backup_kit_inner/include/service_reverse.h +++ b/frameworks/native/backup_kit_inner/include/service_reverse.h @@ -18,6 +18,8 @@ #include "b_session_backup.h" #include "b_session_restore.h" +#include "b_incremental_backup_session.h" +#include "b_incremental_restore_session.h" #include "service_reverse_stub.h" namespace OHOS::FileManagement::Backup { @@ -33,16 +35,30 @@ public: void RestoreOnAllBundlesFinished(int32_t errCode) override; void RestoreOnFileReady(std::string bundleName, std::string fileName, int fd) override; + void IncrementalBackupOnFileReady(std::string bundleName, std::string fileName, int fd, int manifestFd) override; + void IncrementalBackupOnBundleStarted(int32_t errCode, std::string bundleName) override; + void IncrementalBackupOnBundleFinished(int32_t errCode, std::string bundleName) override; + void IncrementalBackupOnAllBundlesFinished(int32_t errCode) override; + + void IncrementalRestoreOnBundleStarted(int32_t errCode, std::string bundleName) override; + void IncrementalRestoreOnBundleFinished(int32_t errCode, std::string bundleName) override; + void IncrementalRestoreOnAllBundlesFinished(int32_t errCode) override; + void IncrementalRestoreOnFileReady(std::string bundleName, std::string fileName, int fd, int manifestFd) override; + public: ServiceReverse() = delete; explicit ServiceReverse(BSessionRestore::Callbacks callbacks); explicit ServiceReverse(BSessionBackup::Callbacks callbacks); + explicit ServiceReverse(BIncrementalBackupSession::Callbacks callbacks); + explicit ServiceReverse(BIncrementalRestoreSession::Callbacks callbacks); ~ServiceReverse() override = default; private: Scenario scenario_ {Scenario::UNDEFINED}; BSessionBackup::Callbacks callbacksBackup_; BSessionRestore::Callbacks callbacksRestore_; + BIncrementalBackupSession::Callbacks callbacksIncrementalBackup_; + BIncrementalRestoreSession::Callbacks callbacksIncrementalRestore_; }; } // namespace OHOS::FileManagement::Backup diff --git a/frameworks/native/backup_kit_inner/include/service_reverse_stub.h b/frameworks/native/backup_kit_inner/include/service_reverse_stub.h index 5c46907b2..ea1e5f8be 100644 --- a/frameworks/native/backup_kit_inner/include/service_reverse_stub.h +++ b/frameworks/native/backup_kit_inner/include/service_reverse_stub.h @@ -43,6 +43,16 @@ private: int32_t CmdRestoreOnBundleFinished(MessageParcel &data, MessageParcel &reply); int32_t CmdRestoreOnAllBundlesFinished(MessageParcel &data, MessageParcel &reply); int32_t CmdRestoreOnFileReady(MessageParcel &data, MessageParcel &reply); + + int32_t CmdIncrementalBackupOnFileReady(MessageParcel &data, MessageParcel &reply); + int32_t CmdIncrementalBackupOnBundleStarted(MessageParcel &data, MessageParcel &reply); + int32_t CmdIncrementalBackupOnBundleFinished(MessageParcel &data, MessageParcel &reply); + int32_t CmdIncrementalBackupOnAllBundlesFinished(MessageParcel &data, MessageParcel &reply); + + int32_t CmdIncrementalRestoreOnBundleStarted(MessageParcel &data, MessageParcel &reply); + int32_t CmdIncrementalRestoreOnBundleFinished(MessageParcel &data, MessageParcel &reply); + int32_t CmdIncrementalRestoreOnAllBundlesFinished(MessageParcel &data, MessageParcel &reply); + int32_t CmdIncrementalRestoreOnFileReady(MessageParcel &data, MessageParcel &reply); }; } // 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 new file mode 100644 index 000000000..9d17428c1 --- /dev/null +++ b/frameworks/native/backup_kit_inner/src/b_incremental_backup_session.cpp @@ -0,0 +1,107 @@ +/* + * Copyright (c) 2023 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_incremental_backup_session.h" + +#include "b_error/b_error.h" +#include "filemgmt_libhilog.h" +#include "service_proxy.h" +#include "service_reverse.h" + +namespace OHOS::FileManagement::Backup { +using namespace std; + +BIncrementalBackupSession::~BIncrementalBackupSession() +{ + if (!deathRecipient_) { + HILOGI("Death Recipient is nullptr"); + return; + } + auto proxy = ServiceProxy::GetInstance(); + if (proxy == nullptr) { + return; + } + auto remoteObject = proxy->AsObject(); + if (remoteObject != nullptr) { + remoteObject->RemoveDeathRecipient(deathRecipient_); + } + deathRecipient_ = nullptr; +} + +unique_ptr BIncrementalBackupSession::Init(Callbacks callbacks) +{ + try { + auto backup = make_unique(); + ServiceProxy::InvaildInstance(); + auto proxy = ServiceProxy::GetInstance(); + if (proxy == nullptr) { + HILOGI("Failed to get backup service"); + return nullptr; + } + + int32_t res = proxy->InitIncrementalBackupSession(sptr(new ServiceReverse(callbacks))); + if (res != 0) { + HILOGE("Failed to Backup because of %{public}d", res); + return nullptr; + } + + backup->RegisterBackupServiceDied(callbacks.onBackupServiceDied); + return backup; + } catch (const exception &e) { + HILOGE("Failed to Backup because of %{public}s", e.what()); + } + return nullptr; +} + +void BIncrementalBackupSession::RegisterBackupServiceDied(function functor) +{ + auto proxy = ServiceProxy::GetInstance(); + if (proxy == nullptr || !functor) { + return; + } + auto remoteObj = proxy->AsObject(); + if (!remoteObj) { + throw BError(BError::Codes::SA_BROKEN_IPC, "Proxy's remote object can't be nullptr"); + } + + auto callback = [functor](const wptr &obj) { + ServiceProxy::InvaildInstance(); + HILOGI("Backup service died"); + functor(); + }; + deathRecipient_ = sptr(new SvcDeathRecipient(callback)); + remoteObj->AddDeathRecipient(deathRecipient_); +} + +ErrCode BIncrementalBackupSession::AppendBundles(vector bundlesToBackup) +{ + auto proxy = ServiceProxy::GetInstance(); + if (proxy == nullptr) { + return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode(); + } + + return proxy->AppendBundlesIncrementalBackupSession(bundlesToBackup); +} + +ErrCode BIncrementalBackupSession::Release() +{ + auto proxy = ServiceProxy::GetInstance(); + if (proxy == nullptr) { + return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode(); + } + + return proxy->Release(); +} +} // namespace OHOS::FileManagement::Backup \ No newline at end of file diff --git a/frameworks/native/backup_kit_inner/src/b_incremental_data.cpp b/frameworks/native/backup_kit_inner/src/b_incremental_data.cpp new file mode 100644 index 000000000..07009c31e --- /dev/null +++ b/frameworks/native/backup_kit_inner/src/b_incremental_data.cpp @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2023 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_incremental_data.h" + +#include "filemgmt_libhilog.h" +#include "message_parcel.h" + +namespace OHOS::FileManagement::Backup { +using namespace std; + +bool BIncrementalData::Marshalling(Parcel &parcel) const +{ + if (!parcel.WriteString(bundleName) || !parcel.WriteInt64(lastIncrementalTime) || + !parcel.WriteString(backupParameters) || !parcel.WriteInt32(backupPriority)) { + HILOGE("Failed"); + return false; + } + auto msgParcel = static_cast(&parcel); + msgParcel->WriteFileDescriptor(manifestFd); + return true; +} + +bool BIncrementalData::ReadFromParcel(Parcel &parcel) +{ + if (!parcel.ReadString(bundleName) || !parcel.ReadInt64(lastIncrementalTime) || + !parcel.ReadString(backupParameters) || !parcel.ReadInt32(backupPriority)) { + HILOGE("Failed"); + return false; + } + auto msgParcel = static_cast(&parcel); + manifestFd = msgParcel->ReadFileDescriptor(); + return true; +} + +BIncrementalData *BIncrementalData::Unmarshalling(Parcel &parcel) +{ + try { + auto result = make_unique(); + if (!result->ReadFromParcel(parcel)) { + return nullptr; + } + return result.release(); + } catch (const bad_alloc &e) { + HILOGE("Failed to unmarshall BIncrementalData because of %{public}s", e.what()); + } + return nullptr; +} +} // 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 new file mode 100644 index 000000000..9a9fe5abc --- /dev/null +++ b/frameworks/native/backup_kit_inner/src/b_incremental_restore_session.cpp @@ -0,0 +1,125 @@ +/* + * Copyright (c) 2023 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_incremental_restore_session.h" + +#include "b_error/b_error.h" +#include "filemgmt_libhilog.h" +#include "service_proxy.h" +#include "service_reverse.h" + +namespace OHOS::FileManagement::Backup { +using namespace std; + +BIncrementalRestoreSession::~BIncrementalRestoreSession() +{ + if (!deathRecipient_) { + HILOGI("Death Recipient is nullptr"); + return; + } + auto proxy = ServiceProxy::GetInstance(); + if (proxy == nullptr) { + return; + } + auto remoteObject = proxy->AsObject(); + if (remoteObject != nullptr) { + remoteObject->RemoveDeathRecipient(deathRecipient_); + } + deathRecipient_ = nullptr; +} + +unique_ptr BIncrementalRestoreSession::Init(Callbacks callbacks) +{ + try { + auto restore = make_unique(); + ServiceProxy::InvaildInstance(); + auto proxy = ServiceProxy::GetInstance(); + if (proxy == nullptr) { + HILOGI("Failed to get backup service"); + return nullptr; + } + int32_t res = proxy->InitRestoreSession(new ServiceReverse(callbacks)); + if (res != 0) { + HILOGE("Failed to Restore because of %{public}d", res); + return nullptr; + } + + restore->RegisterBackupServiceDied(callbacks.onBackupServiceDied); + return restore; + } catch (const exception &e) { + HILOGE("Failed to Restore because of %{public}s", e.what()); + } + return nullptr; +} + +ErrCode BIncrementalRestoreSession::PublishFile(BFileInfo fileInfo) +{ + auto proxy = ServiceProxy::GetInstance(); + if (proxy == nullptr) { + return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode(); + } + return proxy->PublishIncrementalFile(fileInfo); +} + +ErrCode BIncrementalRestoreSession::GetFileHandle(const string &bundleName, const string &fileName) +{ + auto proxy = ServiceProxy::GetInstance(); + if (proxy == nullptr) { + return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode(); + } + + return proxy->GetIncrementalFileHandle(bundleName, fileName); +} + +ErrCode BIncrementalRestoreSession::AppendBundles(UniqueFd remoteCap, vector bundlesToRestore) +{ + auto proxy = ServiceProxy::GetInstance(); + if (proxy == nullptr) { + return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode(); + } + + return proxy->AppendBundlesRestoreSession(move(remoteCap), bundlesToRestore); +} + +ErrCode BIncrementalRestoreSession::Release() +{ + auto proxy = ServiceProxy::GetInstance(); + if (proxy == nullptr) { + return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode(); + } + + return proxy->Release(); +} + +void BIncrementalRestoreSession::RegisterBackupServiceDied(function functor) +{ + auto proxy = ServiceProxy::GetInstance(); + if (proxy == nullptr || !functor) { + return; + } + auto remoteObj = proxy->AsObject(); + if (!remoteObj) { + throw BError(BError::Codes::SA_BROKEN_IPC, "Proxy's remote object can't be nullptr"); + } + + auto callback = [functor](const wptr &obj) { + HILOGI("Backup service died"); + ServiceProxy::InvaildInstance(); + functor(); + }; + deathRecipient_ = sptr(new SvcDeathRecipient(callback)); + remoteObj->AddDeathRecipient(deathRecipient_); +} +} // 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 e89285aba..d96ef7847 100644 --- a/frameworks/native/backup_kit_inner/src/b_session_backup.cpp +++ b/frameworks/native/backup_kit_inner/src/b_session_backup.cpp @@ -114,4 +114,14 @@ ErrCode BSessionBackup::Finish() return proxy->Finish(); } + +ErrCode BSessionBackup::Release() +{ + auto proxy = ServiceProxy::GetInstance(); + if (proxy == nullptr) { + return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode(); + } + + return proxy->Release(); +} } // 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 59a6d67e4..f41ea469b 100644 --- a/frameworks/native/backup_kit_inner/src/b_session_restore.cpp +++ b/frameworks/native/backup_kit_inner/src/b_session_restore.cpp @@ -113,6 +113,16 @@ ErrCode BSessionRestore::Finish() return proxy->Finish(); } +ErrCode BSessionRestore::Release() +{ + auto proxy = ServiceProxy::GetInstance(); + if (proxy == nullptr) { + return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode(); + } + + return proxy->Release(); +} + void BSessionRestore::RegisterBackupServiceDied(std::function functor) { auto proxy = ServiceProxy::GetInstance(); 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 a0a38653c..4416e873b 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 @@ -104,6 +104,16 @@ ErrCode BSessionRestoreAsync::AppendBundles(UniqueFd remoteCap, return proxy->AppendBundlesRestoreSession(move(remoteCap), bundlesToRestore, restoreType, userId); } +ErrCode BSessionRestoreAsync::Release() +{ + auto proxy = ServiceProxy::GetInstance(); + if (proxy == nullptr) { + return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode(); + } + + return proxy->Release(); +} + void BSessionRestoreAsync::RegisterBackupServiceDied(std::function functor) { auto proxy = ServiceProxy::GetInstance(); diff --git a/frameworks/native/backup_kit_inner/src/service_incremental_proxy.cpp b/frameworks/native/backup_kit_inner/src/service_incremental_proxy.cpp new file mode 100644 index 000000000..ea227c5a3 --- /dev/null +++ b/frameworks/native/backup_kit_inner/src/service_incremental_proxy.cpp @@ -0,0 +1,263 @@ +/* + * Copyright (c) 2023 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 "service_proxy.h" + +#include "iservice_registry.h" +#include "system_ability_definition.h" + +#include "b_error/b_error.h" +#include "b_error/b_excep_utils.h" +#include "b_resources/b_constants.h" +#include "filemgmt_libhilog.h" +#include "svc_death_recipient.h" +#include "unique_fd.h" + +namespace OHOS::FileManagement::Backup { +using namespace std; + +ErrCode ServiceProxy::Release() +{ + HILOGI("Begin"); + BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr"); + MessageParcel data; + if (!data.WriteInterfaceToken(GetDescriptor())) { + return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write descriptor").GetCode(); + } + + MessageParcel reply; + MessageOption option; + int32_t ret = Remote()->SendRequest(static_cast(IServiceInterfaceCode::SERVICE_CMD_RELSEASE_SESSION), + data, reply, option); + if (ret != NO_ERROR) { + string str = "Failed to send out the request because of " + to_string(ret); + return BError(BError::Codes::SDK_INVAL_ARG, str.data()).GetCode(); + } + return reply.ReadInt32(); +} + +UniqueFd ServiceProxy::GetLocalCapabilitiesIncremental(const vector &bundleNames) +{ + HILOGI("Begin"); + BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr"); + MessageParcel data; + if (!data.WriteInterfaceToken(GetDescriptor())) { + HILOGE("Failed to write descriptor"); + return UniqueFd(-EPERM); + } + + if (!WriteParcelableVector(bundleNames, data)) { + HILOGE("Failed to send the bundleNames"); + return UniqueFd(-EPERM); + } + + MessageParcel reply; + MessageOption option; + option.SetWaitTime(BConstants::IPC_MAX_WAIT_TIME); + int32_t ret = Remote()->SendRequest( + static_cast(IServiceInterfaceCode::SERVICE_CMD_GET_LOCAL_CAPABILITIES_INCREMENTAL), data, reply, + option); + if (ret != NO_ERROR) { + HILOGE("Received error %{public}d when doing IPC", ret); + return UniqueFd(-ret); + } + UniqueFd fd(reply.ReadFileDescriptor()); + return UniqueFd(fd.Release()); +} + +ErrCode ServiceProxy::InitIncrementalBackupSession(sptr remote) +{ + HILOGI("Begin"); + BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "remote is nullptr"); + MessageParcel data; + if (!data.WriteInterfaceToken(GetDescriptor())) { + HILOGE("Failed to write descriptor"); + return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write descriptor").GetCode(); + } + + MessageParcel reply; + MessageOption option; + + if (!remote) { + HILOGE("Empty reverse stub"); + return BError(BError::Codes::SDK_INVAL_ARG, "Empty reverse stub").GetCode(); + } + if (!data.WriteRemoteObject(remote->AsObject().GetRefPtr())) { + HILOGE("Failed to send the reverse stub"); + return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send the reverse stub").GetCode(); + } + + int32_t ret = Remote()->SendRequest( + static_cast(IServiceInterfaceCode::SERVICE_CMD_INIT_INCREMENTAL_BACKUP_SESSION), data, reply, option); + if (ret != NO_ERROR) { + HILOGE("Received error %{public}d when doing IPC", ret); + return BError(BError::Codes::SDK_INVAL_ARG, "Received error when doing IPC").GetCode(); + } + return reply.ReadInt32(); +} + +ErrCode ServiceProxy::AppendBundlesIncrementalBackupSession(const vector &bundlesToBackup) +{ + HILOGI("Begin"); + BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "remote is nullptr"); + MessageParcel data; + if (!data.WriteInterfaceToken(GetDescriptor())) { + HILOGE("Failed to write descriptor"); + return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write descriptor").GetCode(); + } + + if (!WriteParcelableVector(bundlesToBackup, data)) { + HILOGE("Failed to send the bundleNames"); + return UniqueFd(-EPERM); + } + + MessageParcel reply; + MessageOption option; + + int32_t ret = Remote()->SendRequest( + static_cast(IServiceInterfaceCode::SERVICE_CMD_APPEND_BUNDLES_INCREMENTAL_BACKUP_SESSION), data, + reply, option); + if (ret != NO_ERROR) { + HILOGE("Received error %{public}d when doing IPC", ret); + return BError(BError::Codes::SDK_INVAL_ARG, "Received error when doing IPC").GetCode(); + } + return reply.ReadInt32(); +} + +ErrCode ServiceProxy::PublishIncrementalFile(const BFileInfo &fileInfo) +{ + HILOGI("Begin"); + BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr"); + MessageParcel data; + if (!data.WriteInterfaceToken(GetDescriptor())) { + return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write descriptor").GetCode(); + } + + if (!data.WriteParcelable(&fileInfo)) { + HILOGE("Failed to send the fileInfo"); + return -EPIPE; + } + + MessageParcel reply; + MessageOption option; + int32_t ret = Remote()->SendRequest( + static_cast(IServiceInterfaceCode::SERVICE_CMD_PUBLISH_INCREMENTAL_FILE), data, reply, option); + if (ret != NO_ERROR) { + string str = "Failed to send out the request because of " + to_string(ret); + return BError(BError::Codes::SDK_INVAL_ARG, str.data()).GetCode(); + } + return reply.ReadInt32(); +} + +ErrCode ServiceProxy::AppIncrementalFileReady(const std::string &fileName, UniqueFd fd, UniqueFd manifestFd) +{ + HILOGI("Begin"); + BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr"); + MessageParcel data; + if (!data.WriteInterfaceToken(GetDescriptor())) { + return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write descriptor").GetCode(); + } + + if (!data.WriteString(fileName)) { + return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send the filename").GetCode(); + } + if (!data.WriteFileDescriptor(fd)) { + return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send the fd").GetCode(); + } + if (!data.WriteFileDescriptor(manifestFd)) { + return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send the fd").GetCode(); + } + + MessageParcel reply; + MessageOption option; + int32_t ret = Remote()->SendRequest( + static_cast(IServiceInterfaceCode::SERVICE_CMD_APP_INCREMENTAL_FILE_READY), data, reply, option); + if (ret != NO_ERROR) { + string str = "Failed to send out the request because of " + to_string(ret); + return BError(BError::Codes::SDK_INVAL_ARG, str.data()).GetCode(); + } + return reply.ReadInt32(); +} + +ErrCode ServiceProxy::AppIncrementalDone(ErrCode errCode) +{ + HILOGI("Begin"); + BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr"); + MessageParcel data; + if (!data.WriteInterfaceToken(GetDescriptor())) { + return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write descriptor").GetCode(); + } + + if (!data.WriteInt32(errCode)) { + return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send the errCode").GetCode(); + } + + MessageParcel reply; + MessageOption option; + int32_t ret = Remote()->SendRequest(static_cast(IServiceInterfaceCode::SERVICE_CMD_APP_INCREMENTAL_DONE), + data, reply, option); + if (ret != NO_ERROR) { + string str = "Failed to send out the request because of " + to_string(ret); + return BError(BError::Codes::SDK_INVAL_ARG, str.data()).GetCode(); + } + return reply.ReadInt32(); +} + +ErrCode ServiceProxy::GetIncrementalFileHandle(const std::string &bundleName, const std::string &fileName) +{ + HILOGI("Begin"); + BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr"); + MessageParcel data; + if (!data.WriteInterfaceToken(GetDescriptor())) { + return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write descriptor").GetCode(); + } + + if (!data.WriteString(bundleName)) { + return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send the bundleName").GetCode(); + } + if (!data.WriteString(fileName)) { + return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send the fileName").GetCode(); + } + + MessageParcel reply; + MessageOption option; + option.SetFlags(MessageOption::TF_ASYNC); + int32_t ret = Remote()->SendRequest( + static_cast(IServiceInterfaceCode::SERVICE_CMD_GET_INCREMENTAL_FILE_NAME), data, reply, option); + if (ret != NO_ERROR) { + string str = "Failed to send out the request because of " + to_string(ret); + return BError(BError::Codes::SDK_INVAL_ARG, str.data()).GetCode(); + } + return ret; +} + +template +bool ServiceProxy::WriteParcelableVector(const std::vector &parcelableVector, Parcel &data) +{ + if (!data.WriteUint32(parcelableVector.size())) { + HILOGE("failed to WriteInt32 for parcelableVector.size()"); + return false; + } + + for (const auto &parcelable : parcelableVector) { + if (!data.WriteParcelable(&parcelable)) { + HILOGE("failed to WriteParcelable for parcelable"); + return false; + } + } + + return true; +} +} // namespace OHOS::FileManagement::Backup \ No newline at end of file diff --git a/frameworks/native/backup_kit_inner/src/service_incremental_reverse.cpp b/frameworks/native/backup_kit_inner/src/service_incremental_reverse.cpp new file mode 100644 index 000000000..5378ac946 --- /dev/null +++ b/frameworks/native/backup_kit_inner/src/service_incremental_reverse.cpp @@ -0,0 +1,115 @@ +/* + * Copyright (c) 2023 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 "service_reverse.h" + +#include "b_error/b_error.h" +#include "filemgmt_libhilog.h" + +namespace OHOS::FileManagement::Backup { +using namespace std; + +void ServiceReverse::IncrementalBackupOnFileReady(string bundleName, string fileName, int fd, int manifestFd) +{ + HILOGI("begin"); + if (scenario_ != Scenario::BACKUP || !callbacksIncrementalBackup_.onFileReady) { + HILOGI("Error scenario or callback is nullptr"); + return; + } + BFileInfo bFileInfo(bundleName, fileName, 0); + callbacksIncrementalBackup_.onFileReady(bFileInfo, UniqueFd(fd), UniqueFd(manifestFd)); +} + +void ServiceReverse::IncrementalBackupOnBundleStarted(int32_t errCode, string bundleName) +{ + HILOGI("begin"); + if (scenario_ != Scenario::BACKUP || !callbacksIncrementalBackup_.onBundleStarted) { + HILOGI("Error scenario or callback is nullptr"); + return; + } + callbacksIncrementalBackup_.onBundleStarted(errCode, bundleName); +} + +void ServiceReverse::IncrementalBackupOnBundleFinished(int32_t errCode, string bundleName) +{ + HILOGI("begin"); + if (scenario_ != Scenario::BACKUP || !callbacksIncrementalBackup_.onBundleFinished) { + HILOGI("Error scenario or callback is nullptr"); + return; + } + callbacksIncrementalBackup_.onBundleFinished(errCode, bundleName); +} + +void ServiceReverse::IncrementalBackupOnAllBundlesFinished(int32_t errCode) +{ + HILOGI("errCode = %{public}d", errCode); + if (scenario_ != Scenario::BACKUP || !callbacksIncrementalBackup_.onAllBundlesFinished) { + HILOGI("Error scenario or callback is nullptr"); + return; + } + callbacksIncrementalBackup_.onAllBundlesFinished(errCode); +} + +void ServiceReverse::IncrementalRestoreOnBundleStarted(int32_t errCode, string bundleName) +{ + HILOGI("begin"); + if (scenario_ != Scenario::RESTORE || !callbacksIncrementalRestore_.onBundleStarted) { + HILOGI("Error scenario or callback is nullptr"); + return; + } + callbacksIncrementalRestore_.onBundleStarted(errCode, bundleName); +} + +void ServiceReverse::IncrementalRestoreOnBundleFinished(int32_t errCode, string bundleName) +{ + HILOGI("begin"); + if (scenario_ != Scenario::RESTORE || !callbacksIncrementalRestore_.onBundleFinished) { + HILOGI("Error scenario or callback is nullptr"); + return; + } + callbacksIncrementalRestore_.onBundleFinished(errCode, bundleName); +} + +void ServiceReverse::IncrementalRestoreOnAllBundlesFinished(int32_t errCode) +{ + HILOGI("errCode = %{public}d", errCode); + if (scenario_ != Scenario::RESTORE || !callbacksIncrementalRestore_.onAllBundlesFinished) { + HILOGI("Error scenario or callback is nullptr"); + return; + } + callbacksIncrementalRestore_.onAllBundlesFinished(errCode); +} + +void ServiceReverse::IncrementalRestoreOnFileReady(string bundleName, string fileName, int fd, int manifestFd) +{ + HILOGI("begin"); + if (scenario_ != Scenario::RESTORE || !callbacksIncrementalRestore_.onFileReady) { + HILOGI("Error scenario or callback is nullptr"); + return; + } + BFileInfo bFileInfo(bundleName, fileName, 0); + callbacksIncrementalRestore_.onFileReady(bFileInfo, UniqueFd(fd), UniqueFd(manifestFd)); +} + +ServiceReverse::ServiceReverse(BIncrementalBackupSession::Callbacks callbacks) + : scenario_(Scenario::BACKUP), callbacksIncrementalBackup_(callbacks) +{ +} + +ServiceReverse::ServiceReverse(BIncrementalRestoreSession::Callbacks callbacks) + : scenario_(Scenario::RESTORE), callbacksIncrementalRestore_(callbacks) +{ +} +} // namespace OHOS::FileManagement::Backup \ No newline at end of file diff --git a/frameworks/native/backup_kit_inner/src/service_reverse_stub.cpp b/frameworks/native/backup_kit_inner/src/service_reverse_stub.cpp index 7cfcb79c0..cda7f3521 100644 --- a/frameworks/native/backup_kit_inner/src/service_reverse_stub.cpp +++ b/frameworks/native/backup_kit_inner/src/service_reverse_stub.cpp @@ -63,6 +63,30 @@ ServiceReverseStub::ServiceReverseStub() &ServiceReverseStub::CmdRestoreOnAllBundlesFinished; opToInterfaceMap_[static_cast(IServiceReverseInterfaceCode::SERVICER_RESTORE_ON_FILE_READY)] = &ServiceReverseStub::CmdRestoreOnFileReady; + + opToInterfaceMap_[static_cast(IServiceReverseInterfaceCode::SERVICER_INCREMENTAL_BACKUP_ON_FILE_READY)] = + &ServiceReverseStub::CmdIncrementalBackupOnFileReady; + opToInterfaceMap_[static_cast( + IServiceReverseInterfaceCode::SERVICER_INCREMENTAL_BACKUP_ON_SUB_TASK_STARTED)] = + &ServiceReverseStub::CmdIncrementalBackupOnBundleStarted; + opToInterfaceMap_[static_cast( + IServiceReverseInterfaceCode::SERVICER_INCREMENTAL_BACKUP_ON_SUB_TASK_FINISHED)] = + &ServiceReverseStub::CmdIncrementalBackupOnBundleFinished; + opToInterfaceMap_[static_cast( + IServiceReverseInterfaceCode::SERVICER_INCREMENTAL_BACKUP_ON_TASK_FINISHED)] = + &ServiceReverseStub::CmdIncrementalBackupOnAllBundlesFinished; + + opToInterfaceMap_[static_cast( + IServiceReverseInterfaceCode::SERVICER_INCREMENTAL_RESTORE_ON_SUB_TASK_STARTED)] = + &ServiceReverseStub::CmdIncrementalRestoreOnBundleStarted; + opToInterfaceMap_[static_cast( + IServiceReverseInterfaceCode::SERVICER_INCREMENTAL_RESTORE_ON_SUB_TASK_FINISHED)] = + &ServiceReverseStub::CmdIncrementalRestoreOnBundleFinished; + opToInterfaceMap_[static_cast( + IServiceReverseInterfaceCode::SERVICER_INCREMENTAL_RESTORE_ON_TASK_FINISHED)] = + &ServiceReverseStub::CmdIncrementalRestoreOnAllBundlesFinished; + opToInterfaceMap_[static_cast(IServiceReverseInterfaceCode::SERVICER_INCREMENTAL_RESTORE_ON_FILE_READY)] = + &ServiceReverseStub::CmdIncrementalRestoreOnFileReady; } int32_t ServiceReverseStub::CmdBackupOnFileReady(MessageParcel &data, MessageParcel &reply) @@ -128,4 +152,70 @@ int32_t ServiceReverseStub::CmdRestoreOnFileReady(MessageParcel &data, MessagePa RestoreOnFileReady(bundleName, fileName, fd); return BError(BError::Codes::OK); } + +int32_t ServiceReverseStub::CmdIncrementalBackupOnFileReady(MessageParcel &data, MessageParcel &reply) +{ + auto bundleName = data.ReadString(); + auto fileName = data.ReadString(); + int fd = data.ReadFileDescriptor(); + int manifestFd = data.ReadFileDescriptor(); + IncrementalBackupOnFileReady(bundleName, fileName, fd, manifestFd); + return BError(BError::Codes::OK); +} + +int32_t ServiceReverseStub::CmdIncrementalBackupOnBundleStarted(MessageParcel &data, MessageParcel &reply) +{ + int32_t errCode = data.ReadInt32(); + auto bundleName = data.ReadString(); + IncrementalBackupOnBundleStarted(errCode, bundleName); + return BError(BError::Codes::OK); +} + +int32_t ServiceReverseStub::CmdIncrementalBackupOnBundleFinished(MessageParcel &data, MessageParcel &reply) +{ + int32_t errCode = data.ReadInt32(); + auto bundleName = data.ReadString(); + IncrementalBackupOnBundleFinished(errCode, bundleName); + return BError(BError::Codes::OK); +} + +int32_t ServiceReverseStub::CmdIncrementalBackupOnAllBundlesFinished(MessageParcel &data, MessageParcel &reply) +{ + int32_t errCode = data.ReadInt32(); + IncrementalBackupOnAllBundlesFinished(errCode); + return BError(BError::Codes::OK); +} + +int32_t ServiceReverseStub::CmdIncrementalRestoreOnBundleStarted(MessageParcel &data, MessageParcel &reply) +{ + int32_t errCode = data.ReadInt32(); + auto bundleName = data.ReadString(); + IncrementalRestoreOnBundleStarted(errCode, bundleName); + return BError(BError::Codes::OK); +} + +int32_t ServiceReverseStub::CmdIncrementalRestoreOnBundleFinished(MessageParcel &data, MessageParcel &reply) +{ + int32_t errCode = data.ReadInt32(); + auto bundleName = data.ReadString(); + IncrementalRestoreOnBundleFinished(errCode, bundleName); + return BError(BError::Codes::OK); +} + +int32_t ServiceReverseStub::CmdIncrementalRestoreOnAllBundlesFinished(MessageParcel &data, MessageParcel &reply) +{ + int32_t errCode = data.ReadInt32(); + IncrementalRestoreOnAllBundlesFinished(errCode); + return BError(BError::Codes::OK); +} + +int32_t ServiceReverseStub::CmdIncrementalRestoreOnFileReady(MessageParcel &data, MessageParcel &reply) +{ + auto bundleName = data.ReadString(); + auto fileName = data.ReadString(); + int fd = data.ReadFileDescriptor(); + int manifestFd = data.ReadFileDescriptor(); + IncrementalRestoreOnFileReady(bundleName, fileName, fd, manifestFd); + return BError(BError::Codes::OK); +} } // namespace OHOS::FileManagement::Backup \ No newline at end of file diff --git a/interfaces/inner_api/native/backup_kit_inner/BUILD.gn b/interfaces/inner_api/native/backup_kit_inner/BUILD.gn index 5d4dcbd28..02da6b9fd 100644 --- a/interfaces/inner_api/native/backup_kit_inner/BUILD.gn +++ b/interfaces/inner_api/native/backup_kit_inner/BUILD.gn @@ -43,9 +43,14 @@ ohos_shared_library("backup_kit_inner") { sources = [ "${path_backup}/frameworks/native/backup_kit_inner/src/b_file_info.cpp", + "${path_backup}/frameworks/native/backup_kit_inner/src/b_incremental_backup_session.cpp", + "${path_backup}/frameworks/native/backup_kit_inner/src/b_incremental_data.cpp", + "${path_backup}/frameworks/native/backup_kit_inner/src/b_incremental_restore_session.cpp", "${path_backup}/frameworks/native/backup_kit_inner/src/b_session_backup.cpp", "${path_backup}/frameworks/native/backup_kit_inner/src/b_session_restore.cpp", "${path_backup}/frameworks/native/backup_kit_inner/src/b_session_restore_async.cpp", + "${path_backup}/frameworks/native/backup_kit_inner/src/service_incremental_proxy.cpp", + "${path_backup}/frameworks/native/backup_kit_inner/src/service_incremental_reverse.cpp", "${path_backup}/frameworks/native/backup_kit_inner/src/service_proxy.cpp", "${path_backup}/frameworks/native/backup_kit_inner/src/service_reverse.cpp", "${path_backup}/frameworks/native/backup_kit_inner/src/service_reverse_stub.cpp", diff --git a/interfaces/inner_api/native/backup_kit_inner/backup_kit_inner.h b/interfaces/inner_api/native/backup_kit_inner/backup_kit_inner.h index 7277da994..dfe4e48a4 100644 --- a/interfaces/inner_api/native/backup_kit_inner/backup_kit_inner.h +++ b/interfaces/inner_api/native/backup_kit_inner/backup_kit_inner.h @@ -16,6 +16,8 @@ #ifndef OHOS_FILEMGMT_BACKUP_BACKUP_KIT_INNER_H #define OHOS_FILEMGMT_BACKUP_BACKUP_KIT_INNER_H +#include "impl/b_incremental_backup_session.h" +#include "impl/b_incremental_restore_session.h" #include "impl/b_session_backup.h" #include "impl/b_session_restore.h" #include "impl/b_session_restore_async.h" 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 new file mode 100644 index 000000000..c0805aa79 --- /dev/null +++ b/interfaces/inner_api/native/backup_kit_inner/impl/b_incremental_backup_session.h @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2023 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_B_INCREMENTAL_BACKUP_BACKUP_H +#define OHOS_FILEMGMT_BACKUP_B_INCREMENTAL_BACKUP_BACKUP_H + +#include +#include +#include + +#include "b_file_info.h" +#include "b_incremental_data.h" +#include "errors.h" +#include "svc_death_recipient.h" +#include "unique_fd.h" + +namespace OHOS::FileManagement::Backup { +class BIncrementalBackupSession { +public: + struct Callbacks { + std::function onFileReady; // 当备份服务有文件待发送时执行的回调 + std::function onBundleStarted; // 当启动某个应用的备份流程结束时执行的回调函数 + std::function onBundleFinished; // 当某个应用的备份流程结束或意外中止时执行的回调函数 + std::function onAllBundlesFinished; // 当整个备份流程结束或意外中止时执行的回调函数 + std::function onBackupServiceDied; // 当备份服务意外死亡时执行的回调函数 + }; + +public: + /** + * @brief 获取一个用于控制备份流程的会话 + * + * @param callbacks 注册回调 + * @return std::unique_ptr 指向会话的智能指针。失败时为空指针 + */ + static std::unique_ptr Init(Callbacks callbacks); + + /** + * @brief 用于追加应用,现阶段仅支持在Start之前调用 + * + * @param bundlesToBackup 待备份的应用清单 + * @return ErrCode 规范错误码 + */ + ErrCode AppendBundles(std::vector bundlesToBackup); + + /** + * @brief 用于结束服务 + * + * @return ErrCode 规范错误码 + */ + ErrCode Release(); + + /** + * @brief 注册备份服务意外死亡时执行的回调函数 + * + * @param functor 回调函数 + */ + void RegisterBackupServiceDied(std::function functor); + +public: + ~BIncrementalBackupSession(); + +private: + sptr deathRecipient_; +}; +} // namespace OHOS::FileManagement::Backup + +#endif // OHOS_FILEMGMT_BACKUP_B_INCREMENTAL_BACKUP_BACKUP_H \ No newline at end of file diff --git a/interfaces/inner_api/native/backup_kit_inner/impl/b_incremental_data.h b/interfaces/inner_api/native/backup_kit_inner/impl/b_incremental_data.h new file mode 100644 index 000000000..1c7a25c96 --- /dev/null +++ b/interfaces/inner_api/native/backup_kit_inner/impl/b_incremental_data.h @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2023 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_B_INCREMENTAL_DATA_H +#define OHOS_FILEMGMT_BACKUP_B_INCREMENTAL_DATA_H + +#include +#include + +#include "parcel.h" +#include "unique_fd.h" + +namespace OHOS::FileManagement::Backup { +struct BIncrementalData : public Parcelable { + std::string bundleName; + int64_t lastIncrementalTime; + int32_t manifestFd; + std::string backupParameters; + int32_t backupPriority; + + BIncrementalData() = default; + BIncrementalData(std::string name, int64_t nTime, int fd = -1, std::string parameters = "", int32_t priority = 0) + : bundleName(name), lastIncrementalTime(nTime), manifestFd(fd), backupParameters(parameters), + backupPriority(priority) + { + } + ~BIncrementalData() override = default; + + bool ReadFromParcel(Parcel &parcel); + bool Marshalling(Parcel &parcel) const override; + static BIncrementalData *Unmarshalling(Parcel &parcel); +}; +} // namespace OHOS::FileManagement::Backup + +#endif // OHOS_FILEMGMT_BACKUP_B_INCREMENTAL_DATA_H 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 new file mode 100644 index 000000000..53293d2d9 --- /dev/null +++ b/interfaces/inner_api/native/backup_kit_inner/impl/b_incremental_restore_session.h @@ -0,0 +1,97 @@ +/* + * Copyright (c) 2023 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_B_INCREMENTAL_RESTORE_RESTORE_H +#define OHOS_FILEMGMT_BACKUP_B_INCREMENTAL_RESTORE_RESTORE_H + +#include +#include +#include + +#include "b_file_info.h" +#include "b_incremental_data.h" +#include "errors.h" +#include "svc_death_recipient.h" +#include "unique_fd.h" + +namespace OHOS::FileManagement::Backup { +class BIncrementalRestoreSession { +public: + struct Callbacks { + std::function onFileReady; // 当备份服务有文件待发送时执行的回调 + std::function onBundleStarted; // 当启动某个应用的恢复流程结束时执行的回调函数 + std::function onBundleFinished; // 当某个应用的恢复流程结束或意外中止时执行的回调函数 + std::function onAllBundlesFinished; // 当整个恢复流程结束或意外中止时执行的回调函数 + std::function onBackupServiceDied; // 当备份服务意外死亡时执行的回调函数 + }; + +public: + /** + * @brief 获取一个用于控制恢复流程的会话 + * + * @param callbacks 注册的回调函数 + * @return std::unique_ptr 指向BRestoreSession的智能指针。失败时为空指针 + */ + static std::unique_ptr Init(Callbacks callbacks); + + /** + * @brief 通知备份服务文件内容已就绪 + * + * @param fileInfo 文件描述信息 + * @return ErrCode 规范错误码 + * @see GetFileHandle + */ + ErrCode PublishFile(BFileInfo fileInfo); + + /** + * @brief 请求恢复流程所需的真实文件 + * + * @param bundleName 应用名称 + * @param fileName 文件名称 + */ + ErrCode GetFileHandle(const std::string &bundleName, const std::string &fileName); + + /** + * @brief 用于追加应用,现阶段仅支持在Start之前调用 + * + * @param remoteCap 已打开的保存远端设备能力的Json文件。可使用GetLocalCapabilities方法获取 + * @param bundlesToRestore 待恢复的应用清单 + * @return ErrCode 规范错误码 + */ + ErrCode AppendBundles(UniqueFd remoteCap, std::vector bundlesToRestore); + + /** + * @brief 用于结束服务 + * + * @return ErrCode 规范错误码 + */ + ErrCode Release(); + + /** + * @brief 注册备份服务意外死亡时执行的回调函数 + * + * @param functor 回调函数 + */ + void RegisterBackupServiceDied(std::function functor); + +public: + ~BIncrementalRestoreSession(); + +private: + sptr deathRecipient_; +}; +} // namespace OHOS::FileManagement::Backup + +#endif // OHOS_FILEMGMT_BACKUP_B_INCREMENTAL_RESTORE_RESTORE_H \ No newline at end of file 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 059e3b798..bed22250c 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 @@ -67,6 +67,13 @@ public: */ ErrCode Start(); + /** + * @brief 用于结束服务 + * + * @return ErrCode 规范错误码 + */ + ErrCode Release(); + /** * @brief 注册备份服务意外死亡时执行的回调函数 * diff --git a/interfaces/inner_api/native/backup_kit_inner/impl/b_session_restore.h b/interfaces/inner_api/native/backup_kit_inner/impl/b_session_restore.h index 4654a7bc5..14e730a21 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 @@ -85,6 +85,13 @@ public: */ ErrCode Start(); + /** + * @brief 用于结束服务 + * + * @return ErrCode 规范错误码 + */ + ErrCode Release(); + /** * @brief 注册备份服务意外死亡时执行的回调函数 * 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 0d547909e..2b6e0ddcc 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 @@ -86,6 +86,13 @@ public: RestoreTypeEnum restoreType = RestoreTypeEnum::RESTORE_DATA_WAIT_SEND, int32_t userId = DEFAULT_INVAL_VALUE); + /** + * @brief 用于结束服务 + * + * @return ErrCode 规范错误码 + */ + ErrCode Release(); + public: explicit BSessionRestoreAsync(Callbacks callbacks) : callbacks_(callbacks) {}; ~BSessionRestoreAsync(); @@ -94,24 +101,6 @@ private: /** @brief 注册备份服务意外死亡时执行的回调函数 */ void OnBackupServiceDied(); - /** @brief 从暂存队列中取出一次待恢复应用请求 */ - void PopBundleInfo(); - - /** - * @brief 执行待恢复应用请求 - * - * @param info 待恢复应用请求信息 - */ - void AppendBundlesImpl(AppendBundleInfo info); - - /** - * @brief IPC请求异常时通知回调 - * - * @param errCode - * @param bundlesToRestore - */ - void OnBundleStarted(ErrCode errCode, const std::vector &bundlesToRestore); - /** * @brief 注册备份服务意外死亡时执行的回调函数 * diff --git a/interfaces/inner_api/native/backup_kit_inner/impl/i_extension.h b/interfaces/inner_api/native/backup_kit_inner/impl/i_extension.h index ac9e5c09d..c21393e67 100644 --- a/interfaces/inner_api/native/backup_kit_inner/impl/i_extension.h +++ b/interfaces/inner_api/native/backup_kit_inner/impl/i_extension.h @@ -32,6 +32,9 @@ public: virtual ErrCode HandleBackup() = 0; virtual ErrCode PublishFile(const std::string &fileName) = 0; virtual ErrCode HandleRestore() = 0; + virtual ErrCode GetIncrementalFileHandle(const std::string &fileName) = 0; + virtual ErrCode PublishIncrementalFile(const std::string &fileName) = 0; + virtual ErrCode HandleIncrementalBackup(UniqueFd incrementalFd, UniqueFd manifestFd) = 0; }; } // namespace OHOS::FileManagement::Backup diff --git a/interfaces/inner_api/native/backup_kit_inner/impl/i_extension_ipc_interface_code.h b/interfaces/inner_api/native/backup_kit_inner/impl/i_extension_ipc_interface_code.h index 50a8be51a..091f87107 100644 --- a/interfaces/inner_api/native/backup_kit_inner/impl/i_extension_ipc_interface_code.h +++ b/interfaces/inner_api/native/backup_kit_inner/impl/i_extension_ipc_interface_code.h @@ -24,6 +24,9 @@ enum class IExtensionInterfaceCode { CMD_PUBLISH_FILE, CMD_HANDLE_BACKUP, CMD_HANDLE_RESTORE, + CMD_GET_INCREMENTAL_FILE_HANDLE, + CMD_PUBLISH_INCREMENTAL_FILE, + CMD_HANDLE_INCREMENTAL_BACKUP, }; } // namespace OHOS::FileManagement::Backup 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 e8bb2202a..869a3ca98 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 @@ -22,6 +22,7 @@ #include #include "b_file_info.h" +#include "b_incremental_data.h" #include "i_service_ipc_interface_code.h" #include "i_service_reverse.h" #include "iremote_broker.h" @@ -50,6 +51,15 @@ public: int32_t userId = DEFAULT_INVAL_VALUE) = 0; virtual ErrCode AppendBundlesBackupSession(const std::vector &bundleNames) = 0; virtual ErrCode Finish() = 0; + virtual ErrCode Release() = 0; + virtual UniqueFd GetLocalCapabilitiesIncremental(const std::vector &bundleNames) = 0; + virtual ErrCode InitIncrementalBackupSession(sptr remote) = 0; + virtual ErrCode AppendBundlesIncrementalBackupSession(const std::vector &bundlesToBackup) = 0; + + virtual ErrCode PublishIncrementalFile(const BFileInfo &fileInfo) = 0; + virtual ErrCode AppIncrementalFileReady(const std::string &fileName, UniqueFd fd, UniqueFd manifestFd) = 0; + virtual ErrCode AppIncrementalDone(ErrCode errCode) = 0; + virtual ErrCode GetIncrementalFileHandle(const std::string &bundleName, const std::string &fileName) = 0; DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.Filemanagement.Backup.IService") }; diff --git a/interfaces/inner_api/native/backup_kit_inner/impl/i_service_ipc_interface_code.h b/interfaces/inner_api/native/backup_kit_inner/impl/i_service_ipc_interface_code.h index 797921043..0e224a108 100644 --- a/interfaces/inner_api/native/backup_kit_inner/impl/i_service_ipc_interface_code.h +++ b/interfaces/inner_api/native/backup_kit_inner/impl/i_service_ipc_interface_code.h @@ -30,6 +30,14 @@ enum class IServiceInterfaceCode { SERVICE_CMD_APPEND_BUNDLES_RESTORE_SESSION, SERVICE_CMD_APPEND_BUNDLES_BACKUP_SESSION, SERVICE_CMD_FINISH, + SERVICE_CMD_RELSEASE_SESSION, + SERVICE_CMD_GET_LOCAL_CAPABILITIES_INCREMENTAL, + SERVICE_CMD_INIT_INCREMENTAL_BACKUP_SESSION, + SERVICE_CMD_APPEND_BUNDLES_INCREMENTAL_BACKUP_SESSION, + SERVICE_CMD_PUBLISH_INCREMENTAL_FILE, + SERVICE_CMD_APP_INCREMENTAL_FILE_READY, + SERVICE_CMD_APP_INCREMENTAL_DONE, + SERVICE_CMD_GET_INCREMENTAL_FILE_NAME, }; } // namespace OHOS::FileManagement::Backup diff --git a/interfaces/inner_api/native/backup_kit_inner/impl/i_service_reverse.h b/interfaces/inner_api/native/backup_kit_inner/impl/i_service_reverse.h index 0d0f9a7fe..0f959c9c0 100644 --- a/interfaces/inner_api/native/backup_kit_inner/impl/i_service_reverse.h +++ b/interfaces/inner_api/native/backup_kit_inner/impl/i_service_reverse.h @@ -41,6 +41,19 @@ public: virtual void RestoreOnAllBundlesFinished(int32_t errCode) = 0; virtual void RestoreOnFileReady(std::string bundleName, std::string fileName, int fd) = 0; + virtual void IncrementalBackupOnFileReady(std::string bundleName, std::string fileName, int fd, int manifestFd) = 0; + virtual void IncrementalBackupOnBundleStarted(int32_t errCode, std::string bundleName) = 0; + virtual void IncrementalBackupOnBundleFinished(int32_t errCode, std::string bundleName) = 0; + virtual void IncrementalBackupOnAllBundlesFinished(int32_t errCode) = 0; + + virtual void IncrementalRestoreOnBundleStarted(int32_t errCode, std::string bundleName) = 0; + virtual void IncrementalRestoreOnBundleFinished(int32_t errCode, std::string bundleName) = 0; + virtual void IncrementalRestoreOnAllBundlesFinished(int32_t errCode) = 0; + virtual void IncrementalRestoreOnFileReady(std::string bundleName, + std::string fileName, + int fd, + int manifestFd) = 0; + DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.FileManagement.Backup.IServiceReverse") }; } // namespace OHOS::FileManagement::Backup diff --git a/interfaces/inner_api/native/backup_kit_inner/impl/i_service_reverse_ipc_interface_code.h b/interfaces/inner_api/native/backup_kit_inner/impl/i_service_reverse_ipc_interface_code.h index 5fc63b67e..b99203b22 100644 --- a/interfaces/inner_api/native/backup_kit_inner/impl/i_service_reverse_ipc_interface_code.h +++ b/interfaces/inner_api/native/backup_kit_inner/impl/i_service_reverse_ipc_interface_code.h @@ -27,6 +27,14 @@ enum class IServiceReverseInterfaceCode { SERVICER_RESTORE_ON_SUB_TASK_FINISHED, SERVICER_RESTORE_ON_TASK_FINISHED, SERVICER_RESTORE_ON_FILE_READY, + SERVICER_INCREMENTAL_BACKUP_ON_FILE_READY, + SERVICER_INCREMENTAL_BACKUP_ON_SUB_TASK_STARTED, + SERVICER_INCREMENTAL_BACKUP_ON_SUB_TASK_FINISHED, + SERVICER_INCREMENTAL_BACKUP_ON_TASK_FINISHED, + SERVICER_INCREMENTAL_RESTORE_ON_SUB_TASK_STARTED, + SERVICER_INCREMENTAL_RESTORE_ON_SUB_TASK_FINISHED, + SERVICER_INCREMENTAL_RESTORE_ON_TASK_FINISHED, + SERVICER_INCREMENTAL_RESTORE_ON_FILE_READY, }; } // namespace OHOS::FileManagement::Backup 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 5273921a2..67a93868a 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 @@ -42,12 +42,23 @@ public: int32_t userId = DEFAULT_INVAL_VALUE) override; ErrCode AppendBundlesBackupSession(const std::vector &bundleNames) override; ErrCode Finish() override; + ErrCode Release() override; + UniqueFd GetLocalCapabilitiesIncremental(const std::vector &bundleNames) override; + ErrCode InitIncrementalBackupSession(sptr remote) override; + ErrCode AppendBundlesIncrementalBackupSession(const std::vector &bundlesToBackup) override; + + ErrCode PublishIncrementalFile(const BFileInfo &fileInfo) override; + ErrCode AppIncrementalFileReady(const std::string &fileName, UniqueFd fd, UniqueFd manifestFd) override; + ErrCode AppIncrementalDone(ErrCode errCode) override; + ErrCode GetIncrementalFileHandle(const std::string &bundleName, const std::string &fileName) override; public: explicit ServiceProxy(const sptr &impl) : IRemoteProxy(impl) {} ~ServiceProxy() override {} public: + template + bool WriteParcelableVector(const std::vector &parcelableVector, Parcel &data); static sptr GetInstance(); static void InvaildInstance(); diff --git a/services/backup.para b/services/backup.para index 8f4a7ef77..f4817d298 100644 --- a/services/backup.para +++ b/services/backup.para @@ -14,4 +14,7 @@ backup.debug.overrideExtensionConfig=false backup.debug.overrideAccountConfig=false -backup.debug.overrideAccountNumber=0 \ No newline at end of file +backup.debug.overrideAccountNumber=0 + +backup.overrideBackupSARelease=false +backup.overrideIncrementalRestore=false \ No newline at end of file diff --git a/services/backup_sa/BUILD.gn b/services/backup_sa/BUILD.gn index e89cb1f28..6a42bd130 100644 --- a/services/backup_sa/BUILD.gn +++ b/services/backup_sa/BUILD.gn @@ -29,6 +29,8 @@ ohos_shared_library("backup_sa") { "src/module_app_gallery/app_gallery_dispose_proxy.cpp", "src/module_external/bms_adapter.cpp", "src/module_external/sms_adapter.cpp", + "src/module_ipc/service_incremental_reverse_proxy.cpp", + "src/module_ipc/service_incremental.cpp", "src/module_ipc/service.cpp", "src/module_ipc/service_reverse_proxy.cpp", "src/module_ipc/service_stub.cpp", diff --git a/services/backup_sa/include/module_ipc/service.h b/services/backup_sa/include/module_ipc/service.h index 2a2e639bc..8c966e4e0 100644 --- a/services/backup_sa/include/module_ipc/service.h +++ b/services/backup_sa/include/module_ipc/service.h @@ -46,6 +46,16 @@ public: int32_t userId = DEFAULT_INVAL_VALUE) override; ErrCode AppendBundlesBackupSession(const std::vector &bundleNames) override; ErrCode Finish() override; + ErrCode Release() override; + + UniqueFd GetLocalCapabilitiesIncremental(const std::vector &bundleNames) override; + ErrCode InitIncrementalBackupSession(sptr remote) override; + ErrCode AppendBundlesIncrementalBackupSession(const std::vector &bundlesToBackup) override; + + ErrCode PublishIncrementalFile(const BFileInfo &fileInfo) override; + ErrCode AppIncrementalFileReady(const std::string &fileName, UniqueFd fd, UniqueFd manifestFd) override; + ErrCode AppIncrementalDone(ErrCode errCode) override; + ErrCode GetIncrementalFileHandle(const std::string &bundleName, const std::string &fileName) override; // 以下都是非IPC接口 public: @@ -168,6 +178,15 @@ private: */ void HandleRestoreDepsBundle(const std::string &bundleName); + /** + * @brief 增量备份恢复逻辑处理 + * + * @param bundleName + * @return true + * @return false + */ + bool IncrementalBackup(const std::string &bundleName); + private: static sptr instance_; static std::mutex instanceLock_; diff --git a/services/backup_sa/include/module_ipc/service_reverse_proxy.h b/services/backup_sa/include/module_ipc/service_reverse_proxy.h index 5fd75cb57..a7cc854fc 100644 --- a/services/backup_sa/include/module_ipc/service_reverse_proxy.h +++ b/services/backup_sa/include/module_ipc/service_reverse_proxy.h @@ -32,6 +32,16 @@ public: void RestoreOnAllBundlesFinished(int32_t errCode) override; void RestoreOnFileReady(std::string bundleName, std::string fileName, int fd) override; + void IncrementalBackupOnFileReady(std::string bundleName, std::string fileName, int fd, int manifestFd) override; + void IncrementalBackupOnBundleStarted(int32_t errCode, std::string bundleName) override; + void IncrementalBackupOnBundleFinished(int32_t errCode, std::string bundleName) override; + void IncrementalBackupOnAllBundlesFinished(int32_t errCode) override; + + void IncrementalRestoreOnBundleStarted(int32_t errCode, std::string bundleName) override; + void IncrementalRestoreOnBundleFinished(int32_t errCode, std::string bundleName) override; + void IncrementalRestoreOnAllBundlesFinished(int32_t errCode) override; + void IncrementalRestoreOnFileReady(std::string bundleName, std::string fileName, int fd, int manifestFd) override; + public: explicit ServiceReverseProxy(const sptr &impl) : IRemoteProxy(impl) {} ~ServiceReverseProxy() override = default; diff --git a/services/backup_sa/include/module_ipc/service_stub.h b/services/backup_sa/include/module_ipc/service_stub.h index 1049e1574..4fea6bb3c 100644 --- a/services/backup_sa/include/module_ipc/service_stub.h +++ b/services/backup_sa/include/module_ipc/service_stub.h @@ -45,6 +45,18 @@ private: int32_t CmdAppendBundlesRestoreSession(MessageParcel &data, MessageParcel &reply); int32_t CmdAppendBundlesBackupSession(MessageParcel &data, MessageParcel &reply); int32_t CmdFinish(MessageParcel &data, MessageParcel &reply); + int32_t CmdRelease(MessageParcel &data, MessageParcel &reply); + int32_t CmdGetLocalCapabilitiesIncremental(MessageParcel &data, MessageParcel &reply); + int32_t CmdInitIncrementalBackupSession(MessageParcel &data, MessageParcel &reply); + int32_t CmdAppendBundlesIncrementalBackupSession(MessageParcel &data, MessageParcel &reply); + int32_t CmdPublishIncrementalFile(MessageParcel &data, MessageParcel &reply); + int32_t CmdAppIncrementalFileReady(MessageParcel &data, MessageParcel &reply); + int32_t CmdAppIncrementalDone(MessageParcel &data, MessageParcel &reply); + int32_t CmdGetIncrementalFileHandle(MessageParcel &data, MessageParcel &reply); + +public: + template + bool ReadParcelableVector(std::vector &parcelableInfos, MessageParcel &data); }; } // namespace OHOS::FileManagement::Backup diff --git a/services/backup_sa/include/module_ipc/svc_extension_proxy.h b/services/backup_sa/include/module_ipc/svc_extension_proxy.h index b47ba5ded..ac48221f0 100644 --- a/services/backup_sa/include/module_ipc/svc_extension_proxy.h +++ b/services/backup_sa/include/module_ipc/svc_extension_proxy.h @@ -18,6 +18,7 @@ #include "i_extension.h" #include "iremote_proxy.h" +#include "unique_fd.h" namespace OHOS::FileManagement::Backup { class SvcExtensionProxy : public IRemoteProxy { @@ -27,6 +28,9 @@ public: ErrCode HandleBackup() override; ErrCode PublishFile(const std::string &fileName) override; ErrCode HandleRestore() override; + ErrCode GetIncrementalFileHandle(const std::string &fileName) override; + ErrCode PublishIncrementalFile(const std::string &fileName) override; + ErrCode HandleIncrementalBackup(UniqueFd incrementalFd, UniqueFd manifestFd) override; public: explicit SvcExtensionProxy(const sptr &remote) : IRemoteProxy(remote) {} diff --git a/services/backup_sa/include/module_ipc/svc_session_manager.h b/services/backup_sa/include/module_ipc/svc_session_manager.h index 46671ac84..9473b48b5 100644 --- a/services/backup_sa/include/module_ipc/svc_session_manager.h +++ b/services/backup_sa/include/module_ipc/svc_session_manager.h @@ -30,6 +30,7 @@ #include #include "b_file_info.h" +#include "b_incremental_data.h" #include "b_resources/b_constants.h" #include "i_service.h" #include "i_service_reverse.h" @@ -57,6 +58,10 @@ struct BackupExtInfo { /* Timer Status: true is start & false is stop */ bool timerStatus {false}; int64_t dataSize; + int64_t lastIncrementalTime; + int32_t manifestFd; + std::string backupParameters; + int32_t backupPriority; }; class Service; @@ -75,6 +80,7 @@ public: */ int32_t userId {100}; RestoreTypeEnum restoreDataType {RESTORE_DATA_WAIT_SEND}; + bool isIncrementalBackup {false}; }; public: @@ -377,6 +383,29 @@ public: */ void ClearSessionData(); + /** + * @brief Get the Is Incremental Backup object + * + * @return true + * @return false + */ + bool GetIsIncrementalBackup(); + + /** + * @brief Set the Incremental Data object + * + * @param incrementalData + */ + void SetIncrementalData(const BIncrementalData &incrementalData); + + /** + * @brief Get the Manifest Fd object + * + * @param bundleName 应用名称 + * @return int32_t + */ + int32_t GetIncrementalManifestFd(const std::string &bundleName); + private: /** * @brief 获取backup extension ability diff --git a/services/backup_sa/include/module_sched/sched_scheduler.h b/services/backup_sa/include/module_sched/sched_scheduler.h index 419c87782..5757579fe 100644 --- a/services/backup_sa/include/module_sched/sched_scheduler.h +++ b/services/backup_sa/include/module_sched/sched_scheduler.h @@ -72,11 +72,7 @@ public: */ void TryUnloadService(); - void StartTimer() - { - extTime_.Setup(); - TryUnloadServiceTimer(); - } + void StartTimer(); public: explicit SchedScheduler(wptr reversePtr, wptr sessionPtr) diff --git a/services/backup_sa/src/module_ipc/service.cpp b/services/backup_sa/src/module_ipc/service.cpp index d882ef63f..148e9016c 100644 --- a/services/backup_sa/src/module_ipc/service.cpp +++ b/services/backup_sa/src/module_ipc/service.cpp @@ -592,6 +592,9 @@ void Service::ExtStart(const string &bundleName) { try { HILOGE("begin %{public}s", bundleName.data()); + if (IncrementalBackup(bundleName)) { + return; + } IServiceReverse::Scenario scenario = session_->GetScenario(); auto backUpConnection = session_->GetExtConnection(bundleName); auto proxy = backUpConnection->GetBackupExtProxy(); @@ -652,7 +655,16 @@ void Service::ExtConnectFailed(const string &bundleName, ErrCode ret) try { HILOGE("begin %{public}s", bundleName.data()); IServiceReverse::Scenario scenario = session_->GetScenario(); - if (scenario == IServiceReverse::Scenario::BACKUP) { + if (scenario == IServiceReverse::Scenario::BACKUP && session_->GetIsIncrementalBackup()) { + session_->GetServiceReverseProxy()->IncrementalBackupOnBundleStarted(ret, bundleName); + } else if (scenario == IServiceReverse::Scenario::RESTORE && + BackupPara().GetBackupOverrideIncrementalRestore()) { + session_->GetServiceReverseProxy()->IncrementalRestoreOnBundleStarted(ret, bundleName); + + DisposeErr disposeErr = AppGalleryDisposeProxy::GetInstance()->EndRestore(bundleName); + HILOGI("ExtConnectFailed EndRestore, code=%{public}d, bundleName=%{public}s", disposeErr, + bundleName.c_str()); + } else if (scenario == IServiceReverse::Scenario::BACKUP) { session_->GetServiceReverseProxy()->BackupOnBundleStarted(ret, bundleName); } else if (scenario == IServiceReverse::Scenario::RESTORE) { session_->GetServiceReverseProxy()->RestoreOnBundleStarted(ret, bundleName); @@ -677,7 +689,11 @@ void Service::ExtConnectFailed(const string &bundleName, ErrCode ret) void Service::NoticeClientFinish(const string &bundleName, ErrCode errCode) { auto scenario = session_->GetScenario(); - if (scenario == IServiceReverse::Scenario::BACKUP) { + if (scenario == IServiceReverse::Scenario::BACKUP && session_->GetIsIncrementalBackup()) { + session_->GetServiceReverseProxy()->IncrementalBackupOnBundleFinished(errCode, bundleName); + } else if (scenario == IServiceReverse::Scenario::RESTORE && BackupPara().GetBackupOverrideIncrementalRestore()) { + session_->GetServiceReverseProxy()->IncrementalRestoreOnBundleFinished(errCode, bundleName); + } else if (scenario == IServiceReverse::Scenario::BACKUP) { session_->GetServiceReverseProxy()->BackupOnBundleFinished(errCode, bundleName); } else if (scenario == IServiceReverse::Scenario::RESTORE) { session_->GetServiceReverseProxy()->RestoreOnBundleFinished(errCode, bundleName); @@ -783,12 +799,19 @@ void Service::OnAllBundlesFinished(ErrCode errCode) { if (session_->IsOnAllBundlesFinished()) { IServiceReverse::Scenario scenario = session_->GetScenario(); - if (scenario == IServiceReverse::Scenario::BACKUP) { + if (scenario == IServiceReverse::Scenario::BACKUP && session_->GetIsIncrementalBackup()) { + session_->GetServiceReverseProxy()->IncrementalBackupOnAllBundlesFinished(errCode); + } else if (scenario == IServiceReverse::Scenario::RESTORE && + BackupPara().GetBackupOverrideIncrementalRestore()) { + session_->GetServiceReverseProxy()->IncrementalRestoreOnAllBundlesFinished(errCode); + } else if (scenario == IServiceReverse::Scenario::BACKUP) { session_->GetServiceReverseProxy()->BackupOnAllBundlesFinished(errCode); } else if (scenario == IServiceReverse::Scenario::RESTORE) { session_->GetServiceReverseProxy()->RestoreOnAllBundlesFinished(errCode); } - sched_->TryUnloadServiceTimer(true); + if (!BackupPara().GetBackupOverrideBackupSARelease()) { + sched_->TryUnloadServiceTimer(true); + } } } diff --git a/services/backup_sa/src/module_ipc/service_incremental.cpp b/services/backup_sa/src/module_ipc/service_incremental.cpp new file mode 100644 index 000000000..f8ede49c4 --- /dev/null +++ b/services/backup_sa/src/module_ipc/service_incremental.cpp @@ -0,0 +1,321 @@ +/* + * Copyright (c) 2023 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. + */ + +/* + * 注意: + * - 注意点1:本文件原则上只处理与IPC无关的业务逻辑 + * - 注意点2:This document, in principle, captures all exceptions. + * Prevent exceptions from spreading to insecure modules. + */ +#include "module_ipc/service.h" + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include + +#include "ability_manager_client.h" +#include "accesstoken_kit.h" +#include "b_error/b_error.h" +#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_ohos/startup/backup_para.h" +#include "b_process/b_multiuser.h" +#include "b_resources/b_constants.h" +#include "bundle_mgr_client.h" +#include "filemgmt_libhilog.h" +#include "ipc_skeleton.h" +#include "module_app_gallery/app_gallery_dispose_proxy.h" +#include "module_external/bms_adapter.h" +#include "module_ipc/svc_backup_connection.h" +#include "module_ipc/svc_restore_deps_manager.h" +#include "parameter.h" +#include "system_ability_definition.h" +#include "unique_fd.h" + +namespace OHOS::FileManagement::Backup { +using namespace std; + +namespace { +constexpr int32_t DEBUG_ID = 100; +} // namespace + +static inline int32_t GetUserIdDefault() +{ + auto [isDebug, debugId] = BackupPara().GetBackupDebugOverrideAccount(); + if (isDebug && debugId > DEBUG_ID) { + return debugId; + } + auto multiuser = BMultiuser::ParseUid(IPCSkeleton::GetCallingUid()); + if ((multiuser.userId == BConstants::SYSTEM_UID) || (multiuser.userId == BConstants::XTS_UID)) { + return BConstants::DEFAULT_USER_ID; + } + return multiuser.userId; +} + +ErrCode Service::Release() +{ + HILOGI("KILL"); + VerifyCaller(session_->GetScenario()); + SessionDeactive(); + return BError(BError::Codes::OK); +} + +UniqueFd Service::GetLocalCapabilitiesIncremental(const std::vector &bundleNames) +{ + HILOGI("Begin"); + return UniqueFd(-1); +} + +ErrCode Service::InitIncrementalBackupSession(sptr remote) +{ + HILOGI("Begin"); + try { + VerifyCaller(); + session_->Active({.clientToken = IPCSkeleton::GetCallingTokenID(), + .scenario = IServiceReverse::Scenario::BACKUP, + .clientProxy = remote, + .userId = GetUserIdDefault(), + .isIncrementalBackup = true}); + return BError(BError::Codes::OK); + } catch (const BError &e) { + StopAll(nullptr, true); + return e.GetCode(); + } +} + +ErrCode Service::AppendBundlesIncrementalBackupSession(const std::vector &bundlesToBackup) +{ + try { + HILOGI("Begin"); + session_->IncreaseSessionCnt(); // BundleMgrAdapter::GetBundleInfos可能耗时 + VerifyCaller(IServiceReverse::Scenario::BACKUP); + vector bundleNames {}; + for (auto &bundle : bundlesToBackup) { + bundleNames.emplace_back(bundle.bundleName); + } + auto backupInfos = BundleMgrAdapter::GetBundleInfos(bundleNames, session_->GetSessionUserId()); + session_->AppendBundles(bundleNames); + for (auto info : backupInfos) { + session_->SetBundleDataSize(info.name, info.spaceOccupied); + session_->SetBackupExtName(info.name, info.extensionName); + if (info.allToBackup == false) { + session_->GetServiceReverseProxy()->IncrementalBackupOnBundleStarted( + BError(BError::Codes::SA_FORBID_BACKUP_RESTORE), info.name); + session_->RemoveExtInfo(info.name); + } + } + for (auto &bundleInfo : bundlesToBackup) { + session_->SetIncrementalData(bundleInfo); + } + OnStartSched(); + session_->DecreaseSessionCnt(); + return BError(BError::Codes::OK); + } catch (const BError &e) { + session_->DecreaseSessionCnt(); + HILOGE("Failed, errCode = %{public}d", e.GetCode()); + return e.GetCode(); + } catch (...) { + session_->DecreaseSessionCnt(); + HILOGI("Unexpected exception"); + return EPERM; + } +} + +ErrCode Service::PublishIncrementalFile(const BFileInfo &fileInfo) +{ + try { + HILOGI("Begin"); + VerifyCaller(IServiceReverse::Scenario::RESTORE); + + auto backUpConnection = session_->GetExtConnection(fileInfo.owner); + auto proxy = backUpConnection->GetBackupExtProxy(); + if (!proxy) { + throw BError(BError::Codes::SA_INVAL_ARG, "Extension backup Proxy is empty"); + } + ErrCode res = proxy->PublishIncrementalFile(fileInfo.fileName); + if (res) { + HILOGE("Failed to publish file for backup extension"); + } + + return res; + } catch (const BError &e) { + return e.GetCode(); + } catch (const exception &e) { + HILOGI("Catched an unexpected low-level exception %{public}s", e.what()); + return EPERM; + } catch (...) { + HILOGI("Unexpected exception"); + return EPERM; + } +} + +ErrCode Service::AppIncrementalFileReady(const std::string &fileName, UniqueFd fd, UniqueFd manifestFd) +{ + try { + HILOGI("Begin"); + string callerName = VerifyCallerAndGetCallerName(); + if (fileName == BConstants::EXT_BACKUP_MANAGE) { + fd = session_->OnBunleExtManageInfo(callerName, move(fd)); + } + + if (session_->GetScenario() == IServiceReverse::Scenario::RESTORE) { + session_->GetServiceReverseProxy()->IncrementalRestoreOnFileReady(callerName, fileName, move(fd), + move(manifestFd)); + return BError(BError::Codes::OK); + } + + session_->GetServiceReverseProxy()->IncrementalBackupOnFileReady(callerName, fileName, move(fd), + move(manifestFd)); + if (session_->OnBunleFileReady(callerName, fileName)) { + auto backUpConnection = session_->GetExtConnection(callerName); + auto proxy = backUpConnection->GetBackupExtProxy(); + if (!proxy) { + throw BError(BError::Codes::SA_INVAL_ARG, "Extension backup Proxy is empty"); + } + // 通知extension清空缓存 + proxy->HandleClear(); + // 清除Timer + session_->BundleExtTimerStop(callerName); + // 通知TOOL 备份完成 + session_->GetServiceReverseProxy()->IncrementalBackupOnBundleFinished(BError(BError::Codes::OK), + callerName); + // 断开extension + backUpConnection->DisconnectBackupExtAbility(); + ClearSessionAndSchedInfo(callerName); + } + OnAllBundlesFinished(BError(BError::Codes::OK)); + return BError(BError::Codes::OK); + } catch (const BError &e) { + return e.GetCode(); // 任意异常产生,终止监听该任务 + } catch (const exception &e) { + HILOGI("Catched an unexpected low-level exception %{public}s", e.what()); + return EPERM; + } catch (...) { + HILOGI("Unexpected exception"); + return EPERM; + } +} + +ErrCode Service::AppIncrementalDone(ErrCode errCode) +{ + try { + HILOGI("Begin"); + string callerName = VerifyCallerAndGetCallerName(); + if (session_->OnBunleFileReady(callerName)) { + auto backUpConnection = session_->GetExtConnection(callerName); + auto proxy = backUpConnection->GetBackupExtProxy(); + if (!proxy) { + throw BError(BError::Codes::SA_INVAL_ARG, "Extension backup Proxy is empty"); + } + proxy->HandleClear(); + session_->BundleExtTimerStop(callerName); + IServiceReverse::Scenario scenario = session_->GetScenario(); + if (scenario == IServiceReverse::Scenario::BACKUP) { + session_->GetServiceReverseProxy()->IncrementalBackupOnBundleFinished(errCode, callerName); + } else if (scenario == IServiceReverse::Scenario::RESTORE) { + session_->GetServiceReverseProxy()->IncrementalRestoreOnBundleFinished(errCode, callerName); + } + backUpConnection->DisconnectBackupExtAbility(); + ClearSessionAndSchedInfo(callerName); + } + OnAllBundlesFinished(BError(BError::Codes::OK)); + return BError(BError::Codes::OK); + } catch (const BError &e) { + return e.GetCode(); // 任意异常产生,终止监听该任务 + } catch (const exception &e) { + HILOGI("Catched an unexpected low-level exception %{public}s", e.what()); + return EPERM; + } catch (...) { + HILOGI("Unexpected exception"); + return EPERM; + } +} + +ErrCode Service::GetIncrementalFileHandle(const std::string &bundleName, const std::string &fileName) +{ + try { + HILOGI("Begin"); + VerifyCaller(IServiceReverse::Scenario::RESTORE); + auto action = session_->GetServiceSchedAction(bundleName); + if (action == BConstants::ServiceSchedAction::RUNNING) { + auto backUpConnection = session_->GetExtConnection(bundleName); + auto proxy = backUpConnection->GetBackupExtProxy(); + if (!proxy) { + throw BError(BError::Codes::SA_INVAL_ARG, "Extension backup Proxy is empty"); + } + int res = proxy->GetIncrementalFileHandle(fileName); + if (res) { + HILOGE("Failed to extension file handle"); + } + } else { + SvcRestoreDepsManager::GetInstance().UpdateToRestoreBundleMap(bundleName, fileName); + session_->SetExtFileNameRequest(bundleName, fileName); + } + return BError(BError::Codes::OK); + } catch (const BError &e) { + return e.GetCode(); + } catch (const exception &e) { + HILOGI("Catched an unexpected low-level exception %{public}s", e.what()); + return EPERM; + } catch (...) { + HILOGI("Unexpected exception"); + return EPERM; + } +} + +bool Service::IncrementalBackup(const string &bundleName) +{ + IServiceReverse::Scenario scenario = session_->GetScenario(); + auto backUpConnection = session_->GetExtConnection(bundleName); + auto proxy = backUpConnection->GetBackupExtProxy(); + if (!proxy) { + throw BError(BError::Codes::SA_INVAL_ARG, "Extension backup Proxy is empty"); + } + if (scenario == IServiceReverse::Scenario::BACKUP && session_->GetIsIncrementalBackup()) { + UniqueFd fd; + UniqueFd manifestFd(session_->GetIncrementalManifestFd(bundleName)); + auto ret = proxy->HandleIncrementalBackup(move(fd), move(manifestFd)); + session_->GetServiceReverseProxy()->IncrementalBackupOnBundleStarted(ret, bundleName); + if (ret) { + ClearSessionAndSchedInfo(bundleName); + } + return true; + } else if (scenario == IServiceReverse::Scenario::RESTORE && BackupPara().GetBackupOverrideIncrementalRestore()) { + auto ret = proxy->HandleRestore(); + session_->GetServiceReverseProxy()->IncrementalRestoreOnBundleStarted(ret, bundleName); + auto fileNameVec = session_->GetExtFileNameRequest(bundleName); + for (auto &fileName : fileNameVec) { + ret = proxy->GetIncrementalFileHandle(fileName); + if (ret) { + HILOGE("Failed to extension file handle %{public}s", fileName.c_str()); + } + } + return true; + } + return false; +} +} // namespace OHOS::FileManagement::Backup diff --git a/services/backup_sa/src/module_ipc/service_incremental_reverse_proxy.cpp b/services/backup_sa/src/module_ipc/service_incremental_reverse_proxy.cpp new file mode 100644 index 000000000..dc9b1dfc3 --- /dev/null +++ b/services/backup_sa/src/module_ipc/service_incremental_reverse_proxy.cpp @@ -0,0 +1,183 @@ +/* + * Copyright (c) 2023 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 "module_ipc/service_reverse_proxy.h" + +#include "b_error/b_error.h" +#include "b_error/b_excep_utils.h" +#include "filemgmt_libhilog.h" +#include "module_app_gallery/app_gallery_dispose_proxy.h" + +namespace OHOS::FileManagement::Backup { +using namespace std; + +void ServiceReverseProxy::IncrementalBackupOnFileReady(string bundleName, string fileName, int fd, int manifestFd) +{ + HILOGI("Begin"); + BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr"); + MessageParcel data; + if (!data.WriteInterfaceToken(GetDescriptor()) || !data.WriteString(bundleName) || !data.WriteString(fileName) || + !data.WriteFileDescriptor(fd) || !data.WriteFileDescriptor(manifestFd)) { + throw BError(BError::Codes::SA_BROKEN_IPC); + } + + MessageParcel reply; + MessageOption option; + if (int err = Remote()->SendRequest( + static_cast(IServiceReverseInterfaceCode::SERVICER_INCREMENTAL_BACKUP_ON_FILE_READY), data, reply, + option); + err != ERR_OK) { + throw BError(BError::Codes::SA_BROKEN_IPC, to_string(err)); + } +} + +void ServiceReverseProxy::IncrementalBackupOnBundleStarted(int32_t errCode, string bundleName) +{ + HILOGI("Begin"); + BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr"); + MessageParcel data; + if (!data.WriteInterfaceToken(GetDescriptor()) || !data.WriteInt32(errCode) || !data.WriteString(bundleName)) { + throw BError(BError::Codes::SA_BROKEN_IPC); + }; + + MessageParcel reply; + MessageOption option; + if (int err = Remote()->SendRequest( + static_cast(IServiceReverseInterfaceCode::SERVICER_INCREMENTAL_BACKUP_ON_SUB_TASK_STARTED), data, + reply, option); + err != ERR_OK) { + throw BError(BError::Codes::SA_BROKEN_IPC, to_string(err)); + } +} + +void ServiceReverseProxy::IncrementalBackupOnBundleFinished(int32_t errCode, string bundleName) +{ + HILOGI("Begin"); + BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr"); + MessageParcel data; + if (!data.WriteInterfaceToken(GetDescriptor()) || !data.WriteInt32(errCode) || !data.WriteString(bundleName)) { + throw BError(BError::Codes::SA_BROKEN_IPC); + } + + MessageParcel reply; + MessageOption option; + if (int err = Remote()->SendRequest( + static_cast(IServiceReverseInterfaceCode::SERVICER_INCREMENTAL_BACKUP_ON_SUB_TASK_FINISHED), data, + reply, option); + err != ERR_OK) { + throw BError(BError::Codes::SA_BROKEN_IPC, to_string(err)); + } +} + +void ServiceReverseProxy::IncrementalBackupOnAllBundlesFinished(int32_t errCode) +{ + HILOGI("Begin"); + BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr"); + MessageParcel data; + if (!data.WriteInterfaceToken(GetDescriptor()) || !data.WriteInt32(errCode)) { + throw BError(BError::Codes::SA_BROKEN_IPC); + } + + MessageParcel reply; + MessageOption option; + if (int err = Remote()->SendRequest( + static_cast(IServiceReverseInterfaceCode::SERVICER_INCREMENTAL_BACKUP_ON_TASK_FINISHED), data, + reply, option); + err != ERR_OK) { + throw BError(BError::Codes::SA_BROKEN_IPC, to_string(err)); + } +} + +void ServiceReverseProxy::IncrementalRestoreOnBundleStarted(int32_t errCode, string bundleName) +{ + HILOGI("Begin"); + BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr"); + MessageParcel data; + if (!data.WriteInterfaceToken(GetDescriptor()) || !data.WriteInt32(errCode) || !data.WriteString(bundleName)) { + throw BError(BError::Codes::SA_BROKEN_IPC); + } + + MessageParcel reply; + MessageOption option; + if (int err = Remote()->SendRequest( + static_cast(IServiceReverseInterfaceCode::SERVICER_INCREMENTAL_RESTORE_ON_SUB_TASK_STARTED), data, + reply, option); + err != ERR_OK) { + throw BError(BError::Codes::SA_BROKEN_IPC, to_string(err)); + } +} + +void ServiceReverseProxy::IncrementalRestoreOnBundleFinished(int32_t errCode, string bundleName) +{ + HILOGI("Begin"); + BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr"); + MessageParcel data; + if (!data.WriteInterfaceToken(GetDescriptor()) || !data.WriteInt32(errCode) || !data.WriteString(bundleName)) { + throw BError(BError::Codes::SA_BROKEN_IPC); + } + + MessageParcel reply; + MessageOption option; + if (int err = Remote()->SendRequest( + static_cast(IServiceReverseInterfaceCode::SERVICER_INCREMENTAL_RESTORE_ON_SUB_TASK_FINISHED), + data, reply, option); + err != ERR_OK) { + throw BError(BError::Codes::SA_BROKEN_IPC, to_string(err)); + } + + DisposeErr disposeErr = AppGalleryDisposeProxy::GetInstance()->EndRestore(bundleName); + HILOGI("RestoreOnBundleFinished EndRestore, code=%{public}d, bundleName=%{public}s", disposeErr, + bundleName.c_str()); +} + +void ServiceReverseProxy::IncrementalRestoreOnAllBundlesFinished(int32_t errCode) +{ + HILOGI("Begin"); + BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr"); + MessageParcel data; + if (!data.WriteInterfaceToken(GetDescriptor()) || !data.WriteInt32(errCode)) { + throw BError(BError::Codes::SA_BROKEN_IPC); + } + + MessageParcel reply; + MessageOption option; + if (int err = Remote()->SendRequest( + static_cast(IServiceReverseInterfaceCode::SERVICER_INCREMENTAL_RESTORE_ON_TASK_FINISHED), data, + reply, option); + err != ERR_OK) { + throw BError(BError::Codes::SA_BROKEN_IPC, to_string(err)); + } +} + +void ServiceReverseProxy::IncrementalRestoreOnFileReady(string bundleName, string fileName, int fd, int manifestFd) +{ + HILOGI("Begin"); + BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr"); + MessageParcel data; + if (!data.WriteInterfaceToken(GetDescriptor()) || !data.WriteString(bundleName) || !data.WriteString(fileName) || + !data.WriteFileDescriptor(fd) || !data.WriteFileDescriptor(manifestFd)) { + throw BError(BError::Codes::SA_BROKEN_IPC); + } + + MessageParcel reply; + MessageOption option; + if (int err = Remote()->SendRequest( + static_cast(IServiceReverseInterfaceCode::SERVICER_INCREMENTAL_RESTORE_ON_FILE_READY), data, + reply, option); + err != ERR_OK) { + throw BError(BError::Codes::SA_BROKEN_IPC, to_string(err)); + } +} +} // namespace OHOS::FileManagement::Backup \ No newline at end of file diff --git a/services/backup_sa/src/module_ipc/service_stub.cpp b/services/backup_sa/src/module_ipc/service_stub.cpp index eb0f9470d..327574e6d 100644 --- a/services/backup_sa/src/module_ipc/service_stub.cpp +++ b/services/backup_sa/src/module_ipc/service_stub.cpp @@ -25,6 +25,7 @@ #include "b_error/b_error.h" #include "b_error/b_excep_utils.h" +#include "b_resources/b_constants.h" #include "filemgmt_libhilog.h" #include "module_ipc/service_reverse_proxy.h" @@ -52,6 +53,23 @@ ServiceStub::ServiceStub() opToInterfaceMap_[static_cast(IServiceInterfaceCode::SERVICE_CMD_APPEND_BUNDLES_BACKUP_SESSION)] = &ServiceStub::CmdAppendBundlesBackupSession; opToInterfaceMap_[static_cast(IServiceInterfaceCode::SERVICE_CMD_FINISH)] = &ServiceStub::CmdFinish; + opToInterfaceMap_[static_cast(IServiceInterfaceCode::SERVICE_CMD_RELSEASE_SESSION)] = + &ServiceStub::CmdRelease; + opToInterfaceMap_[static_cast(IServiceInterfaceCode::SERVICE_CMD_GET_LOCAL_CAPABILITIES_INCREMENTAL)] = + &ServiceStub::CmdGetLocalCapabilitiesIncremental; + opToInterfaceMap_[static_cast(IServiceInterfaceCode::SERVICE_CMD_INIT_INCREMENTAL_BACKUP_SESSION)] = + &ServiceStub::CmdInitIncrementalBackupSession; + opToInterfaceMap_[static_cast( + IServiceInterfaceCode::SERVICE_CMD_APPEND_BUNDLES_INCREMENTAL_BACKUP_SESSION)] = + &ServiceStub::CmdAppendBundlesIncrementalBackupSession; + opToInterfaceMap_[static_cast(IServiceInterfaceCode::SERVICE_CMD_PUBLISH_INCREMENTAL_FILE)] = + &ServiceStub::CmdPublishIncrementalFile; + opToInterfaceMap_[static_cast(IServiceInterfaceCode::SERVICE_CMD_APP_INCREMENTAL_FILE_READY)] = + &ServiceStub::CmdAppIncrementalFileReady; + opToInterfaceMap_[static_cast(IServiceInterfaceCode::SERVICE_CMD_APP_INCREMENTAL_DONE)] = + &ServiceStub::CmdAppIncrementalDone; + opToInterfaceMap_[static_cast(IServiceInterfaceCode::SERVICE_CMD_GET_INCREMENTAL_FILE_NAME)] = + &ServiceStub::CmdGetIncrementalFileHandle; } int32_t ServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) @@ -257,4 +275,161 @@ int32_t ServiceStub::CmdFinish(MessageParcel &data, MessageParcel &reply) } return BError(BError::Codes::OK); } + +int32_t ServiceStub::CmdRelease(MessageParcel &data, MessageParcel &reply) +{ + HILOGI("Begin"); + int res = Release(); + if (!reply.WriteInt32(res)) { + return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to send the result ") + to_string(res)); + } + return BError(BError::Codes::OK); +} + +int32_t ServiceStub::CmdGetLocalCapabilitiesIncremental(MessageParcel &data, MessageParcel &reply) +{ + HILOGI("Begin"); + vector bundleNames; + if (!ReadParcelableVector(bundleNames, data)) { + return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive bundleNames"); + } + + UniqueFd fd(GetLocalCapabilitiesIncremental(bundleNames)); + if (!reply.WriteFileDescriptor(fd)) { + return BError(BError::Codes::SA_BROKEN_IPC, "Failed to send out the file"); + } + return BError(BError::Codes::OK); +} + +int32_t ServiceStub::CmdInitIncrementalBackupSession(MessageParcel &data, MessageParcel &reply) +{ + HILOGI("Begin"); + auto remote = data.ReadRemoteObject(); + if (!remote) { + return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive the reverse stub"); + } + auto iremote = iface_cast(remote); + if (!iremote) { + return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive the reverse stub"); + } + + int32_t res = InitIncrementalBackupSession(iremote); + if (!reply.WriteInt32(res)) { + stringstream ss; + ss << "Failed to send the result " << res; + return BError(BError::Codes::SA_BROKEN_IPC, ss.str()); + } + return BError(BError::Codes::OK); +} + +int32_t ServiceStub::CmdAppendBundlesIncrementalBackupSession(MessageParcel &data, MessageParcel &reply) +{ + HILOGI("Begin"); + vector bundlesToBackup; + if (!ReadParcelableVector(bundlesToBackup, data)) { + return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive bundleNames"); + } + + int32_t res = AppendBundlesIncrementalBackupSession(bundlesToBackup); + if (!reply.WriteInt32(res)) { + return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to send the result ") + to_string(res)); + } + return BError(BError::Codes::OK); +} + +int32_t ServiceStub::CmdPublishIncrementalFile(MessageParcel &data, MessageParcel &reply) +{ + HILOGI("Begin"); + unique_ptr fileInfo(data.ReadParcelable()); + if (!fileInfo) { + return BError(BError::Codes::SA_BROKEN_IPC, "Failed to receive fileInfo"); + } + int res = PublishIncrementalFile(*fileInfo); + if (!reply.WriteInt32(res)) { + stringstream ss; + ss << "Failed to send the result " << res; + return BError(BError::Codes::SA_BROKEN_IPC, ss.str()); + } + return BError(BError::Codes::OK); +} + +int32_t ServiceStub::CmdAppIncrementalFileReady(MessageParcel &data, MessageParcel &reply) +{ + HILOGI("Begin"); + string fileName; + if (!data.ReadString(fileName)) { + return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive fileName"); + } + UniqueFd fd(data.ReadFileDescriptor()); + if (fd < 0) { + return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive fd"); + } + + UniqueFd manifestFd(data.ReadFileDescriptor()); + if (manifestFd < 0) { + return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive fd"); + } + + int res = AppIncrementalFileReady(fileName, move(fd), move(manifestFd)); + if (!reply.WriteInt32(res)) { + stringstream ss; + ss << "Failed to send the result " << res; + return BError(BError::Codes::SA_BROKEN_IPC, ss.str()); + } + return BError(BError::Codes::OK); +} + +int32_t ServiceStub::CmdAppIncrementalDone(MessageParcel &data, MessageParcel &reply) +{ + HILOGI("Begin"); + int32_t ret; + if (!data.ReadInt32(ret)) { + return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive bool flag"); + } + int res = AppIncrementalDone(ret); + if (!reply.WriteInt32(res)) { + stringstream ss; + ss << "Failed to send the result " << res; + return BError(BError::Codes::SA_BROKEN_IPC, ss.str()); + } + return BError(BError::Codes::OK); +} + +int32_t ServiceStub::CmdGetIncrementalFileHandle(MessageParcel &data, MessageParcel &reply) +{ + HILOGI("Begin"); + string bundleName; + if (!data.ReadString(bundleName)) { + return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive bundleName").GetCode(); + } + string fileName; + if (!data.ReadString(fileName)) { + return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive fileName").GetCode(); + } + + return GetIncrementalFileHandle(bundleName, fileName); +} + +template +bool ServiceStub::ReadParcelableVector(std::vector &parcelableInfos, MessageParcel &data) +{ + int32_t infoSize = 0; + if (!data.ReadInt32(infoSize)) { + HILOGE("Failed to read Parcelable size."); + return false; + } + + parcelableInfos.clear(); + infoSize = (infoSize < BConstants::MAX_PARCELABLE_VECTOR_NUM) ? infoSize : BConstants::MAX_PARCELABLE_VECTOR_NUM; + for (int32_t index = 0; index < infoSize; index++) { + sptr info = data.ReadParcelable(); + if (info == nullptr) { + HILOGE("Failed to read Parcelable infos."); + return false; + } + parcelableInfos.emplace_back(move(*info)); + } + + return true; +} } // 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 8d1c9276c..0eec93ecb 100644 --- a/services/backup_sa/src/module_ipc/svc_extension_proxy.cpp +++ b/services/backup_sa/src/module_ipc/svc_extension_proxy.cpp @@ -134,4 +134,77 @@ ErrCode SvcExtensionProxy::HandleRestore() HILOGI("Successful"); return reply.ReadInt32(); } + +ErrCode SvcExtensionProxy::GetIncrementalFileHandle(const string &fileName) +{ + HILOGI("Start"); + BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr"); + MessageParcel data; + data.WriteInterfaceToken(GetDescriptor()); + + if (!data.WriteString(fileName)) { + BError(BError::Codes::SDK_INVAL_ARG, "Failed to send the fileName"); + return ErrCode(EPERM); + } + + MessageParcel reply; + MessageOption option; + int32_t ret = Remote()->SendRequest(static_cast(IExtensionInterfaceCode::CMD_GET_INCREMENTAL_FILE_HANDLE), + data, reply, option); + if (ret != NO_ERROR) { + HILOGE("Received error %{public}d when doing IPC", ret); + return ErrCode(ret); + } + + HILOGI("Successful"); + return reply.ReadInt32(); +} + +ErrCode SvcExtensionProxy::PublishIncrementalFile(const string &fileName) +{ + HILOGI("Start"); + BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr"); + MessageParcel data; + data.WriteInterfaceToken(GetDescriptor()); + + if (!data.WriteString(fileName)) { + BError(BError::Codes::SDK_INVAL_ARG, "Failed to send the fileName"); + return ErrCode(EPERM); + } + + MessageParcel reply; + MessageOption option; + int32_t ret = Remote()->SendRequest(static_cast(IExtensionInterfaceCode::CMD_PUBLISH_INCREMENTAL_FILE), + data, reply, option); + if (ret != NO_ERROR) { + HILOGE("Received error %{public}d when doing IPC", ret); + return ErrCode(ret); + } + + HILOGI("Successful"); + return reply.ReadInt32(); +} + +ErrCode SvcExtensionProxy::HandleIncrementalBackup(UniqueFd incrementalFd, UniqueFd manifestFd) +{ + HILOGI("Start"); + BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr"); + MessageParcel data; + data.WriteInterfaceToken(GetDescriptor()); + + data.WriteFileDescriptor(incrementalFd); + data.WriteFileDescriptor(manifestFd); + + MessageParcel reply; + MessageOption option; + int32_t ret = Remote()->SendRequest(static_cast(IExtensionInterfaceCode::CMD_HANDLE_INCREMENTAL_BACKUP), + data, reply, option); + if (ret != NO_ERROR) { + HILOGE("Received error %{public}d when doing IPC", ret); + return ErrCode(ret); + } + + HILOGI("Successful"); + return reply.ReadInt32(); +} } // namespace OHOS::FileManagement::Backup \ No newline at end of file diff --git a/services/backup_sa/src/module_ipc/svc_session_manager.cpp b/services/backup_sa/src/module_ipc/svc_session_manager.cpp index b82bd8c8c..6b2f8534c 100644 --- a/services/backup_sa/src/module_ipc/svc_session_manager.cpp +++ b/services/backup_sa/src/module_ipc/svc_session_manager.cpp @@ -30,6 +30,7 @@ #include "filemgmt_libhilog.h" #include "module_ipc/service.h" #include "module_ipc/svc_restore_deps_manager.h" +#include "unique_fd.h" namespace OHOS::FileManagement::Backup { using namespace std; @@ -618,6 +619,10 @@ void SvcSessionManager::ClearSessionData() extBundleTimer.Unregister(it->second.extTimerId); // disconnect extension if (it->second.schedAction == BConstants::ServiceSchedAction::RUNNING) { + auto proxy = it->second.backUpConnection->GetBackupExtProxy(); + if (proxy && impl_.restoreDataType != RestoreTypeEnum::RESTORE_DATA_READDY) { + proxy->HandleClear(); + } it->second.backUpConnection->DisconnectBackupExtAbility(); } // clear data @@ -625,4 +630,36 @@ void SvcSessionManager::ClearSessionData() } impl_.backupExtNameMap.clear(); } + +bool SvcSessionManager::GetIsIncrementalBackup() +{ + unique_lock lock(lock_); + if (!impl_.clientToken) { + throw BError(BError::Codes::SA_INVAL_ARG, "No caller token was specified"); + } + return impl_.isIncrementalBackup; +} + +void SvcSessionManager::SetIncrementalData(const BIncrementalData &incrementalData) +{ + unique_lock lock(lock_); + if (!impl_.clientToken) { + throw BError(BError::Codes::SA_INVAL_ARG, "No caller token was specified"); + } + auto it = GetBackupExtNameMap(incrementalData.bundleName); + it->second.lastIncrementalTime = incrementalData.lastIncrementalTime; + it->second.manifestFd = incrementalData.manifestFd; + it->second.backupParameters = incrementalData.backupParameters; + it->second.backupPriority = incrementalData.backupPriority; +} + +int32_t SvcSessionManager::GetIncrementalManifestFd(const string &bundleName) +{ + unique_lock lock(lock_); + if (!impl_.clientToken) { + throw BError(BError::Codes::SA_INVAL_ARG, "No caller token was specified"); + } + auto it = GetBackupExtNameMap(bundleName); + return it->second.manifestFd; +} } // namespace OHOS::FileManagement::Backup diff --git a/services/backup_sa/src/module_sched/sched_scheduler.cpp b/services/backup_sa/src/module_sched/sched_scheduler.cpp index 04e5db2bc..ffa5771bb 100644 --- a/services/backup_sa/src/module_sched/sched_scheduler.cpp +++ b/services/backup_sa/src/module_sched/sched_scheduler.cpp @@ -28,6 +28,7 @@ #include #include "b_error/b_error.h" +#include "b_ohos/startup/backup_para.h" #include "filemgmt_libhilog.h" #include "iservice_registry.h" #include "module_external/bms_adapter.h" @@ -117,6 +118,14 @@ void SchedScheduler::RemoveExtConn(const string &bundleName) } } +void SchedScheduler::StartTimer() +{ + extTime_.Setup(); + if (!BackupPara().GetBackupOverrideBackupSARelease()) { + TryUnloadServiceTimer(); + } +} + void SchedScheduler::TryUnloadServiceTimer(bool force) { auto tryUnload = [sessionPtr {sessionPtr_}]() { diff --git a/tests/mock/backup_kit_inner/b_session_backup_mock.cpp b/tests/mock/backup_kit_inner/b_session_backup_mock.cpp index f3a8f5193..4f34a37d7 100644 --- a/tests/mock/backup_kit_inner/b_session_backup_mock.cpp +++ b/tests/mock/backup_kit_inner/b_session_backup_mock.cpp @@ -92,4 +92,9 @@ ErrCode BSessionBackup::Finish() { return BError(BError::Codes::OK); } + +ErrCode BSessionBackup::Release() +{ + return BError(BError::Codes::OK); +} } // namespace OHOS::FileManagement::Backup \ 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 4068bd936..def40ed6e 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 @@ -96,5 +96,10 @@ ErrCode BSessionRestoreAsync::AppendBundles(UniqueFd remoteCap, void BSessionRestoreAsync::OnBackupServiceDied() {} +ErrCode BSessionRestoreAsync::Release() +{ + return BError(BError::Codes::OK); +} + void BSessionRestoreAsync::RegisterBackupServiceDied(function functor) {} } // namespace OHOS::FileManagement::Backup \ No newline at end of file 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 49d4126bc..3af358b95 100644 --- a/tests/mock/backup_kit_inner/b_session_restore_mock.cpp +++ b/tests/mock/backup_kit_inner/b_session_restore_mock.cpp @@ -96,5 +96,10 @@ ErrCode BSessionRestore::Finish() return BError(BError::Codes::OK); } +ErrCode BSessionRestore::Release() +{ + return BError(BError::Codes::OK); +} + void BSessionRestore::RegisterBackupServiceDied(function functor) {} } // namespace OHOS::FileManagement::Backup \ No newline at end of file diff --git a/tests/mock/backup_kit_inner/service_proxy_mock.cpp b/tests/mock/backup_kit_inner/service_proxy_mock.cpp index cd0808e1a..760bd1158 100644 --- a/tests/mock/backup_kit_inner/service_proxy_mock.cpp +++ b/tests/mock/backup_kit_inner/service_proxy_mock.cpp @@ -98,6 +98,46 @@ ErrCode ServiceProxy::Finish() return BError(BError::Codes::OK); } +ErrCode ServiceProxy::Release() +{ + return BError(BError::Codes::OK); +} + +UniqueFd ServiceProxy::GetLocalCapabilitiesIncremental(const vector &bundleNames) +{ + return UniqueFd(-1); +} + +ErrCode ServiceProxy::InitIncrementalBackupSession(sptr remote) +{ + return BError(BError::Codes::OK); +} + +ErrCode ServiceProxy::AppendBundlesIncrementalBackupSession(const vector &bundlesToBackup) +{ + return BError(BError::Codes::OK); +} + +ErrCode ServiceProxy::PublishIncrementalFile(const BFileInfo &fileInfo) +{ + return BError(BError::Codes::OK); +} + +ErrCode ServiceProxy::AppIncrementalFileReady(const string &fileName, UniqueFd fd, UniqueFd manifestFd) +{ + return BError(BError::Codes::OK); +} + +ErrCode ServiceProxy::AppIncrementalDone(ErrCode errCode) +{ + return BError(BError::Codes::OK); +} + +ErrCode ServiceProxy::GetIncrementalFileHandle(const std::string &bundleName, const std::string &fileName) +{ + return BError(BError::Codes::OK); +} + sptr ServiceProxy::GetInstance() { if (!GetMockGetInstance()) { diff --git a/tests/mock/module_ipc/service_mock.cpp b/tests/mock/module_ipc/service_mock.cpp index 898c0f835..bf99081c7 100644 --- a/tests/mock/module_ipc/service_mock.cpp +++ b/tests/mock/module_ipc/service_mock.cpp @@ -129,4 +129,44 @@ void Service::OnStartSched() {} void Service::SendAppGalleryNotify(const BundleName &bundleName) {} void Service::SessionDeactive() {} + +ErrCode Service::Release() +{ + return BError(BError::Codes::OK); +} + +UniqueFd Service::GetLocalCapabilitiesIncremental(const std::vector &bundleNames) +{ + return UniqueFd(-1); +} + +ErrCode Service::InitIncrementalBackupSession(sptr remote) +{ + return BError(BError::Codes::OK); +} + +ErrCode Service::AppendBundlesIncrementalBackupSession(const std::vector &bundlesToBackup) +{ + return BError(BError::Codes::OK); +} + +ErrCode Service::PublishIncrementalFile(const BFileInfo &fileInfo) +{ + return BError(BError::Codes::OK); +} + +ErrCode Service::AppIncrementalFileReady(const string &fileName, UniqueFd fd, UniqueFd manifestFd) +{ + return BError(BError::Codes::OK); +} + +ErrCode Service::AppIncrementalDone(ErrCode errCode) +{ + return BError(BError::Codes::OK); +} + +ErrCode Service::GetIncrementalFileHandle(const string &bundleName, const string &fileName) +{ + return BError(BError::Codes::OK); +} } // namespace OHOS::FileManagement::Backup diff --git a/tests/mock/module_ipc/service_reverse_proxy_mock.cpp b/tests/mock/module_ipc/service_reverse_proxy_mock.cpp index 0f98e5e2e..456a81465 100644 --- a/tests/mock/module_ipc/service_reverse_proxy_mock.cpp +++ b/tests/mock/module_ipc/service_reverse_proxy_mock.cpp @@ -35,4 +35,20 @@ void ServiceReverseProxy::RestoreOnBundleFinished(int32_t errCode, string bundle void ServiceReverseProxy::RestoreOnAllBundlesFinished(int32_t errCode) {} void ServiceReverseProxy::RestoreOnFileReady(string bundleName, string fileName, int fd) {} + +void ServiceReverseProxy::IncrementalBackupOnFileReady(string bundleName, string fileName, int fd, int manifestFd) {} + +void ServiceReverseProxy::IncrementalBackupOnBundleStarted(int32_t errCode, string bundleName) {} + +void ServiceReverseProxy::IncrementalBackupOnBundleFinished(int32_t errCode, string bundleName) {} + +void ServiceReverseProxy::IncrementalBackupOnAllBundlesFinished(int32_t errCode) {} + +void ServiceReverseProxy::IncrementalRestoreOnBundleStarted(int32_t errCode, string bundleName) {} + +void ServiceReverseProxy::IncrementalRestoreOnBundleFinished(int32_t errCode, string bundleName) {} + +void ServiceReverseProxy::IncrementalRestoreOnAllBundlesFinished(int32_t errCode) {} + +void ServiceReverseProxy::IncrementalRestoreOnFileReady(string bundleName, string fileName, int fd, int manifestFd) {} } // namespace OHOS::FileManagement::Backup \ 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 07e131cac..3e65392b3 100644 --- a/tests/mock/module_ipc/service_stub_mock.cpp +++ b/tests/mock/module_ipc/service_stub_mock.cpp @@ -44,6 +44,21 @@ ServiceStub::ServiceStub() opToInterfaceMap_[static_cast(IServiceInterfaceCode::SERVICE_CMD_APPEND_BUNDLES_BACKUP_SESSION)] = &ServiceStub::CmdAppendBundlesBackupSession; opToInterfaceMap_[static_cast(IServiceInterfaceCode::SERVICE_CMD_FINISH)] = &ServiceStub::CmdFinish; + opToInterfaceMap_[static_cast(IServiceInterfaceCode::SERVICE_CMD_RELSEASE_SESSION)] = + &ServiceStub::CmdRelease; + opToInterfaceMap_[static_cast(IServiceInterfaceCode::SERVICE_CMD_GET_LOCAL_CAPABILITIES_INCREMENTAL)] = + &ServiceStub::CmdGetLocalCapabilitiesIncremental; + opToInterfaceMap_[static_cast(IServiceInterfaceCode::SERVICE_CMD_INIT_INCREMENTAL_BACKUP_SESSION)] = + &ServiceStub::CmdInitIncrementalBackupSession; + opToInterfaceMap_[static_cast( + IServiceInterfaceCode::SERVICE_CMD_APPEND_BUNDLES_INCREMENTAL_BACKUP_SESSION)] = + &ServiceStub::CmdAppendBundlesIncrementalBackupSession; + opToInterfaceMap_[static_cast(IServiceInterfaceCode::SERVICE_CMD_PUBLISH_INCREMENTAL_FILE)] = + &ServiceStub::CmdPublishIncrementalFile; + opToInterfaceMap_[static_cast(IServiceInterfaceCode::SERVICE_CMD_APP_INCREMENTAL_FILE_READY)] = + &ServiceStub::CmdAppIncrementalFileReady; + opToInterfaceMap_[static_cast(IServiceInterfaceCode::SERVICE_CMD_GET_INCREMENTAL_FILE_NAME)] = + &ServiceStub::CmdGetIncrementalFileHandle; } int32_t ServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) @@ -159,4 +174,41 @@ int32_t ServiceStub::CmdFinish(MessageParcel &data, MessageParcel &reply) reply.WriteInt32(res); return BError(BError::Codes::OK); } + +int32_t ServiceStub::CmdRelease(MessageParcel &data, MessageParcel &reply) +{ + int res = Release(); + reply.WriteInt32(res); + return BError(BError::Codes::OK); +} + +int32_t ServiceStub::CmdGetLocalCapabilitiesIncremental(MessageParcel &data, MessageParcel &reply) +{ + return BError(BError::Codes::OK); +} + +int32_t ServiceStub::CmdInitIncrementalBackupSession(MessageParcel &data, MessageParcel &reply) +{ + return BError(BError::Codes::OK); +} + +int32_t ServiceStub::CmdAppendBundlesIncrementalBackupSession(MessageParcel &data, MessageParcel &reply) +{ + return BError(BError::Codes::OK); +} + +int32_t ServiceStub::CmdPublishIncrementalFile(MessageParcel &data, MessageParcel &reply) +{ + return BError(BError::Codes::OK); +} + +int32_t ServiceStub::CmdAppIncrementalFileReady(MessageParcel &data, MessageParcel &reply) +{ + return BError(BError::Codes::OK); +} + +int32_t ServiceStub::CmdGetIncrementalFileHandle(MessageParcel &data, MessageParcel &reply) +{ + return BError(BError::Codes::OK); +} } // namespace OHOS::FileManagement::Backup diff --git a/tests/mock/module_ipc/svc_extension_proxy_mock.cpp b/tests/mock/module_ipc/svc_extension_proxy_mock.cpp index 2403f2d2f..6c1fbe7e3 100644 --- a/tests/mock/module_ipc/svc_extension_proxy_mock.cpp +++ b/tests/mock/module_ipc/svc_extension_proxy_mock.cpp @@ -42,4 +42,19 @@ ErrCode SvcExtensionProxy::HandleRestore() { return 0; } + +ErrCode SvcExtensionProxy::GetIncrementalFileHandle(const string &fileName) +{ + return 0; +} + +ErrCode SvcExtensionProxy::PublishIncrementalFile(const string &fileName) +{ + return 0; +} + +ErrCode SvcExtensionProxy::HandleIncrementalBackup(UniqueFd incrementalFd, UniqueFd manifestFd) +{ + return 0; +} } // namespace OHOS::FileManagement::Backup \ No newline at end of file diff --git a/tests/mock/module_ipc/svc_session_manager_mock.cpp b/tests/mock/module_ipc/svc_session_manager_mock.cpp index 8c9ff7ab9..e5b8d488b 100644 --- a/tests/mock/module_ipc/svc_session_manager_mock.cpp +++ b/tests/mock/module_ipc/svc_session_manager_mock.cpp @@ -314,4 +314,16 @@ void SvcSessionManager::IncreaseSessionCnt() {} void SvcSessionManager::DecreaseSessionCnt() {} void SvcSessionManager::ClearSessionData() {} + +bool SvcSessionManager::GetIsIncrementalBackup() +{ + return true; +} + +void SvcSessionManager::SetIncrementalData(const BIncrementalData &incrementalData) {} + +int32_t SvcSessionManager::GetIncrementalManifestFd(const string &bundleName) +{ + return 0; +} } // namespace OHOS::FileManagement::Backup diff --git a/tests/mock/module_sched/sched_scheduler_mock.cpp b/tests/mock/module_sched/sched_scheduler_mock.cpp index 82fdc4485..65c744fa7 100644 --- a/tests/mock/module_sched/sched_scheduler_mock.cpp +++ b/tests/mock/module_sched/sched_scheduler_mock.cpp @@ -30,6 +30,8 @@ void SchedScheduler::ExecutingQueueTasks(const string &bundleName) {} void SchedScheduler::RemoveExtConn(const string &bundleName) {} +void SchedScheduler::StartTimer() {} + void SchedScheduler::TryUnloadServiceTimer(bool force) {} void SchedScheduler::TryUnloadService() {} diff --git a/tests/moduletests/backup_kit_inner/BUILD.gn b/tests/moduletests/backup_kit_inner/BUILD.gn index 961434ce1..9d382efd3 100644 --- a/tests/moduletests/backup_kit_inner/BUILD.gn +++ b/tests/moduletests/backup_kit_inner/BUILD.gn @@ -21,6 +21,7 @@ ohos_unittest("b_session_test") { "${path_backup}/frameworks/native/backup_kit_inner/src/b_session_backup.cpp", "${path_backup}/frameworks/native/backup_kit_inner/src/b_session_restore.cpp", "${path_backup}/frameworks/native/backup_kit_inner/src/b_session_restore_async.cpp", + "${path_backup}/frameworks/native/backup_kit_inner/src/service_incremental_reverse.cpp", "${path_backup}/frameworks/native/backup_kit_inner/src/service_reverse.cpp", "${path_backup}/frameworks/native/backup_kit_inner/src/service_reverse_stub.cpp", "b_session_backup_test.cpp", diff --git a/tests/unittests/backup_api/backup_impl/include/ext_extension_mock.h b/tests/unittests/backup_api/backup_impl/include/ext_extension_mock.h index bc7ed16eb..d6fe04fba 100644 --- a/tests/unittests/backup_api/backup_impl/include/ext_extension_mock.h +++ b/tests/unittests/backup_api/backup_impl/include/ext_extension_mock.h @@ -97,6 +97,21 @@ public: return BError(BError::Codes::OK); }; + ErrCode GetIncrementalFileHandle(const std::string &fileName) override + { + return BError(BError::Codes::OK); + }; + + ErrCode PublishIncrementalFile(const std::string &fileName) override + { + return BError(BError::Codes::OK); + }; + + ErrCode HandleIncrementalBackup(UniqueFd incrementalFd, UniqueFd manifestFd) override + { + return BError(BError::Codes::OK); + }; + private: int32_t nHandleBackupNum_ = 0; }; 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 2af0cb1e1..3436577a1 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 @@ -110,6 +110,46 @@ public: { return BError(BError::Codes::OK); } + + ErrCode Release() override + { + return BError(BError::Codes::OK); + } + + UniqueFd GetLocalCapabilitiesIncremental(const std::vector &bundleNames) override + { + return UniqueFd(-1); + } + + ErrCode InitIncrementalBackupSession(sptr remote) override + { + return BError(BError::Codes::OK); + } + + ErrCode AppendBundlesIncrementalBackupSession(const std::vector &bundlesToBackup) override + { + return BError(BError::Codes::OK); + } + + ErrCode PublishIncrementalFile(const BFileInfo &fileInfo) + { + return BError(BError::Codes::OK); + } + + ErrCode AppIncrementalFileReady(const std::string &fileName, UniqueFd fd, UniqueFd manifestFd) + { + return BError(BError::Codes::OK); + } + + ErrCode AppIncrementalDone(ErrCode errCode) + { + return BError(BError::Codes::OK); + } + + ErrCode GetIncrementalFileHandle(const std::string &bundleName, const std::string &fileName) + { + return BError(BError::Codes::OK); + } }; } // namespace OHOS::FileManagement::Backup #endif // MOCK_I_SERVICE_MOCK_H \ No newline at end of file diff --git a/tests/unittests/backup_api/backup_impl/include/service_reverse_mock.h b/tests/unittests/backup_api/backup_impl/include/service_reverse_mock.h index e4f6e2a2a..9808e5d41 100644 --- a/tests/unittests/backup_api/backup_impl/include/service_reverse_mock.h +++ b/tests/unittests/backup_api/backup_impl/include/service_reverse_mock.h @@ -45,6 +45,16 @@ public: void RestoreOnBundleFinished(int32_t errCode, std::string bundleName) override {} void RestoreOnAllBundlesFinished(int32_t errCode) override {} void RestoreOnFileReady(std::string bundleName, std::string fileName, int fd) override {} + + void IncrementalBackupOnFileReady(std::string bundleName, std::string fileName, int fd, int manifestFd) override {} + void IncrementalBackupOnBundleStarted(int32_t errCode, std::string bundleName) override {} + void IncrementalBackupOnBundleFinished(int32_t errCode, std::string bundleName) override {} + void IncrementalBackupOnAllBundlesFinished(int32_t errCode) override {} + + void IncrementalRestoreOnBundleStarted(int32_t errCode, std::string bundleName) override {} + void IncrementalRestoreOnBundleFinished(int32_t errCode, std::string bundleName) override {} + void IncrementalRestoreOnAllBundlesFinished(int32_t errCode) override {} + void IncrementalRestoreOnFileReady(std::string bundleName, std::string fileName, int fd, int manifestFd) override {} }; } // namespace OHOS::FileManagement::Backup #endif // MOCK_SERVICE_REVERSE_MOCK_H \ No newline at end of file diff --git a/tests/unittests/backup_api/backup_impl/service_reverse_stub_test.cpp b/tests/unittests/backup_api/backup_impl/service_reverse_stub_test.cpp index c3f41057d..2fd0b60a7 100644 --- a/tests/unittests/backup_api/backup_impl/service_reverse_stub_test.cpp +++ b/tests/unittests/backup_api/backup_impl/service_reverse_stub_test.cpp @@ -47,6 +47,14 @@ public: MOCK_METHOD2(RestoreOnBundleFinished, void(int32_t errCode, string bundleName)); MOCK_METHOD1(RestoreOnAllBundlesFinished, void(int32_t errCode)); MOCK_METHOD3(RestoreOnFileReady, void(string bundleName, string fileName, int fd)); + MOCK_METHOD4(IncrementalBackupOnFileReady, void(string bundleName, string fileName, int fd, int manifestFd)); + MOCK_METHOD2(IncrementalBackupOnBundleStarted, void(int32_t errCode, string bundleName)); + MOCK_METHOD2(IncrementalBackupOnBundleFinished, void(int32_t errCode, string bundleName)); + MOCK_METHOD1(IncrementalBackupOnAllBundlesFinished, void(int32_t errCode)); + MOCK_METHOD2(IncrementalRestoreOnBundleStarted, void(int32_t errCode, std::string bundleName)); + MOCK_METHOD2(IncrementalRestoreOnBundleFinished, void(int32_t errCode, string bundleName)); + MOCK_METHOD1(IncrementalRestoreOnAllBundlesFinished, void(int32_t errCode)); + MOCK_METHOD4(IncrementalRestoreOnFileReady, void(string bundleName, string fileName, int fd, int manifestFd)); }; class ServiceReverseStubTest : public testing::Test { diff --git a/tests/unittests/backup_sa/module_ipc/BUILD.gn b/tests/unittests/backup_sa/module_ipc/BUILD.gn index fa9f7762e..3864c6553 100644 --- a/tests/unittests/backup_sa/module_ipc/BUILD.gn +++ b/tests/unittests/backup_sa/module_ipc/BUILD.gn @@ -68,6 +68,7 @@ ohos_unittest("backup_service_test") { "${path_backup_mock}/accesstoken/accesstoken_kit_mock.cpp", "${path_backup_mock}/module_ipc/app_gallery_dispose_proxy_mock.cpp", "${path_backup_mock}/timer/timer_mock.cpp", + "${path_backup}/services/backup_sa/src/module_ipc/service_incremental.cpp", "${path_backup}/services/backup_sa/src/module_ipc/service.cpp", "${path_backup}/services/backup_sa/src/module_ipc/svc_restore_deps_manager.cpp", "service_test.cpp", @@ -217,6 +218,7 @@ ohos_unittest("backup_restore_deps_manager_test") { sources = [ "${path_backup_mock}/accesstoken/accesstoken_kit_mock.cpp", "${path_backup}/services/backup_sa/src/module_app_gallery/app_gallery_dispose_proxy.cpp", + "${path_backup}/services/backup_sa/src/module_ipc/service_incremental.cpp", "${path_backup}/services/backup_sa/src/module_ipc/service.cpp", "${path_backup}/services/backup_sa/src/module_ipc/svc_restore_deps_manager.cpp", "svc_restore_deps_manager_test.cpp", 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 8b06ed40c..2e221933d 100644 --- a/tests/unittests/backup_sa/module_ipc/service_stub_test.cpp +++ b/tests/unittests/backup_sa/module_ipc/service_stub_test.cpp @@ -53,6 +53,15 @@ public: ErrCode(UniqueFd fd, const std::vector &bundleNames, RestoreTypeEnum restoreType, int32_t userId)); MOCK_METHOD1(AppendBundlesBackupSession, ErrCode(const std::vector &bundleNames)); MOCK_METHOD0(Finish, ErrCode()); + MOCK_METHOD0(Release, ErrCode()); + MOCK_METHOD1(GetLocalCapabilitiesIncremental, UniqueFd(const std::vector &bundleNames)); + MOCK_METHOD1(InitIncrementalBackupSession, ErrCode(sptr remote)); + MOCK_METHOD1(AppendBundlesIncrementalBackupSession, ErrCode(const std::vector &bundlesToBackup)); + + MOCK_METHOD1(PublishIncrementalFile, ErrCode(const BFileInfo &fileInfo)); + MOCK_METHOD3(AppIncrementalFileReady, ErrCode(const std::string &fileName, UniqueFd fd, UniqueFd manifestFd)); + MOCK_METHOD1(AppIncrementalDone, ErrCode(ErrCode errCode)); + MOCK_METHOD2(GetIncrementalFileHandle, ErrCode(const std::string &bundleName, const std::string &fileName)); UniqueFd InvokeGetLocalCapabilities() { if (bCapabilities_) { @@ -466,4 +475,34 @@ HWTEST_F(ServiceStubTest, SUB_backup_sa_ServiceStub_Finish_0100, testing::ext::T } GTEST_LOG_(INFO) << "ServiceStubTest-end SUB_backup_sa_ServiceStub_Finish_0100"; } + +/** + * @tc.number: SUB_backup_sa_ServiceStub_Release_0100 + * @tc.name: SUB_backup_sa_ServiceStub_Release_0100 + * @tc.desc: Test function of Release interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I6URNZ + */ +HWTEST_F(ServiceStubTest, SUB_backup_sa_ServiceStub_Release_0100, testing::ext::TestSize.Level0) +{ + GTEST_LOG_(INFO) << "ServiceStubTest-begin SUB_backup_sa_ServiceStub_Release_0100"; + try { + MockService service; + EXPECT_CALL(service, Release()).WillOnce(Return(BError(BError::Codes::OK))); + MessageParcel data; + MessageParcel reply; + MessageOption option; + + EXPECT_TRUE(data.WriteInterfaceToken(IService::GetDescriptor())); + EXPECT_EQ(BError(BError::Codes::OK), + service.OnRemoteRequest(static_cast(IServiceInterfaceCode::SERVICE_CMD_RELSEASE_SESSION), + data, reply, option)); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ServiceStubTest-an exception occurred by Release."; + } + GTEST_LOG_(INFO) << "ServiceStubTest-end SUB_backup_sa_ServiceStub_Release_0100"; +} } // namespace OHOS::FileManagement::Backup \ No newline at end of file diff --git a/tools/backup_tool/src/tools_op_backup.cpp b/tools/backup_tool/src/tools_op_backup.cpp index d79b1ba62..e9c1e97e6 100644 --- a/tools/backup_tool/src/tools_op_backup.cpp +++ b/tools/backup_tool/src/tools_op_backup.cpp @@ -258,6 +258,7 @@ static int32_t InitPathCapFile(const string &pathCapFile, vector bundleN ctx->SetBundleFinishedCount(bundleNames.size()); ctx->Wait(); FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + ctx->session_->Release(); return 0; } diff --git a/tools/backup_tool/src/tools_op_restore.cpp b/tools/backup_tool/src/tools_op_restore.cpp index 333099dbd..9f21842b5 100644 --- a/tools/backup_tool/src/tools_op_restore.cpp +++ b/tools/backup_tool/src/tools_op_restore.cpp @@ -160,8 +160,8 @@ static void OnBundleFinished(shared_ptr ctx, ErrCode err, const BundleN ctx->UpdateBundleFinishedCount(); if (err != 0) { ctx->isAllBundelsFinished.store(true); - ctx->ClearBundleOfMap(name); } + ctx->ClearBundleOfMap(name); ctx->TryNotify(); } @@ -286,6 +286,7 @@ static int32_t InitPathCapFile(const string &pathCapFile, vector bundleN ctx->SetBundleFinishedCount(bundleNames.size()); RestoreApp(ctx, bundleNames); ctx->Wait(); + ctx->session_->Release(); return 0; } diff --git a/utils/include/b_json/b_report_entity.h b/utils/include/b_json/b_report_entity.h new file mode 100644 index 000000000..83fad2433 --- /dev/null +++ b/utils/include/b_json/b_report_entity.h @@ -0,0 +1,193 @@ +/* + * Copyright (c) 2022-2023 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_B_REPORT_ENTITY_H +#define OHOS_FILEMGMT_BACKUP_B_REPORT_ENTITY_H + +#include +#include + +namespace OHOS::FileManagement::Backup { +struct ReportFileInfo { + string filePath {""}; + mode_t mode {0}; + bool isDir {false}; + off_t size {0}; + off_t mtime {0}; + string hash {""}; +}; +enum ReportFileInfoAttr {FILE_PATH, MODE, IS_DIR, SIZE, MTIME, HASH}; +class BReportEntity { +public: + ErrCode ParseReportFileInfo(ReportFileInfo &info, const std::vector &attrs) + { + try { + if (attrs[FILE_PATH].empty()) { + HILOGE("Invalid filePath:%{public}s", attrs[FILE_PATH].c_str()); + return EPERM; + } + info.filePath = attrs[FILE_PATH]; + + if (!attrs[MODE].empty()) { + info.mode = static_cast(atoi(attrs[MODE].c_str())); + } + + if (attrs[IS_DIR].empty()) { + HILOGE("Invalid isDir:%{public}s", attrs[IS_DIR].c_str()); + return EPERM; + } + info.isDir = atoi(attrs[IS_DIR].c_str()) == 1 ? true : false; + + if (!attrs[SIZE].empty()) { + info.size = static_cast(atol(attrs[SIZE].c_str())); + } + + if (!attrs[MTIME].empty()) { + info.mtime = static_cast(atol(attrs[MTIME].c_str())); + } + + info.hash = attrs[HASH]; + + return ERR_OK; + } catch (...) { + HILOGE("Failed to ParseReportFileInfo"); + return EPERM; + } + } + + std::vector SplitStringByChar(const std::string &str, const char &sep) + { + std::vector eles = {}; + std::stringstream ss = std::stringstream(str); + std::string ele = {}; + while (!ss.eof()) { + getline(ss, ele, sep); + eles.push_back(ele); + } + + return eles; + } + + ErrCode ParseInfoLine(const std::string &line) + { + try { + std::stringstream lineStream = std::stringstream(line); + std::stringstream infoStream; + std::string info = {}; + std::string key = {}; + std::vector keys = {}; + while (!lineStream.eof()) { + getline(lineStream, info, infoSep_); + keys.clear(); + keys = SplitStringByChar(info, infoAlign_); + if (keys.size() != INFO_ALIGN_NUM_) { + HILOGE("Failed to Parse info:%{public}s", info.c_str()); + continue; + } + if (keys[0] == VERSION_TAG_) { + version = keys[1]; + } else if (keys[0] == ATTR_NUM_TAG_) { + attrNum = atoi(keys[1].c_str()); + } else { + HILOGE("invalid info:%{public}s", info.c_str()); + } + } + + return ERR_OK; + } catch (...) { + HILOGE("Failed to ParseInfoLine"); + return EPERM; + } + } + + /** + * @brief 获取Report信息 + * + * @return std::vector + */ + std::vector GetReportFileInfos() + { + std::vector infos {}; + + std::unique_ptr rawBuf = BFile::ReadFile(srcFile_); + std::stringstream fileStream(rawBuf.get()); + std::string line; + std::stringstream lineStream; + std::string attr = {}; + std::vector attrs; + bool infoLine = false; + bool attrLine = false; + while (!fileStream.eof()) { + getline(fileStream, line, lineSep_); + + if (!line.empty()) { + if (!infoLine) { + infoLine = true; + ParseInfoLine(line); + continue; + } + if (!attrLine) { + attrLine = true; + continue; + } + + attrs.clear(); + attrs = SplitStringByChar(line, attrSep_); + if (attrs.size() != attrNum) { + HILOGE("Invalid line:%{public}s", line.c_str()); + continue; + } + ReportFileInfo info = {}; + auto code = ParseReportFileInfo(info, attrs); + if (code != ERR_OK) { + HILOGE("ParseReportFileInfo err:%{public}d %{public}s", code, line.c_str()); + continue; + } else { + infos.emplace_back(info); + } + } + } + + return infos; + } + +public: + /** + * @brief 构造方法 + * + * @param fd + */ + explicit BReportEntity(UniqueFd fd) : srcFile_(std::move(fd)) {} + + BReportEntity() = delete; + virtual ~BReportEntity() = default; + +public: + string version = ""; + unsigned int attrNum = 0; + +protected: + UniqueFd srcFile_; + const char lineSep_ = '\n'; + const char attrSep_ = ';'; + const char infoSep_ = '&'; + const char infoAlign_ = '='; + const unsigned int INFO_ALIGN_NUM_ = 2; + const string VERSION_TAG_ = "version"; + const string ATTR_NUM_TAG_ = "attrNum"; +}; +} // namespace OHOS::FileManagement::Backup + +#endif // OHOS_FILEMGMT_BACKUP_B_REPORT_ENTITY_H \ No newline at end of file diff --git a/utils/include/b_ohos/startup/backup_para.h b/utils/include/b_ohos/startup/backup_para.h index 1f6cad364..bac2a4300 100644 --- a/utils/include/b_ohos/startup/backup_para.h +++ b/utils/include/b_ohos/startup/backup_para.h @@ -35,6 +35,20 @@ public: * @return int32_t值为配置项backup.debug.overrideAccountNumber值 */ std::tuple GetBackupDebugOverrideAccount(); + + /** + * @brief 获取backup.para配置项backup.overrideBackupSARelease的值 + * + * @return 获取的配置项backup.overrideBackupSARelease的值为true时则返回true,否则返回false + */ + bool GetBackupOverrideBackupSARelease(); + + /** + * @brief 获取backup.para配置项backup.overrideIncrementalRestore的值 + * + * @return 获取的配置项backup.overrideIncrementalRestore的值为true时则返回true,否则返回false + */ + bool GetBackupOverrideIncrementalRestore(); }; } // namespace OHOS::FileManagement::Backup diff --git a/utils/include/b_resources/b_constants.h b/utils/include/b_resources/b_constants.h index 9f4ae877b..9059ca1ab 100644 --- a/utils/include/b_resources/b_constants.h +++ b/utils/include/b_resources/b_constants.h @@ -62,6 +62,7 @@ constexpr int EXT_CONNECT_MAX_COUNT = 3; // extension 最大启动数 constexpr int EXT_CONNECT_MAX_TIME = 15000; // SA 启动 extension 等待连接最大时间 constexpr int IPC_MAX_WAIT_TIME = 3000; // IPC通讯最大等待时间(s) +constexpr int MAX_PARCELABLE_VECTOR_NUM = 10000; // backup.para内配置项的名称,该配置项值为true时可在不更新hap包的情况下,可以读取包管理元数据配置文件的内容 static inline std::string BACKUP_DEBUG_OVERRIDE_EXTENSION_CONFIG_KEY = "backup.debug.overrideExtensionConfig"; @@ -70,6 +71,12 @@ static inline std::string BACKUP_DEBUG_OVERRIDE_EXTENSION_CONFIG_KEY = "backup.d static inline std::string BACKUP_DEBUG_OVERRIDE_ACCOUNT_CONFIG_KEY = "backup.debug.overrideAccountConfig"; static inline std::string BACKUP_DEBUG_OVERRIDE_ACCOUNT_NUMBER_KEY = "backup.debug.overrideAccountNumber"; +// backup.para内配置项的名称,该配置项为true时备份恢复支持Release接口调用 +static inline std::string BACKUP_OVERRIDE_BACKUP_SA_RELEASE_KEY = "backup.overrideBackupSARelease"; + +// backup.para内配置项的名称,该配置项为true时备份恢复支持增量恢复 +static inline std::string BACKUP_OVERRIDE_INCREMENTAL_KEY = "backup.overrideIncrementalRestore"; + // 应用备份数据暂存路径 static inline std::string_view SA_BUNDLE_BACKUP_BACKUP = "/backup/"; static inline std::string_view SA_BUNDLE_BACKUP_RESTORE = "/restore/"; @@ -116,6 +123,9 @@ static inline std::string_view EXT_BACKUP_MANAGE = "manage.json"; // 包管理元数据配置文件 static inline std::string_view BACKUP_CONFIG_JSON = "backup_config.json"; +// 简报文件名后缀 +static inline std::string_view REPORT_FILE_EXT = "rp"; + // 特殊版本信息 constexpr int DEFAULT_VERSION_CODE = 0; static inline std::string_view DEFAULT_VERSION_NAME = "0.0.0.0"; diff --git a/utils/src/b_ohos/startup/backup_para.cpp b/utils/src/b_ohos/startup/backup_para.cpp index 8047ef0e1..c3028632a 100644 --- a/utils/src/b_ohos/startup/backup_para.cpp +++ b/utils/src/b_ohos/startup/backup_para.cpp @@ -66,6 +66,26 @@ bool BackupPara::GetBackupDebugOverrideExtensionConfig() return value == "true"; } +bool BackupPara::GetBackupOverrideBackupSARelease() +{ + auto [getCfgParaValSucc, value] = + GetConfigParameterValue(BConstants::BACKUP_OVERRIDE_BACKUP_SA_RELEASE_KEY, BConstants::BACKUP_PARA_VALUE_MAX); + if (!getCfgParaValSucc) { + throw BError(BError::Codes::SA_INVAL_ARG, "Fail to get configuration parameter value of backup.para"); + } + return value == "true"; +} + +bool BackupPara::GetBackupOverrideIncrementalRestore() +{ + auto [getCfgParaValSucc, value] = + GetConfigParameterValue(BConstants::BACKUP_OVERRIDE_INCREMENTAL_KEY, BConstants::BACKUP_PARA_VALUE_MAX); + if (!getCfgParaValSucc) { + throw BError(BError::Codes::SA_INVAL_ARG, "Fail to get configuration parameter value of backup.para"); + } + return value == "true"; +} + tuple BackupPara::GetBackupDebugOverrideAccount() { auto [getCfgParaValSucc, value] = GetConfigParameterValue(BConstants::BACKUP_DEBUG_OVERRIDE_ACCOUNT_CONFIG_KEY, -- Gitee