From c11fa8c8edd17ef0fc2c4900b4e9a6d72d5ea823 Mon Sep 17 00:00:00 2001 From: ngxson Date: Fri, 24 May 2024 00:23:23 +0200 Subject: [PATCH 1/3] fix missing slash in fs_get_cache_directory() --- common/common.cpp | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/common/common.cpp b/common/common.cpp index 7500e08ff1b..de1963db070 100644 --- a/common/common.cpp +++ b/common/common.cpp @@ -1855,11 +1855,15 @@ bool fs_create_directory_with_parents(const std::string & path) { std::string fs_get_cache_directory() { std::string cache_directory = ""; - if (getenv("LLAMA_CACHE")) { - cache_directory = std::getenv("LLAMA_CACHE"); - if (cache_directory.back() != DIRECTORY_SEPARATOR) { - cache_directory += DIRECTORY_SEPARATOR; + auto ensure_trailing_slash = [](std::string p) { + // Make sure to add trailing slash + if (p.back() != DIRECTORY_SEPARATOR) { + p += DIRECTORY_SEPARATOR; } + return p; + }; + if (getenv("LLAMA_CACHE")) { + cache_directory = ensure_trailing_slash(std::getenv("LLAMA_CACHE")); } else { #ifdef __linux__ if (std::getenv("XDG_CACHE_HOME")) { @@ -1872,6 +1876,7 @@ std::string fs_get_cache_directory() { #elif defined(_WIN32) cache_directory = std::getenv("APPDATA"); #endif // __linux__ + cache_directory = ensure_trailing_slash(cache_directory); cache_directory += "llama.cpp"; cache_directory += DIRECTORY_SEPARATOR; } From e0a2d830ca72d08ebab0fb5f48e04aab362bb3fc Mon Sep 17 00:00:00 2001 From: ngxson Date: Fri, 24 May 2024 00:25:07 +0200 Subject: [PATCH 2/3] use LOCALAPPDATA for fs_get_cache_directory() --- common/common.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/common.cpp b/common/common.cpp index de1963db070..a15a94d5756 100644 --- a/common/common.cpp +++ b/common/common.cpp @@ -1874,7 +1874,7 @@ std::string fs_get_cache_directory() { #elif defined(__APPLE__) cache_directory = std::getenv("HOME") + std::string("/Library/Caches/"); #elif defined(_WIN32) - cache_directory = std::getenv("APPDATA"); + cache_directory = std::getenv("LOCALAPPDATA"); #endif // __linux__ cache_directory = ensure_trailing_slash(cache_directory); cache_directory += "llama.cpp"; From 43321db396ef6bfa8ce79dd4bd118dbdbf0506a8 Mon Sep 17 00:00:00 2001 From: ngxson Date: Fri, 24 May 2024 00:27:50 +0200 Subject: [PATCH 3/3] better code style --- common/common.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/common/common.cpp b/common/common.cpp index a15a94d5756..401d72bac00 100644 --- a/common/common.cpp +++ b/common/common.cpp @@ -1863,7 +1863,7 @@ std::string fs_get_cache_directory() { return p; }; if (getenv("LLAMA_CACHE")) { - cache_directory = ensure_trailing_slash(std::getenv("LLAMA_CACHE")); + cache_directory = std::getenv("LLAMA_CACHE"); } else { #ifdef __linux__ if (std::getenv("XDG_CACHE_HOME")) { @@ -1878,9 +1878,8 @@ std::string fs_get_cache_directory() { #endif // __linux__ cache_directory = ensure_trailing_slash(cache_directory); cache_directory += "llama.cpp"; - cache_directory += DIRECTORY_SEPARATOR; } - return cache_directory; + return ensure_trailing_slash(cache_directory); }