diff --git a/frameworks/native/backup_ext/include/ext_extension.h b/frameworks/native/backup_ext/include/ext_extension.h index 8b83b4a35dd4f5430ee30d3167d6dd98eff38c76..4abcac15bc02b963b39266b3e3266b3c08cb2a70 100644 --- a/frameworks/native/backup_ext/include/ext_extension.h +++ b/frameworks/native/backup_ext/include/ext_extension.h @@ -345,6 +345,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_; @@ -379,6 +382,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 43edecfa9d6960d7684bceff0495350e01111645..ba0764b4f7336d6c4d47ad0004c8d858798667ba 100644 --- a/frameworks/native/backup_ext/src/ext_extension.cpp +++ b/frameworks/native/backup_ext/src/ext_extension.cpp @@ -206,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; @@ -214,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; } @@ -297,39 +287,57 @@ 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); + lock.unlock(); + 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); - errCode = errno; + HILOGE("Failed to open report file = %{public}s, err = %{public}d", + GetAnonyPath(reportFullFileName).c_str(), errno); } - return {errCode, move(fd), move(reportFd)}; + return { errCode, move(fd), move(reportFd) }; } static ErrCode GetIncrementalFileHandlePath(const string &fileName, const string &bundleName, std::string &tarName) @@ -359,8 +367,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) { @@ -375,19 +383,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; } @@ -469,9 +476,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)); @@ -525,9 +529,6 @@ ErrCode BackupExtExtension::BigFileReady(TarMap &bigFileInfo, sptr pro HILOGE("open file failed, file name is %{public}s, err = %{public}d", GetAnonyString(filePath).c_str(), errno); errCode = errno; - AuditLog auditLog = {false, "Open fd failed", "ADD", "", 1, "FAILED", "Backup File", - "BigFile", GetAnonyPath(filePath)}; - HiAudit::GetInstance(false).Write(auditLog); if (errCode == ERR_NO_PERMISSION) { noPermissionFiles.emplace_back(item.first.c_str()); continue; @@ -543,9 +544,6 @@ ErrCode BackupExtExtension::BigFileReady(TarMap &bigFileInfo, sptr pro RefreshTimeInfo(startTime, fdNum); } ClearNoPermissionFiles(bigFileInfo, noPermissionFiles); - AuditLog auditLog = {false, "Send Big File Fd", "ADD", "", bigFileInfo.size(), "SUCCESS", "Backup Files", - "BigFile", ""}; - HiAudit::GetInstance(false).Write(auditLog); HILOGI("BigFileReady End"); return ret; } @@ -709,9 +707,6 @@ static ErrCode TarFileReady(const TarMap &tarFileInfo, sptr proxy) if (fdval < 0) { HILOGE("TarFileReady open file failed, file name is %{public}s, err = %{public}d", tarName.c_str(), errno); errCode = errno; - AuditLog auditLog = {false, "Open fd failed", "ADD", "", 1, "FAILED", - "TarFileReady", "tarFile", tarPath}; - HiAudit::GetInstance(false).Write(auditLog); } int ret = proxy->AppFileReady(tarName, fdval, errCode); if (SUCCEEDED(ret)) { @@ -1076,7 +1071,7 @@ void BackupExtExtension::RestoreBigFilesForSpecialCloneCloud(const ExtManageInfo const struct stat &sta = item.sta; string fileName = item.hashName; if (!BDir::IsFilePathValid(fileName)) { - HILOGE("Check big spec file path : %{public}s err, path is forbidden", GetAnonyPath(fileName).c_str()); + HILOGE("Check big special file path : %{public}s err, path is forbidden", GetAnonyPath(fileName).c_str()); errFileInfos_[fileName].emplace_back(DEFAULT_INVAL_VALUE); if (!RemoveFile(fileName)) { HILOGE("Failed to delete the backup bigFile %{public}s", GetAnonyPath(fileName).c_str()); @@ -1089,31 +1084,20 @@ void BackupExtExtension::RestoreBigFilesForSpecialCloneCloud(const ExtManageInfo } if (chmod(fileName.c_str(), sta.st_mode) != 0) { HILOGE("Failed to chmod filePath, err = %{public}d", errno); - AuditLog auditLog = {false, "chmod file failed", "ADD", "", 1, "FAILED", - "RestoreBigFilesForSpecialCloneCloud", "CommonFile", GetAnonyPath(fileName)}; - HiAudit::GetInstance(false).Write(auditLog); errFileInfos_[fileName].emplace_back(errno); } - struct timespec tv[2] = {sta.st_atim, sta.st_mtim}; UniqueFd fd(open(fileName.data(), O_RDONLY)); if (fd < 0) { HILOGE("Failed to open file = %{public}s, err = %{public}d", GetAnonyPath(fileName).c_str(), errno); errFileInfos_[fileName].emplace_back(errno); - AuditLog auditLog = {false, "open fd failed", "ADD", "", 1, "FAILED", - "RestoreBigFilesForSpecialCloneCloud", "CommonFile", GetAnonyPath(fileName)}; - HiAudit::GetInstance(false).Write(auditLog); return; } if (futimens(fd.Get(), tv) != 0) { 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) @@ -1221,6 +1205,7 @@ void BackupExtExtension::DeleteIndexAndRpFile() HILOGE("Failed to delete the backup report index %{public}s", reportManagePath.c_str()); } } + static bool RestoreBigFilePrecheck(string &fileName, const string &path, const string &hashName, const string &filePath) { if (filePath.empty()) { @@ -1253,9 +1238,6 @@ void BackupExtExtension::RestoreBigFileAfter(const string &filePath, const struc if (fd < 0) { errFileInfos_[filePath].emplace_back(errno); HILOGE("Failed to open file = %{public}s, err = %{public}d", GetAnonyPath(filePath).c_str(), errno); - AuditLog auditLog = {false, "open fd failed", "ADD", "", 1, "FAILED", - "RestoreBigFileAfter", "CommonFile", GetAnonyPath(filePath)}; - HiAudit::GetInstance(false).Write(auditLog); return; } if (futimens(fd.Get(), tv) != 0) { @@ -1277,9 +1259,6 @@ void BackupExtExtension::RestoreOneBigFile(const std::string &path, if (fd < 0) { HILOGE("Failed to open report file = %{public}s, err = %{public}d", reportPath.c_str(), errno); errFileInfos_[item.hashName].emplace_back(errno); - AuditLog auditLog = {false, "Open fd failed", "ADD", "", 1, "FAILED", "RestoreOneBigFile", - "RestoreOneBigFile", GetAnonyPath(reportPath)}; - HiAudit::GetInstance(false).Write(auditLog); throw BError(BError::Codes::EXT_INVAL_ARG, string("open report file failed")); } BReportEntity rp(move(fd)); @@ -1305,9 +1284,6 @@ void BackupExtExtension::RestoreOneBigFile(const std::string &path, if (!BFile::MoveFile(fileName, filePath)) { errFileInfos_[filePath].emplace_back(errno); HILOGE("failed to move the file. err = %{public}d", errno); - AuditLog auditLog = {false, "Move file failed", "ADD", "", 1, "FAILED", "MoveFile", - "RestoreOneBigFile", GetAnonyPath(filePath)}; - HiAudit::GetInstance(false).Write(auditLog); return; } if (BDir::CheckAndRmSoftLink(filePath)) { diff --git a/frameworks/native/backup_ext/src/sub_ext_extension.cpp b/frameworks/native/backup_ext/src/sub_ext_extension.cpp index 11ef67483ae8d9e5688e7a2d700aef9089f57083..5f67ee933f366f3022ec65313b1872efb4578de1 100644 --- a/frameworks/native/backup_ext/src/sub_ext_extension.cpp +++ b/frameworks/native/backup_ext/src/sub_ext_extension.cpp @@ -1387,4 +1387,35 @@ ErrCode BackupExtExtension::CloudSpecialRestore(string tarName, string untarPath DeleteBackupIncrementalTars(tarName); return err; } + +void BackupExtExtension::RmBigFileReportForSpecialCloneCloud(const std::string &srcFileName) +{ + // 删除大文件的rp文件 + string reportFileName = GetReportFileName(srcFileName); + if (reportHashSrcPathMap_.empty()) { + if (!RemoveFile(reportFileName)) { + HILOGE("Failed to delete backup report %{public}s, err = %{public}d", + GetAnonyPath(reportFileName).c_str(), errno); + } + return; + } + std::unique_lock lock(reportHashLock_); + 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); + lock.unlock(); +} } // namespace OHOS::FileManagement::Backup diff --git a/frameworks/native/backup_kit_inner/src/b_incremental_restore_session.cpp b/frameworks/native/backup_kit_inner/src/b_incremental_restore_session.cpp index b0f8179ce497d9ff0f98c35df7c28ea1678e66f8..25bcef8131ffaa18dd98ac7b02d30de472700afe 100644 --- a/frameworks/native/backup_kit_inner/src/b_incremental_restore_session.cpp +++ b/frameworks/native/backup_kit_inner/src/b_incremental_restore_session.cpp @@ -15,6 +15,7 @@ #include "b_incremental_restore_session.h" +#include "b_anony/b_anony.h" #include "b_error/b_error.h" #include "b_radar/b_radar.h" #include "filemgmt_libhilog.h" @@ -133,7 +134,8 @@ ErrCode BIncrementalRestoreSession::GetFileHandle(const string &bundleName, cons if (proxy == nullptr) { return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode(); } - + HILOGI("Begin getFileHandle, bundle:%{public}s, fileName:%{public}s", bundleName.c_str(), + GetAnonyPath(fileName).c_str()); return proxy->GetIncrementalFileHandle(bundleName, fileName); } diff --git a/frameworks/native/backup_kit_inner/src/b_incremental_session_restore_async.cpp b/frameworks/native/backup_kit_inner/src/b_incremental_session_restore_async.cpp index d5925422f0c999f0480e18526af4313ef2f0831e..d812f6beef2b95da9ed56c6d1498b331cedd2488 100644 --- a/frameworks/native/backup_kit_inner/src/b_incremental_session_restore_async.cpp +++ b/frameworks/native/backup_kit_inner/src/b_incremental_session_restore_async.cpp @@ -15,6 +15,7 @@ #include "b_incremental_session_restore_async.h" +#include "b_anony/b_anony.h" #include "b_error/b_error.h" #include "b_radar/b_radar.h" #include "b_resources/b_constants.h" @@ -81,7 +82,8 @@ ErrCode BIncrementalSessionRestoreAsync::GetFileHandle(const string &bundleName, if (proxy == nullptr) { return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode(); } - + HILOGI("Begin getFileHandle, bundle:%{public}s, fileName:%{public}s", bundleName.c_str(), + GetAnonyPath(fileName).c_str()); return proxy->GetIncrementalFileHandle(bundleName, fileName); } 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 501b054ba468f71860bcdfdb969fb2c0255e7d02..3daf19df33ddc3948cbd8824583dda21949283a9 100644 --- a/frameworks/native/backup_kit_inner/src/b_session_restore.cpp +++ b/frameworks/native/backup_kit_inner/src/b_session_restore.cpp @@ -15,6 +15,7 @@ #include "b_session_restore.h" +#include "b_anony/b_anony.h" #include "b_error/b_error.h" #include "b_radar/b_radar.h" #include "filemgmt_libhilog.h" @@ -101,7 +102,8 @@ ErrCode BSessionRestore::GetFileHandle(const string &bundleName, const string &f if (proxy == nullptr) { return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode(); } - + HILOGI("Begin getFileHandle, bundle:%{public}s, fileName:%{public}s", bundleName.c_str(), + GetAnonyPath(fileName).c_str()); return proxy->GetFileHandle(bundleName, fileName); } diff --git a/services/backup_sa/src/module_ipc/service_incremental.cpp b/services/backup_sa/src/module_ipc/service_incremental.cpp index 4eb82331a87e6d8378c315e7113b08a657e28c12..71c77ce06c907af5b364b9eb0c13e46f23220d85 100644 --- a/services/backup_sa/src/module_ipc/service_incremental.cpp +++ b/services/backup_sa/src/module_ipc/service_incremental.cpp @@ -715,6 +715,8 @@ ErrCode Service::GetIncrementalFileHandle(const std::string &bundleName, const s return BError(BError::Codes::SA_INVAL_ARG); } if (action == BConstants::ServiceSchedAction::RUNNING) { + HILOGI("Restore getIncrementalFileHandle begin, bundleName:%{public}s, fileName:%{public}s", + bundleName.c_str(), GetAnonyPath(fileName).c_str()); auto err = SendIncrementalFileHandle(bundleName, fileName); if (err != ERR_OK) { HILOGE("SendIncrementalFileHandle failed, bundle:%{public}s", bundleName.c_str()); diff --git a/tests/unittests/backup_ext/ext_extension_test.cpp b/tests/unittests/backup_ext/ext_extension_test.cpp index ca36c7b7cf94eda09be614e2227244af64c77c4d..9cb6f7cfdfff3a70325053d57ddfdd1c75e8c6c4 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 @@ -567,32 +544,6 @@ 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"; -} - /** * @tc.number: SUB_Ext_Extension_1100 * @tc.name: Ext_Extension_Test_1100 diff --git a/tests/unittests/backup_sa/module_ipc/sched_scheduler_test.cpp b/tests/unittests/backup_sa/module_ipc/sched_scheduler_test.cpp index 788616b821116ef47a81915196441e250d9222a3..f5dc79c0d9b3afe83a5c8e77ec4a3632a8c85876 100644 --- a/tests/unittests/backup_sa/module_ipc/sched_scheduler_test.cpp +++ b/tests/unittests/backup_sa/module_ipc/sched_scheduler_test.cpp @@ -21,6 +21,7 @@ #include #include "b_error/b_error.h" +#include "b_radar/b_radar.h" #include "module_ipc/service.h" #include "module_ipc/svc_session_manager.h" #include "module_sched/sched_scheduler.h" @@ -77,7 +78,7 @@ void SchedSchedulerTest::Init(IServiceReverseType::Scenario scenario) }; transform(bundleNames.begin(), bundleNames.end(), inserter(backupExtNameMap, backupExtNameMap.end()), setBackupExtNameMap); - + EXPECT_TRUE(sessionManagerPtr_ != nullptr); sessionManagerPtr_->Active({ .clientToken = CLIENT_TOKEN_ID, 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 e745dedf4766bfc5f51eb532433f31a54e61edbc..903c9e44cf09a1c5b04c91bff8672650103ed2ab 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 f745e81b36a7c91e938bdfa3705bcce8f4c2bbf7..42b9633237e942ccffc0be986578d15b2dc6c1c0 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 0c93f1f19b5b597a622842222db34d19ee78c4f0..7b7adda9a0ac0d9e079859ada203a2957b81d61d 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 807f9050d23f337135f8b5577624fc5e1fd2ac1a..9f0c4389f1926de3810f38c68e5156a7a92a56ae 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