libs/nixio: allow calls to nixio.util.consume() with no iterator given
[project/luci.git] / libs / nixio / lua / nixio / util.lua
1 --[[
2 nixio - Linux I/O library for lua
3
4 Copyright 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 $Id$
13 ]]--
14
15 local table = require "table"
16 local nixio = require "nixio"
17 local getmetatable, assert, pairs, type = getmetatable, assert, pairs, type
18
19 module "nixio.util"
20
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"
28
29 function consume(iter, append)
30         local tbl = append or {}
31         if iter then
32                 for obj in iter do
33                         tbl[#tbl+1] = obj
34                 end
35         end
36         return tbl
37 end
38
39 local meta = {}
40
41 function meta.is_socket(self)
42         return (getmetatable(self) == socket)
43 end
44
45 function meta.is_tls_socket(self)
46         return (getmetatable(self) == tls_socket)
47 end
48
49 function meta.is_file(self)
50         return (getmetatable(self) == file)
51 end
52
53 function meta.readall(self, len)
54         local block, code, msg = self:read(len or BUFFERSIZE)
55
56         if not block then
57                 return nil, code, msg, ""
58         elseif #block == 0 then
59                 return "", nil, nil, ""
60         end
61
62         local data, total = {block}, #block
63
64         while not len or len > total do
65                 block, code, msg = self:read(len and (len - total) or BUFFERSIZE)
66
67                 if not block then
68                         return nil, code, msg, table.concat(data)
69                 elseif #block == 0 then
70                         break
71                 end
72
73                 data[#data+1], total = block, total + #block
74         end
75
76         local data = #data > 1 and table.concat(data) or data[1]
77         return data, nil, nil, data
78 end
79 meta.recvall = meta.readall
80
81 function meta.writeall(self, data)
82         local sent, code, msg = self:write(data)
83
84         if not sent then
85                 return nil, code, msg, 0
86         end
87
88         local total = sent 
89
90         while total < #data do
91                 sent, code, msg = self:write(data, total)
92
93                 if not sent then
94                         return nil, code, msg, total
95                 end
96
97                 total = total + sent
98         end
99         
100         return total, nil, nil, total
101 end
102 meta.sendall = meta.writeall
103
104 function meta.linesource(self, limit)
105         limit = limit or BUFFERSIZE
106         local buffer = ""
107         local bpos = 0
108         return function(flush)
109                 local line, endp, _
110                 
111                 if flush then
112                         line = buffer:sub(bpos + 1)
113                         buffer = type(flush) == "string" and flush or ""
114                         bpos = 0
115                         return line
116                 end
117
118                 while not line do
119                         _, endp, line = buffer:find("(.-)\r?\n", bpos + 1)
120                         if line then
121                                 bpos = endp
122                                 return line
123                         elseif #buffer < limit + bpos then
124                                 local newblock, code, msg = self:read(limit + bpos - #buffer)
125                                 if not newblock then
126                                         return nil, code, msg
127                                 elseif #newblock == 0 then
128                                         return nil
129                                 end
130                                 buffer = buffer:sub(bpos + 1) .. newblock
131                                 bpos = 0
132                         else
133                                 return nil, 0
134                         end
135                 end
136         end
137 end
138
139 function meta.blocksource(self, bs, limit)
140         bs = bs or BUFFERSIZE
141         return function()
142                 local toread = bs
143                 if limit then
144                         if limit < 1 then
145                                 return nil
146                         elseif limit < toread then
147                                 toread = limit
148                         end
149                 end
150
151                 local block, code, msg = self:read(toread)
152
153                 if not block then
154                         return nil, code, msg
155                 elseif #block == 0 then
156                         return nil
157                 else
158                         if limit then
159                                 limit = limit - #block
160                         end
161
162                         return block
163                 end
164         end
165 end
166
167 function meta.sink(self, close)
168         return function(chunk, src_err)
169                 if not chunk and not src_err and close then
170                         if self.shutdown then
171                                 self:shutdown()
172                         end
173                         self:close()
174                 elseif chunk and #chunk > 0 then
175                         return self:writeall(chunk)
176                 end
177                 return true
178         end
179 end
180
181 function meta.copy(self, fdout, size)
182         local source = self:blocksource(nil, size)
183         local sink = fdout:sink()
184         local sent, chunk, code, msg = 0
185         
186         repeat
187                 chunk, code, msg = source()
188                 sink(chunk, code, msg)
189                 sent = chunk and (sent + #chunk) or sent
190         until not chunk
191         return not code and sent or nil, code, msg, sent
192 end
193
194 function meta.copyz(self, fd, size)
195         local sent, lsent, code, msg = 0
196         local splicable
197
198         if not ZBUG and self:is_file() then
199                 local ftype = self:stat("type")
200                 if nixio.sendfile and fd:is_socket() and ftype == "reg" then
201                         repeat
202                                 lsent, code, msg = nixio.sendfile(fd, self, size or ZIOBLKSIZE)
203                                 if lsent then
204                                         sent = sent + lsent
205                                         size = size and (size - lsent)
206                                 end
207                         until (not lsent or lsent == 0 or (size and size == 0))
208                         if lsent or (not lsent and sent == 0 and
209                          code ~= nixio.const.ENOSYS and code ~= nixio.const.EINVAL) then
210                                 return lsent and sent, code, msg, sent
211                         end
212                 elseif nixio.splice and not fd:is_tls_socket() and ftype == "fifo" then 
213                         splicable = true
214                 end
215         end
216
217         if nixio.splice and fd:is_file() and not splicable then
218                 splicable = not self:is_tls_socket() and fd:stat("type") == "fifo"
219         end
220
221         if splicable then
222                 repeat
223                         lsent, code, msg = nixio.splice(self, fd, size or ZIOBLKSIZE)
224                         if lsent then
225                                 sent = sent + lsent
226                                 size = size and (size - lsent)
227                         end
228                 until (not lsent or lsent == 0 or (size and size == 0))
229                 if lsent or (not lsent and sent == 0 and
230                  code ~= nixio.const.ENOSYS and code ~= nixio.const.EINVAL) then
231                         return lsent and sent, code, msg, sent
232                 end             
233         end
234
235         return self:copy(fd, size)
236 end
237
238 function tls_socket.close(self)
239         return self.socket:close()
240 end
241
242 function tls_socket.getsockname(self)
243         return self.socket:getsockname()
244 end
245
246 function tls_socket.getpeername(self)
247         return self.socket:getpeername()
248 end
249
250 function tls_socket.getsockopt(self, ...)
251         return self.socket:getsockopt(...)
252 end
253 tls_socket.getopt = tls_socket.getsockopt
254
255 function tls_socket.setsockopt(self, ...)
256         return self.socket:setsockopt(...)
257 end
258 tls_socket.setopt = tls_socket.setsockopt
259
260 for k, v in pairs(meta) do
261         file[k] = v
262         socket[k] = v
263         tls_socket[k] = v
264 end