diff --git a/common/arg.cpp b/common/arg.cpp index 7accabaf7..e3ab084c6 100644 --- a/common/arg.cpp +++ b/common/arg.cpp @@ -327,7 +327,7 @@ static bool parse_bool_value(const std::string & value) { static std::string get_default_local_path(const std::string & url) { auto f = string_split(url, '#').front(); f = string_split(f, '?').front(); - return fs_path_to_utf8(fs_get_cache_file(string_split(f, '/').back())); + return fs_get_cache_file(string_split(f, '/').back()).string(); } static bool spec_types_is_default(const common_params & params) { @@ -738,7 +738,7 @@ static void common_params_apply_system_config(common_params & params, llama_exam common_preset_context ctx(ex); ctx.ignore_unknown_keys = true; // the same config file is shared by all programs for (const auto & path : found) { - LOG_INF("using config file: %s\n", fs_path_to_utf8(path).c_str()); + LOG_INF("using config file: %s\n", path.string().c_str()); common_preset global; common_presets presets = ctx.load_from_ini(path, global); global.apply_to_params(params); diff --git a/common/common.cpp b/common/common.cpp index de140a211..04e348805 100644 --- a/common/common.cpp +++ b/common/common.cpp @@ -934,12 +934,6 @@ std::string wstring_to_utf8(const std::wstring & str) { } #endif -// returns the path as a UTF-8 string, preserving its separators -std::string fs_path_to_utf8(const std::filesystem::path & path) { - const auto value = path.u8string(); - return std::string(value.begin(), value.end()); -} - void fs_write_atomic(const std::filesystem::path & path, const std::string & data) { // a random suffix gives every writer its own temporary file, across processes and containers std::random_device rd; @@ -963,7 +957,7 @@ void fs_write_atomic(const std::filesystem::path & path, const std::string & dat if (file.fail() || ec) { std::filesystem::remove(path_tmp, ec); - throw std::runtime_error("failed to write file: " + fs_path_to_utf8(path)); + throw std::runtime_error("failed to write file: " + path.string()); } } @@ -1057,7 +1051,7 @@ std::filesystem::path fs_get_cache_file(const std::string & filename) { std::error_code ec; common_create_directories(cache_directory, ec); if (ec) { - throw std::runtime_error("failed to create cache directory: " + fs_path_to_utf8(cache_directory)); + throw std::runtime_error("failed to create cache directory: " + cache_directory.string()); } return cache_directory / std::filesystem::u8path(filename); } diff --git a/common/common.h b/common/common.h index ab08d1591..d0e33fb75 100644 --- a/common/common.h +++ b/common/common.h @@ -910,9 +910,6 @@ std::wstring utf8_to_wstring(const std::string & str); std::string wstring_to_utf8(const std::wstring & str); #endif -// returns the path as a UTF-8 string, preserving its separators -std::string fs_path_to_utf8(const std::filesystem::path & path); - // // Environment utils // diff --git a/common/download.cpp b/common/download.cpp index 6d4b5e506..08531a2d0 100644 --- a/common/download.cpp +++ b/common/download.cpp @@ -1005,7 +1005,7 @@ std::string common_docker_resolve_model(const std::string & docker) { std::string model_filename = repo; std::replace(model_filename.begin(), model_filename.end(), '/', '_'); model_filename += "_" + tag + ".gguf"; - std::string local_path = fs_path_to_utf8(fs_get_cache_file(model_filename)); + std::string local_path = fs_get_cache_file(model_filename).string(); const std::string blob_url = url_prefix + "/blobs/" + gguf_digest; common_download_opts opts; diff --git a/common/hf-cache.cpp b/common/hf-cache.cpp index f0c501b62..aafc940c5 100644 --- a/common/hf-cache.cpp +++ b/common/hf-cache.cpp @@ -63,7 +63,7 @@ static fs::path get_cache_directory() { } std::string get_cache_path() { - return fs_path_to_utf8(get_cache_directory()); + return get_cache_directory().string(); } static std::string folder_name_to_repo(const std::string & folder) { @@ -340,11 +340,11 @@ hf_files get_repo_files(const std::string & repo_id, file.url = endpoint + repo_id + "/resolve/" + commit + "/" + file.path; fs::path final_path = commit_path / subpath; - file.final_path = fs_path_to_utf8(final_path); + file.final_path = final_path.string(); if (!file.oid.empty() && !fs::exists(final_path)) { fs::path local_path = blobs_path / file.oid; - file.local_path = fs_path_to_utf8(local_path); + file.local_path = local_path.string(); } else { file.local_path = file.final_path; } @@ -411,7 +411,7 @@ hf_files get_cached_files(const std::string & repo_id) { if (!fs::exists(snapshots_path)) { continue; } - std::string _repo_id = folder_name_to_repo(fs_path_to_utf8(repo.path().filename())); + std::string _repo_id = folder_name_to_repo(repo.path().filename().string()); if (!is_valid_repo_id(_repo_id)) { continue; @@ -436,7 +436,7 @@ hf_files get_cached_files(const std::string & repo_id) { file.repo_id = _repo_id; const auto generic_path = path.generic_u8string(); file.path = std::string(generic_path.begin(), generic_path.end()); - file.local_path = fs_path_to_utf8(entry.path()); + file.local_path = entry.path().string(); file.final_path = file.local_path; files.push_back(std::move(file)); } @@ -516,7 +516,7 @@ bool remove_cached_repo(const std::string & repo_id) { std::error_code ec; auto removed = fs::remove_all(repo_path, ec); if (ec) { - LOG_ERR("%s: failed to remove repo cache %s: %s\n", __func__, fs_path_to_utf8(repo_path).c_str(), ec.message().c_str()); + LOG_ERR("%s: failed to remove repo cache %s: %s\n", __func__, repo_path.string().c_str(), ec.message().c_str()); return false; } return removed > 0; diff --git a/common/preset.cpp b/common/preset.cpp index 14d51809d..31a8bd513 100644 --- a/common/preset.cpp +++ b/common/preset.cpp @@ -171,12 +171,12 @@ static std::map> parse_ini_from_ std::map> parsed; if (!std::filesystem::exists(path)) { - throw std::runtime_error("preset file does not exist: " + fs_path_to_utf8(path)); + throw std::runtime_error("preset file does not exist: " + path.string()); } std::ifstream file(path); if (!file.good()) { - throw std::runtime_error("failed to open server preset file: " + fs_path_to_utf8(path)); + throw std::runtime_error("failed to open server preset file: " + path.string()); } std::string contents((std::istreambuf_iterator(file)), std::istreambuf_iterator()); @@ -225,7 +225,7 @@ static std::map> parse_ini_from_ common_peg_parse_context ctx(contents); const auto result = parser.parse(ctx); if (!result.success()) { - throw std::runtime_error("failed to parse server config file: " + fs_path_to_utf8(path)); + throw std::runtime_error("failed to parse server config file: " + path.string()); } std::string current_section = COMMON_PRESET_DEFAULT_NAME; @@ -323,7 +323,7 @@ common_presets common_preset_context::load_from_ini(const std::filesystem::path } LOG_DBG("accepted option: %s = %s\n", key.c_str(), preset.options[opt].c_str()); } else if (ignore_unknown_keys) { - LOG_WRN("ignoring option '%s' from %s: not supported by this program\n", key.c_str(), fs_path_to_utf8(path).c_str()); + LOG_WRN("ignoring option '%s' from %s: not supported by this program\n", key.c_str(), path.string().c_str()); } else { throw std::runtime_error(string_format( "option '%s' not recognized in preset '%s'", @@ -401,7 +401,7 @@ common_presets common_preset_context::load_from_models_dir(const std::string & m if (!entry.is_regular_file(ec)) { continue; } - const std::string fname = fs_path_to_utf8(entry.path().filename()); + const std::string fname = entry.path().filename().string(); if (!string_ends_with(fname, ".gguf")) { continue; } @@ -422,9 +422,9 @@ common_presets common_preset_context::load_from_models_dir(const std::string & m if (!path.empty()) { models.push_back({ /* name */ name, - /* path */ fs_path_to_utf8(path), - /* path_mmproj */ fs_path_to_utf8(mmproj_file), // can be empty - /* path_draft */ fs_path_to_utf8(draft_file) // can be empty + /* path */ path.string(), + /* path_mmproj */ mmproj_file.string(), // can be empty + /* path_draft */ draft_file.string() // can be empty }); } }; @@ -432,13 +432,13 @@ common_presets common_preset_context::load_from_models_dir(const std::string & m for (const auto & entry : std::filesystem::directory_iterator(dir)) { std::error_code ec; if (entry.is_directory(ec)) { - scan_subdir(entry.path(), fs_path_to_utf8(entry.path().filename())); + scan_subdir(entry.path(), entry.path().filename().string()); continue; } if (!entry.is_regular_file(ec)) { continue; } - const std::string fname = fs_path_to_utf8(entry.path().filename()); + const std::string fname = entry.path().filename().string(); if (!string_ends_with(fname, ".gguf")) { continue; } @@ -450,7 +450,7 @@ common_presets common_preset_context::load_from_models_dir(const std::string & m string_replace_all(name, ".gguf", ""); models.push_back({ /* name */ name, - /* path */ fs_path_to_utf8(entry.path()), + /* path */ entry.path().string(), /* path_mmproj */ "", /* path_draft */ "" }); diff --git a/tests/gguf-model-data.cpp b/tests/gguf-model-data.cpp index 3e2147dd2..99acada22 100644 --- a/tests/gguf-model-data.cpp +++ b/tests/gguf-model-data.cpp @@ -584,7 +584,7 @@ static std::optional fetch_or_cached( auto result = gguf_parse_meta(cached); if (result.has_value()) { if (verbose) { - fprintf(stderr, "gguf_fetch: loaded from cache: %s\n", fs_path_to_utf8(cache_path).c_str()); + fprintf(stderr, "gguf_fetch: loaded from cache: %s\n", cache_path.string().c_str()); } return result; } @@ -685,7 +685,7 @@ gguf_context_ptr gguf_fetch_gguf_ctx( ggml_context_ptr ggml_ctx_ptr; ggml_context * ggml_ctx{}; gguf_init_params params{true, &ggml_ctx}; - gguf_context_ptr ctx{gguf_init_from_file(fs_path_to_utf8(cache_path).c_str(), params)}; + gguf_context_ptr ctx{gguf_init_from_file(cache_path.string().c_str(), params)}; ggml_ctx_ptr.reset(ggml_ctx); if (ctx == nullptr) { @@ -723,7 +723,7 @@ gguf_context_ptr gguf_fetch_gguf_ctx( ggml_context_ptr shard_ggml_ctx_ptr; ggml_context * shard_ggml_ctx{}; gguf_init_params shard_params{true, &shard_ggml_ctx}; - gguf_context_ptr shard_ctx{gguf_init_from_file(fs_path_to_utf8(shard_path).c_str(), shard_params)}; + gguf_context_ptr shard_ctx{gguf_init_from_file(shard_path.string().c_str(), shard_params)}; shard_ggml_ctx_ptr.reset(shard_ggml_ctx); if (shard_ctx == nullptr) { diff --git a/tools/server/server-models.cpp b/tools/server/server-models.cpp index 9f583b738..a18488715 100644 --- a/tools/server/server-models.cpp +++ b/tools/server/server-models.cpp @@ -408,7 +408,7 @@ server_models::server_models( // set binary path try { - bin_path = fs_path_to_utf8(get_server_exec_path()); + bin_path = get_server_exec_path().string(); } catch (const std::exception & e) { bin_path = argv[0]; SRV_WRN("failed to get server executable path: %s\n", e.what());