X-Git-Url: https://git.archive.openwrt.org/?p=project%2Fluci.git;a=blobdiff_plain;f=libs%2Fnixio%2Fsrc%2Fsplice.c;h=4f7c04301c67e53ed466946735556ed6196f67e3;hp=cbfc6749940ab7451ed2f837d6210fae2860727e;hb=6ffdb72a4df3c06f56d594fdd56cce69c1ef664c;hpb=33fe5b57d76b287bad42de085e63fe2130ba2be3 diff --git a/libs/nixio/src/splice.c b/libs/nixio/src/splice.c index cbfc67499..4f7c04301 100644 --- a/libs/nixio/src/splice.c +++ b/libs/nixio/src/splice.c @@ -16,19 +16,36 @@ * limitations under the License. */ +#ifdef __linux__ #define _GNU_SOURCE +#endif #include "nixio.h" #include #include #include #include +#include + + +#ifndef __WINNT__ + +#ifndef BSD #include +#else +#include +#include +#include +#endif + +#ifdef _GNU_SOURCE +#ifdef SPLICE_F_MOVE /* guess what sucks... */ #ifdef __UCLIBC__ #include #include + ssize_t splice(int __fdin, __off64_t *__offin, int __fdout, __off64_t *__offout, size_t __len, unsigned int __flags) { #ifdef __NR_splice @@ -44,9 +61,40 @@ ssize_t splice(int __fdin, __off64_t *__offin, int __fdout, return -1; #endif } + +#undef SPLICE_F_MOVE +#undef SPLICE_F_NONBLOCK +#undef SPLICE_F_MORE + +#define SPLICE_F_MOVE 1 +#define SPLICE_F_NONBLOCK 2 +#define SPLICE_F_MORE 4 + #endif /* __UCLIBC__ */ /** + * splice(fd_in, fd_out, length, flags) + */ +static int nixio_splice(lua_State *L) { + int fd_in = nixio__checkfd(L, 1); + int fd_out = nixio__checkfd(L, 2); + size_t len = luaL_checkinteger(L, 3); + int flags = luaL_optinteger(L, 4, 0); + long spliced; + + do { + spliced = splice(fd_in, NULL, fd_out, NULL, len, flags); + } while (spliced == -1 && errno == EINTR); + + if (spliced < 0) { + return nixio__perror(L); + } + + lua_pushinteger(L, spliced); + return 1; +} + +/** * Translate splice flags to integer */ static int nixio_splice_flags(lua_State *L) { @@ -70,61 +118,67 @@ static int nixio_splice_flags(lua_State *L) { return 1; } +#endif /* SPLICE_F_MOVE */ +#endif /* _GNU_SOURCE */ + /** - * splice(fd_in, fd_out, length, flags) + * sendfile(outfd, infd, length) */ -static int nixio_splice(lua_State *L) { - int fd_in = nixio__checkfd(L, 1); - int fd_out = nixio__checkfd(L, 2); +static int nixio_sendfile(lua_State *L) { + int sock = nixio__checksockfd(L); + int infd = nixio__checkfd(L, 2); size_t len = luaL_checkinteger(L, 3); - int flags = luaL_optinteger(L, 4, 0); - long spliced; + off_t spliced; +#ifndef BSD do { - spliced = splice(fd_in, NULL, fd_out, NULL, len, flags); + spliced = sendfile(sock, infd, NULL, len); } while (spliced == -1 && errno == EINTR); - if (spliced < 0) { + if (spliced == -1) { return nixio__perror(L); } +#else + int r; + const off_t offset = lseek(infd, 0, SEEK_CUR); - lua_pushnumber(L, spliced); - return 1; -} - -static int nixio_splice_avail(lua_State *L) { - splice(-1, 0, -1, 0, 0, 0); - lua_pushboolean(L, errno != ENOSYS); - return 1; -} - -/** - * sendfile(outfd, infd, length) - */ -static int nixio_sendfile(lua_State *L) { - int sockfd = nixio__checksockfd(L); - int infd = nixio__checkfd(L, 2); - size_t len = luaL_checkinteger(L, 3); - - long spliced = sendfile(sockfd, infd, NULL, len); + do { +#ifdef __DARWIN__ + r = sendfile(infd, sock, offset, len, NULL, 0); +#else + r = sendfile(infd, sock, offset, len, NULL, &spliced, 0); +#endif + } while (r == -1 && errno == EINTR); - if (spliced < 0) { + if (r == -1) { return nixio__perror(L); } +#endif - lua_pushnumber(L, spliced); + lua_pushinteger(L, spliced); return 1; } /* module table */ static const luaL_reg R[] = { +#ifdef _GNU_SOURCE +#ifdef SPLICE_F_MOVE {"splice", nixio_splice}, {"splice_flags", nixio_splice_flags}, - {"splice_avail", nixio_splice_avail}, +#endif +#endif {"sendfile", nixio_sendfile}, {NULL, NULL} }; + void nixio_open_splice(lua_State *L) { luaL_register(L, NULL, R); } + +#else /* __WINNT__ */ + +void nixio_open_splice(lua_State *L) { +} + +#endif /* !__WINNT__ */