From 2b6680df33ad709f82b05f64ada70ec6d4193d74 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Wed, 22 Feb 2017 17:15:18 +0000 Subject: [PATCH] [Minor] Add ability to save rspamd_text in file --- src/lua/lua_task.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/lua/lua_task.c b/src/lua/lua_task.c index adbb36adb..dd8495d8a 100644 --- a/src/lua/lua_task.c +++ b/src/lua/lua_task.c @@ -843,12 +843,14 @@ static const struct luaL_reg archivelib_m[] = { LUA_FUNCTION_DEF (text, len); LUA_FUNCTION_DEF (text, str); LUA_FUNCTION_DEF (text, ptr); +LUA_FUNCTION_DEF (text, save_in_file); LUA_FUNCTION_DEF (text, gc); static const struct luaL_reg textlib_m[] = { LUA_INTERFACE_DEF (text, len), LUA_INTERFACE_DEF (text, str), LUA_INTERFACE_DEF (text, ptr), + LUA_INTERFACE_DEF (text, save_in_file), {"__len", lua_text_len}, {"__tostring", lua_text_str}, {"__gc", lua_text_gc}, @@ -3769,6 +3771,46 @@ lua_text_ptr (lua_State *L) return 1; } +static gint +lua_text_save_in_file (lua_State *L) +{ + struct rspamd_lua_text *t = lua_check_text (L, 1); + const gchar *fname = luaL_checkstring (L, 2); + guint mode = 00644; + gint fd; + + if (t != NULL && fname != NULL) { + if (lua_type (L, 3) == LUA_TNUMBER) { + mode = lua_tonumber (L, 3); + } + + fd = rspamd_file_xopen (fname, O_CREAT | O_WRONLY | O_EXCL, mode); + + if (fd == -1) { + lua_pushboolean (L, false); + lua_pushstring (L, strerror (errno)); + + return 2; + } + + if (write (fd, t->start, t->len) == -1) { + close (fd); + lua_pushboolean (L, false); + lua_pushstring (L, strerror (errno)); + + return 2; + } + + close (fd); + lua_pushboolean (L, true); + } + else { + return luaL_error (L, "invalid arguments"); + } + + return 1; +} + static gint lua_text_gc (lua_State *L) { -- 2.39.5