diff options
Diffstat (limited to 'libs/nixio/lua')
-rw-r--r-- | libs/nixio/lua/bit.lua | 15 | ||||
-rw-r--r-- | libs/nixio/lua/nixio/fs.lua | 175 | ||||
-rw-r--r-- | libs/nixio/lua/nixio/util.lua | 86 |
3 files changed, 256 insertions, 20 deletions
diff --git a/libs/nixio/lua/bit.lua b/libs/nixio/lua/bit.lua new file mode 100644 index 000000000..cdfecb1a4 --- /dev/null +++ b/libs/nixio/lua/bit.lua @@ -0,0 +1,15 @@ +--[[ +nixio - Linux I/O library for lua + +Copyright 2009 Steven Barth <steven@midlink.org> + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +$Id$ +]]-- + +return require "nixio".bit
\ No newline at end of file diff --git a/libs/nixio/lua/nixio/fs.lua b/libs/nixio/lua/nixio/fs.lua new file mode 100644 index 000000000..f745ffe78 --- /dev/null +++ b/libs/nixio/lua/nixio/fs.lua @@ -0,0 +1,175 @@ +--[[ +nixio - Linux I/O library for lua + +Copyright 2009 Steven Barth <steven@midlink.org> + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +$Id$ +]]-- + +local table = require "table" +local nixio = require "nixio" +local type, ipairs, setmetatable = type, ipairs, setmetatable +require "nixio.util" + + +module ("nixio.fs", function(m) setmetatable(m, {__index = nixio.fs}) end) + + +function readfile(path, limit) + local fd, code, msg = nixio.open(path, "r") + local data + if not fd then + return nil, code, msg + end + + data, code, msg = fd:readall(limit) + + fd:close() + return data, code, msg +end + + +function writefile(path, data) + local fd, code, msg, stat = nixio.open(path, "w") + if not fd then + return nil, code, msg + end + + stat, code, msg = fd:writeall(data) + + fd:close() + return stat, code, msg +end + +function datacopy(src, dest, size) + local fdin, code, msg = nixio.open(src, "r") + if not fdin then + return nil, code, msg + end + + local fdout, code, msg = nixio.open(dest, "w") + if not fdout then + return nil, code, msg + end + + local stat, code, msg, sent = fdin:copy(fdout, size) + fdin:close() + fdout:close() + + return stat, code, msg, sent +end + +function copy(src, dest) + local stat, code, msg, res = nixio.lstat(src) + if not stat then + return nil, code, msg + end + + if stat.type == "dir" then + if nixio.stat(dest, type) ~= "dir" then + res, code, msg = nixio.mkdir(dest) + else + stat = true + end + elseif stat.type == "lnk" then + res, code, msg = nixio.symlink(nixio.readlink(src), dest) + else + res, code, msg = datacopy(src, dest) + end + + if not res then + return nil, code, msg + end + + nixio.utimes(dest, stat.atime, stat.mtime) + + if nixio.lchown then + nixio.lchown(dest, stat.uid, stat.gid) + end + + if stat.type ~= "lnk" then + nixio.chmod(dest, stat.modedec) + end + + return true +end + +function move(src, dest) + local stat, code, msg = nixio.rename(src, dest) + if not stat and code == nixio.const.EXDEV then + stat, code, msg = nixio.copy(src, dest) + if stat then + stat, code, msg = nixio.unlink(src) + end + end + return stat, code, msg +end + +function mkdirr(dest, mode) + if nixio.stat(dest, "type") == "dir" then + return true + else + local stat, code, msg = nixio.mkdir(dest, mode) + if not stat and code == nixio.const.ENOENT then + stat, code, msg = mkdirr(nixio.dirname(dest), mode) + if stat then + stat, code, msg = nixio.mkdir(dest, mode) + end + end + return stat, code, msg + end +end + +local function _recurse(cb, src, dest) + local type = nixio.lstat(src, "type") + if type ~= "dir" then + return cb(src, dest) + else + local stat, se, code, msg, s, c, m = true, nixio.const.sep + if dest then + s, c, m = cb(src, dest) + stat, code, msg = stat and s, c or code, m or msg + end + + for e in nixio.dir(src) do + if dest then + s, c, m = _recurse(cb, src .. se .. e, dest .. se .. e) + else + s, c, m = _recurse(cb, src .. se .. e) + end + stat, code, msg = stat and s, c or code, m or msg + end + + if not dest then -- Postfix + s, c, m = cb(src) + stat, code, msg = stat and s, c or code, m or msg + end + + return stat, code, msg + end +end + +function copyr(src, dest) + return _recurse(copy, src, dest) +end + +function mover(src, dest) + local stat, code, msg = nixio.rename(src, dest) + if not stat and code == nixio.const.EXDEV then + stat, code, msg = _recurse(copy, src, dest) + if stat then + stat, code, msg = _recurse(nixio.remove, src) + end + end + return stat, code, msg +end + +function remover(src) + return _recurse(nixio.remove, src) +end
\ No newline at end of file diff --git a/libs/nixio/lua/nixio/util.lua b/libs/nixio/lua/nixio/util.lua index 59bdce8f4..2c9fc93a3 100644 --- a/libs/nixio/lua/nixio/util.lua +++ b/libs/nixio/lua/nixio/util.lua @@ -18,11 +18,20 @@ local getmetatable, assert, pairs = getmetatable, assert, pairs module "nixio.util" -local BUFFERSIZE = 8096 +local BUFFERSIZE = nixio.const.buffersize +local ZIOBLKSIZE = 65536 local socket = nixio.meta_socket local tls_socket = nixio.meta_tls_socket local file = nixio.meta_file +function consume(iter) + local tbl = {} + for obj in iter do + tbl[#tbl+1] = obj + end + return tbl +end + local meta = {} function meta.is_socket(self) @@ -38,50 +47,53 @@ function meta.is_file(self) end function meta.readall(self, len) - local block, code, msg = self:read(len) + local block, code, msg = self:read(len or BUFFERSIZE) if not block then - return "", code, msg, len + return nil, code, msg, "" elseif #block == 0 then - return "", nil, nil, len + return "", nil, nil, "" end local data, total = {block}, #block - while len > total do - block, code, msg = self:read(len - total) + while not len or len > total do + block, code, msg = self:read(len and (len - total) or BUFFERSIZE) if not block then - return data, code, msg, len - #data + return nil, code, msg, table.concat(data) elseif #block == 0 then - return data, nil, nil, len - #data + break end data[#data+1], total = block, total + #block end - return (#data > 1 and table.concat(data) or data[1]), nil, nil, 0 + local data = #data > 1 and table.concat(data) or data[1] + return data, nil, nil, data end meta.recvall = meta.readall function meta.writeall(self, data) - local total, block = 0 local sent, code, msg = self:write(data) if not sent then - return total, code, msg, data + return nil, code, msg, 0 end - while sent < #data do - block, total = data:sub(sent + 1), total + sent - sent, code, msg = self:write(block) - + local total = sent + + while total < #data do + sent, code, msg = self:write(data, total) + if not sent then - return total, code, msg, block + return nil, code, msg, total end + + total = total + sent end - return total + sent, nil, nil, "" + return total, nil, nil, total end meta.sendall = meta.writeall @@ -105,9 +117,9 @@ function meta.linesource(self, limit) bpos = endp return line elseif #buffer < limit + bpos then - local newblock, code = self:read(limit + bpos - #buffer) + local newblock, code, msg = self:read(limit + bpos - #buffer) if not newblock then - return nil, code + return nil, code, msg elseif #newblock == 0 then return nil end @@ -135,7 +147,7 @@ function meta.blocksource(self, bs, limit) local block, code, msg = self:read(toread) if not block then - return nil, code + return nil, code, msg elseif #block == 0 then return nil else @@ -162,6 +174,40 @@ function meta.sink(self, close) end end +function meta.copy(self, fdout, size) + local source = self:blocksource(nil, size) + local sink = fdout:sink() + local sent, chunk, code, msg = 0 + + repeat + chunk, code, msg = source() + sink(chunk, code, msg) + sent = chunk and (sent + #chunk) or sent + until not chunk + return not code and sent or nil, code, msg, sent +end + +function meta.copyz(self, fd, size) + local sent, lsent, code, msg = 0 + if self:is_file() then + if nixio.sendfile and fd:is_socket() and self:stat("type") == "reg" then + repeat + lsent, code, msg = nixio.sendfile(fd, self, size or ZIOBLKSIZE) + if lsent then + sent = sent + lsent + size = size and (size - lsent) + end + until (not lsent or lsent == 0 or (size and size == 0)) + if lsent or (not lsent and sent == 0 and + code ~= nixio.const.ENOSYS and code ~= nixio.const.EINVAL) then + return lsent and sent, code, msg, sent + end + end + end + + return self:copy(fd, size) +end + function tls_socket.close(self) return self.socket:close() end |