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
21 local BUFFERSIZE = nixio.const.buffersize
22 local ZIOBLKSIZE = 65536
23 local socket = nixio.meta_socket
24 local tls_socket = nixio.meta_tls_socket
25 local file = nixio.meta_file
26 local uname = nixio.uname()
27 local ZBUG = uname.sysname == "Linux" and uname.release:sub(1, 3) == "2.4"
29 function consume(iter, append)
30 local tbl = append or {}
41 function meta.is_socket(self)
42 return (getmetatable(self) == socket)
45 function meta.is_tls_socket(self)
46 return (getmetatable(self) == tls_socket)
49 function meta.is_file(self)
50 return (getmetatable(self) == file)
53 function meta.readall(self, len)
54 local block, code, msg = self:read(len or BUFFERSIZE)
57 return nil, code, msg, ""
58 elseif #block == 0 then
59 return "", nil, nil, ""
62 local data, total = {block}, #block
64 while not len or len > total do
65 block, code, msg = self:read(len and (len - total) or BUFFERSIZE)
68 return nil, code, msg, table.concat(data)
69 elseif #block == 0 then
73 data[#data+1], total = block, total + #block
76 local data = #data > 1 and table.concat(data) or data[1]
77 return data, nil, nil, data
79 meta.recvall = meta.readall
81 function meta.writeall(self, data)
83 local sent, code, msg = self:write(data)
86 return nil, code, msg, 0
91 while total < #data do
92 sent, code, msg = self:write(data, total)
95 return nil, code, msg, total
101 return total, nil, nil, total
103 meta.sendall = meta.writeall
105 function meta.linesource(self, limit)
106 limit = limit or BUFFERSIZE
109 return function(flush)
113 line = buffer:sub(bpos + 1)
114 buffer = type(flush) == "string" and flush or ""
120 _, endp, line = buffer:find("(.-)\r?\n", bpos + 1)
124 elseif #buffer < limit + bpos then
125 local newblock, code, msg = self:read(limit + bpos - #buffer)
127 return nil, code, msg
128 elseif #newblock == 0 then
131 buffer = buffer:sub(bpos + 1) .. newblock
140 function meta.blocksource(self, bs, limit)
141 bs = bs or BUFFERSIZE
147 elseif limit < toread then
152 local block, code, msg = self:read(toread)
155 return nil, code, msg
156 elseif #block == 0 then
160 limit = limit - #block
168 function meta.sink(self, close)
169 return function(chunk, src_err)
170 if not chunk and not src_err and close then
171 if self.shutdown then
175 elseif chunk and #chunk > 0 then
176 return self:writeall(chunk)
182 function meta.copy(self, fdout, size)
183 local source = self:blocksource(nil, size)
184 local sink = fdout:sink()
185 local sent, chunk, code, msg = 0
188 chunk, code, msg = source()
189 sink(chunk, code, msg)
190 sent = chunk and (sent + #chunk) or sent
192 return not code and sent or nil, code, msg, sent
195 function meta.copyz(self, fd, size)
196 local sent, lsent, code, msg = 0
199 if not ZBUG and self:is_file() then
200 local ftype = self:stat("type")
201 if nixio.sendfile and fd:is_socket() and ftype == "reg" then
203 lsent, code, msg = nixio.sendfile(fd, self, size or ZIOBLKSIZE)
206 size = size and (size - lsent)
208 until (not lsent or lsent == 0 or (size and size == 0))
209 if lsent or (not lsent and sent == 0 and
210 code ~= nixio.const.ENOSYS and code ~= nixio.const.EINVAL) then
211 return lsent and sent, code, msg, sent
213 elseif nixio.splice and not fd:is_tls_socket() and ftype == "fifo" then
218 if nixio.splice and fd:is_file() and not splicable then
219 splicable = not self:is_tls_socket() and fd:stat("type") == "fifo"
224 lsent, code, msg = nixio.splice(self, fd, size or ZIOBLKSIZE)
227 size = size and (size - lsent)
229 until (not lsent or lsent == 0 or (size and size == 0))
230 if lsent or (not lsent and sent == 0 and
231 code ~= nixio.const.ENOSYS and code ~= nixio.const.EINVAL) then
232 return lsent and sent, code, msg, sent
236 return self:copy(fd, size)
239 function tls_socket.close(self)
240 return self.socket:close()
243 function tls_socket.getsockname(self)
244 return self.socket:getsockname()
247 function tls_socket.getpeername(self)
248 return self.socket:getpeername()
251 function tls_socket.getsockopt(self, ...)
252 return self.socket:getsockopt(...)
254 tls_socket.getopt = tls_socket.getsockopt
256 function tls_socket.setsockopt(self, ...)
257 return self.socket:setsockopt(...)
259 tls_socket.setopt = tls_socket.setsockopt
261 for k, v in pairs(meta) do