From b08e7e1aadf5bc7e0908ba0626685e50d3865842 Mon Sep 17 00:00:00 2001 From: "yaoruozi1@huawei.com" Date: Fri, 25 Apr 2025 10:19:23 +0800 Subject: [PATCH] add dependency Signed-off-by: yaoruozi1@huawei.com --- .../native/backup_ext/include/ext_extension.h | 6 ++ .../native/backup_ext/src/ext_extension.cpp | 85 ++++++++++--------- .../backup_ext/src/sub_ext_extension.cpp | 31 +++++++ .../backup_sa/src/module_ipc/sub_service.cpp | 3 +- .../backup_ext/ext_extension_test.cpp | 49 ----------- .../b_filesystem/b_file_hash_test.cpp | 22 +++++ utils/include/b_filesystem/b_file_hash.h | 1 + utils/include/b_resources/b_constants.h | 1 + utils/src/b_filesystem/b_file_hash.cpp | 22 +++++ 9 files changed, 128 insertions(+), 92 deletions(-) diff --git a/frameworks/native/backup_ext/include/ext_extension.h b/frameworks/native/backup_ext/include/ext_extension.h index 0f4960881..2c86cb770 100644 --- a/frameworks/native/backup_ext/include/ext_extension.h +++ b/frameworks/native/backup_ext/include/ext_extension.h @@ -340,6 +340,9 @@ private: ErrCode CloudSpecialRestore(std::string tarName, std::string untarPath, off_t tarFileSize); void GetTarIncludes(const string &tarName, unordered_map &infos); void DeleteIndexAndRpFile(); + tuple GetIncreFileHandleForSpecialVersion(const string &fileName); + void RmBigFileReportForSpecialCloneCloud(const std::string &srcFile); + string GetReportFileName(const string &fileName); private: std::shared_mutex lock_; std::shared_ptr extension_; @@ -374,6 +377,9 @@ private: std::atomic isExecAppDone_ {false}; OHOS::ThreadPool reportOnProcessRetPool_; + std::mutex reportHashLock_; + std::map reportHashSrcPathMap_; + BackupRestoreScenario curScenario_ { BackupRestoreScenario::FULL_BACKUP }; }; } // 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 0fa51bd06..af2fcf728 100644 --- a/frameworks/native/backup_ext/src/ext_extension.cpp +++ b/frameworks/native/backup_ext/src/ext_extension.cpp @@ -17,6 +17,7 @@ #include #include +#include #include #include #include @@ -205,7 +206,6 @@ static bool CheckAndCreateDirectory(const string &filePath) static UniqueFd GetFileHandleForSpecialCloneCloud(const string &fileName) { HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); - HILOGI("GetFileHandleForSpecialCloneCloud: fileName is %{public}s", GetAnonyPath(fileName).c_str()); string filePath = fileName; if (fileName.front() != BConstants::FILE_SEPARATOR_CHAR) { filePath = BConstants::FILE_SEPARATOR_CHAR + fileName; @@ -213,29 +213,20 @@ static UniqueFd GetFileHandleForSpecialCloneCloud(const string &fileName) size_t filePathPrefix = filePath.find_last_of(BConstants::FILE_SEPARATOR_CHAR); if (filePathPrefix == string::npos) { HILOGE("GetFileHandleForSpecialCloneCloud: Invalid fileName"); - AuditLog auditLog = {false, "Open fd failed", "ADD", "", 1, "FAILED", - "GetFileHandleForSpecialCloneCloud", "CommonFile", GetAnonyPath(filePath)}; - HiAudit::GetInstance(false).Write(auditLog); - return UniqueFd(-1); + return UniqueFd(BConstants::INVALID_FD_NUM); } string path = filePath.substr(0, filePathPrefix); if (access(path.c_str(), F_OK) != 0) { bool created = ForceCreateDirectory(path.data()); if (!created) { HILOGE("Failed to create restore folder."); - AuditLog auditLog = {false, "ForceCreateDirectory failed", "ADD", "", 1, - "FAILED", "GetFileHandleForSpecialCloneCloud", "CommonFile", GetAnonyPath(path)}; - HiAudit::GetInstance(false).Write(auditLog); - return UniqueFd(-1); + return UniqueFd(BConstants::INVALID_FD_NUM); } } UniqueFd fd(open(fileName.data(), O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR)); if (fd < 0) { - HILOGE("Open file failed, file name is %{private}s, err = %{public}d", fileName.data(), errno); - AuditLog auditLog = {false, "open fd failed", "ADD", "", 1, "FAILED", - "GetFileHandleForSpecialCloneCloud", "CommonFile", GetAnonyPath(fileName)}; - HiAudit::GetInstance(false).Write(auditLog); - return UniqueFd(-1); + HILOGE("Open file failed, file name is %{public}s, err = %{public}d", GetAnonyPath(fileName).c_str(), errno); + return UniqueFd(BConstants::INVALID_FD_NUM); } return fd; } @@ -287,36 +278,54 @@ UniqueFd BackupExtExtension::GetFileHandle(const string &fileName, int32_t &errC } } -static string GetReportFileName(const string &fileName) +string BackupExtExtension::GetReportFileName(const string &fileName) { string reportName = fileName + "." + string(BConstants::REPORT_FILE_EXT); return reportName; } -static tuple GetIncreFileHandleForSpecialVersion(const string &fileName) +tuple BackupExtExtension::GetIncreFileHandleForSpecialVersion(const string &fileName) { ErrCode errCode = ERR_OK; + HILOGI("Begin GetFileHandleForSpecialCloneCloud: fileName is %{public}s", GetAnonyPath(fileName).c_str()); UniqueFd fd = GetFileHandleForSpecialCloneCloud(fileName); if (fd < 0) { - HILOGE("Failed to open file = %{private}s, err = %{public}d", fileName.c_str(), errno); - AuditLog auditLog = {false, "Open fd failed", "ADD", "", 1, "FAILED", - "GetIncreFileHandleForSpecialVersion", "CommonFile", GetAnonyPath(fileName)}; - HiAudit::GetInstance(false).Write(auditLog); + HILOGE("Failed to open file = %{public}s, err = %{public}d", GetAnonyPath(fileName).c_str(), errno); errCode = errno; } string path = string(BConstants::PATH_BUNDLE_BACKUP_HOME).append(BConstants::SA_BUNDLE_BACKUP_RESTORE); if (mkdir(path.data(), S_IRWXU) && errno != EEXIST) { - HILOGE("Failed to create restore folder : %{private}s, err = %{public}d", path.c_str(), errno); + HILOGE("Failed to create restore folder : %{public}s, err = %{public}d", path.c_str(), errno); errCode = errno; - AuditLog auditLog = {false, "mkdir failed", "ADD", "", 1, "FAILED", - "GetIncreFileHandleForSpecialVersion", "CommonFile", GetAnonyPath(path)}; - HiAudit::GetInstance(false).Write(auditLog); } - string reportName = GetReportFileName(fileName); - UniqueFd reportFd(open(reportName.data(), O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR)); + size_t reportParentPathPos = fileName.find_last_of(BConstants::FILE_SEPARATOR_CHAR); + if (reportParentPathPos == std::string::npos) { + HILOGE("Get current file parent path error"); + UniqueFd invalidFd(BConstants::INVALID_FD_NUM); + return { errCode, move(fd), move(invalidFd) }; + } + string reportFullFileName = GetReportFileName(fileName); + string reportFileName = reportFullFileName.substr(reportParentPathPos + 1); + if (reportFileName.size() > BConstants::LONG_FILE_NAME_BOUNDARY_VAL) { + string reportHashName = BackupFileHash::HashFilePath(reportFullFileName); + HILOGI("FileName is too long, report HashName is:%{public}s", reportHashName.c_str()); + std::string reportParentPath = fileName.substr(0, reportParentPathPos); + std::string reportFullHashName = reportParentPath + BConstants::FILE_SEPARATOR_CHAR + reportHashName; + UniqueFd reportHashFd(open(reportFullHashName.data(), O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR)); + if (reportHashFd < 0) { + HILOGE("Failed open report file = %{public}s, err = %{public}d", + GetAnonyPath(reportFullHashName).c_str(), errno); + errCode = errno; + } + std::unique_lock lock(reportHashLock_); + reportHashSrcPathMap_.emplace(fileName, reportFullHashName); + return { errCode, move(fd), move(reportHashFd) }; + } + UniqueFd reportFd(open(reportFullFileName.data(), O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR)); if (reportFd < 0) { - HILOGE("Failed to open report file = %{private}s, err = %{public}d", reportName.c_str(), errno); + HILOGE("Failed to open report file = %{public}s, err = %{public}d", + GetAnonyPath(reportFullFileName).c_str(), errno); errCode = errno; } return {errCode, move(fd), move(reportFd)}; @@ -349,8 +358,8 @@ tuple BackupExtExtension::GetIncreFileHandleForNorm HILOGI("extension: GetIncrementalFileHandle single to single Name:%{public}s", GetAnonyPath(fileName).c_str()); std::string tarName; int32_t errCode = ERR_OK; - UniqueFd fd(-1); - UniqueFd reportFd(-1); + UniqueFd fd(BConstants::INVALID_FD_NUM); + UniqueFd reportFd(BConstants::INVALID_FD_NUM); do { errCode = GetIncrementalFileHandlePath(fileName, bundleName_, tarName); if (errCode != ERR_OK) { @@ -365,19 +374,18 @@ tuple BackupExtExtension::GetIncreFileHandleForNorm if (fd < 0) { HILOGE("Failed to open tar file = %{public}s, err = %{public}d", GetAnonyPath(tarName).c_str(), errno); errCode = errno; - AuditLog auditLog = {false, "Open fd failed", "ADD", "", 1, "FAILED", - "GetIncreFileHandleForNormalVersion", "CommonFile", GetAnonyPath(tarName)}; - HiAudit::GetInstance(false).Write(auditLog); break; } // 对应的简报文件 string reportName = GetReportFileName(tarName); if (access(reportName.c_str(), F_OK) == 0) { - HILOGE("The report file already exists, Name = %{private}s, err =%{public}d", reportName.c_str(), errno); + HILOGE("The report file already exists, Name = %{public}s, err =%{public}d", + GetAnonyPath(reportName).c_str(), errno); } reportFd = UniqueFd(open(reportName.data(), O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR)); if (reportFd < 0) { - HILOGE("Failed to open report file = %{private}s, err = %{public}d", reportName.c_str(), errno); + HILOGE("Failed to open report file = %{public}s, err = %{public}d", + GetAnonyPath(reportName).c_str(), errno); errCode = errno; break; } @@ -449,9 +457,6 @@ static ErrCode IndexFileReady(const TarMap &pkgInfo, sptr proxy) UniqueFd fd(open(INDEX_FILE_BACKUP.data(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR)); if (fd < 0) { HILOGE("Failed to open index json file = %{private}s, err = %{public}d", INDEX_FILE_BACKUP.c_str(), errno); - AuditLog auditLog = {false, "Open fd failed", "ADD", "", 1, "FAILED", "Backup File", - "IndexFileReady", GetAnonyPath(INDEX_FILE_BACKUP)}; - HiAudit::GetInstance(false).Write(auditLog); return BError::GetCodeByErrno(errno); } BJsonCachedEntity cachedEntity(move(fd)); @@ -1089,11 +1094,7 @@ void BackupExtExtension::RestoreBigFilesForSpecialCloneCloud(const ExtManageInfo errFileInfos_[fileName].emplace_back(errno); HILOGE("Failed to change the file time. %{public}s , %{public}d", GetAnonyPath(fileName).c_str(), errno); } - // 删除大文件的rp文件 - string reportPath = GetReportFileName(fileName); - if (!RemoveFile(reportPath)) { - HILOGE("Failed to delete backup report %{public}s, err = %{public}d", GetAnonyPath(reportPath).c_str(), errno); - } + RmBigFileReportForSpecialCloneCloud(fileName); } ErrCode BackupExtExtension::RestoreTarForSpecialCloneCloud(const ExtManageInfo &item) diff --git a/frameworks/native/backup_ext/src/sub_ext_extension.cpp b/frameworks/native/backup_ext/src/sub_ext_extension.cpp index afcaa1a10..b55ff3809 100644 --- a/frameworks/native/backup_ext/src/sub_ext_extension.cpp +++ b/frameworks/native/backup_ext/src/sub_ext_extension.cpp @@ -17,6 +17,7 @@ #include #include +#include #include #include #include @@ -1387,4 +1388,34 @@ ErrCode BackupExtExtension::CloudSpecialRestore(string tarName, string untarPath DeleteBackupIncrementalTars(tarName); return err; } + +void BackupExtExtension::RmBigFileReportForSpecialCloneCloud(const std::string &srcFileName) +{ + // 删除大文件的rp文件 + string reportFileName = GetReportFileName(srcFileName); + std::unique_lock lock(reportHashLock_); + if (reportHashSrcPathMap_.empty()) { + if (!RemoveFile(reportFileName)) { + HILOGE("Failed to delete backup report %{public}s, err = %{public}d", + GetAnonyPath(reportFileName).c_str(), errno); + } + return; + } + auto iter = reportHashSrcPathMap_.find(srcFileName); + if (iter == reportHashSrcPathMap_.end()) { + if (!RemoveFile(reportFileName)) { + HILOGE("Failed to remove cuurent file report %{public}s, err = %{public}d", + GetAnonyPath(reportFileName).c_str(), errno); + } + return; + } + std::string reportHashFilePath = iter->second; + HILOGI("Will remove current reportHashFile, reportHashFilePath:%{public}s", + GetAnonyPath(reportHashFilePath).c_str()); + if (!RemoveFile(reportHashFilePath)) { + HILOGE("Failed to delete backup report %{public}s, err = %{public}d", + GetAnonyPath(reportHashFilePath).c_str(), errno); + } + reportHashSrcPathMap_.erase(iter); +} } // 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 b679afd82..6447557aa 100644 --- a/services/backup_sa/src/module_ipc/sub_service.cpp +++ b/services/backup_sa/src/module_ipc/sub_service.cpp @@ -16,8 +16,9 @@ #include "module_ipc/service.h" #include -#include #include +#include +#include #include #include #include diff --git a/tests/unittests/backup_ext/ext_extension_test.cpp b/tests/unittests/backup_ext/ext_extension_test.cpp index 204661ec8..07da8c215 100644 --- a/tests/unittests/backup_ext/ext_extension_test.cpp +++ b/tests/unittests/backup_ext/ext_extension_test.cpp @@ -116,29 +116,6 @@ HWTEST_F(ExtExtensionTest, Ext_Extension_Test_0101, testing::ext::TestSize.Level GTEST_LOG_(INFO) << "ExtExtensionTest-end Ext_Extension_Test_0101"; } -/** - * @tc.number: SUB_Ext_Extension_0200 - * @tc.name: Ext_Extension_Test_0200 - * @tc.desc: 测试成功 - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - * @tc.require: I9P3Y3 - */ -HWTEST_F(ExtExtensionTest, Ext_Extension_Test_0200, testing::ext::TestSize.Level1) -{ - GTEST_LOG_(INFO) << "ExtExtensionTest-begin Ext_Extension_Test_0200"; - try { - string fileName = "filename"; - string reportName = GetReportFileName(fileName); - EXPECT_EQ(reportName, "filename.rp"); - } catch (...) { - EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "ExtExtensionTest-an exception occurred by construction."; - } - GTEST_LOG_(INFO) << "ExtExtensionTest-end Ext_Extension_Test_0200"; -} - /** * @tc.number: SUB_Ext_Extension_0300 * @tc.name: Ext_Extension_Test_0300 @@ -566,30 +543,4 @@ HWTEST_F(ExtExtensionTest, Ext_Extension_Test_0900, testing::ext::TestSize.Level } GTEST_LOG_(INFO) << "ExtExtensionTest-end Ext_Extension_Test_0900"; } - -/** - * @tc.number: SUB_Ext_Extension_1000 - * @tc.name: Ext_Extension_Test_1000 - * @tc.desc: 测试 GetFileHandleForSpecialCloneCloud - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - * @tc.require: I9P3Y3 - */ -HWTEST_F(ExtExtensionTest, Ext_Extension_Test_1000, testing::ext::TestSize.Level1) -{ - GTEST_LOG_(INFO) << "ExtExtensionTest-begin Ext_Extension_Test_1000"; - try { - std::string fileName = "test.txt"; - auto [ret, fd, reportFd] = GetIncreFileHandleForSpecialVersion(fileName); - EXPECT_NE(ret, ERR_OK); - fileName = "/test.txt"; - tie(ret, fd, reportFd) = GetIncreFileHandleForSpecialVersion(fileName); - EXPECT_NE(ret, ERR_OK); - } catch (...) { - EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "ExtExtensionTest-an exception occurred by construction."; - } - GTEST_LOG_(INFO) << "ExtExtensionTest-end Ext_Extension_Test_1000"; -} } // namespace OHOS::FileManagement::Backup \ No newline at end of file diff --git a/tests/unittests/backup_utils/b_filesystem/b_file_hash_test.cpp b/tests/unittests/backup_utils/b_filesystem/b_file_hash_test.cpp index bedfa2e60..8a16b730d 100644 --- a/tests/unittests/backup_utils/b_filesystem/b_file_hash_test.cpp +++ b/tests/unittests/backup_utils/b_filesystem/b_file_hash_test.cpp @@ -96,4 +96,26 @@ HWTEST_F(BFileHashTest, b_file_hash_HashWithSHA256_0101, testing::ext::TestSize. } GTEST_LOG_(INFO) << "BFileHashTest-end b_file_hash_HashWithSHA256_0101"; } + +/** + * @tc.number: SUB_backup_b_file_hash_HashFilePath_0100 + * @tc.name: b_file_hash_HashFilePath_0100 + * @tc.desc: Test function of HashFilePath interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(BFileHashTest, b_file_hash_HashFilePath_0100, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "BFileHashTest-begin b_file_hash_HashFilePath_0100"; + try { + std::string filePath = "/AAA/BBB/C.txt"; + std::string hashResult = BackupFileHash::HashFilePath(filePath); + EXPECT_TRUE(!hashResult.empty()); + } catch (const exception &e) { + GTEST_LOG_(INFO) << "BFileHashTest-an exception occurred by HashFilePath."; + e.what(); + } + GTEST_LOG_(INFO) << "BFileHashTest-end b_file_hash_HashFilePath_0100"; +} } // namespace OHOS::FileManagement::Backup \ No newline at end of file diff --git a/utils/include/b_filesystem/b_file_hash.h b/utils/include/b_filesystem/b_file_hash.h index f745e81b3..42b963323 100644 --- a/utils/include/b_filesystem/b_file_hash.h +++ b/utils/include/b_filesystem/b_file_hash.h @@ -23,6 +23,7 @@ namespace OHOS::FileManagement::Backup { class BackupFileHash { public: static std::tuple HashWithSHA256(const std::string &fpath); + static std::string HashFilePath(const std::string &fileName); }; } // namespace OHOS::FileManagement::Backup diff --git a/utils/include/b_resources/b_constants.h b/utils/include/b_resources/b_constants.h index 0c93f1f19..7b7adda9a 100644 --- a/utils/include/b_resources/b_constants.h +++ b/utils/include/b_resources/b_constants.h @@ -89,6 +89,7 @@ constexpr uint32_t H2MS = 60 * 60 * 1000; constexpr uint32_t MAX_UPDATE_TIMER = 4 * H2MS; constexpr uint32_t DEFAULT_TIMEOUT = 15 * 60 * 1000; constexpr uint32_t TIMEOUT_INVALID = UINT32_MAX; +constexpr int LONG_FILE_NAME_BOUNDARY_VAL = 255; constexpr int CALL_APP_ON_PROCESS_TIME_INTERVAL = 5; // 框架每隔5s去调用应用的onProcess constexpr int APP_ON_PROCESS_MAX_TIMEOUT = 1000; // 应用的onProcess接口最大超时时间为1秒 diff --git a/utils/src/b_filesystem/b_file_hash.cpp b/utils/src/b_filesystem/b_file_hash.cpp index 807f9050d..9f0c4389f 100644 --- a/utils/src/b_filesystem/b_file_hash.cpp +++ b/utils/src/b_filesystem/b_file_hash.cpp @@ -15,6 +15,10 @@ #include "b_filesystem/b_file_hash.h" +#include +#include +#include +#include #include #include #include @@ -22,6 +26,7 @@ #include #include #include +#include "b_resources/b_constants.h" namespace OHOS::FileManagement::Backup { using namespace std; @@ -74,4 +79,21 @@ tuple BackupFileHash::HashWithSHA256(const string &fpath) SHA256_Final(res.get(), &ctx); return HashFinal(err, res, SHA256_DIGEST_LENGTH); } + +std::string BackupFileHash::HashFilePath(const string &fileName) +{ + std::filesystem::path filePath = fileName; + std::string realFileName = filePath.filename().string(); + ostringstream strHex; + strHex << hex; + + hash strHash; + size_t szHash = strHash(realFileName); + strHex << setfill('0') << setw(BConstants::BIG_FILE_NAME_SIZE) << szHash; + string hashResult = strHex.str(); + szHash = strHash(realFileName); + strHex << setfill('0') << setw(BConstants::BIG_FILE_NAME_SIZE) << szHash; + hashResult = strHex.str(); + return hashResult; +} } // namespace OHOS::FileManagement::Backup \ No newline at end of file -- Gitee