change file search to return on first match

This commit is contained in:
Nikita Smith
2025-09-09 18:59:07 -07:00
committed by Ryan Fleury
parent 7095a8cd6e
commit 488849792c
4 changed files with 26 additions and 79 deletions
+4 -12
View File
@@ -1083,10 +1083,10 @@ lnk_inputer_push_lib_thin(LNK_Inputer *inputer, LNK_Config *config, LNK_InputSou
}
// search disk for library
String8List matches = lnk_file_search(scratch.arena, config->lib_dir_list, path);
String8 first_match = lnk_find_first_file(scratch.arena, config->lib_dir_list, path);
// warn about missing library
if (matches.node_count == 0) {
if (first_match.size == 0) {
KeyValuePair *was_reported = hash_table_search_path(inputer->missing_lib_ht, path);
if (was_reported == 0) {
hash_table_push_path_u64(inputer->arena, inputer->missing_lib_ht, path, 0);
@@ -1095,18 +1095,12 @@ lnk_inputer_push_lib_thin(LNK_Inputer *inputer, LNK_Config *config, LNK_InputSou
goto exit;
}
String8 first_match = matches.first->string;
// was input with full path already loaded?
input = hash_table_search_path_raw(inputer->libs_ht, first_match);
if (input) {
goto exit;
}
// warn about multiple matches
if (matches.node_count > 1) {
lnk_error(LNK_Warning_MultipleLibMatch, "multiple libraries match `%S` (picking first match)", path);
lnk_supplement_error_list(matches);
}
lnk_log(LNK_Log_InputLib, "Input Lib: %S", first_match);
input = lnk_inputer_push_thin(inputer->arena, &inputer->new_libs[input_source], inputer->libs_ht, first_match);
@@ -2878,7 +2872,7 @@ THREAD_POOL_TASK_FUNC(lnk_obj_reloc_patcher)
S64 symbol_voff = 0;
{
COFF_ParsedSymbol symbol = lnk_parsed_symbol_from_coff_symbol_idx(obj, reloc->isymbol);
COFF_SymbolValueInterpType interp = coff_interp_symbol(symbol.section_number, symbol.value, symbol.storage_class);
COFF_SymbolValueInterpType interp = coff_interp_from_parsed_symbol(symbol);
if (interp == COFF_SymbolValueInterp_Regular) {
if (symbol.section_number == lnk_obj_get_removed_section_number(obj)) {
if (!lnk_is_coff_section_debug(obj, sect_idx)) {
@@ -2887,7 +2881,6 @@ THREAD_POOL_TASK_FUNC(lnk_obj_reloc_patcher)
}
continue;
}
symbol_secnum = symbol.section_number;
symbol_secoff = symbol.value;
symbol_voff = safe_cast_u32((U64)task->image_section_table[symbol.section_number]->voff + (U64)symbol_secoff);
@@ -2898,7 +2891,6 @@ THREAD_POOL_TASK_FUNC(lnk_obj_reloc_patcher)
if (str8_match(symbol.name, str8_lit("__ImageBase"), 0)) {
symbol.value = task->image_base;
}
symbol_secnum = 0;
symbol_secoff = 0;
symbol_voff = (S64)symbol.value - (S64)task->image_base;
+7 -26
View File
@@ -549,29 +549,11 @@ lnk_make_code_view_input(TP_Context *tp, TP_Arena *tp_arena, LNK_IO_Flags io_fla
CV_TypeServerInfo ts = cv_type_server_info_from_leaf(leaf);
// search disk for type server
String8List match_list = lnk_file_search(scratch.arena, lib_dir_list, ts.name);
// chop file name from path and search on it
//
// TODO: check if ts.name is a path and in that case do file search
if (match_list.node_count == 0) {
String8 file_name = str8_skip_last_slash(ts.name);
match_list = lnk_file_search(scratch.arena, lib_dir_list, file_name);
}
String8 type_server_path = lnk_find_first_file(scratch.arena, lib_dir_list, ts.name);
// report no match
B32 do_debug_info_discard = 0;
// too many matches?
if (match_list.node_count > 1) {
if (!hash_table_search_path(ignored_path_ht, ts.name)) {
hash_table_push_path_u64(scratch.arena, ignored_path_ht, ts.name, 0);
lnk_error_obj(LNK_Warning_MultipleExternalTypeServers, obj_arr[obj_idx], "located multiple external type servers:");
lnk_supplement_error_list(match_list);
}
do_debug_info_discard = 1;
}
// no match?
else if (match_list.node_count == 0) {
if (type_server_path.size == 0) {
if (!hash_table_search_path(ignored_path_ht, ts.name)) {
hash_table_push_string_u64(scratch.arena, ignored_path_ht, ts.name, 0);
lnk_error_obj(LNK_Warning_MissingExternalTypeServer, obj_arr[obj_idx], "unable to open external type server %S", ts.name);
@@ -585,7 +567,6 @@ lnk_make_code_view_input(TP_Context *tp, TP_Arena *tp_arena, LNK_IO_Flags io_fla
continue;
}
String8 path = match_list.first->string;
{
struct HT_Value {
CV_TypeServerInfo ts;
@@ -594,7 +575,7 @@ lnk_make_code_view_input(TP_Context *tp, TP_Arena *tp_arena, LNK_IO_Flags io_fla
};
// was this type server queued?
KeyValuePair *is_path_queued = hash_table_search_path(type_server_path_ht, path);
KeyValuePair *is_path_queued = hash_table_search_path(type_server_path_ht, type_server_path);
if (is_path_queued) {
struct HT_Value *present = is_path_queued->value_raw;
@@ -616,12 +597,12 @@ lnk_make_code_view_input(TP_Context *tp, TP_Arena *tp_arena, LNK_IO_Flags io_fla
// when we search matches on disk we store path on scratch,
// make path copy in case we need it for error reporting
path = push_str8_copy(tp_arena->v[0], path);
type_server_path = push_str8_copy(tp_arena->v[0], type_server_path);
// fill out type server info we read from obj
CV_TypeServerInfoNode *ts_info_node = push_array(scratch.arena, CV_TypeServerInfoNode, 1);
ts_info_node->data = ts;
ts_info_node->data.name = path;
ts_info_node->data.name = type_server_path;
// push to type server info list
SLLQueuePush(ts_info_list.first, ts_info_list.last, ts_info_node);
@@ -640,7 +621,7 @@ lnk_make_code_view_input(TP_Context *tp, TP_Arena *tp_arena, LNK_IO_Flags io_fla
value->ts_idx = ts_idx;
// update hash table
hash_table_push_path_raw(scratch.arena, type_server_path_ht, path, value);
hash_table_push_path_raw(scratch.arena, type_server_path_ht, type_server_path, value);
}
}
}
+15 -39
View File
@@ -48,51 +48,27 @@ lnk_write_file(void *raw_handle, uint64_t offset, void *buffer, uint64_t buffer_
return write_size;
}
internal String8List
lnk_file_search(Arena *arena, String8List dir_list, String8 file_path)
internal String8
lnk_find_first_file(Arena *arena, String8List dir_list, String8 path)
{
ProfBeginFunction();
Temp scratch = scratch_begin(&arena, 1);
String8List match_list; MemoryZeroStruct(&match_list);
if (os_file_path_exists(file_path)) {
String8 str = push_str8_copy(arena, file_path);
str8_list_push(arena, &match_list, str);
}
PathStyle file_path_style = path_style_from_str8(file_path);
B32 is_relative = file_path_style != PathStyle_WindowsAbsolute &&
file_path_style != PathStyle_UnixAbsolute;
if (is_relative) {
for (String8Node *i = dir_list.first; i != 0; i = i->next) {
String8List path_list = {0};
str8_list_push(scratch.arena, &path_list, i->string);
str8_list_push(scratch.arena, &path_list, file_path);
String8 path = str8_path_list_join_by_style(scratch.arena, &path_list, PathStyle_SystemAbsolute);
B32 file_exists = os_file_path_exists(path);
if (file_exists) {
B32 is_unique = 1;
OS_FileID file_id = os_id_from_file_path(path);
for (String8Node *k = match_list.first; k != 0; k = k->next) {
OS_FileID test_id = os_id_from_file_path(k->string);
int cmp = os_file_id_compare(test_id, file_id) != 0;
if (cmp == 0) {
is_unique = 0;
break;
}
}
if (is_unique) {
String8 str = push_str8_copy(arena, path);
str8_list_push(arena, &match_list, str);
}
String8 result = {0};
if (os_file_path_exists(path)) {
result = path;
} else {
Temp scratch = scratch_begin(&arena, 1);
String8 file_name = str8_skip_last_slash(path);
for EachNode(n, String8Node, dir_list.first) {
String8 full_path = push_str8f(scratch.arena, "%S/%S", n->string, file_name);
if (os_file_path_exists(full_path)) {
result = push_str8_copy(arena, full_path);
break;
}
}
scratch_end(scratch);
}
scratch_end(scratch);
ProfEnd();
return match_list;
return result;
}
internal OS_Handle
-2
View File
@@ -28,8 +28,6 @@ shared_function uint64_t lnk_write_file(void *raw_handle, uint64_t offset, void
// --- IO Functions ------------------------------------------------------------
internal String8List lnk_file_search(Arena *arena, String8List dir_list, String8 file_path);
internal OS_Handle lnk_file_open_with_rename_permissions(String8 path);
internal B32 lnk_file_set_delete_on_close(OS_Handle handle, B32 delete_file);
internal B32 lnk_file_rename(OS_Handle handle, String8 new_name);