Remove some crap
[project/luci.git] / libs / nixio / src / splice.c
1 /*
2  * nixio - Linux I/O library for lua
3  *
4  *   Copyright (C) 2009 Steven Barth <steven@midlink.org>
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  */
18
19 #define _GNU_SOURCE
20
21 #include "nixio.h"
22 #include <fcntl.h>
23 #include <sys/sendfile.h>
24
25 /* guess what sucks... */
26 #ifdef __UCLIBC__
27 #include <unistd.h>
28 #include <errno.h>
29 #include <sys/syscall.h>
30 ssize_t splice(int __fdin, __off64_t *__offin, int __fdout,
31         __off64_t *__offout, size_t __len, unsigned int __flags) {
32 #ifdef __NR_splice
33         return syscall(__NR_splice, __fdin, __offin, __fdout, __offout, __len, __flags);
34 #else
35         (void)__fdin;
36         (void)__offin;
37         (void)__fdout;
38         (void)__offout;
39         (void)__len;
40         (void)__flags;
41         errno = ENOSYS;
42         return -1;
43 #endif
44 }
45 #endif /* __UCLIBC__ */
46
47 /**
48  * Checks whether a flag is set in the table and translates it into a bitmap
49  */
50 static void nixio_splice_flags__w(lua_State *L, int *m, int f, const char *t) {
51         lua_pushstring(L, t);
52         lua_rawget(L, -2);
53         if (lua_toboolean(L, -1)) {
54                 *m |= f;
55         }
56         lua_pop(L, 1);
57 }
58
59 /**
60  * Translate integer to poll flags and vice versa
61  */
62 static int nixio_splice_flags(lua_State *L) {
63         int flags = 0;
64
65         luaL_checktype(L, 1, LUA_TTABLE);
66         lua_settop(L, 1);
67         nixio_splice_flags__w(L, &flags, SPLICE_F_MOVE, "move");
68         nixio_splice_flags__w(L, &flags, SPLICE_F_NONBLOCK, "nonblock");
69         nixio_splice_flags__w(L, &flags, SPLICE_F_MORE, "more");
70         lua_pushinteger(L, flags);
71
72         return 1;
73 }
74
75 /**
76  * splice(fd_in, fd_out, length, flags)
77  */
78 static int nixio_splice(lua_State *L) {
79         int fd_in = nixio__checkfd(L, 1);
80         int fd_out = nixio__checkfd(L, 2);
81         size_t len = luaL_checkinteger(L, 3);
82         int flags = luaL_optinteger(L, 4, 0);
83
84
85         long spliced = splice(fd_in, NULL, fd_out, NULL, len, flags);
86
87         if (spliced < 0) {
88                 return nixio__perror(L);
89         }
90
91         lua_pushnumber(L, spliced);
92         return 1;
93 }
94
95 /**
96  * sendfile(outfd, infd, length)
97  */
98 static int nixio_sendfile(lua_State *L) {
99         int sockfd = nixio__checksockfd(L);
100         int infd = nixio__checkfd(L, 2);
101         size_t len = luaL_checkinteger(L, 3);
102
103         long spliced = sendfile(sockfd, infd, NULL, len);
104
105         if (spliced < 0) {
106                 return nixio__perror(L);
107         }
108
109         lua_pushnumber(L, spliced);
110         return 1;
111 }
112
113 /* module table */
114 static const luaL_reg R[] = {
115         {"splice",                      nixio_splice},
116         {"splice_flags",        nixio_splice_flags},
117         {"sendfile",            nixio_sendfile},
118         {NULL,                  NULL}
119 };
120
121 void nixio_open_splice(lua_State *L) {
122         luaL_register(L, NULL, R);
123 }