]> source.dussan.org Git - rspamd.git/commitdiff
[Minor] Use new routine for globbing
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Sat, 26 May 2018 13:53:18 +0000 (14:53 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Sat, 26 May 2018 13:53:18 +0000 (14:53 +0100)
src/libserver/cfg_rcl.c
src/libutil/util.c

index deb4a8ed36999797979b56e95c66be7d06e78aa0..62f9e3beff19b0315a8fbfa604f0119f7759f1fc 100644 (file)
 #include <syslog.h>
 #endif
 
-#ifdef HAVE_GLOB_H
-#include <glob.h>
-#endif
-
 #include <math.h>
 
 struct rspamd_rcl_default_handler_data {
@@ -1047,9 +1043,8 @@ rspamd_rcl_add_lua_plugins_path (struct rspamd_config *cfg,
 {
        struct stat st;
        struct script_module *cur_mod;
-       glob_t globbuf;
-       gchar *pattern, *ext_pos;
-       size_t len;
+       GPtrArray *paths;
+       gchar *fname, *ext_pos;
        guint i;
 
        if (stat (path, &st) == -1) {
@@ -1064,52 +1059,39 @@ rspamd_rcl_add_lua_plugins_path (struct rspamd_config *cfg,
 
        /* Handle directory */
        if (S_ISDIR (st.st_mode)) {
-               globbuf.gl_offs = 0;
-               len = strlen (path) + sizeof ("*.lua");
-               pattern = g_malloc (len);
-               rspamd_snprintf (pattern, len, "%s%s", path, "*.lua");
-
-               if (glob (pattern, GLOB_DOOFFS, NULL, &globbuf) == 0) {
-                       for (i = 0; i < globbuf.gl_pathc; i++) {
-                               cur_mod =
+               paths = rspamd_glob_path (path, "*.lua", TRUE, err);
+
+               if (!paths) {
+                       return FALSE;
+               }
+
+               PTR_ARRAY_FOREACH (paths, i, fname) {
+                       cur_mod =
                                        rspamd_mempool_alloc (cfg->cfg_pool,
-                                               sizeof (struct script_module));
-                               cur_mod->path = rspamd_mempool_strdup (cfg->cfg_pool,
-                                               globbuf.gl_pathv[i]);
-                               cur_mod->name = g_path_get_basename (cur_mod->path);
-                               rspamd_mempool_add_destructor (cfg->cfg_pool, g_free,
-                                               cur_mod->name);
-                               ext_pos = strstr (cur_mod->name, ".lua");
-
-                               if (ext_pos != NULL) {
-                                       *ext_pos = '\0';
-                               }
+                                                       sizeof (struct script_module));
+                       cur_mod->path = rspamd_mempool_strdup (cfg->cfg_pool, fname);
+                       cur_mod->name = g_path_get_basename (cur_mod->path);
+                       rspamd_mempool_add_destructor (cfg->cfg_pool, g_free,
+                                       cur_mod->name);
+                       ext_pos = strstr (cur_mod->name, ".lua");
+
+                       if (ext_pos != NULL) {
+                               *ext_pos = '\0';
+                       }
 
-                               if (cfg->script_modules == NULL) {
-                                       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);
-                               }
+                       if (cfg->script_modules == NULL) {
+                               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);
                        }
-                       globfree (&globbuf);
-                       g_free (pattern);
-               }
-               else {
-                       g_set_error (err,
-                               CFG_RCL_ERROR,
-                               errno,
-                               "glob failed for %s, %s",
-                               pattern,
-                               strerror (errno));
-                       g_free (pattern);
-                       return FALSE;
                }
+
+               g_ptr_array_free (paths, TRUE);
        }
        else {
                /* Handle single file */
index 0b76f193b0c48f847e47380b2323a9a60976bdf6..510b16045b20bf08b662f85e0253da0f96f4430d 100644 (file)
@@ -80,7 +80,7 @@
 #endif
 #endif
 #include <math.h> /* for pow */
-#include <glob.h>
+#include <glob.h> /* in fact, we require this file ultimately */
 
 #include "cryptobox.h"
 #include "zlib.h"
@@ -2858,6 +2858,7 @@ rspamd_glob_dir (const gchar *full_path, const gchar *pattern,
        const gchar *path;
        static gchar pathbuf[PATH_MAX]; /* Static to help recursion */
        guint i;
+       gint rc;
        static const guint rec_lim = 16;
        struct stat st;
 
@@ -2870,12 +2871,20 @@ rspamd_glob_dir (const gchar *full_path, const gchar *pattern,
 
        memset (&globbuf, 0, sizeof (globbuf));
 
-       if (glob (full_path, 0, NULL, &globbuf) != 0) {
-               g_set_error (err, g_quark_from_static_string ("glob"), errno,
-                               "glob %s failed: %s", full_path, strerror (errno));
-               globfree (&globbuf);
+       if ((rc = glob (full_path, 0, NULL, &globbuf)) != 0) {
 
-               return FALSE;
+               if (rc != GLOB_NOMATCH) {
+                       g_set_error (err, g_quark_from_static_string ("glob"), errno,
+                                       "glob %s failed: %s", full_path, strerror (errno));
+                       globfree (&globbuf);
+
+                       return FALSE;
+               }
+               else {
+                       globfree (&globbuf);
+
+                       return TRUE;
+               }
        }
 
        for (i = 0; i < globbuf.gl_pathc; i ++) {