1- #if defined(_MSC_VER)
2- #define _SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING
3- #endif
4-
51#include " ggml-rpc.h"
62#ifdef _WIN32
73# define NOMINMAX
84# define DIRECTORY_SEPARATOR ' \\ '
9- # include < locale>
105# include < windows.h>
116# include < fcntl.h>
127# include < io.h>
1510# include < unistd.h>
1611# include < sys/stat.h>
1712#endif
18- #include < codecvt>
1913#include < string>
2014#include < stdio.h>
2115#include < vector>
22- #include < filesystem>
2316#include < algorithm>
2417#include < thread>
2518#include < regex>
2619
27- namespace fs = std::filesystem;
20+ #if defined(__linux__)
21+ #include < sys/types.h>
22+ #include < pwd.h>
23+ #endif
24+
25+ // NOTE: this is copied from common.cpp to avoid linking with libcommon
26+ #ifdef _WIN32
27+ static std::wstring utf8_to_wstring (const std::string & str) {
28+ if (str.empty ()) {
29+ return std::wstring ();
30+ }
31+
32+ int size = MultiByteToWideChar (CP_UTF8, 0 , str.c_str (), (int )str.size (), NULL , 0 );
33+
34+ if (size <= 0 ) {
35+ return std::wstring ();
36+ }
37+
38+ std::wstring wstr (size, 0 );
39+ MultiByteToWideChar (CP_UTF8, 0 , str.c_str (), (int )str.size (), &wstr[0 ], size);
40+
41+ return wstr;
42+ }
43+ #endif
2844
2945// NOTE: this is copied from common.cpp to avoid linking with libcommon
3046// returns true if successful, false otherwise
3147static bool fs_create_directory_with_parents (const std::string & path) {
3248#ifdef _WIN32
33- std::wstring_convert<std::codecvt_utf8<wchar_t >> converter;
34- std::wstring wpath = converter.from_bytes (path);
49+ std::wstring wpath = utf8_to_wstring (path);
3550
3651 // if the path already exists, check whether it's a directory
3752 const DWORD attributes = GetFileAttributesW (wpath.c_str ());
@@ -44,9 +59,16 @@ static bool fs_create_directory_with_parents(const std::string & path) {
4459 // process path from front to back, procedurally creating directories
4560 while ((pos_slash = path.find (' \\ ' , pos_slash)) != std::string::npos) {
4661 const std::wstring subpath = wpath.substr (0 , pos_slash);
47- const wchar_t * test = subpath.c_str ();
4862
49- const bool success = CreateDirectoryW (test, NULL );
63+ pos_slash += 1 ;
64+
65+ // skip the drive letter, in some systems it can return an access denied error
66+ if (subpath.length () == 2 && subpath[1 ] == ' :' ) {
67+ continue ;
68+ }
69+
70+ const bool success = CreateDirectoryW (subpath.c_str (), NULL );
71+
5072 if (!success) {
5173 const DWORD error = GetLastError ();
5274
@@ -60,8 +82,6 @@ static bool fs_create_directory_with_parents(const std::string & path) {
6082 return false ;
6183 }
6284 }
63-
64- pos_slash += 1 ;
6585 }
6686
6787 return true ;
@@ -115,13 +135,27 @@ static std::string fs_get_cache_directory() {
115135#if defined(__linux__) || defined(__FreeBSD__) || defined(_AIX) || defined(__OpenBSD__)
116136 if (std::getenv (" XDG_CACHE_HOME" )) {
117137 cache_directory = std::getenv (" XDG_CACHE_HOME" );
118- } else {
138+ } else if ( std::getenv ( " HOME " )) {
119139 cache_directory = std::getenv (" HOME" ) + std::string (" /.cache/" );
140+ } else {
141+ #if defined(__linux__)
142+ /* no $HOME is defined, fallback to getpwuid */
143+ struct passwd *pw = getpwuid (getuid ());
144+ if ((!pw) || (!pw->pw_dir )) {
145+ throw std::runtime_error (" Failed to find $HOME directory" );
146+ }
147+
148+ cache_directory = std::string (pw->pw_dir ) + std::string (" /.cache/" );
149+ #else /* defined(__linux__) */
150+ throw std::runtime_error (" Failed to find $HOME directory" );
151+ #endif /* defined(__linux__) */
120152 }
121153#elif defined(__APPLE__)
122154 cache_directory = std::getenv (" HOME" ) + std::string (" /Library/Caches/" );
123155#elif defined(_WIN32)
124156 cache_directory = std::getenv (" LOCALAPPDATA" );
157+ #elif defined(__EMSCRIPTEN__)
158+ GGML_ABORT (" not implemented on this platform" );
125159#else
126160# error Unknown architecture
127161#endif
0 commit comments