diff --git a/frameworks/native/backup_ext/include/ext_extension.h b/frameworks/native/backup_ext/include/ext_extension.h index ac4246976a97cc7b0e73ed9746e6b0b154b33623..d210b9c47ec6863de707e22771af7fb71a2f02a3 100644 --- a/frameworks/native/backup_ext/include/ext_extension.h +++ b/frameworks/native/backup_ext/include/ext_extension.h @@ -336,7 +336,7 @@ private: void RestoreBigFiles(bool appendTargetPath); void FillEndFileInfos(const std::string &path, const unordered_map &result); void RestoreBigFileAfter(const string &filePath, const struct stat &sta); - int DealIncreUnPacketResult(const off_t tarFileSize, const std::string &tarFileName, + void DealIncreUnPacketResult(const off_t tarFileSize, const std::string &tarFileName, const std::tuple &result); ErrCode StartOnProcessTaskThread(wptr obj, BackupRestoreScenario scenario); diff --git a/frameworks/native/backup_ext/include/untar_file.h b/frameworks/native/backup_ext/include/untar_file.h index 49eb36e2620430df21d52040d89a495a2f609554..ed05bae6c95332a0e2658526a456055220415656 100644 --- a/frameworks/native/backup_ext/include/untar_file.h +++ b/frameworks/native/backup_ext/include/untar_file.h @@ -18,7 +18,6 @@ #include "tar_file.h" #include "b_json/b_report_entity.h" -#include "b_filesystem/b_dir.h" namespace OHOS::FileManagement::Backup { struct FileStatInfo { @@ -31,6 +30,7 @@ struct FileStatInfo { }; using ErrFileInfo = std::map>; +using EndFileInfo = std::map; const int FIRST_PARAM = 0; const int SECOND_PARAM = 1; diff --git a/frameworks/native/backup_ext/src/ext_extension.cpp b/frameworks/native/backup_ext/src/ext_extension.cpp index 7845876e7d1ed8e735cd044b785732670283b9ee..65c10b663a96bb82a01512885f70797c777be9b5 100644 --- a/frameworks/native/backup_ext/src/ext_extension.cpp +++ b/frameworks/native/backup_ext/src/ext_extension.cpp @@ -924,10 +924,6 @@ int BackupExtExtension::DoRestore(const string &fileName, const off_t fileSize) endFileInfos_.merge(fileInfos); errFileInfos_.merge(errInfos); } - if (BDir::CheckAndRmSoftLink(tarName) || BDir::CheckAndRmSoftLink(fileInfos)) { - HILOGE("File soft links are forbidden"); - return BError(BError::Codes::EXT_FORBID_BACKUP_RESTORE).GetCode(); - } if (ret != 0) { HILOGE("Failed to untar file = %{public}s, err = %{public}d", tarName.c_str(), ret); return ret; @@ -955,25 +951,23 @@ void BackupExtExtension::GetTarIncludes(const string &tarName, unordered_map &result) { + if (!isDebug_) { + return; + } int err = std::get(result); - EndFileInfo tmpEndInfo = std::get(result); if (!isRpValid_) { if (err != ERR_OK) { endFileInfos_[tarFileName] = tarFileSize; errFileInfos_[tarFileName] = {err}; } + EndFileInfo tmpEndInfo = std::get(result); endFileInfos_.merge(tmpEndInfo); } - if (BDir::CheckAndRmSoftLink(tmpEndInfo)) { - HILOGE("File soft links are forbidden"); - return BError(BError::Codes::EXT_FORBID_BACKUP_RESTORE).GetCode(); - } ErrFileInfo tmpErrInfo = std::get(result); errFileInfos_.merge(tmpErrInfo); - return ERR_OK; } int BackupExtExtension::DoIncrementalRestore() @@ -1001,9 +995,9 @@ int BackupExtExtension::DoIncrementalRestore() string tarName = path + item; // 当用户指定fullBackupOnly字段或指定版本的恢复,解压目录当前在/backup/restore - if (!BDir::IsFilePathValid(tarName) || BDir::CheckAndRmSoftLink(tarName)) { + if (!BDir::IsFilePathValid(tarName)) { HILOGE("Check incre tarfile path : %{public}s err, path is forbidden", GetAnonyPath(tarName).c_str()); - return BError(BError::Codes::EXT_FORBID_BACKUP_RESTORE).GetCode(); + return ERR_INVALID_VALUE; } unordered_map result; GetTarIncludes(tarName, result); @@ -1016,9 +1010,7 @@ int BackupExtExtension::DoIncrementalRestore() std::tuple unPacketRes = UntarFile::GetInstance().IncrementalUnPacket(tarName, path, result); err = std::get(unPacketRes); - if (int tmpErr = DealIncreUnPacketResult(tarFileSize, item, unPacketRes); tmpErr != ERR_OK) { - return BError(BError::Codes::EXT_FORBID_BACKUP_RESTORE).GetCode(); - } + DealIncreUnPacketResult(tarFileSize, item, unPacketRes); HILOGI("Application recovered successfully, package path is %{public}s", tarName.c_str()); DeleteBackupIncrementalTars(tarName); } @@ -1096,13 +1088,6 @@ void BackupExtExtension::RestoreBigFilesForSpecialCloneCloud(const ExtManageInfo if (!BDir::IsFilePathValid(fileName)) { 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()); - } - return; - } - if (BDir::CheckAndRmSoftLink(fileName)) { - HILOGE("File soft links are forbidden"); return; } if (chmod(fileName.c_str(), sta.st_mode) != 0) { @@ -1141,10 +1126,6 @@ ErrCode BackupExtExtension::RestoreTarForSpecialCloneCloud(const ExtManageInfo & HILOGE("Check spec tarfile hash path : %{public}s err, path is forbidden", GetAnonyPath(tarName).c_str()); return ERR_INVALID_VALUE; } - if (BDir::CheckAndRmSoftLink(tarName)) { - HILOGE("File soft links are forbidden"); - return BError(BError::Codes::EXT_FORBID_BACKUP_RESTORE).GetCode(); - } if (!BDir::IsFilePathValid(untarPath)) { HILOGE("Check spec tarfile path : %{public}s err, path is forbidden", GetAnonyPath(untarPath).c_str()); return ERR_INVALID_VALUE; @@ -1155,18 +1136,18 @@ ErrCode BackupExtExtension::RestoreTarForSpecialCloneCloud(const ExtManageInfo & } auto [err, fileInfos, errInfos] = UntarFile::GetInstance().UnPacket(tarName, untarPath); if (isDebug_) { + if (err != 0) { + endFileInfos_[tarName] = item.sta.st_size; + errFileInfos_[tarName] = { err }; + } endFileInfos_.merge(fileInfos); errFileInfos_.merge(errInfos); } - if (BDir::CheckAndRmSoftLink(fileInfos)) { - HILOGE("File soft links are forbidden"); - return BError(BError::Codes::EXT_FORBID_BACKUP_RESTORE).GetCode(); - } - DeleteBackupIncrementalTars(tarName); if (err != ERR_OK) { HILOGE("Failed to untar file = %{public}s, err = %{public}d", tarName.c_str(), err); return err; } + DeleteBackupIncrementalTars(tarName); return ERR_OK; } @@ -1380,10 +1361,6 @@ void BackupExtExtension::RestoreOneBigFile(const std::string &path, HILOGE("Check big file path : %{public}s err, path is forbidden", GetAnonyPath(filePath).c_str()); return; } - if (BDir::CheckAndRmSoftLink(fileName)) { - HILOGE("File soft links are forbidden"); - return; - } if (isDebug_) { endFileInfos_[filePath] = item.sta.st_size; } @@ -1396,10 +1373,7 @@ void BackupExtExtension::RestoreOneBigFile(const std::string &path, HILOGE("failed to move the file. err = %{public}d", errno); return; } - if (BDir::CheckAndRmSoftLink(filePath)) { - HILOGE("File soft links are forbidden"); - return; - } + RestoreBigFileAfter(filePath, item.sta); } diff --git a/frameworks/native/backup_ext/src/sub_ext_extension.cpp b/frameworks/native/backup_ext/src/sub_ext_extension.cpp index ff18af6c142571addffc07de9c9c57b781d0042e..cb1bd5b5c3c4860b88ee982c993ba6b57776b741 100644 --- a/frameworks/native/backup_ext/src/sub_ext_extension.cpp +++ b/frameworks/native/backup_ext/src/sub_ext_extension.cpp @@ -1516,9 +1516,7 @@ ErrCode BackupExtExtension::CloudSpecialRestore(string tarName, string untarPath auto unPacketRes = UntarFile::GetInstance().IncrementalUnPacket(tarName, untarPath, result); ErrCode err = ERR_OK; err = std::get(unPacketRes); - if (int tmpErr = DealIncreUnPacketResult(tarFileSize, tarName, unPacketRes); tmpErr != ERR_OK) { - return BError(BError::Codes::EXT_FORBID_BACKUP_RESTORE).GetCode(); - } + DealIncreUnPacketResult(tarFileSize, tarName, unPacketRes); HILOGI("Application recovered successfully, package path is %{public}s", tarName.c_str()); DeleteBackupIncrementalTars(tarName); return err; diff --git a/utils/include/b_filesystem/b_dir.h b/utils/include/b_filesystem/b_dir.h index c4d89d1c8a71c9ac39541b5955bf1d158494b8dc..bb5b12e042a384fcda9c18dcd7f222afe0e61bb2 100644 --- a/utils/include/b_filesystem/b_dir.h +++ b/utils/include/b_filesystem/b_dir.h @@ -32,7 +32,6 @@ #include "errors.h" namespace OHOS::FileManagement::Backup { -using EndFileInfo = std::map; class BDir { public: /** @@ -103,22 +102,6 @@ public: */ static bool IsFilePathValid(const std::string &filePath); - /** - * @brief 核实文件是否存在软链接并删除 - * - * @param filePath 待核实的路径 - * @return 是否存在软连接 - */ - static bool CheckAndRmSoftLink(const std::string &filePath); - - /** - * @brief 核实文件是否存在软链接并删除 - * - * @param filePaths 待核实的路径集合 - * @return 是否存在软连接 - */ - static bool CheckAndRmSoftLink(const EndFileInfo &filePaths); - /** * @brief 判断目录列表是否包含路径 */ diff --git a/utils/src/b_filesystem/b_dir.cpp b/utils/src/b_filesystem/b_dir.cpp index a725c3f0912123e270de600b6ef246e9923dd9ba..7e89517c73c3e078ba71299b097086144d0fc092 100644 --- a/utils/src/b_filesystem/b_dir.cpp +++ b/utils/src/b_filesystem/b_dir.cpp @@ -532,33 +532,6 @@ bool BDir::IsFilePathValid(const std::string &filePath) return AppFileService::SandboxHelper::IsValidPath(filePath); } -bool BDir::CheckAndRmSoftLink(const std::string &filePath) -{ - if (std::filesystem::is_symlink(filePath)) { - HILOGE("Soft link is not allowed, path = %{public}s", GetAnonyPath(filePath).c_str()); - if (unlink(filePath.c_str()) < 0) { - HILOGE("Failed to unlink the backup file : %{public}s", GetAnonyPath(filePath).c_str()); - } - return true; - } - return false; -} - -bool BDir::CheckAndRmSoftLink(const EndFileInfo &filePaths) -{ - bool isSoftLink = false; - for (const auto &it : filePaths) { - if (std::filesystem::is_symlink(it.first)) { - HILOGE("Soft link is not allowed, path = %{public}s", GetAnonyPath(it.first).c_str()); - isSoftLink = true; - if (unlink(it.first.c_str()) < 0) { - HILOGE("Failed to unlink the backup file : %{public}s", GetAnonyPath(it.first).c_str()); - } - } - } - return isSoftLink; -} - bool BDir::IsDirsMatch(const vector &excludePaths, const string &path) { if (path.empty()) {