diff options
Diffstat (limited to 'libs/nixio/src')
-rw-r--r-- | libs/nixio/src/file.c | 65 | ||||
-rw-r--r-- | libs/nixio/src/io.c | 2 | ||||
-rw-r--r-- | libs/nixio/src/nixio.c | 5 | ||||
-rw-r--r-- | libs/nixio/src/poll.c | 48 | ||||
-rw-r--r-- | libs/nixio/src/sockopt.c | 3 | ||||
-rw-r--r-- | libs/nixio/src/splice.c | 35 | ||||
-rw-r--r-- | libs/nixio/src/tls-socket.c | 8 |
7 files changed, 83 insertions, 83 deletions
diff --git a/libs/nixio/src/file.c b/libs/nixio/src/file.c index 4c18eed8b..de43ee487 100644 --- a/libs/nixio/src/file.c +++ b/libs/nixio/src/file.c @@ -78,51 +78,44 @@ static int nixio_pipe(lua_State *L) { } static int nixio_file_write(lua_State *L) { - FILE *fp = nixio__checkfile(L); - size_t len, written; + int fd = nixio__checkfd(L, 1); + size_t len; + ssize_t sent; const char *data = luaL_checklstring(L, 2, &len); - written = fwrite(data, sizeof(char), len, fp); - if (written < 0) { - return nixio__perror(L); - } else { - lua_pushnumber(L, written); + + do { + sent = write(fd, data, len); + } while(sent == -1 && errno == EINTR); + if (sent >= 0) { + lua_pushinteger(L, sent); return 1; + } else { + return nixio__perror(L); } } - -/* Some code borrowed from Lua 5.1.4 liolib.c */ static int nixio_file_read(lua_State *L) { - FILE *f = nixio__checkfile(L); - size_t n = (size_t)luaL_checkinteger(L, 2); - luaL_argcheck(L, 2, n >= 0, "invalid length"); - - if (n == 0) { - if (feof(f)) { - return 0; - } else { - lua_pushliteral(L, ""); - return 1; - } - } + int fd = nixio__checkfd(L, 1); + char buffer[NIXIO_BUFFERSIZE]; + int req = luaL_checkinteger(L, 2); + int readc; - size_t rlen; /* how much to read */ - size_t nr; /* number of chars actually read */ - luaL_Buffer b; - luaL_buffinit(L, &b); - rlen = LUAL_BUFFERSIZE; /* try to read that much each time */ + /* We limit the readsize to NIXIO_BUFFERSIZE */ + req = (req > NIXIO_BUFFERSIZE) ? NIXIO_BUFFERSIZE : req; do { - char *p = luaL_prepbuffer(&b); - if (rlen > n) rlen = n; /* cannot read more than asked */ - nr = fread(p, sizeof(char), rlen, f); - luaL_addsize(&b, nr); - n -= nr; /* still have to read `n' chars */ - } while (n > 0 && nr == rlen); /* until end of count or eof */ - luaL_pushresult(&b); /* close buffer */ - return (n == 0 || lua_objlen(L, -1) > 0); + readc = read(fd, buffer, req); + } while (readc == -1 && errno == EINTR); + + if (readc < 0) { + return nixio__perror(L); + } else { + lua_pushlstring(L, buffer, readc); + return 1; + } } + static int nixio_file_seek(lua_State *L) { FILE *f = nixio__checkfile(L); off_t len = (off_t)luaL_checknumber(L, 2); @@ -176,7 +169,7 @@ static int nixio_file_lock(lua_State *L) { } } - return nixio__pstatus(L, flock(fd, flags)); + return nixio__pstatus(L, !flock(fd, flags)); } static int nixio_file_close(lua_State *L) { @@ -232,5 +225,5 @@ void nixio_open_file(lua_State *L) { luaL_register(L, NULL, M); lua_pushvalue(L, -1); lua_setfield(L, -2, "__index"); - lua_pop(L, 1); + lua_setfield(L, -2, "meta_file"); } diff --git a/libs/nixio/src/io.c b/libs/nixio/src/io.c index b33746121..3727f8c02 100644 --- a/libs/nixio/src/io.c +++ b/libs/nixio/src/io.c @@ -169,6 +169,8 @@ static const luaL_reg M[] = { {"sendto", nixio_sock_sendto}, {"recv", nixio_sock_recv}, {"recvfrom",nixio_sock_recvfrom}, + {"write", nixio_sock_send}, + {"read", nixio_sock_recv}, {NULL, NULL} }; diff --git a/libs/nixio/src/nixio.c b/libs/nixio/src/nixio.c index 0c8ee6eeb..383fc0af9 100644 --- a/libs/nixio/src/nixio.c +++ b/libs/nixio/src/nixio.c @@ -107,7 +107,7 @@ LUALIB_API int luaopen_nixio(lua_State *L) { /* register metatable as socket_meta */ lua_pushvalue(L, -2); - lua_setfield(L, -2, "socket_meta"); + lua_setfield(L, -2, "meta_socket"); /* register methods */ nixio_open_file(L); @@ -126,7 +126,7 @@ LUALIB_API int luaopen_nixio(lua_State *L) { lua_setfield(L, -2, "version"); /* some constants */ - lua_createtable(L, 0, 1); + lua_createtable(L, 0, 7); NIXIO_PUSH_CONSTANT(EACCES); NIXIO_PUSH_CONSTANT(ENOSYS); @@ -134,6 +134,7 @@ LUALIB_API int luaopen_nixio(lua_State *L) { NIXIO_PUSH_CONSTANT(EWOULDBLOCK); NIXIO_PUSH_CONSTANT(EAGAIN); NIXIO_PUSH_CONSTANT(ENOMEM); + NIXIO_PUSH_CONSTANT(ENOENT); lua_setfield(L, -2, "const"); diff --git a/libs/nixio/src/poll.c b/libs/nixio/src/poll.c index 8fd585f22..fdec2caaf 100644 --- a/libs/nixio/src/poll.c +++ b/libs/nixio/src/poll.c @@ -20,6 +20,7 @@ #include <poll.h> #include <time.h> #include <errno.h> +#include <string.h> #include <stdlib.h> #include "nixio.h" @@ -49,18 +50,6 @@ static int nixio_nanosleep(lua_State *L) { } /** - * Checks whether a flag is set in the table and translates it into a bitmap - */ -static void nixio_poll_flags__w(lua_State *L, int *map, int f, const char *t) { - lua_pushstring(L, t); - lua_rawget(L, -2); - if (lua_toboolean(L, -1)) { - *map |= f; - } - lua_pop(L, 1); -} - -/** * Checks whether a flag is set in the bitmap and sets the matching table value */ static void nixio_poll_flags__r(lua_State *L, int *map, int f, const char *t) { @@ -78,17 +67,7 @@ static void nixio_poll_flags__r(lua_State *L, int *map, int f, const char *t) { */ static int nixio_poll_flags(lua_State *L) { int flags; - if (lua_istable(L, 1)) { - lua_settop(L, 1); - flags = 0; - nixio_poll_flags__w(L, &flags, POLLIN, "in"); - nixio_poll_flags__w(L, &flags, POLLPRI, "pri"); - nixio_poll_flags__w(L, &flags, POLLOUT, "out"); - nixio_poll_flags__w(L, &flags, POLLERR, "err"); - nixio_poll_flags__w(L, &flags, POLLHUP, "hup"); - nixio_poll_flags__w(L, &flags, POLLNVAL, "nval"); - lua_pushinteger(L, flags); - } else { + if (lua_isnumber(L, 1)) { flags = luaL_checkinteger(L, 1); lua_newtable(L); nixio_poll_flags__r(L, &flags, POLLIN, "in"); @@ -97,6 +76,29 @@ static int nixio_poll_flags(lua_State *L) { nixio_poll_flags__r(L, &flags, POLLERR, "err"); nixio_poll_flags__r(L, &flags, POLLHUP, "hup"); nixio_poll_flags__r(L, &flags, POLLNVAL, "nval"); + } else { + flags = 0; + const int j = lua_gettop(L); + for (int i=1; i<=j; i++) { + const char *flag = luaL_checkstring(L, i); + if (!strcmp(flag, "in")) { + flags |= POLLIN; + } else if (!strcmp(flag, "pri")) { + flags |= POLLPRI; + } else if (!strcmp(flag, "out")) { + flags |= POLLOUT; + } else if (!strcmp(flag, "err")) { + flags |= POLLERR; + } else if (!strcmp(flag, "hup")) { + flags |= POLLHUP; + } else if (!strcmp(flag, "nval")) { + flags |= POLLNVAL; + } else { + return luaL_argerror(L, i, + "supported values: in, pri, out, err, hup, nval"); + } + } + lua_pushinteger(L, flags); } return 1; } diff --git a/libs/nixio/src/sockopt.c b/libs/nixio/src/sockopt.c index c92254e88..68a4c5590 100644 --- a/libs/nixio/src/sockopt.c +++ b/libs/nixio/src/sockopt.c @@ -30,6 +30,7 @@ */ static int nixio_sock_setblocking(lua_State *L) { int fd = nixio__checkfd(L, 1); + luaL_checkany(L, 2); int set = lua_toboolean(L, 2); int flags = fcntl(fd, F_GETFL); @@ -37,7 +38,7 @@ static int nixio_sock_setblocking(lua_State *L) { return nixio__perror(L); } - if (set) { + if (!set) { flags |= O_NONBLOCK; } else { flags &= ~O_NONBLOCK; diff --git a/libs/nixio/src/splice.c b/libs/nixio/src/splice.c index 538b99e69..556b4d7da 100644 --- a/libs/nixio/src/splice.c +++ b/libs/nixio/src/splice.c @@ -20,6 +20,7 @@ #include "nixio.h" #include <fcntl.h> +#include <string.h> #include <sys/sendfile.h> /* guess what sucks... */ @@ -45,28 +46,24 @@ ssize_t splice(int __fdin, __off64_t *__offin, int __fdout, #endif /* __UCLIBC__ */ /** - * Checks whether a flag is set in the table and translates it into a bitmap - */ -static void nixio_splice_flags__w(lua_State *L, int *m, int f, const char *t) { - lua_pushstring(L, t); - lua_rawget(L, -2); - if (lua_toboolean(L, -1)) { - *m |= f; - } - lua_pop(L, 1); -} - -/** - * Translate integer to poll flags and vice versa + * Translate splice flags to integer */ static int nixio_splice_flags(lua_State *L) { + const int j = lua_gettop(L); int flags = 0; - - luaL_checktype(L, 1, LUA_TTABLE); - lua_settop(L, 1); - nixio_splice_flags__w(L, &flags, SPLICE_F_MOVE, "move"); - nixio_splice_flags__w(L, &flags, SPLICE_F_NONBLOCK, "nonblock"); - nixio_splice_flags__w(L, &flags, SPLICE_F_MORE, "more"); + for (int i=1; i<=j; i++) { + const char *flag = luaL_checkstring(L, i); + if (!strcmp(flag, "move")) { + flags |= SPLICE_F_MOVE; + } else if (!strcmp(flag, "nonblock")) { + flags |= SPLICE_F_NONBLOCK; + } else if (!strcmp(flag, "more")) { + flags |= SPLICE_F_MORE; + } else { + return luaL_argerror(L, i, "supported values: " + "move, nonblock, more"); + } + } lua_pushinteger(L, flags); return 1; diff --git a/libs/nixio/src/tls-socket.c b/libs/nixio/src/tls-socket.c index b0cfb5c3f..3b0744f04 100644 --- a/libs/nixio/src/tls-socket.c +++ b/libs/nixio/src/tls-socket.c @@ -96,13 +96,15 @@ static int nixio_tls_sock_recv(lua_State *L) { /* There is an error */ free(t->pbuffer); t->pbuffer = t->pbufpos = NULL; - t->pbufsiz = 0; if (axread != SSL_ERROR_CONN_LOST) { + t->pbufsiz = 0; return nixio__tls_sock_perror(L, sock, axread); } else { if (!t->pbufsiz) { lua_pushliteral(L, ""); + } else { + t->pbufsiz = 0; } } } else { @@ -198,6 +200,8 @@ static int nixio_tls_sock__tostring(lua_State *L) { static const luaL_reg M[] = { {"recv", nixio_tls_sock_recv}, {"send", nixio_tls_sock_send}, + {"read", nixio_tls_sock_recv}, + {"write", nixio_tls_sock_send}, {"accept", nixio_tls_sock_accept}, {"connect", nixio_tls_sock_connect}, {"shutdown", nixio_tls_sock_shutdown}, @@ -213,5 +217,5 @@ void nixio_open_tls_socket(lua_State *L) { luaL_register(L, NULL, M); lua_pushvalue(L, -1); lua_setfield(L, -2, "__index"); - lua_setfield(L, -2, "tls_socket_meta"); + lua_setfield(L, -2, "meta_tls_socket"); } |