]> source.dussan.org Git - rspamd.git/commitdiff
[Minor] Sort lua modules and use GPtrArray instead of GList
authorVsevolod Stakhov <vsevolod@rspamd.com>
Thu, 7 Sep 2023 15:12:44 +0000 (16:12 +0100)
committerVsevolod Stakhov <vsevolod@rspamd.com>
Thu, 7 Sep 2023 15:12:44 +0000 (16:12 +0100)
src/libserver/cfg_file.h
src/libserver/cfg_rcl.cxx
src/libserver/cfg_utils.cxx
src/lua/lua_common.c

index 1415fd8659994ad891c7dc0a5d761da84a9ae75f..4e432af608b12339bb85dcfa6a206795659051f6 100644 (file)
@@ -390,7 +390,7 @@ struct rspamd_config {
 
        gboolean compat_messages; /**< use old messages in the protocol (array)                         */
 
-       GList *script_modules;        /**< linked list of script modules to load                                */
+       GPtrArray *script_modules;    /**< a list of script modules to load                             */
        GHashTable *explicit_modules; /**< modules that should be always loaded                         */
 
        GList *filters;                                  /**< linked list of all filters                                                        */
index 26b799cdf83df554ef122b2d23d16d9ac2610517..e7828ff90f9973173d97d9fe09fc0de1228d671c 100644 (file)
@@ -920,6 +920,15 @@ rspamd_rcl_lua_handler(rspamd_mempool_t *pool, const ucl_object_t *obj,
        return TRUE;
 }
 
+static int
+rspamd_lua_mod_sort_fn(gconstpointer a, gconstpointer b)
+{
+       auto *m1 = *(const script_module **) a;
+       auto *m2 = *(const script_module **) b;
+
+       return strcmp(m1->name, m2->name);
+}
+
 gboolean
 rspamd_rcl_add_lua_plugins_path(struct rspamd_rcl_sections_map *sections,
                                                                struct rspamd_config *cfg,
@@ -949,18 +958,7 @@ rspamd_rcl_add_lua_plugins_path(struct rspamd_rcl_sections_map *sections,
                        return false;
                }
 
-               if (cfg->script_modules == nullptr) {
-                       cfg->script_modules = g_list_append(cfg->script_modules,
-                                                                                               cur_mod);
-                       rspamd_mempool_add_destructor(cfg->cfg_pool,
-                                                                                 (rspamd_mempool_destruct_t) g_list_free,
-                                                                                 cfg->script_modules);
-               }
-               else {
-                       cfg->script_modules = g_list_append(cfg->script_modules,
-                                                                                               cur_mod);
-               }
-
+               g_ptr_array_add(cfg->script_modules, cur_mod);
                sections->lua_modules_seen.insert(fname.string());
 
                return true;
@@ -994,6 +992,8 @@ rspamd_rcl_add_lua_plugins_path(struct rspamd_rcl_sections_map *sections,
                }
        }
 
+       g_ptr_array_sort(cfg->script_modules, rspamd_lua_mod_sort_fn);
+
        return TRUE;
 }
 
index 12074a83a3374166d8abb931f6ba754b8c753855..416d52e056b94a05358dca4bb9103f03cbc82d98 100644 (file)
@@ -339,6 +339,7 @@ rspamd_config_new(enum rspamd_config_init_flags flags)
        cfg->heartbeat_interval = 10.0;
 
        cfg->enable_css_parser = true;
+       cfg->script_modules = g_ptr_array_new();
 
        REF_INIT_RETAIN(cfg, rspamd_config_free);
 
@@ -398,6 +399,7 @@ void rspamd_config_free(struct rspamd_config *cfg)
 
        rspamd_re_cache_unref(cfg->re_cache);
        g_ptr_array_free(cfg->c_modules, TRUE);
+       g_ptr_array_free(cfg->script_modules, TRUE);
 
        if (cfg->monitored_ctx) {
                rspamd_monitored_ctx_destroy(cfg->monitored_ctx);
index 5991af65663b8f24f8b5257d8b2305b5e41957f7..bafe8ea93f752847b3f605e7d4eec8f4f7b8d747 100644 (file)
@@ -1109,25 +1109,20 @@ gboolean
 rspamd_init_lua_filters(struct rspamd_config *cfg, bool force_load, bool strict)
 {
        struct rspamd_config **pcfg;
-       GList *cur;
        struct script_module *module;
        lua_State *L = cfg->lua_state;
-       gint err_idx;
+       gint err_idx, i;
 
        pcfg = lua_newuserdata(L, sizeof(struct rspamd_config *));
        rspamd_lua_setclass(L, "rspamd{config}", -1);
        *pcfg = cfg;
        lua_setglobal(L, "rspamd_config");
 
-       cur = g_list_first(cfg->script_modules);
-
-       while (cur) {
-               module = cur->data;
-
+       PTR_ARRAY_FOREACH(cfg->script_modules, i, module)
+       {
                if (module->path) {
                        if (!force_load) {
                                if (!rspamd_config_is_module_enabled(cfg, module->name)) {
-                                       cur = g_list_next(cur);
                                        continue;
                                }
                        }
@@ -1154,7 +1149,6 @@ rspamd_init_lua_filters(struct rspamd_config *cfg, bool force_load, bool strict)
                                        return FALSE;
                                }
 
-                               cur = g_list_next(cur);
                                continue;
                        }
 
@@ -1182,7 +1176,6 @@ rspamd_init_lua_filters(struct rspamd_config *cfg, bool force_load, bool strict)
                                        return FALSE;
                                }
 
-                               cur = g_list_next(cur);
                                continue;
                        }
 
@@ -1202,7 +1195,6 @@ rspamd_init_lua_filters(struct rspamd_config *cfg, bool force_load, bool strict)
                                        return FALSE;
                                }
 
-                               cur = g_list_next(cur);
                                continue;
                        }
 
@@ -1215,8 +1207,6 @@ rspamd_init_lua_filters(struct rspamd_config *cfg, bool force_load, bool strict)
 
                        lua_pop(L, 1); /* Error function */
                }
-
-               cur = g_list_next(cur);
        }
 
        return TRUE;