]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
Add util.glob routine to lua API
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Wed, 30 Dec 2015 14:31:28 +0000 (14:31 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Wed, 30 Dec 2015 14:31:28 +0000 (14:31 +0000)
src/lua/lua_util.c

index 3cae28211a6a9b7ecf66bd665231523ac1a986b9..a5d3a0f87a74c6d528a8df1e6716f34c39bc97aa 100644 (file)
@@ -29,6 +29,7 @@
 #include "tokenizers/tokenizers.h"
 #include "libserver/url.h"
 #include <math.h>
+#include <glob.h>
 
 /***
  * @module rspamd_util
@@ -152,6 +153,15 @@ LUA_FUNCTION_DEF (util, humanize_number);
  */
 LUA_FUNCTION_DEF (util, get_tld);
 
+/**
+ * @function util.glob(pattern)
+ * Returns results for the glob match for the specified pattern
+ *
+ * @param {string} pattern glob pattern to match ('?' and '*' are supported)
+ * @return {table/string} list of matched files
+ */
+LUA_FUNCTION_DEF (util, glob);
+
 static const struct luaL_reg utillib_f[] = {
        LUA_INTERFACE_DEF (util, create_event_base),
        LUA_INTERFACE_DEF (util, load_rspamd_config),
@@ -168,6 +178,7 @@ static const struct luaL_reg utillib_f[] = {
        LUA_INTERFACE_DEF (util, is_uppercase),
        LUA_INTERFACE_DEF (util, humanize_number),
        LUA_INTERFACE_DEF (util, get_tld),
+       LUA_INTERFACE_DEF (util, glob),
        {NULL, NULL}
 };
 
@@ -736,6 +747,38 @@ lua_util_get_tld (lua_State *L)
        return 1;
 }
 
+
+static gint
+lua_util_glob (lua_State *L)
+{
+       const gchar *pattern;
+       glob_t gl;
+       gint top, i, flags;
+
+       top = lua_gettop (L);
+       memset (&gl, 0, sizeof (gl));
+       flags = GLOB_NOSORT;
+
+       for (i = 1; i <= top; i ++, flags |= GLOB_APPEND) {
+               pattern = luaL_checkstring (L, i);
+
+               if (pattern) {
+                       glob (pattern, flags, NULL, &gl);
+               }
+       }
+
+       lua_newtable (L);
+       /* Push results */
+       for (i = 0; i < (gint)gl.gl_pathc; i ++) {
+               lua_pushstring (L, gl.gl_pathv[i]);
+               lua_rawseti (L, -2, i + 1);
+       }
+
+       globfree (&gl);
+
+       return 1;
+}
+
 static gint
 lua_load_util (lua_State * L)
 {