| 938 | } |
| 939 | |
| 940 | std::string fs_get_cache_directory() { |
| 941 | std::string cache_directory = ""; |
| 942 | auto ensure_trailing_slash = [](std::string p) { |
| 943 | // Make sure to add trailing slash |
| 944 | if (p.back() != DIRECTORY_SEPARATOR) { |
| 945 | p += DIRECTORY_SEPARATOR; |
| 946 | } |
| 947 | return p; |
| 948 | }; |
| 949 | if (getenv("LLAMA_CACHE")) { |
| 950 | cache_directory = std::getenv("LLAMA_CACHE"); |
| 951 | } else { |
| 952 | #if defined(__linux__) || defined(__FreeBSD__) || defined(_AIX) || \ |
| 953 | defined(__OpenBSD__) || defined(__NetBSD__) |
| 954 | if (std::getenv("XDG_CACHE_HOME")) { |
| 955 | cache_directory = std::getenv("XDG_CACHE_HOME"); |
| 956 | } else if (std::getenv("HOME")) { |
| 957 | cache_directory = std::getenv("HOME") + std::string("/.cache/"); |
| 958 | } else { |
| 959 | #if defined(__linux__) |
| 960 | /* no $HOME is defined, fallback to getpwuid */ |
| 961 | struct passwd *pw = getpwuid(getuid()); |
| 962 | if ((!pw) || (!pw->pw_dir)) { |
| 963 | throw std::runtime_error("Failed to find $HOME directory"); |
| 964 | } |
| 965 | |
| 966 | cache_directory = std::string(pw->pw_dir) + std::string("/.cache/"); |
| 967 | #else /* defined(__linux__) */ |
| 968 | throw std::runtime_error("Failed to find $HOME directory"); |
| 969 | #endif /* defined(__linux__) */ |
| 970 | } |
| 971 | #elif defined(__APPLE__) |
| 972 | cache_directory = std::getenv("HOME") + std::string("/Library/Caches/"); |
| 973 | #elif defined(_WIN32) |
| 974 | cache_directory = std::getenv("LOCALAPPDATA"); |
| 975 | #elif defined(__EMSCRIPTEN__) |
| 976 | GGML_ABORT("not implemented on this platform"); |
| 977 | #else |
| 978 | # error Unknown architecture |
| 979 | #endif |
| 980 | cache_directory = ensure_trailing_slash(cache_directory); |
| 981 | cache_directory += "llama.cpp"; |
| 982 | } |
| 983 | return ensure_trailing_slash(cache_directory); |
| 984 | } |
| 985 | |
| 986 | std::string fs_get_cache_file(const std::string & filename) { |
| 987 | GGML_ASSERT(filename.find(DIRECTORY_SEPARATOR) == std::string::npos); |
no test coverage detected