aboutsummaryrefslogtreecommitdiffstats
path: root/src/lua/lua_task.c
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2018-11-11 17:57:44 +0000
committerVsevolod Stakhov <vsevolod@highsecure.ru>2018-11-11 17:57:44 +0000
commit6f0dcceaf232ad9dbd1fab761defd67dd0b33aa1 (patch)
tree9638c0be228a1216e1bb31379452753e4fdb674d /src/lua/lua_task.c
parent41cea9563e310ac85b148b2ade712e104290e573 (diff)
downloadrspamd-6f0dcceaf232ad9dbd1fab761defd67dd0b33aa1.tar.gz
rspamd-6f0dcceaf232ad9dbd1fab761defd67dd0b33aa1.zip
[Minor] Allow to write rspamd_text directly to fd
Diffstat (limited to 'src/lua/lua_task.c')
-rw-r--r--src/lua/lua_task.c35
1 files changed, 22 insertions, 13 deletions
diff --git a/src/lua/lua_task.c b/src/lua/lua_task.c
index 125e488b6..b8ac864df 100644
--- a/src/lua/lua_task.c
+++ b/src/lua/lua_task.c
@@ -5334,28 +5334,37 @@ lua_text_save_in_file (lua_State *L)
struct rspamd_lua_text *t = lua_check_text (L, 1);
const gchar *fname = NULL;
guint mode = 00644;
- gint fd;
+ gint fd = -1;
+ gboolean need_close = FALSE;
if (t != NULL) {
if (lua_type (L, 2) == LUA_TSTRING) {
fname = luaL_checkstring (L, 2);
+
+ if (lua_type (L, 3) == LUA_TNUMBER) {
+ mode = lua_tonumber (L, 3);
+ }
}
- if (lua_type (L, 3) == LUA_TNUMBER) {
- mode = lua_tonumber (L, 3);
+ else if (lua_type (L, 2) == LUA_TNUMBER) {
+ /* Created fd */
+ fd = lua_tonumber (L, 2);
}
- if (fname) {
- fd = rspamd_file_xopen (fname, O_CREAT | O_WRONLY | O_EXCL, mode, 0);
+ if (fd == -1) {
+ if (fname) {
+ fd = rspamd_file_xopen (fname, O_CREAT | O_WRONLY | O_EXCL, mode, 0);
- if (fd == -1) {
- lua_pushboolean (L, false);
- lua_pushstring (L, strerror (errno));
+ if (fd == -1) {
+ lua_pushboolean (L, false);
+ lua_pushstring (L, strerror (errno));
- return 2;
+ return 2;
+ }
+ need_close = TRUE;
+ }
+ else {
+ fd = STDOUT_FILENO;
}
- }
- else {
- fd = STDOUT_FILENO;
}
if (write (fd, t->start, t->len) == -1) {
@@ -5369,7 +5378,7 @@ lua_text_save_in_file (lua_State *L)
return 2;
}
- if (fd != STDOUT_FILENO) {
+ if (need_close) {
close (fd);
}