diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2017-11-09 07:51:11 +0000 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2017-11-09 07:51:11 +0000 |
commit | aabec91dfb30a2deb71bd6fc8a60760c421e0c10 (patch) | |
tree | dfb5ed090913ec9914a71edbc5fdb464fda2e074 /src/lua/lua_util.c | |
parent | 1544764919a1c3393d091795db5f73760b6eed28 (diff) | |
download | rspamd-aabec91dfb30a2deb71bd6fc8a60760c421e0c10.tar.gz rspamd-aabec91dfb30a2deb71bd6fc8a60760c421e0c10.zip |
[Minor] Add function to check file access from Lua
Diffstat (limited to 'src/lua/lua_util.c')
-rw-r--r-- | src/lua/lua_util.c | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/src/lua/lua_util.c b/src/lua/lua_util.c index 8af61d6d5..6ca9f35ab 100644 --- a/src/lua/lua_util.c +++ b/src/lua/lua_util.c @@ -383,6 +383,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, ...) * * Backport of Lua 5.3 `string.pack` function: @@ -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 */ /****************************************************************************** |