diff --git a/frameworks/native/backup_ext/include/ext_extension.h b/frameworks/native/backup_ext/include/ext_extension.h index ca5611f4a20b19cb3a3aa0c3c532bce132a89c60..c658859de6d6f5d7932652a5f247ea0ae4d7aea8 100644 --- a/frameworks/native/backup_ext/include/ext_extension.h +++ b/frameworks/native/backup_ext/include/ext_extension.h @@ -341,6 +341,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_; @@ -375,6 +378,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 cead8c8870b3f577d93a201d38996a81212a09f2..551a7d9b158930e215c4c6597b7f5513b951cd2e 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; } @@ -288,39 +278,55 @@ 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); + if (reportName.size() > BConstants::LONG_FILE_NAME_BOUNDARY_VAL) { + string reportHashName = BackupFileHash::HashFilePath(reportName); + HILOGI("report HashName is:%{public}s", reportHashName.c_str()); + 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) }; + } + std::string reportParentPath = fileName.substr(0, reportParentPathPos); + std::string reportFullName = reportParentPath + BConstants::FILE_SEPARATOR_CHAR + reportHashName; + UniqueFd reportHashFd(open(reportFullName.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(reportFullName).c_str(), errno); + errCode = errno; + } + std::unique_lock lock(reportHashLock_); + reportHashSrcPathMap_[fileName] = reportFullName; + return { errCode, move(fd), move(reportHashFd) }; + + } UniqueFd reportFd(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); - errCode = errno; + HILOGE("Failed to open report file = %{public}s, err = %{public}d", GetAnonyPath(reportName).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) @@ -350,8 +356,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) { @@ -366,19 +372,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; } @@ -450,9 +455,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)); @@ -506,9 +508,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; @@ -524,9 +523,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; } @@ -690,9 +686,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)) { @@ -1057,7 +1050,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()); @@ -1070,31 +1063,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) @@ -1202,6 +1184,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()) { @@ -1234,9 +1217,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) { @@ -1258,9 +1238,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)); @@ -1286,9 +1263,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 5e126e256fef316d0156efe8cfa384b1939d9947..83d128bf3f0b1a2afe9a31a2c8ef58cbb8fc8513 100644 --- a/frameworks/native/backup_ext/src/sub_ext_extension.cpp +++ b/frameworks/native/backup_ext/src/sub_ext_extension.cpp @@ -1387,4 +1387,30 @@ 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; + } + } + auto iter = reportHashSrcPathMap_.find(srcFileName); + if (iter == reportHashSrcPathMap_.end()) { + if (!RemoveFile(reportFileName)) { + HILOGE("Failed to delete backup report %{public}s, err = %{public}d", + GetAnonyPath(reportFileName).c_str(), errno); + return; + } + } + std::string reportHashFilePath = iter->second; + if (!RemoveFile(reportHashFilePath)) { + HILOGE("Failed to delete backup report %{public}s, err = %{public}d", + GetAnonyPath(reportHashFilePath).c_str(), errno); + } +} } // 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/frameworks/native/backup_kit_inner/src/service_reverse_stub.cpp b/frameworks/native/backup_kit_inner/src/service_reverse_stub.cpp index 2fae0a24aa3980a95bbdb7700849d9f10f8720ee..e4fce0e20c74a2587fcd9e02ba99fc57aaf89bd9 100644 --- a/frameworks/native/backup_kit_inner/src/service_reverse_stub.cpp +++ b/frameworks/native/backup_kit_inner/src/service_reverse_stub.cpp @@ -330,11 +330,8 @@ int32_t ServiceReverseStub::CmdIncrementalRestoreOnFileReady(MessageParcel &data auto fileName = data.ReadString(); int fd = INVALID_FD; int manifestFd = INVALID_FD; - bool fdFlag = data.ReadBool(); - if (fdFlag == true) { - fd = data.ReadFileDescriptor(); - manifestFd = data.ReadFileDescriptor(); - } + fd = data.ReadFileDescriptor(); + manifestFd = data.ReadFileDescriptor(); int32_t errCode = data.ReadInt32(); IncrementalRestoreOnFileReady(bundleName, fileName, fd, manifestFd, errCode); return BError(BError::Codes::OK); diff --git a/services/backup_sa/src/module_ipc/service_incremental.cpp b/services/backup_sa/src/module_ipc/service_incremental.cpp index fd596c6f8e2e0de7e3bc937b6fefdadadcfe6f85..cdfcd62c8aa179182b45f732f815de949425274a 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/services/backup_sa/src/module_ipc/service_incremental_reverse_proxy.cpp b/services/backup_sa/src/module_ipc/service_incremental_reverse_proxy.cpp index 8aecf1ee6cb835def1c1844025a145b14f22ab37..133c6f0a22c4714146e7935788e0c5e847d1db72 100644 --- a/services/backup_sa/src/module_ipc/service_incremental_reverse_proxy.cpp +++ b/services/backup_sa/src/module_ipc/service_incremental_reverse_proxy.cpp @@ -29,19 +29,27 @@ void ServiceReverseProxy::IncrementalBackupOnFileReady(string bundleName, string { BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr"); MessageParcel data; - bool appFlag = true; - if (SAUtils::IsSABundleName(bundleName)) { - appFlag = false; - } - - bool fdFlag = true; - if (fd < 0 || (appFlag && manifestFd < 0)) { - fdFlag = false; - } - if (!data.WriteInterfaceToken(GetDescriptor()) || !data.WriteString(bundleName) || - !data.WriteString(fileName) || !data.WriteBool(fdFlag) || - (fdFlag == true && (!data.WriteFileDescriptor(fd) || (!data.WriteFileDescriptor(manifestFd) && appFlag))) || - !data.WriteInt32(errCode)) { + if (!data.WriteInterfaceToken(GetDescriptor())) { + HILOGE("Incremental Backup file ready error, failed to write token"); + throw BError(BError::Codes::SA_BROKEN_IPC); + } + if (!data.WriteString(bundleName)) { + HILOGE("Incremental Backup file ready error, failed to write bundleName, %{public}s", bundleName.c_str()); + throw BError(BError::Codes::SA_BROKEN_IPC); + } + if (!data.WriteString(fileName)) { + HILOGE("Incremental Backup file ready error, failed to write fileName"); + throw BError(BError::Codes::SA_BROKEN_IPC); + } + if (!data.WriteFileDescriptor(fd)) { + HILOGE("Incremental Backup file ready error, failed to write fd"); + throw BError(BError::Codes::SA_BROKEN_IPC); + } + if (!data.WriteFileDescriptor(manifestFd)) { + HILOGE("Incremental Backup file ready error, failed to write manifestFd"); + } + if (!data.WriteInt32(errCode)) { + HILOGE("Incremental Backup file ready error, failed to write errCode"); throw BError(BError::Codes::SA_BROKEN_IPC); } @@ -242,14 +250,29 @@ void ServiceReverseProxy::IncrementalRestoreOnFileReady(string bundleName, strin { BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr"); MessageParcel data; - bool fdFlag = (fd < 0 || manifestFd < 0) ? false : true; - if (!data.WriteInterfaceToken(GetDescriptor()) || !data.WriteString(bundleName) || - !data.WriteString(fileName) || !data.WriteBool(fdFlag) || - (fdFlag == true && (!data.WriteFileDescriptor(fd) || !data.WriteFileDescriptor(manifestFd))) || - !data.WriteInt32(errCode)) { + if (!data.WriteInterfaceToken(GetDescriptor()) ) { + HILOGE("Restore onFileReady error, failed to write token"); + throw BError(BError::Codes::SA_BROKEN_IPC); + } + if (!data.WriteString(bundleName)) { + HILOGE("Restore onFileReady error, failed to write bundleName, %{public}s", bundleName.c_str()); + throw BError(BError::Codes::SA_BROKEN_IPC); + } + if (!data.WriteString(fileName)) { + HILOGE("Restore onFileReady error, failed to write fileName"); + throw BError(BError::Codes::SA_BROKEN_IPC); + } + if (!data.WriteFileDescriptor(fd)) { + HILOGE("Restore onFileReady error, failed to write fd"); + throw BError(BError::Codes::SA_BROKEN_IPC); + } + if (!data.WriteFileDescriptor(manifestFd)) { + HILOGE("Restore onFileReady error, failed to write manifestFd"); + } + if (!data.WriteInt32(errCode)) { + HILOGE("Restore onFileReady error, failed to write errCode"); throw BError(BError::Codes::SA_BROKEN_IPC); } - MessageParcel reply; MessageOption option; if (int err = Remote()->SendRequest( diff --git a/tests/unittests/backup_ext/BUILD.gn b/tests/unittests/backup_ext/BUILD.gn index 3257f81f003da9ce5bdfcf3256c5b00b9729a685..f10bcb812aefe078b4a3e5dd90d5c9693300c35f 100644 --- a/tests/unittests/backup_ext/BUILD.gn +++ b/tests/unittests/backup_ext/BUILD.gn @@ -37,6 +37,8 @@ ohos_unittest("ext_extension_test") { ] deps = [ + "${path_backup}/frameworks/native/backup_ext:backup_extension_ability_native", + "${path_backup}/interfaces/inner_api/native/backup_kit_inner:backup_kit_inner", "${path_backup}/services/backup_sa:backup_sa_ipc_stub", "${path_backup}/utils:backup_utils", ] diff --git a/tests/unittests/backup_ext/ext_extension_test.cpp b/tests/unittests/backup_ext/ext_extension_test.cpp index ca36c7b7cf94eda09be614e2227244af64c77c4d..ee6d2c860edbc64d6b58ef2f3b8bc3306d2d8a7d 100644 --- a/tests/unittests/backup_ext/ext_extension_test.cpp +++ b/tests/unittests/backup_ext/ext_extension_test.cpp @@ -51,6 +51,8 @@ public: void SetUp() {}; //每次测试用例执行之后执行 void TearDown() {}; + static inline std::shared_ptr extension = nullptr; + static inline std::shared_ptr extPtr = nullptr; }; void ExtExtensionTest::SetUpTestCase(void) @@ -63,6 +65,8 @@ void ExtExtensionTest::SetUpTestCase(void) system(touchFile.c_str()); string touchFile2 = string("touch ") + PATH + BUNDLE_NAME + "2.txt"; system(touchFile2.c_str()); + extension = make_shared(); + extPtr = make_shared(extension, ""); }; void ExtExtensionTest::TearDownTestCase(void) @@ -70,6 +74,8 @@ void ExtExtensionTest::TearDownTestCase(void) //删除测试文件夹和文件 string rmDir = string("rm -r") + PATH + BUNDLE_NAME; system(rmDir.c_str()); + extension = nullptr; + extPtr = nullptr; }; /** @@ -130,7 +136,7 @@ HWTEST_F(ExtExtensionTest, Ext_Extension_Test_0200, testing::ext::TestSize.Level GTEST_LOG_(INFO) << "ExtExtensionTest-begin Ext_Extension_Test_0200"; try { string fileName = "filename"; - string reportName = GetReportFileName(fileName); + string reportName = extPtr->GetReportFileName(fileName); EXPECT_EQ(reportName, "filename.rp"); } catch (...) { EXPECT_TRUE(false); @@ -581,10 +587,10 @@ HWTEST_F(ExtExtensionTest, Ext_Extension_Test_1000, testing::ext::TestSize.Level GTEST_LOG_(INFO) << "ExtExtensionTest-begin Ext_Extension_Test_1000"; try { std::string fileName = "test.txt"; - auto [ret, fd, reportFd] = GetIncreFileHandleForSpecialVersion(fileName); + auto [ret, fd, reportFd] = extPtr->GetIncreFileHandleForSpecialVersion(fileName); EXPECT_NE(ret, ERR_OK); fileName = "/test.txt"; - tie(ret, fd, reportFd) = GetIncreFileHandleForSpecialVersion(fileName); + tie(ret, fd, reportFd) = extPtr->GetIncreFileHandleForSpecialVersion(fileName); EXPECT_NE(ret, ERR_OK); } catch (...) { EXPECT_TRUE(false); 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