From: Vsevolod Stakhov Date: Thu, 9 Nov 2017 07:51:11 +0000 (+0000) Subject: [Minor] Add function to check file access from Lua X-Git-Tag: 1.7.0~458 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=aabec91dfb30a2deb71bd6fc8a60760c421e0c10;p=thirdparty%2Frspamd.git [Minor] Add function to check file access from Lua --- diff --git a/src/lua/lua_util.c b/src/lua/lua_util.c index 8af61d6d55..6ca9f35abe 100644 --- a/src/lua/lua_util.c +++ b/src/lua/lua_util.c @@ -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 */ /******************************************************************************