From 391b62a2f118dd907a3ae1b12f7276f82d9ff366 Mon Sep 17 00:00:00 2001 From: LiuYiqing <2360461449@qq.com> Date: Tue, 3 Mar 2026 16:42:58 +0800 Subject: [PATCH 1/7] =?UTF-8?q?Fix:improve=20ResolveAbsolutePath=20functio?= =?UTF-8?q?n=20in=20file-utils.cc,=E8=BF=98=E6=9C=89=E5=88=A4=E6=96=AD?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=AD=98=E5=9C=A8=E5=87=BD=E6=95=B0=E4=B9=9F?= =?UTF-8?q?=E8=BF=9B=E8=A1=8Cwin=E5=B9=B3=E5=8F=B0=E5=AE=BD=E5=AD=97?= =?UTF-8?q?=E7=AC=A6=E9=80=82=E9=85=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sherpa-onnx/csrc/file-utils.cc | 63 +++++++++++++++++----------------- 1 file changed, 31 insertions(+), 32 deletions(-) diff --git a/sherpa-onnx/csrc/file-utils.cc b/sherpa-onnx/csrc/file-utils.cc index b1802334ab..b81f4539e0 100644 --- a/sherpa-onnx/csrc/file-utils.cc +++ b/sherpa-onnx/csrc/file-utils.cc @@ -5,24 +5,28 @@ #include "sherpa-onnx/csrc/file-utils.h" #include +#include #include #include #include #include -#ifdef _WIN32 -#include -#else -#include -#include -#endif - #include "sherpa-onnx/csrc/macros.h" +namespace sherpa_onnx { + std::wstring ToWideString(const std::string &s); +} // namespace sherpa_onnx + namespace sherpa_onnx { bool FileExists(const std::string &filename) { +#ifdef _WIN32 + std::wstring wide_path = ToWideString(filename); + std::ifstream file(wide_path); + return file.good(); +#else return std::ifstream(filename).good(); +#endif } void AssertFileExists(const std::string &filename) { @@ -33,7 +37,12 @@ void AssertFileExists(const std::string &filename) { } std::vector ReadFile(const std::string &filename) { +#ifdef _WIN32 + std::wstring wide_path = ToWideString(filename); + std::ifstream file(wide_path, std::ios::binary | std::ios::ate); +#else std::ifstream file(filename, std::ios::binary | std::ios::ate); +#endif if (!file.is_open()) { return {}; } @@ -119,33 +128,23 @@ std::string ResolveAbsolutePath(const std::string &path) { return path; } -#ifdef _WIN32 - // Check if path is already absolute (drive letter or UNC path) - if ((path.size() > 1 && path[1] == ':') || - (path.size() > 1 && path[0] == '\\' && path[1] == '\\')) { - return path; - } - - char buffer[MAX_PATH]; - if (GetFullPathNameA(path.c_str(), MAX_PATH, buffer, nullptr)) { - return std::string(buffer); - } - - return path; // fallback on failure - -#else - // POSIX: absolute paths start with '/' - if (path[0] == '/') { + try { + std::filesystem::path fs_path(path); + + // If already absolute, return normalized path + if (fs_path.is_absolute()) { + return fs_path.lexically_normal().u8string(); + } + + // Convert to absolute path and normalize + std::filesystem::path abs_path = std::filesystem::absolute(fs_path); + abs_path = abs_path.lexically_normal(); + + return abs_path.u8string(); + } catch (const std::filesystem::filesystem_error&) { + // If conversion fails, return original path return path; } - - char buffer[PATH_MAX]; - if (realpath(path.c_str(), buffer)) { - return std::string(buffer); - } - - return path; // fallback on failure -#endif } } // namespace sherpa_onnx From 750f6a3ef9e1da46b46c6a4078b48ba614b6a155 Mon Sep 17 00:00:00 2001 From: LiuYiqing <2360461449@qq.com> Date: Tue, 3 Mar 2026 17:34:20 +0800 Subject: [PATCH 2/7] =?UTF-8?q?=E4=BC=98=E5=8C=96=EF=BC=9AFileExists?= =?UTF-8?q?=E5=92=8CReadFile=E9=83=BD=E4=BD=BF=E7=94=A8=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E7=9A=84filesystem=20API=EF=BC=8C=E8=A1=A5=E5=85=85=EF=BC=9ARe?= =?UTF-8?q?solveAbsolutePath=E7=9A=84=E5=AE=BD=E5=AD=97=E7=AC=A6API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sherpa-onnx/csrc/file-utils.cc | 47 ++++++++++++++++++++++------------ 1 file changed, 31 insertions(+), 16 deletions(-) diff --git a/sherpa-onnx/csrc/file-utils.cc b/sherpa-onnx/csrc/file-utils.cc index b81f4539e0..6d42f5a844 100644 --- a/sherpa-onnx/csrc/file-utils.cc +++ b/sherpa-onnx/csrc/file-utils.cc @@ -20,13 +20,18 @@ namespace sherpa_onnx { namespace sherpa_onnx { bool FileExists(const std::string &filename) { + try { #ifdef _WIN32 - std::wstring wide_path = ToWideString(filename); - std::ifstream file(wide_path); - return file.good(); + std::wstring wide_path = ToWideString(filename); + std::filesystem::path file_path(wide_path); #else - return std::ifstream(filename).good(); + std::filesystem::path file_path(filename); #endif + return std::filesystem::exists(file_path) && + std::filesystem::is_regular_file(file_path); + } catch (const std::filesystem::filesystem_error&) { + return false; + } } void AssertFileExists(const std::string &filename) { @@ -37,25 +42,30 @@ void AssertFileExists(const std::string &filename) { } std::vector ReadFile(const std::string &filename) { + try { #ifdef _WIN32 - std::wstring wide_path = ToWideString(filename); - std::ifstream file(wide_path, std::ios::binary | std::ios::ate); + std::wstring wide_path = ToWideString(filename); + std::filesystem::path file_path(wide_path); #else - std::ifstream file(filename, std::ios::binary | std::ios::ate); + std::filesystem::path file_path(filename); #endif - if (!file.is_open()) { - return {}; - } + std::ifstream file(file_path, std::ios::binary | std::ios::ate); + if (!file.is_open()) { + return {}; + } - std::streamsize size = file.tellg(); - file.seekg(0, std::ios::beg); + std::streamsize size = file.tellg(); + file.seekg(0, std::ios::beg); - std::vector buffer(size); - if (!file.read(buffer.data(), size)) { + std::vector buffer(size); + if (!file.read(buffer.data(), size)) { + return {}; + } + + return buffer; + } catch (const std::filesystem::filesystem_error&) { return {}; } - - return buffer; } #if __ANDROID_API__ >= 9 @@ -129,7 +139,12 @@ std::string ResolveAbsolutePath(const std::string &path) { } try { +#ifdef _WIN32 + std::wstring wide_path = ToWideString(path); + std::filesystem::path fs_path(wide_path); +#else std::filesystem::path fs_path(path); +#endif // If already absolute, return normalized path if (fs_path.is_absolute()) { From 3e6f2590a3ad293b60b1e3d8e2d872e47024904d Mon Sep 17 00:00:00 2001 From: LiuYiqing <2360461449@qq.com> Date: Tue, 3 Mar 2026 18:00:34 +0800 Subject: [PATCH 3/7] =?UTF-8?q?=E4=BC=98=E5=8C=96=EF=BC=9A=E6=89=A9?= =?UTF-8?q?=E5=A4=A7catch=E8=8C=83=E5=9B=B4=EF=BC=8C=E5=90=88=E5=B9=B6?= =?UTF-8?q?=E5=89=8D=E5=90=91=E5=A3=B0=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sherpa-onnx/csrc/file-utils.cc | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/sherpa-onnx/csrc/file-utils.cc b/sherpa-onnx/csrc/file-utils.cc index 6d42f5a844..513c81c1c5 100644 --- a/sherpa-onnx/csrc/file-utils.cc +++ b/sherpa-onnx/csrc/file-utils.cc @@ -14,10 +14,7 @@ #include "sherpa-onnx/csrc/macros.h" namespace sherpa_onnx { - std::wstring ToWideString(const std::string &s); -} // namespace sherpa_onnx - -namespace sherpa_onnx { +std::wstring ToWideString(const std::string &s); bool FileExists(const std::string &filename) { try { @@ -29,7 +26,7 @@ bool FileExists(const std::string &filename) { #endif return std::filesystem::exists(file_path) && std::filesystem::is_regular_file(file_path); - } catch (const std::filesystem::filesystem_error&) { + } catch (const std::exception&) { return false; } } @@ -63,7 +60,7 @@ std::vector ReadFile(const std::string &filename) { } return buffer; - } catch (const std::filesystem::filesystem_error&) { + } catch (const std::exception&) { return {}; } } @@ -156,7 +153,7 @@ std::string ResolveAbsolutePath(const std::string &path) { abs_path = abs_path.lexically_normal(); return abs_path.u8string(); - } catch (const std::filesystem::filesystem_error&) { + } catch (const std::exception&) { // If conversion fails, return original path return path; } From 25c5a4eba6bdcace72f4f8358040f152a1767755 Mon Sep 17 00:00:00 2001 From: LiuYiqing <2360461449@qq.com> Date: Thu, 5 Mar 2026 12:00:46 +0800 Subject: [PATCH 4/7] =?UTF-8?q?=E6=94=B9=E7=94=A8windowsAPI=E8=80=8C?= =?UTF-8?q?=E9=9D=9Efilesystem?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sherpa-onnx/csrc/file-utils.cc | 99 +++++++++++++++++++++++++--------- 1 file changed, 74 insertions(+), 25 deletions(-) diff --git a/sherpa-onnx/csrc/file-utils.cc b/sherpa-onnx/csrc/file-utils.cc index 513c81c1c5..f5f4e606f5 100644 --- a/sherpa-onnx/csrc/file-utils.cc +++ b/sherpa-onnx/csrc/file-utils.cc @@ -5,27 +5,35 @@ #include "sherpa-onnx/csrc/file-utils.h" #include -#include #include #include #include #include +#ifdef _WIN32 +#include +#else +#include +#include +#include +#endif + #include "sherpa-onnx/csrc/macros.h" namespace sherpa_onnx { std::wstring ToWideString(const std::string &s); +std::string ToString(const std::wstring &s); bool FileExists(const std::string &filename) { try { #ifdef _WIN32 - std::wstring wide_path = ToWideString(filename); - std::filesystem::path file_path(wide_path); + DWORD attributes = GetFileAttributesW(ToWideString(filename).c_str()); + + return attributes != INVALID_FILE_ATTRIBUTES && !(attributes & FILE_ATTRIBUTE_DIRECTORY); #else - std::filesystem::path file_path(filename); + struct stat file_stat; + return stat(filename.c_str(), &file_stat) == 0 && S_ISREG(file_stat.st_mode); #endif - return std::filesystem::exists(file_path) && - std::filesystem::is_regular_file(file_path); } catch (const std::exception&) { return false; } @@ -39,14 +47,44 @@ void AssertFileExists(const std::string &filename) { } std::vector ReadFile(const std::string &filename) { + if (filename.empty()) { + return {}; + } try { -#ifdef _WIN32 - std::wstring wide_path = ToWideString(filename); - std::filesystem::path file_path(wide_path); +#ifdef _WIN32 + HANDLE hFile = CreateFileW( + ToWideString(filename).c_str(), + GENERIC_READ, + FILE_SHARE_READ, + nullptr, + OPEN_EXISTING, + FILE_ATTRIBUTE_NORMAL, + nullptr + ); + + if (hFile == INVALID_HANDLE_VALUE) { + return {}; + } + + std::unique_ptr file_guard( + hFile, CloseHandle); + + LARGE_INTEGER file_size; + if (!GetFileSizeEx(hFile, &file_size) || file_size.QuadPart > SIZE_MAX) { + return {}; + } + + std::vector buffer(static_cast(file_size.QuadPart)); + + DWORD bytes_read = 0; + if (!::ReadFile(hFile, buffer.data(), static_cast(buffer.size()), &bytes_read, nullptr) || + bytes_read != buffer.size()) { + return {}; + } + + return buffer; #else - std::filesystem::path file_path(filename); -#endif - std::ifstream file(file_path, std::ios::binary | std::ios::ate); + std::ifstream file(filename, std::ios::binary | std::ios::ate); if (!file.is_open()) { return {}; } @@ -60,6 +98,7 @@ std::vector ReadFile(const std::string &filename) { } return buffer; +#endif } catch (const std::exception&) { return {}; } @@ -138,23 +177,33 @@ std::string ResolveAbsolutePath(const std::string &path) { try { #ifdef _WIN32 std::wstring wide_path = ToWideString(path); - std::filesystem::path fs_path(wide_path); -#else - std::filesystem::path fs_path(path); -#endif - - // If already absolute, return normalized path - if (fs_path.is_absolute()) { - return fs_path.lexically_normal().u8string(); + DWORD required_size = GetFullPathNameW(wide_path.c_str(), 0, nullptr, nullptr); + if (required_size == 0) { + return path; } - // Convert to absolute path and normalize - std::filesystem::path abs_path = std::filesystem::absolute(fs_path); - abs_path = abs_path.lexically_normal(); + std::vector buffer(required_size); + DWORD actual_size = GetFullPathNameW( + wide_path.c_str(), + required_size, + buffer.data(), + nullptr + ); + + if (actual_size == 0 || actual_size >= required_size) { + return path; + } - return abs_path.u8string(); + std::wstring resolved_wide(buffer.data(), actual_size); + return ToString(resolved_wide); +#else + char resolved_path[PATH_MAX]; + if (realpath(path.c_str(), resolved_path) == nullptr) { + return path; + } + return std::string(resolved_path); +#endif } catch (const std::exception&) { - // If conversion fails, return original path return path; } } From 43db5d0287c62691d2cb23a44bff2858159596aa Mon Sep 17 00:00:00 2001 From: LiuYiqing <2360461449@qq.com> Date: Thu, 5 Mar 2026 19:36:50 +0800 Subject: [PATCH 5/7] =?UTF-8?q?=E5=8E=BB=E6=8E=89=E4=B8=8D=E5=BF=85?= =?UTF-8?q?=E8=A6=81=E7=9A=84try-catch=EF=BC=8C=E6=8B=86=E5=88=86=E8=BF=87?= =?UTF-8?q?=E4=BA=8E=E5=A4=8D=E6=9D=82=E7=9A=84=E5=88=A4=E6=96=AD=E6=9D=A1?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sherpa-onnx/csrc/file-utils.cc | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/sherpa-onnx/csrc/file-utils.cc b/sherpa-onnx/csrc/file-utils.cc index f5f4e606f5..d872daf02e 100644 --- a/sherpa-onnx/csrc/file-utils.cc +++ b/sherpa-onnx/csrc/file-utils.cc @@ -25,7 +25,6 @@ std::wstring ToWideString(const std::string &s); std::string ToString(const std::wstring &s); bool FileExists(const std::string &filename) { - try { #ifdef _WIN32 DWORD attributes = GetFileAttributesW(ToWideString(filename).c_str()); @@ -34,9 +33,6 @@ bool FileExists(const std::string &filename) { struct stat file_stat; return stat(filename.c_str(), &file_stat) == 0 && S_ISREG(file_stat.st_mode); #endif - } catch (const std::exception&) { - return false; - } } void AssertFileExists(const std::string &filename) { @@ -77,8 +73,14 @@ std::vector ReadFile(const std::string &filename) { std::vector buffer(static_cast(file_size.QuadPart)); DWORD bytes_read = 0; - if (!::ReadFile(hFile, buffer.data(), static_cast(buffer.size()), &bytes_read, nullptr) || - bytes_read != buffer.size()) { + bool read_success = ::ReadFile( + hFile, + buffer.data(), + static_cast(buffer.size()), + &bytes_read, + nullptr + ); + if (!read_success || bytes_read != buffer.size()) { return {}; } From 01881d35a9fa10fb3e09104d4625ff6c0fded55f Mon Sep 17 00:00:00 2001 From: XiaoBaiCanDo <111476033+XiaoBaiCanDo@users.noreply.github.com> Date: Tue, 17 Mar 2026 17:14:15 +0800 Subject: [PATCH 6/7] Apply suggestion from @Copilot MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 包含 支持POSIX realpath()的使用 Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- sherpa-onnx/csrc/file-utils.cc | 1 + 1 file changed, 1 insertion(+) diff --git a/sherpa-onnx/csrc/file-utils.cc b/sherpa-onnx/csrc/file-utils.cc index d872daf02e..ccdecb7d24 100644 --- a/sherpa-onnx/csrc/file-utils.cc +++ b/sherpa-onnx/csrc/file-utils.cc @@ -16,6 +16,7 @@ #include #include #include +#include #endif #include "sherpa-onnx/csrc/macros.h" From 989488ccae79584d547ab595f8d5595ca21c0bef Mon Sep 17 00:00:00 2001 From: LiuYiqing <2360461449@qq.com> Date: Tue, 17 Mar 2026 17:44:17 +0800 Subject: [PATCH 7/7] =?UTF-8?q?file.tellg()=E5=8F=AF=E8=83=BD=E8=BF=94?= =?UTF-8?q?=E5=9B=9E-1=EF=BC=8C=E9=9C=80=E8=A6=81=E6=A3=80=E6=9F=A5?= =?UTF-8?q?=EF=BC=8C=E6=98=BE=E7=A4=BA=E8=BD=AC=E6=8D=A2size=E4=B8=BA?= =?UTF-8?q?=E6=97=A0=E7=AC=A6=E5=8F=B7=E6=95=B4=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sherpa-onnx/csrc/file-utils.cc | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/sherpa-onnx/csrc/file-utils.cc b/sherpa-onnx/csrc/file-utils.cc index d872daf02e..40f287a9ed 100644 --- a/sherpa-onnx/csrc/file-utils.cc +++ b/sherpa-onnx/csrc/file-utils.cc @@ -92,9 +92,12 @@ std::vector ReadFile(const std::string &filename) { } std::streamsize size = file.tellg(); + if (size < 0) { + return {}; + } file.seekg(0, std::ios::beg); - std::vector buffer(size); + std::vector buffer(static_cast(size)); if (!file.read(buffer.data(), size)) { return {}; }