]> source.dussan.org Git - rspamd.git/commitdiff
[Minor] Allow to take ownership on rspamd_text
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Sat, 18 Mar 2017 13:28:53 +0000 (13:28 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Sat, 18 Mar 2017 13:28:53 +0000 (13:28 +0000)
src/lua/lua_task.c

index 96cab157b2bf8ba19d6a71b0d575522c27456afa..3460aebcbb7adea8406a381912bdbc864f07ad18 100644 (file)
@@ -906,12 +906,14 @@ 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, take_ownership);
 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, take_ownership),
        LUA_INTERFACE_DEF (text, save_in_file),
        {"__len", lua_text_len},
        {"__tostring", lua_text_str},
@@ -4197,6 +4199,32 @@ lua_text_ptr (lua_State *L)
        return 1;
 }
 
+static gint
+lua_text_take_ownership (lua_State *L)
+{
+       struct rspamd_lua_text *t = lua_check_text (L, 1);
+       gchar *dest;
+
+       if (t != NULL) {
+               if (t->flags & RSPAMD_TEXT_FLAG_OWN) {
+                       /* We already own it */
+                       lua_pushboolean (L, true);
+               }
+               else {
+                       dest = g_malloc (t->len);
+                       memcpy (dest, t->start, t->len);
+                       t->start = dest;
+                       t->flags |= RSPAMD_TEXT_FLAG_OWN;
+                       lua_pushboolean (L, true);
+               }
+       }
+       else {
+               return luaL_error (L, "invalid arguments");
+       }
+
+       return 1;
+}
+
 static gint
 lua_text_save_in_file (lua_State *L)
 {