#include <string.h>
#include <errno.h>
#include <unistd.h>
+#include <sys/param.h>
+
+
+#ifndef __WINNT__
+
+#ifndef BSD
#include <sys/sendfile.h>
+#else
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/uio.h>
+#endif
#ifdef _GNU_SOURCE
+#ifdef SPLICE_F_MOVE
/* guess what sucks... */
#ifdef __UCLIBC__
#include <unistd.h>
#include <sys/syscall.h>
+
ssize_t splice(int __fdin, __off64_t *__offin, int __fdout,
__off64_t *__offout, size_t __len, unsigned int __flags) {
#ifdef __NR_splice
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__ */
/**
return nixio__perror(L);
}
- lua_pushnumber(L, spliced);
+ lua_pushinteger(L, spliced);
return 1;
}
return 1;
}
+#endif /* SPLICE_F_MOVE */
#endif /* _GNU_SOURCE */
/**
* sendfile(outfd, infd, length)
*/
static int nixio_sendfile(lua_State *L) {
- int sockfd = nixio__checksockfd(L);
+ int sock = nixio__checksockfd(L);
int infd = nixio__checkfd(L, 2);
size_t len = luaL_checkinteger(L, 3);
+ off_t spliced;
- long spliced = sendfile(sockfd, infd, NULL, len);
+#ifndef BSD
+ do {
+ 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);
+
+ do {
+#ifdef __DARWIN__
+ r = sendfile(infd, sock, offset, (off_t *)&len, NULL, 0);
+ spliced = r;
+#else
+ r = sendfile(infd, sock, offset, len, NULL, &spliced, 0);
+#endif
+ } while (r == -1 && errno == EINTR);
+
+ 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},
#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__ */