From c88614ca26e6014b8983d2ab167823c4b4080701 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E5=8F=8B=E6=9D=BE?= Date: Thu, 31 Jul 2025 13:22:53 +0000 Subject: [PATCH] codex MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 邹友松 --- .../core/ability/utils/include/file_utils.h | 1 + .../core/ability/utils/src/file_utils.cpp | 24 +++++++++++++++++++ services/engine/src/progress_thread.cpp | 21 ++++------------ 3 files changed, 29 insertions(+), 17 deletions(-) diff --git a/services/core/ability/utils/include/file_utils.h b/services/core/ability/utils/include/file_utils.h index d1ea4580..b8759b3a 100644 --- a/services/core/ability/utils/include/file_utils.h +++ b/services/core/ability/utils/include/file_utils.h @@ -45,6 +45,7 @@ public: static void DestroyBaseDirectory(const std::vector &dirInfos); static std::string ReadDataFromFile(const std::string &filePath); static std::string GetParentDir(const std::string &fileDir); + static std::string GetFileRealPath(const std::string &filePath); private: static std::string GetCurrentDir(const std::string &fileDir); diff --git a/services/core/ability/utils/src/file_utils.cpp b/services/core/ability/utils/src/file_utils.cpp index 8c5c9752..8cd26ab5 100644 --- a/services/core/ability/utils/src/file_utils.cpp +++ b/services/core/ability/utils/src/file_utils.cpp @@ -221,5 +221,29 @@ std::string FileUtils::ReadDataFromFile(const std::string &filePath) readFile.close(); return fileRaw; } + +std::string FileUtils::GetFileRealPath(const std::string &filePath) +{ + if (filePath.empty()) { + ENGINE_LOGE("GetFileRealPath file path is empty"); + return ""; + } + + size_t lastSlashPos = filePath.find_last_of('/'); + if (lastSlashPos == std::string::npos) { + ENGINE_LOGE("GetFileRealPath , file path format is error"); + return ""; + } + + std::string parentDir = filePath.substr(0, lastSlashPos); + char realPath[PATH_MAX] = {}; + if (realpath(parentDir.c_str(), realPath) == NULL) { + ENGINE_LOGE("file path not exist or no permission to access the path"); + return ""; + } + + std::string fileName = filePath.substr(lastSlashPos + 1); + return std::string(realPath) + "/" + fileName; +} } // namespace UpdateService } // namespace OHOS diff --git a/services/engine/src/progress_thread.cpp b/services/engine/src/progress_thread.cpp index 26d5c727..613160a4 100644 --- a/services/engine/src/progress_thread.cpp +++ b/services/engine/src/progress_thread.cpp @@ -261,26 +261,13 @@ bool DownloadThread::DealAbnormal(uint32_t percent) FILE* DownloadThread::FileOpen(const std::string &fileName, const std::string &mode) { - if (fileName.empty() || fileName.size() > PATH_MAX) { - ENGINE_LOGI("DownloadThread file is empty or exceed path_max"); - return nullptr; - } - std::string fileDir = fileName; - auto pos = fileDir.find_last_of("/"); - if (pos != std::string::npos) { - fileDir.erase(pos + 1); - } else { - ENGINE_LOGI("DownloadThread file %{public}s, mode: %{public}s", fileName.c_str(), mode.c_str()); + std::string realFileName = FileUtils::GetFileRealPath(fileName); + if (realFileName.empty()) { + ENGINE_LOGE("fileName is error"); return nullptr; } - char *path = realpath(fileDir.c_str(), NULL); - if (path == NULL) { - ENGINE_LOGI("DownloadThread file %{public}s, mode: %{public}s", fileName.c_str(), mode.c_str()); - return nullptr; - } - free(path); - FILE* fp = fopen(fileName.c_str(), mode.c_str()); + FILE* fp = fopen(realFileName.c_str(), mode.c_str()); return fp; } } // namespace UpdateService -- Gitee