]> source.dussan.org Git - rspamd.git/commitdiff
[Minor] Add function to check file access from Lua
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Thu, 9 Nov 2017 07:51:11 +0000 (07:51 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Thu, 9 Nov 2017 07:51:11 +0000 (07:51 +0000)
src/lua/lua_util.c

index 8af61d6d55ff228a143cb4f373f6f52c9ba1bafa..6ca9f35abed37def6cd21efa7d735050f2d585e0 100644 (file)
@@ -382,6 +382,14 @@ LUA_FUNCTION_DEF (util, is_valid_utf8);
  */
 LUA_FUNCTION_DEF (util, readline);
 
+/***
+ * @function util.file_exists(file)
+ * Checks if a specified file exists and is available for reading
+ * @return {boolean} true if file exists
+ */
+LUA_FUNCTION_DEF (util, file_exists);
+
+
 /***
  * @function util.pack(fmt, ...)
  *
@@ -525,6 +533,7 @@ static const struct luaL_reg utillib_f[] = {
        LUA_INTERFACE_DEF (util, is_utf_spoofed),
        LUA_INTERFACE_DEF (util, is_valid_utf8),
        LUA_INTERFACE_DEF (util, readline),
+       LUA_INTERFACE_DEF (util, file_exists),
        LUA_INTERFACE_DEF (util, get_hostname),
        LUA_INTERFACE_DEF (util, pack),
        LUA_INTERFACE_DEF (util, unpack),
@@ -2084,6 +2093,21 @@ lua_util_readline (lua_State *L)
        return 1;
 }
 
+static gint
+lua_util_file_exists (lua_State *L)
+{
+       const gchar *fname = luaL_checkstring (L, 1);
+
+       if (fname) {
+               lua_pushboolean (L, access (fname, R_OK) != -1);
+       }
+       else {
+               return luaL_error (L, "invalid arguments");
+       }
+
+       return 1;
+}
+
 /* Backport from Lua 5.3 */
 
 /******************************************************************************