2 nixio - Linux I/O library for lua
4 Copyright 2009 Steven Barth <steven@midlink.org>
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
10 http://www.apache.org/licenses/LICENSE-2.0
15 local table = require "table"
16 local nixio = require "nixio"
17 local getmetatable, assert, pairs, type = getmetatable, assert, pairs, type
18 local tostring = tostring
22 local BUFFERSIZE = nixio.const.buffersize
23 local ZIOBLKSIZE = 65536
24 local socket = nixio.meta_socket
25 local tls_socket = nixio.meta_tls_socket
26 local file = nixio.meta_file
27 local uname = nixio.uname()
28 local ZBUG = uname.sysname == "Linux" and uname.release:sub(1, 3) == "2.4"
30 function consume(iter, append)
31 local tbl = append or {}
42 function meta.is_socket(self)
43 return (getmetatable(self) == socket)
46 function meta.is_tls_socket(self)
47 return (getmetatable(self) == tls_socket)
50 function meta.is_file(self)
51 return (getmetatable(self) == file)
54 function meta.readall(self, len)
55 local block, code, msg = self:read(len or BUFFERSIZE)
58 return nil, code, msg, ""
59 elseif #block == 0 then
60 return "", nil, nil, ""
63 local data, total = {block}, #block
65 while not len or len > total do
66 block, code, msg = self:read(len and (len - total) or BUFFERSIZE)
69 return nil, code, msg, table.concat(data)
70 elseif #block == 0 then
74 data[#data+1], total = block, total + #block
77 local data = #data > 1 and table.concat(data) or data[1]
78 return data, nil, nil, data
80 meta.recvall = meta.readall
82 function meta.writeall(self, data)
84 local sent, code, msg = self:write(data)
87 return nil, code, msg, 0
92 while total < #data do
93 sent, code, msg = self:write(data, total)
96 return nil, code, msg, total
102 return total, nil, nil, total
104 meta.sendall = meta.writeall
106 function meta.linesource(self, limit)
107 limit = limit or BUFFERSIZE
110 return function(flush)
114 line = buffer:sub(bpos + 1)
115 buffer = type(flush) == "string" and flush or ""
121 _, endp, line = buffer:find("(.-)\r?\n", bpos + 1)
125 elseif #buffer < limit + bpos then
126 local newblock, code, msg = self:read(limit + bpos - #buffer)
128 return nil, code, msg
129 elseif #newblock == 0 then
132 buffer = buffer:sub(bpos + 1) .. newblock
141 function meta.blocksource(self, bs, limit)
142 bs = bs or BUFFERSIZE
148 elseif limit < toread then
153 local block, code, msg = self:read(toread)
156 return nil, code, msg
157 elseif #block == 0 then
161 limit = limit - #block
169 function meta.sink(self, close)
170 return function(chunk, src_err)
171 if not chunk and not src_err and close then
172 if self.shutdown then
176 elseif chunk and #chunk > 0 then
177 return self:writeall(chunk)
183 function meta.copy(self, fdout, size)
184 local source = self:blocksource(nil, size)
185 local sink = fdout:sink()
186 local sent, chunk, code, msg = 0
189 chunk, code, msg = source()
190 sink(chunk, code, msg)
191 sent = chunk and (sent + #chunk) or sent
193 return not code and sent or nil, code, msg, sent
196 function meta.copyz(self, fd, size)
197 local sent, lsent, code, msg = 0
200 if not ZBUG and self:is_file() then
201 local ftype = self:stat("type")
202 if nixio.sendfile and fd:is_socket() and ftype == "reg" then
204 lsent, code, msg = nixio.sendfile(fd, self, size or ZIOBLKSIZE)
207 size = size and (size - lsent)
209 until (not lsent or lsent == 0 or (size and size == 0))
210 if lsent or (not lsent and sent == 0 and
211 code ~= nixio.const.ENOSYS and code ~= nixio.const.EINVAL) then
212 return lsent and sent, code, msg, sent
214 elseif nixio.splice and not fd:is_tls_socket() and ftype == "fifo" then
219 if nixio.splice and fd:is_file() and not splicable then
220 splicable = not self:is_tls_socket() and fd:stat("type") == "fifo"
225 lsent, code, msg = nixio.splice(self, fd, size or ZIOBLKSIZE)
228 size = size and (size - lsent)
230 until (not lsent or lsent == 0 or (size and size == 0))
231 if lsent or (not lsent and sent == 0 and
232 code ~= nixio.const.ENOSYS and code ~= nixio.const.EINVAL) then
233 return lsent and sent, code, msg, sent
237 return self:copy(fd, size)
241 function tls_socket.close(self)
242 return self.socket:close()
245 function tls_socket.getsockname(self)
246 return self.socket:getsockname()
249 function tls_socket.getpeername(self)
250 return self.socket:getpeername()
253 function tls_socket.getsockopt(self, ...)
254 return self.socket:getsockopt(...)
256 tls_socket.getopt = tls_socket.getsockopt
258 function tls_socket.setsockopt(self, ...)
259 return self.socket:setsockopt(...)
261 tls_socket.setopt = tls_socket.setsockopt
264 for k, v in pairs(meta) do