From 9db11a5ca72228b10e0e4893f539d8b24107ccff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=99=9F=E9=92=B0?= Date: Thu, 2 Jan 2025 21:08:39 +0800 Subject: [PATCH] =?UTF-8?q?000=E6=9D=83=E9=99=90=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E4=B8=8D=E5=A4=87=E4=BB=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 张晟钰 --- .../native/backup_ext/include/ext_extension.h | 5 ++- .../native/backup_ext/include/tar_file.h | 3 ++ .../native/backup_ext/src/ext_extension.cpp | 28 +++++++++++- frameworks/native/backup_ext/src/tar_file.cpp | 43 ++++++++++++++----- .../backup_sa/include/module_ipc/service.h | 6 ++- .../src/module_external/bms_adapter.cpp | 1 + services/backup_sa/src/module_ipc/service.cpp | 4 +- .../src/module_ipc/service_incremental.cpp | 26 +++++++---- .../backup_sa/src/module_ipc/sub_service.cpp | 24 ++++++++++- tests/mock/module_ipc/service_mock.cpp | 5 ++- 10 files changed, 117 insertions(+), 28 deletions(-) diff --git a/frameworks/native/backup_ext/include/ext_extension.h b/frameworks/native/backup_ext/include/ext_extension.h index c8bd321cd..12d08e820 100644 --- a/frameworks/native/backup_ext/include/ext_extension.h +++ b/frameworks/native/backup_ext/include/ext_extension.h @@ -187,9 +187,9 @@ private: void AsyncTaskDoIncrementalBackup(UniqueFd incrementalFd, UniqueFd manifestFd); void AsyncTaskOnIncrementalBackup(); int DoIncrementalBackupTask(UniqueFd incrementalFd, UniqueFd manifestFd); - ErrCode IncrementalBigFileReady(const TarMap &pkgInfo, const vector &bigInfos, + ErrCode IncrementalBigFileReady(TarMap &pkgInfo, const vector &bigInfos, sptr proxy); - ErrCode BigFileReady(const TarMap &bigFileInfo, sptr proxy); + ErrCode BigFileReady(TarMap &bigFileInfo, sptr proxy); void WaitToSendFd(std::chrono::system_clock::time_point &startTime, int &fdSendNum); void RefreshTimeInfo(std::chrono::system_clock::time_point &startTime, int &fdSendNum); void IncrementalPacket(const vector &infos, TarMap &tar, sptr proxy); @@ -289,6 +289,7 @@ private: ErrCode GetIncreFileHandleForNormalVersion(const std::string &fileName); void RestoreOneBigFile(const std::string &path, const ExtManageInfo &item, const bool appendTargetPath); int DealIncreRestoreBigAndTarFile(); + void ClearNoPermissionFiles(TarMap &pkgInfo, vector &noPermissionFiles); std::function ReportOnProcessResultCallback(wptr obj, BackupRestoreScenario scenario); diff --git a/frameworks/native/backup_ext/include/tar_file.h b/frameworks/native/backup_ext/include/tar_file.h index 2d19e241e..27c798966 100644 --- a/frameworks/native/backup_ext/include/tar_file.h +++ b/frameworks/native/backup_ext/include/tar_file.h @@ -56,6 +56,7 @@ const char AREGTYPE = '\0'; // regular file const char SYMTYPE = '2'; // reserved const char DIRTYPE = '5'; // directory const char GNUTYPE_LONGNAME = 'L'; +const int ERR_NO_PERMISSION = 13; } // namespace // 512 bytes @@ -215,6 +216,8 @@ private: */ bool I2OcsConvert(const struct stat &st, TarHeader &hdr, std::string &fileName); + bool ToAddFile(std::string &path, int &err); + private: uint32_t fileCount_ {0}; TarMap tarMap_ {}; diff --git a/frameworks/native/backup_ext/src/ext_extension.cpp b/frameworks/native/backup_ext/src/ext_extension.cpp index c3dbed240..3a4f797e8 100644 --- a/frameworks/native/backup_ext/src/ext_extension.cpp +++ b/frameworks/native/backup_ext/src/ext_extension.cpp @@ -467,13 +467,27 @@ static ErrCode IndexFileReady(const TarMap &pkgInfo, sptr proxy) return ret; } -ErrCode BackupExtExtension::BigFileReady(const TarMap &bigFileInfo, sptr proxy) +void BackupExtExtension::ClearNoPermissionFiles(TarMap &pkgInfo, vector &noPermissionFiles) +{ + HILOGI("start ClearNoPermissionFiles;"); + for (const auto &item : noPermissionFiles) { + auto it = pkgInfo.find(item); + if (it != pkgInfo.end()) { + HILOGI("noPermissionFile, don't need to backup, path = %{public}s", + GetAnonyString(std::get<0>(it->second)).c_str()); + pkgInfo.erase(it); + } + } +} + +ErrCode BackupExtExtension::BigFileReady(TarMap &bigFileInfo, sptr proxy) { HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); HILOGI("BigFileReady Begin: bigFileInfo file size is: %{public}zu", bigFileInfo.size()); ErrCode ret {ERR_OK}; auto startTime = std::chrono::system_clock::now(); int fdNum = 0; + vector noPermissionFiles; for (auto &item : bigFileInfo) { WaitToSendFd(startTime, fdNum); int32_t errCode = ERR_OK; @@ -482,8 +496,11 @@ ErrCode BackupExtExtension::BigFileReady(const TarMap &bigFileInfo, sptrAppFileReady(item.first, std::move(fd), errCode); if (SUCCEEDED(ret)) { HILOGI("The application is packaged successfully, package name is %{public}s", item.first.c_str()); @@ -493,6 +510,7 @@ ErrCode BackupExtExtension::BigFileReady(const TarMap &bigFileInfo, sptr noPermissionFiles; for (auto &item : pkgInfo) { if (item.first.empty()) { continue; @@ -1939,6 +1958,10 @@ ErrCode BackupExtExtension::IncrementalBigFileReady(const TarMap &pkgInfo, HILOGE("IncrementalBigFileReady open file failed, file name is %{public}s, err = %{public}d", path.c_str(), errno); errCode = errno; + if (errCode == ERR_NO_PERMISSION) { + noPermissionFiles.emplace_back(item.first.c_str()); + continue; + } } vector bigInfo; for (const auto &tempFile : bigInfos) { @@ -1960,6 +1983,7 @@ ErrCode BackupExtExtension::IncrementalBigFileReady(const TarMap &pkgInfo, fdNum += BConstants::FILE_AND_MANIFEST_FD_COUNT; RefreshTimeInfo(startTime, fdNum); } + ClearNoPermissionFiles(pkgInfo, noPermissionFiles); HILOGI("IncrementalBigFileReady End"); return ret; } diff --git a/frameworks/native/backup_ext/src/tar_file.cpp b/frameworks/native/backup_ext/src/tar_file.cpp index 6f456bf84..cb149c28d 100644 --- a/frameworks/native/backup_ext/src/tar_file.cpp +++ b/frameworks/native/backup_ext/src/tar_file.cpp @@ -100,30 +100,53 @@ bool TarFile::Packet(const vector &srcFiles, const string &tarFileName, return true; } -bool TarFile::TraversalFile(string &filePath, int &err) +bool TarFile::ToAddFile(std::string &path, int &err) { - if (access(filePath.c_str(), F_OK) != 0) { - err = errno; - HILOGE("File path does not exists, err = %{public}d", errno); - return false; - } - struct stat curFileStat {}; auto ret = memset_s(&curFileStat, sizeof(curFileStat), 0, sizeof(curFileStat)); if (ret != EOK) { HILOGE("Failed to call memset_s, err = %{public}d", ret); return false; } - if (lstat(filePath.c_str(), &curFileStat) != 0) { + if (lstat(path.c_str(), &curFileStat) != 0) { err = errno; HILOGE("Failed to lstat, err = %{public}d", errno); + AuditLog auditLog = {false, "lstat file failed", "ADD", "", 1, "FAILED", "TraversalFile", + "Packet File", GetAnonyPath(path)}; + HiAudit::GetInstance(false).Write(auditLog); return false; } - if (!AddFile(filePath, curFileStat, err)) { - HILOGE("Failed to add file to tar package, file path is:%{public}s", GetAnonyPath(filePath).c_str()); + if (!AddFile(path, curFileStat, err)) { + HILOGE("Failed to add file to tar package, file path is:%{public}s", GetAnonyPath(path).c_str()); + AuditLog auditLog = {false, "AddFile failed", "ADD", "", 1, "FAILED", "TraversalFile", + "Packet File", GetAnonyPath(path)}; + HiAudit::GetInstance(false).Write(auditLog); return false; } + return true; +} +bool TarFile::TraversalFile(string &filePath, int &err) +{ + if (access(filePath.c_str(), F_OK) != 0) { + err = errno; + HILOGE("File path does not exists, err = %{public}d", errno); + AuditLog auditLog = {false, "access file failed", "ADD", "", 1, "FAILED", "TraversalFile", + "Packet File", GetAnonyPath(filePath)}; + HiAudit::GetInstance(false).Write(auditLog); + return false; + } + int fd = open(filePath.c_str(), O_RDONLY); + if (fd < 0 && errno == ERR_NO_PERMISSION) { + HILOGI("noPermissionFlie, don't need to backup, path = %{public}s, err = %{public}d", + GetAnonyString(filePath).c_str(), errno); + return true; + } else if (fd > 0) { + close(fd); + } + if (!ToAddFile(filePath, err)) { + return false; + } if (isReset_) { return true; } diff --git a/services/backup_sa/include/module_ipc/service.h b/services/backup_sa/include/module_ipc/service.h index 1b9d438e8..a9f059b32 100644 --- a/services/backup_sa/include/module_ipc/service.h +++ b/services/backup_sa/include/module_ipc/service.h @@ -541,7 +541,11 @@ private: void ClearFailedBundles(); std::vector GetSupportBackupBundleNames(vector &bundleInfos, - bool isIncBackup); + bool isIncBackup, const vector &srcBundleNames); + void HandleNotSupportBundleNames(const std::vector &srcBundleNames, + std::vector &supportBundleNames, bool isIncBackup); + void SetBundleIncDataInfo(const std::vector &bundlesToBackup, + std::vector &supportBundleNames); private: static sptr instance_; static std::mutex instanceLock_; diff --git a/services/backup_sa/src/module_external/bms_adapter.cpp b/services/backup_sa/src/module_external/bms_adapter.cpp index 7b79c511f..0738d24b5 100644 --- a/services/backup_sa/src/module_external/bms_adapter.cpp +++ b/services/backup_sa/src/module_external/bms_adapter.cpp @@ -308,6 +308,7 @@ vector BundleMgrAdapter::GetBundleInfosForIncrement } if (!CreateIPCInteractionFiles(userId, bundleName, bundleNameTime.lastIncrementalTime, backupPara.includes, backupPara.excludes)) { + HILOGE("Create bundleInteraction dir failed, bundleName:%{public}s", bundleName.c_str()); continue; } bundleInfos.emplace_back(BJsonEntityCaps::BundleInfo {installedBundle.name, installedBundle.appIndex, diff --git a/services/backup_sa/src/module_ipc/service.cpp b/services/backup_sa/src/module_ipc/service.cpp index d68d6a415..7f9409847 100644 --- a/services/backup_sa/src/module_ipc/service.cpp +++ b/services/backup_sa/src/module_ipc/service.cpp @@ -788,7 +788,7 @@ ErrCode Service::AppendBundlesBackupSession(const vector &bundleName VerifyCaller(IServiceReverse::Scenario::BACKUP); auto bundleDetails = MakeDetailList(bundleNames); auto backupInfos = BundleMgrAdapter::GetBundleInfosForAppend(bundleDetails, session_->GetSessionUserId()); - std::vector supportBackupNames = GetSupportBackupBundleNames(backupInfos, false); + std::vector supportBackupNames = GetSupportBackupBundleNames(backupInfos, false, bundleNames); session_->AppendBundles(supportBackupNames); SetCurrentBackupSessProperties(supportBackupNames, session_->GetSessionUserId(), backupInfos, false); OnStartSched(); @@ -830,7 +830,7 @@ ErrCode Service::AppendBundlesDetailsBackupSession(const vector &bun session_->GetSessionUserId(), isClearDataFlags); auto bundleDetails = MakeDetailList(bundleNames); auto backupInfos = BundleMgrAdapter::GetBundleInfosForAppend(bundleDetails, session_->GetSessionUserId()); - std::vector supportBackupNames = GetSupportBackupBundleNames(backupInfos, false); + std::vector supportBackupNames = GetSupportBackupBundleNames(backupInfos, false, bundleNames); session_->AppendBundles(supportBackupNames); HandleCurGroupBackupInfos(backupInfos, bundleNameDetailMap, isClearDataFlags); OnStartSched(); diff --git a/services/backup_sa/src/module_ipc/service_incremental.cpp b/services/backup_sa/src/module_ipc/service_incremental.cpp index b948183b4..d3ddc141c 100644 --- a/services/backup_sa/src/module_ipc/service_incremental.cpp +++ b/services/backup_sa/src/module_ipc/service_incremental.cpp @@ -322,11 +322,9 @@ ErrCode Service::AppendBundlesIncrementalBackupSession(const std::vector bundleNames = GetBundleNameByDetails(bundlesToBackup); auto backupInfos = BundleMgrAdapter::GetBundleInfosForAppend(bundlesToBackup, session_->GetSessionUserId()); - std::vector supportBackupNames = GetSupportBackupBundleNames(backupInfos, true); + std::vector supportBackupNames = GetSupportBackupBundleNames(backupInfos, true, bundleNames); session_->AppendBundles(supportBackupNames); - for (auto &bundleInfo : bundlesToBackup) { - session_->SetIncrementalData(bundleInfo); - } + SetBundleIncDataInfo(bundlesToBackup, supportBackupNames); SetCurrentBackupSessProperties(supportBackupNames, session_->GetSessionUserId(), backupInfos, true); OnStartSched(); session_->DecreaseSessionCnt(__PRETTY_FUNCTION__); @@ -364,11 +362,9 @@ ErrCode Service::AppendBundlesIncrementalBackupSession(const std::vectorGetSessionUserId(), isClearDataFlags); auto backupInfos = BundleMgrAdapter::GetBundleInfosForAppend(bundlesToBackup, session_->GetSessionUserId()); - std::vector supportBackupNames = GetSupportBackupBundleNames(backupInfos, true); - for (auto &bundleInfo : bundlesToBackup) { - session_->SetIncrementalData(bundleInfo); - } + std::vector supportBackupNames = GetSupportBackupBundleNames(backupInfos, true, bundleNames); session_->AppendBundles(supportBackupNames); + SetBundleIncDataInfo(bundlesToBackup, supportBackupNames); HandleCurGroupIncBackupInfos(backupInfos, bundleNameDetailMap, isClearDataFlags); OnStartSched(); session_->DecreaseSessionCnt(__PRETTY_FUNCTION__); @@ -717,4 +713,18 @@ void Service::SetCurrentBackupSessProperties(const vector &bundleNames, } HILOGI("end SetCurrentBackupSessProperties"); } + +void Service::SetBundleIncDataInfo(const std::vector& bundlesToBackup, + std::vector& supportBundleNames) +{ + for (auto &bundleInfo : bundlesToBackup) { + std::string bundleName = bundleInfo.bundleName; + auto it = std::find(supportBundleNames.begin(), supportBundleNames.end(), bundleName); + if (it == supportBundleNames.end()) { + HILOGE("Current bundle is not support to backup, bundleName:%{public}s", bundleName.c_str()); + continue; + } + session_->SetIncrementalData(bundleInfo); + } +} } // namespace OHOS::FileManagement::Backup diff --git a/services/backup_sa/src/module_ipc/sub_service.cpp b/services/backup_sa/src/module_ipc/sub_service.cpp index 96c10ca86..c33c232ae 100644 --- a/services/backup_sa/src/module_ipc/sub_service.cpp +++ b/services/backup_sa/src/module_ipc/sub_service.cpp @@ -289,7 +289,7 @@ void Service::SetWant(AAFwk::Want &want, const BundleName &bundleName, const BCo } std::vector Service::GetSupportBackupBundleNames(vector &backupInfos, - bool isIncBackup) + bool isIncBackup, const std::vector &srcBundleNames) { HILOGI("Begin"); std::vector supportBackupNames; @@ -311,6 +311,7 @@ std::vector Service::GetSupportBackupBundleNames(vector &srcBundleNames, + std::vector &supportBundleNames, bool isIncBackup) +{ + for (auto bundleName : srcBundleNames) { + auto it = std::find(supportBundleNames.begin(), supportBundleNames.end(), bundleName); + if (it != supportBundleNames.end()) { + continue; + } + HILOGE("bundleName:%{public}s, can not find from supportBundleNames", bundleName.c_str()); + if (isIncBackup) { + session_->GetServiceReverseProxy()->IncrementalBackupOnBundleStarted( + BError(BError::Codes::SA_BUNDLE_INFO_EMPTY), bundleName); + } else { + session_->GetServiceReverseProxy()->BackupOnBundleStarted( + BError(BError::Codes::SA_BUNDLE_INFO_EMPTY), bundleName); + } + } +} +} diff --git a/tests/mock/module_ipc/service_mock.cpp b/tests/mock/module_ipc/service_mock.cpp index a03e7285e..5e8a7d953 100644 --- a/tests/mock/module_ipc/service_mock.cpp +++ b/tests/mock/module_ipc/service_mock.cpp @@ -290,8 +290,11 @@ void Service::UpdateFailedBundles(const std::string &bundleName, BundleTaskInfo void Service::ClearFailedBundles() {} void Service::StartRunningTimer(const std::string &bundleName) {} -std::vector Service::GetSupportBackupBundleNames(vector&, bool) +std::vector Service::GetSupportBackupBundleNames(vector&, bool, + const vector&) { return {}; } +void Service::HandleNotSupportBundleNames(const vector&, vector&, bool) {} +void Service::SetBundleIncDataInfo(const std::vector&, std::vector&) {} } // namespace OHOS::FileManagement::Backup -- Gitee