Skip to content

Commit 287b5b1

Browse files
authored
common : add getpwuid fallback for HF cache when HOME is not set (#21035)
Signed-off-by: Adrien Gallouët <angt@huggingface.co>
1 parent a73bbd5 commit 287b5b1

1 file changed

Lines changed: 9 additions & 0 deletions

File tree

common/hf-cache.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ namespace nl = nlohmann;
2626
#include <windows.h>
2727
#else
2828
#define HOME_DIR "HOME"
29+
#include <unistd.h>
30+
#include <pwd.h>
2931
#endif
3032

3133
namespace hf_cache {
@@ -51,6 +53,13 @@ static fs::path get_cache_directory() {
5153
return entry.path.empty() ? base : base / entry.path;
5254
}
5355
}
56+
#ifndef _WIN32
57+
const struct passwd * pw = getpwuid(getuid());
58+
59+
if (pw->pw_dir && *pw->pw_dir) {
60+
return fs::path(pw->pw_dir) / ".cache" / "huggingface" / "hub";
61+
}
62+
#endif
5463
throw std::runtime_error("Failed to determine HF cache directory");
5564
}();
5665

0 commit comments

Comments
 (0)