diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2016-04-27 15:06:44 +0100 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2016-04-27 15:06:44 +0100 |
commit | 5bcdb685d29c217866e122b9455afd26c3301015 (patch) | |
tree | 21928b3fd47d74c18f11940fff8873236d6c7a44 /src/lua/lua_util.c | |
parent | fc2cc895256c5b232a44b8f546f82a6a23356765 (diff) | |
download | rspamd-5bcdb685d29c217866e122b9455afd26c3301015.tar.gz rspamd-5bcdb685d29c217866e122b9455afd26c3301015.zip |
[Feature] Add more file utilities to lua_util
Diffstat (limited to 'src/lua/lua_util.c')
-rw-r--r-- | src/lua/lua_util.c | 87 |
1 files changed, 81 insertions, 6 deletions
diff --git a/src/lua/lua_util.c b/src/lua/lua_util.c index 68d5dd0f6..059b47cfc 100644 --- a/src/lua/lua_util.c +++ b/src/lua/lua_util.c @@ -266,23 +266,41 @@ LUA_FUNCTION_DEF (util, unlink); * Lock the specified file. This function returns {number} which must be passed to `util.unlock_file` after usage * or you'll have a resource leak * - * @param {string} fname filename to remove + * @param {string} fname filename to lock * @param {number} fd use the specified fd instead of opening one - * @return {number|nil} number if locking was successful or nil otherwise + * @return {number|nil,string} number if locking was successful or nil + error otherwise */ LUA_FUNCTION_DEF (util, lock_file); - /*** - * @function util.unlock_file(fname, [close_fd]) + * @function util.unlock_file(fd, [close_fd]) * Unlock the specified file closing the file descriptor associated. * - * @param {string} fname filename to remove + * @param {number} fd descriptor to unlock * @param {boolean} close_fd close descriptor on unlocking (default: TRUE) - * @return {boolean} true if a file was unlocked + * @return {boolean[,string]} true if a file was unlocked */ LUA_FUNCTION_DEF (util, unlock_file); +/*** + * @function util.create_file(fname, [mode]) + * Creates the specified file with the default mode 0644 + * + * @param {string} fname filename to create + * @param {number} mode open mode (you should use octal number here) + * @return {number|nil,string} file descriptor or pair nil + error string + */ +LUA_FUNCTION_DEF (util, create_file); + +/*** + * @function util.close_file(fd) + * Closes descriptor fd + * + * @param {number} fd descriptor to close + * @return {boolean[,string]} true if a file was closed + */ +LUA_FUNCTION_DEF (util, close_file); + static const struct luaL_reg utillib_f[] = { LUA_INTERFACE_DEF (util, create_event_base), LUA_INTERFACE_DEF (util, load_rspamd_config), @@ -312,6 +330,8 @@ static const struct luaL_reg utillib_f[] = { LUA_INTERFACE_DEF (util, unlink), LUA_INTERFACE_DEF (util, lock_file), LUA_INTERFACE_DEF (util, unlock_file), + LUA_INTERFACE_DEF (util, create_file), + LUA_INTERFACE_DEF (util, close_file), {NULL, NULL} }; @@ -1286,6 +1306,61 @@ lua_util_unlock_file (lua_State *L) } static gint +lua_util_create_file (lua_State *L) +{ + gint fd, mode = 00644; + const gchar *fpath; + + fpath = luaL_checkstring (L, 1); + + if (fpath) { + if (lua_isnumber (L, 2)) { + mode = lua_tonumber (L, 2); + } + + fd = rspamd_file_xopen (fpath, O_RDWR|O_CREAT|O_EXCL, mode); + + if (fd == -1) { + lua_pushnil (L); + lua_pushstring (L, strerror (errno)); + + return 2; + } + + lua_pushnumber (L, fd); + } + else { + return luaL_error (L, "invalid arguments"); + } + + return 1; +} + +static gint +lua_util_close_file (lua_State *L) +{ + gint fd = -1; + + if (lua_isnumber (L, 1)) { + fd = lua_tonumber (L, 1); + + if (close (fd) == -1) { + lua_pushboolean (L, false); + lua_pushstring (L, strerror (errno)); + + return 2; + } + + lua_pushboolean (L, true); + } + else { + return luaL_error (L, "invalid arguments"); + } + + return 1; +} + +static gint lua_load_util (lua_State * L) { lua_newtable (L); |