Skip to content

Commit 7b43f55

Browse files
ggml : improve error handling for search path existence checks (ggml-org#17653)
* Improve error handling for search path existence checks Refactor existence checks for search paths using std::error_code to handle potential errors. * Improve cache file existence check with error code Update fs::exists to use std::error_code for error handling. * Simplify existence check for search paths Simplify existence check for search paths * Fix logging path in error message for posix_stat * Update ggml/src/ggml-backend-reg.cpp Co-authored-by: Aman Gupta <[email protected]> * Adapt to the coding standard --------- Co-authored-by: Aman Gupta <[email protected]>
1 parent 444f00b commit 7b43f55

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

ggml/src/ggml-backend-reg.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -534,8 +534,12 @@ static ggml_backend_reg_t ggml_backend_load_best(const char * name, bool silent,
534534
fs::path best_path;
535535

536536
for (const auto & search_path : search_paths) {
537-
if (!fs::exists(search_path)) {
538-
GGML_LOG_DEBUG("%s: search path %s does not exist\n", __func__, path_str(search_path).c_str());
537+
if (std::error_code ec; !fs::exists(search_path, ec)) {
538+
if (ec) {
539+
GGML_LOG_DEBUG("%s: posix_stat(%s) failure, error-message: %s\n", __func__, path_str(search_path).c_str(), ec.message().c_str());
540+
} else {
541+
GGML_LOG_DEBUG("%s: search path %s does not exist\n", __func__, path_str(search_path).c_str());
542+
}
539543
continue;
540544
}
541545
fs::directory_iterator dir_it(search_path, fs::directory_options::skip_permission_denied);
@@ -575,8 +579,12 @@ static ggml_backend_reg_t ggml_backend_load_best(const char * name, bool silent,
575579
for (const auto & search_path : search_paths) {
576580
fs::path filename = backend_filename_prefix().native() + name_path.native() + backend_filename_extension().native();
577581
fs::path path = search_path / filename;
578-
if (fs::exists(path)) {
582+
if (std::error_code ec; fs::exists(path, ec)) {
579583
return get_reg().load_backend(path, silent);
584+
} else {
585+
if (ec) {
586+
GGML_LOG_DEBUG("%s: posix_stat(%s) failure, error-message: %s\n", __func__, path_str(path).c_str(), ec.message().c_str());
587+
}
580588
}
581589
}
582590
return nullptr;

ggml/src/ggml-rpc/ggml-rpc.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1257,7 +1257,8 @@ bool rpc_server::get_cached_file(uint64_t hash, std::vector<uint8_t> & data) {
12571257
char hash_str[17];
12581258
snprintf(hash_str, sizeof(hash_str), "%016" PRIx64, hash);
12591259
fs::path cache_file = fs::path(cache_dir) / hash_str;
1260-
if (!fs::exists(cache_file)) {
1260+
std::error_code ec;
1261+
if (!fs::exists(cache_file, ec)) {
12611262
return false;
12621263
}
12631264
std::ifstream ifs(cache_file, std::ios::binary);

0 commit comments

Comments
 (0)