common : keep HF cache dir as path, expose UTF-8 only for logs (#29320)

Restore get_cache_directory() as fs::path as string() can be lossy on Windows

Partially reverts #29125

Signed-off-by: Adrien Gallouët <angt@huggingface.co>
This commit is contained in:
Adrien Gallouët 2026-09-23 18:24:23 +02:00 • committed by GitHub
parent 9575389609
commit bddf8263c3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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<const char *>(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 {};
}