From bddf8263c31c3dce3212263b00ebd2d98c1a752b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20Gallou=C3=ABt?= Date: Wed, 23 Sep 2026 18:24:23 +0200 Subject: [PATCH] common : keep HF cache dir as path, expose UTF-8 only for logs (#29320) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Restore get_cache_directory() as fs::path as string() can be lossy on Windows Partially reverts #29125 Signed-off-by: Adrien Gallouët --- common/hf-cache.cpp | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/common/hf-cache.cpp b/common/hf-cache.cpp index 4f8a1bb3d..12d4fcc80 100644 --- a/common/hf-cache.cpp +++ b/common/hf-cache.cpp @@ -30,8 +30,8 @@ namespace hf_cache { namespace fs = std::filesystem; -std::string get_cache_path() { - static const std::string cache = []() { +static fs::path get_cache_directory() { + static const fs::path cache = []() { struct { const char * var; fs::path path; @@ -46,14 +46,14 @@ std::string get_cache_path() { for (const auto & entry : entries) { if (auto * p = std::getenv(entry.var); p && *p) { fs::path base(p); - return (entry.path.empty() ? base : base / entry.path).string(); + return entry.path.empty() ? base : base / entry.path; } } #ifndef _WIN32 const struct passwd * pw = getpwuid(getuid()); if (pw && pw->pw_dir && *pw->pw_dir) { - return (fs::path(pw->pw_dir) / ".cache" / "huggingface" / "hub").string(); + return fs::path(pw->pw_dir) / ".cache" / "huggingface" / "hub"; } #endif throw std::runtime_error("Failed to determine HF cache directory"); @@ -62,6 +62,15 @@ std::string get_cache_path() { return cache; } +std::string get_cache_path() { +#if defined(__cpp_lib_char8_t) + const std::u8string u8str = get_cache_directory().u8string(); + return std::string(reinterpret_cast(u8str.data()), u8str.size()); +#else + return get_cache_directory().u8string(); +#endif +} + static std::string folder_name_to_repo(const std::string & folder) { constexpr std::string_view prefix = "models--"; if (folder.rfind(prefix, 0)) { @@ -80,7 +89,7 @@ static std::string repo_to_folder_name(const std::string & repo_id) { } static fs::path get_repo_path(const std::string & repo_id) { - return fs::path(get_cache_path()) / repo_to_folder_name(repo_id); + return get_cache_directory() / repo_to_folder_name(repo_id); } static bool is_hex_char(const char c) { @@ -393,7 +402,7 @@ static std::string get_cached_ref(const fs::path & repo_path) { } hf_files get_cached_files(const std::string & repo_id) { - const fs::path cache_path = get_cache_path(); + const fs::path cache_path = get_cache_directory(); if (!fs::exists(cache_path)) { return {}; }