@@ -92,33 +92,17 @@ int RAI_ExportFunc(const char *func_name, void **targetFuncPtr) {
92
92
return REDISMODULE_OK ;
93
93
}
94
94
95
- RedisModuleString * RAI_GetModulePath (RedisModuleCtx * ctx ) {
95
+ void RAI_SetBackendsDefaultPath (char * * backends_path ) {
96
+ RedisModule_Assert (* backends_path == NULL );
96
97
Dl_info info ;
97
- RedisModuleString * module_path = NULL ;
98
- if (dladdr (RAI_GetModulePath , & info )) {
99
- char * dli_fname = RedisModule_Strdup (info .dli_fname );
100
- const char * dli_dirname = dirname (dli_fname );
101
- module_path = RedisModule_CreateString (ctx , dli_dirname , strlen (dli_dirname ));
102
- RedisModule_Free (dli_fname );
103
- }
104
-
105
- return module_path ;
106
- }
107
-
108
- RedisModuleString * RAI_GetBackendsPath (RedisModuleCtx * ctx ) {
109
- Dl_info info ;
110
- RedisModuleString * backends_path = NULL ;
111
- if (Config_GetBackendsPath () != NULL ) {
112
- backends_path = RedisModule_CreateString (ctx , Config_GetBackendsPath (),
113
- strlen (Config_GetBackendsPath ()));
114
- } else {
115
- RedisModuleString * module_path = RAI_GetModulePath (ctx );
116
- backends_path = RedisModule_CreateStringPrintf (ctx , "%s/backends" ,
117
- RedisModule_StringPtrLen (module_path , NULL ));
118
- RedisModule_FreeString (ctx , module_path );
119
- }
120
-
121
- return backends_path ;
98
+ // Retrieve the info about the module's dynamic library, and extract the .so file dir name.
99
+ RedisModule_Assert (dladdr (RAI_SetBackendsDefaultPath , & info ) != 0 );
100
+ const char * dyn_lib_dir_name = dirname ((char * )info .dli_fname );
101
+
102
+ // Populate backends_path global string with the default path.
103
+ size_t backends_default_path_len = strlen (dyn_lib_dir_name ) + strlen ("/backends" );
104
+ * backends_path = RedisModule_Alloc (backends_default_path_len + 1 );
105
+ RedisModule_Assert (sprintf (* backends_path , "%s/backends" , dyn_lib_dir_name ) > 0 );
122
106
}
123
107
124
108
const char * RAI_GetBackendName (RAI_Backend backend ) {
@@ -460,10 +444,8 @@ int RAI_LoadBackend(RedisModuleCtx *ctx, int backend, const char *path) {
460
444
if (path [0 ] == '/' ) {
461
445
fullpath = RedisModule_CreateString (ctx , path , strlen (path ));
462
446
} else {
463
- RedisModuleString * backends_path = RAI_GetBackendsPath (ctx );
464
- fullpath = RedisModule_CreateStringPrintf (
465
- ctx , "%s/%s" , RedisModule_StringPtrLen (backends_path , NULL ), path );
466
- RedisModule_FreeString (ctx , backends_path );
447
+ const char * backends_path = Config_GetBackendsPath ();
448
+ fullpath = RedisModule_CreateStringPrintf (ctx , "%s/%s" , backends_path , path );
467
449
}
468
450
469
451
int ret ;
0 commit comments