Skip to content

Commit 2578ea0

Browse files
committed
Add previous check for ext_loader in order to detect before loading dynlink if the library exists.
1 parent d2cacbf commit 2578ea0

File tree

3 files changed

+44
-8
lines changed

3 files changed

+44
-8
lines changed

source/dynlink/include/dynlink/dynlink.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,18 @@ DYNLINK_API void dynlink_unload(dynlink handle);
145145
*/
146146
DYNLINK_API int dynlink_library_path(dynlink_name name, dynlink_library_path_str path, size_t *length);
147147

148+
/**
149+
* @brief
150+
* Retrieve the library platform standard name by using @name as a base for it
151+
*
152+
* @param[in] name
153+
* Name of the library that will be used for generating the platform dependant library name (i.e example)
154+
*
155+
* @param[out] result
156+
* The resulting library name that will be generated (i.e libexample.so in Linux, or example.dll in Windows)
157+
*/
158+
DYNLINK_API void dynlink_platform_name(dynlink_name name, dynlink_name_impl result);
159+
148160
/**
149161
* @brief
150162
* Provide the module information

source/dynlink/source/dynlink.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,11 @@ int dynlink_library_path(dynlink_name name, dynlink_library_path_str path, size_
163163
return 0;
164164
}
165165

166+
void dynlink_platform_name(dynlink_name name, dynlink_name_impl result)
167+
{
168+
dynlink_impl_get_name(name, result, PORTABILITY_PATH_SIZE);
169+
}
170+
166171
const char *dynlink_print_info(void)
167172
{
168173
static const char dynlink_info[] =

source/loaders/ext_loader/source/ext_loader_impl.cpp

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
#include <reflect/reflect_scope.h>
3131
#include <reflect/reflect_type.h>
3232

33+
#include <dynlink/dynlink.h>
34+
3335
#include <log/log.h>
3436

3537
#include <filesystem>
@@ -65,8 +67,9 @@ union loader_impl_function_cast
6567
int (*fn)(void *, void *, void *);
6668
};
6769

68-
dynlink ext_loader_impl_load_from_file_dynlink(loader_impl_ext ext_impl, const loader_path path);
69-
int ext_loader_impl_load_from_file_handle(loader_impl_ext ext_impl, loader_impl_ext_handle ext_handle, const loader_path path);
70+
static dynlink ext_loader_impl_load_from_file_dynlink(const char *path, const char *library_name);
71+
static dynlink ext_loader_impl_load_from_file_dynlink(loader_impl_ext ext_impl, const loader_path path);
72+
static int ext_loader_impl_load_from_file_handle(loader_impl_ext ext_impl, loader_impl_ext_handle ext_handle, const loader_path path);
7073
static void ext_loader_impl_destroy_handle(loader_impl_ext_handle ext_handle);
7174

7275
int ext_loader_impl_initialize_types(loader_impl impl)
@@ -122,6 +125,26 @@ int ext_loader_impl_execution_path(loader_impl impl, const loader_path path)
122125
return 0;
123126
}
124127

128+
dynlink ext_loader_impl_load_from_file_dynlink(const char *path, const char *library_name)
129+
{
130+
/* This function will try to check if the library exists before loading it,
131+
so we avoid error messages from dynlink when guessing the file path for relative load from file */
132+
dynlink_name_impl platform_name;
133+
134+
dynlink_platform_name(library_name, platform_name);
135+
136+
std::filesystem::path lib_path(path);
137+
138+
lib_path /= platform_name;
139+
140+
if (std::filesystem::exists(lib_path) == false)
141+
{
142+
return NULL;
143+
}
144+
145+
return dynlink_load(path, library_name, DYNLINK_FLAGS_BIND_LAZY | DYNLINK_FLAGS_BIND_GLOBAL);
146+
}
147+
125148
dynlink ext_loader_impl_load_from_file_dynlink(loader_impl_ext ext_impl, const loader_path path)
126149
{
127150
std::string lib_path_str(path);
@@ -136,18 +159,14 @@ dynlink ext_loader_impl_load_from_file_dynlink(loader_impl_ext ext_impl, const l
136159
if (lib_path.is_absolute())
137160
{
138161
std::filesystem::path lib_dir = lib_path.parent_path();
139-
dynlink lib = dynlink_load(lib_dir.string().c_str(), lib_name.c_str(), DYNLINK_FLAGS_BIND_LAZY | DYNLINK_FLAGS_BIND_GLOBAL);
140162

141-
if (lib != NULL)
142-
{
143-
return lib;
144-
}
163+
return ext_loader_impl_load_from_file_dynlink(lib_dir.string().c_str(), lib_name.c_str());
145164
}
146165
else
147166
{
148167
for (auto exec_path : ext_impl->paths)
149168
{
150-
dynlink lib = dynlink_load(exec_path.string().c_str(), lib_name.c_str(), DYNLINK_FLAGS_BIND_LAZY | DYNLINK_FLAGS_BIND_GLOBAL);
169+
dynlink lib = ext_loader_impl_load_from_file_dynlink(exec_path.string().c_str(), lib_name.c_str());
151170

152171
if (lib != NULL)
153172
{

0 commit comments

Comments
 (0)