summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRob Kendrick (humdrum) <rjek@rjek.com>2013-11-06 17:02:36 +0000
committerRob Kendrick (humdrum) <rjek@rjek.com>2013-11-06 17:02:36 +0000
commit6f0bfc1576c628d2de43edc7b20b8ba81fe10050 (patch)
tree8a20898aa61c5c31a39a0c53f1ab249576fbd9cf
parentcbb83bbca88eb1795a82f5e53848ef1d7416d1dc (diff)
downloadluxio-6f0bfc1576c628d2de43edc7b20b8ba81fe10050.tar.gz
First half of luxio.simple reformatted to have consistent indentation; up to and including seek
-rw-r--r--luxio/simple.lua523
1 files changed, 261 insertions, 262 deletions
diff --git a/luxio/simple.lua b/luxio/simple.lua
index 0d6937d..4c54401 100644
--- a/luxio/simple.lua
+++ b/luxio/simple.lua
@@ -249,59 +249,59 @@ local descriptor_mt
local sio_wrap_mt
local function sio_mt_gc(o)
- if o.fd and not o.nogcfd then
- l_close(o.fd)
- end
+ if o.fd and not o.nogcfd then
+ l_close(o.fd)
+ end
end
if lua_version == 5.1 then
- sio_wrap_mt = function(fd, nogcfd, name)
- assert(type(fd) == "number", "expected file descriptor number at #1")
- local p = newproxy(true)
- local r = { fd = fd, proxy = p, nogcfd = nogcfd, name = name, closed = false }
- getmetatable(p).__gc = function()
- return sio_mt_gc(r)
- end
- return setmetatable(r, descriptor_mt)
- end
+ sio_wrap_mt = function(fd, nogcfd, name)
+ assert(type(fd) == "number", "expected file descriptor number at #1")
+ local p = newproxy(true)
+ local r = { fd = fd, proxy = p, nogcfd = nogcfd, name = name, closed = false }
+ getmetatable(p).__gc = function()
+ return sio_mt_gc(r)
+ end
+ return setmetatable(r, descriptor_mt)
+ end
elseif lua_version >= 5.2 then
- sio_wrap_mt = function(fd, nogcfd, name)
- assert(type(fd) == "number", "expected file descriptor number at #1")
- local r = { fd = fd, nogcfs = nogcfd, name = name, closed = false }
- return setmetatable(r, descriptor_mt)
- end
+ sio_wrap_mt = function(fd, nogcfd, name)
+ assert(type(fd) == "number", "expected file descriptor number at #1")
+ local r = { fd = fd, nogcfs = nogcfd, name = name, closed = false }
+ return setmetatable(r, descriptor_mt)
+ end
end
local function sio_check_mt(t)
- if getmetatable(t) ~= descriptor_mt then
- error "not passed an sio object"
- end
+ if getmetatable(t) ~= descriptor_mt then
+ error "not passed an sio object"
+ end
- if t.closed then
- error "passed closed sio object"
- end
+ if t.closed then
+ error "passed closed sio object"
+ end
- return true
+ return true
end
local function err(s, errno)
- return nil, ("%s: %s"):format(s, strerror(errno)), errno
+ return nil, ("%s: %s"):format(s, strerror(errno)), errno
end
local function sio_perm_flags_help(s, read, write, execute, sid)
- local c = { }
- for l in s:gmatch "." do
- if l == "r" then c[#c + 1] = read
- elseif l == "w" then c[#c + 1] = write
- elseif l == "x" then c[#c + 1] = execute
- elseif l == "s" then c[#c + 1] = sid
- elseif l == "t" then c[#c + 1] = l.S_ISVTX
- elseif l == "-" then -- skip -
- else error(("unknown permission flag '%s'"):format(l))
- end
- end
-
- return l_bit_bor(unpack(c))
+ local c = { }
+ for l in s:gmatch "." do
+ if l == "r" then c[#c + 1] = read
+ elseif l == "w" then c[#c + 1] = write
+ elseif l == "x" then c[#c + 1] = execute
+ elseif l == "s" then c[#c + 1] = sid
+ elseif l == "t" then c[#c + 1] = l.S_ISVTX
+ elseif l == "-" then -- skip -
+ else error(("unknown permission flag '%s'"):format(l))
+ end
+ end
+
+ return l_bit_bor(unpack(c))
end
local S_IRALL = l_bit_bor(l.S_IRUSR, l.S_IRGRP, l.S_IROTH)
@@ -310,308 +310,307 @@ local S_IXALL = l_bit_bor(l.S_IXUSR, l.S_IXGRP, l.S_IXOTH)
local S_ISUIDGID = l_bit_bor(l.S_ISUID, l.S_ISGID)
local function sio_mode_flags(f)
- if type(f) == "number" then
- -- is already a suitable mode
- return f
- end
-
- if type(f) ~= "string" then
- -- if it's not a string, we don't know how to process it
- error("string describing access mode expected")
- end
-
- if f:match "0?[0-7][0-7][0-7][0-7]" then
- -- octal number as a string
- return tonumber(f, 8)
- end
-
- if not f:match "[^-rwx]" then
- -- ls-style
- local usr, grp, oth = f:match "(...)(...)(...)"
- if not (usr and grp and oth) then
- error "incorrect format for ls-style permissions flags"
- end
- local m
- m = l_bit_bor(sio_perm_flags_help(usr, l.S_IRUSR, l.S_IWUSR, l.S_IXUSR),
- sio_perm_flags_help(grp, l.S_IRGRP, l.S_IWGRP, l.S_IXGRP),
- sio_perm_flags_help(oth, l.S_IROTH, l.S_IWOTH, l.S_IXOTH))
- return m
- end
-
- -- try to parse this as a chmod-style command
- local m = 0
- for e, p in f:gmatch "(.)=([^,]+),?" do
- if e == "u" then
- m = l_bit_bor(m, sio_perm_flags_help(p, l.S_IRUSR, l.S_IWUSR, l.S_IXUSR, l.S_ISUID))
- elseif e == "g" then
- m = l_bit_bor(m, sio_perm_flags_help(p, l.S_IRGRP, l.S_IWGRP, l.S_IXGRP, l.S_ISGID))
- elseif e == "o" then
- m = l_bit_bor(m, sio_perm_flags_help(p, l.S_IROTH, l.S_IWOTH, l.S_IXOTH, 0))
- elseif e == "a" then
- m = l_bit_bor(m, sio_perm_flags_help(p, S_IRALL, S_IWALL, S_IXALL, S_ISUIDGID))
- else
- error(("unknown access group '%s'"):format(e))
- end
- end
-
- return m
+ if type(f) == "number" then
+ -- is already a suitable mode
+ return f
+ end
+
+ if type(f) ~= "string" then
+ -- if it's not a string, we don't know how to process it
+ error("string describing access mode expected")
+ end
+
+ if f:match "0?[0-7][0-7][0-7][0-7]" then
+ -- octal number as a string
+ return tonumber(f, 8)
+ end
+
+ if not f:match "[^-rwx]" then
+ -- ls-style
+ local usr, grp, oth = f:match "(...)(...)(...)"
+ if not (usr and grp and oth) then
+ error "incorrect format for ls-style permissions flags"
+ end
+ local m = l_bit_bor(sio_perm_flags_help(usr, l.S_IRUSR, l.S_IWUSR, l.S_IXUSR),
+ sio_perm_flags_help(grp, l.S_IRGRP, l.S_IWGRP, l.S_IXGRP),
+ sio_perm_flags_help(oth, l.S_IROTH, l.S_IWOTH, l.S_IXOTH))
+ return m
+ end
+
+ -- try to parse this as a chmod-style command
+ local m = 0
+ for e, p in f:gmatch "(.)=([^,]+),?" do
+ if e == "u" then
+ m = l_bit_bor(m, sio_perm_flags_help(p, l.S_IRUSR, l.S_IWUSR, l.S_IXUSR, l.S_ISUID))
+ elseif e == "g" then
+ m = l_bit_bor(m, sio_perm_flags_help(p, l.S_IRGRP, l.S_IWGRP, l.S_IXGRP, l.S_ISGID))
+ elseif e == "o" then
+ m = l_bit_bor(m, sio_perm_flags_help(p, l.S_IROTH, l.S_IWOTH, l.S_IXOTH, 0))
+ elseif e == "a" then
+ m = l_bit_bor(m, sio_perm_flags_help(p, S_IRALL, S_IWALL, S_IXALL, S_ISUIDGID))
+ else
+ error(("unknown access group '%s'"):format(e))
+ end
+ end
+
+ return m
end
local open_flags = {
- r = 0, w = 0, -- handled manually
- ["+"] = l.O_APPEND,
- c = l.O_CREAT,
- e = l_bit_bor(l.O_EXCL, l.O_CREAT),
- n = l.O_NOATIME,
- d = l.O_NDELAY,
- t = l.O_TRUNC
+ r = 0, w = 0, -- handled manually
+ ["+"] = l.O_APPEND,
+ c = l.O_CREAT,
+ e = l_bit_bor(l.O_EXCL, l.O_CREAT),
+ n = l.O_NOATIME,
+ d = l.O_NDELAY,
+ t = l.O_TRUNC
}
local function sio_open_flags(flags)
- local r = { }
- if flags:match "r" and flags:match "w" then
- r[#r + 1] = l.O_RDWR
- elseif flags:match "r" then
- r[#r + 1] = l.O_RDONLY
- elseif flags:match "w" then
- r[#r + 1] = l.O_WRONLY
- end
-
- for m in flags:gmatch "(.)" do
- local f = open_flags[m] or error(("unknown open flag '%s'"):format(m))
- r[#r + 1] = f
- end
-
- return l_bit_bor(unpack(r))
+ local r = { }
+ if flags:match "r" and flags:match "w" then
+ r[#r + 1] = l.O_RDWR
+ elseif flags:match "r" then
+ r[#r + 1] = l.O_RDONLY
+ elseif flags:match "w" then
+ r[#r + 1] = l.O_WRONLY
+ end
+
+ for m in flags:gmatch "(.)" do
+ local f = open_flags[m] or error(("unknown open flag '%s'"):format(m))
+ r[#r + 1] = f
+ end
+
+ return l_bit_bor(unpack(r))
end
local function open(filename, flags, mode)
- mode = mode or tonumber("666", 8) -- TODO: parse mode intellengently.
- assert(type(filename) == "string", "filename string expected at #1")
- assert(type(flags) == "string", "flags string expected at #2")
- flags = sio_open_flags(flags)
+ mode = mode or tonumber("666", 8) -- TODO: parse mode intellengently.
+ assert(type(filename) == "string", "filename string expected at #1")
+ assert(type(flags) == "string", "flags string expected at #2")
+ flags = sio_open_flags(flags)
- local r, errno = l_open(filename, flags, sio_mode_flags(mode))
- if r < 0 then
- return err("open", errno)
- end
+ local r, errno = l_open(filename, flags, sio_mode_flags(mode))
+ if r < 0 then
+ return err("open", errno)
+ end
- return sio_wrap_mt(r, false, filename)
+ return sio_wrap_mt(r, false, filename)
end
local pipe_count = 0
local function pipe()
- local fds = { }
- local r, errno = l_pipe(fds)
+ local fds = { }
+ local r, errno = l_pipe(fds)
- if r ~= 0 then
- return err("pipe", errno)
- end
+ if r ~= 0 then
+ return err("pipe", errno)
+ end
- local pipename1 = ("<anonpipe %d/reader>"):format(pipe_count)
- local pipename2 = ("<anonpipe %d/writer>"):format(pipe_count)
- pipe_count = pipe_count + 1
+ local pipename1 = ("<anonpipe %d/reader>"):format(pipe_count)
+ local pipename2 = ("<anonpipe %d/writer>"):format(pipe_count)
+ pipe_count = pipe_count + 1
- return sio_wrap_mt(fds[1], false, pipename1),
- sio_wrap_mt(fds[2], false, pipename2)
+ return sio_wrap_mt(fds[1], false, pipename1),
+ sio_wrap_mt(fds[2], false, pipename2)
end
local socketpair_count = 0
local function socketpair(type)
- type = type or l.SOCK_STREAM
- local typen = type == l.SOCK_STREAM and "stream" or "datagram"
- local sv = { }
- local r, errno = l_socketpair(l.AF_UNIX, type or l.SOCK_STREAM,
- 0, sv)
+ type = type or l.SOCK_STREAM
+ local typen = type == l.SOCK_STREAM and "stream" or "datagram"
+ local sv = { }
+ local r, errno = l_socketpair(l.AF_UNIX, type or l.SOCK_STREAM, 0, sv)
- if r ~= 0 then
- return err("pipe", errno)
- end
+ if r ~= 0 then
+ return err("pipe", errno)
+ end
- local spname1 = ("<%s socketpair %d/0>"):format(typen, socketpair_count)
- local spname2 = ("<%s socketpair %d/1>"):format(typen, socketpair_count)
- socketpair_count = socketpair_count + 1
+ local spname1 = ("<%s socketpair %d/0>"):format(typen, socketpair_count)
+ local spname2 = ("<%s socketpair %d/1>"):format(typen, socketpair_count)
+ socketpair_count = socketpair_count + 1
- return sio_wrap_mt(sv[1], false, spname1),
- sio_wrap_mt(sv[2], false, spname2)
+ return sio_wrap_mt(sv[1], false, spname1), sio_wrap_mt(sv[2], false, spname2)
end
local function close(o)
- sio_check_mt(o)
+ sio_check_mt(o)
- local r, errno = l_close(o.fd)
- if r ~= 0 then
- return err("close", errno)
- end
+ local r, errno = l_close(o.fd)
+ if r ~= 0 then
+ return err("close", errno)
+ end
- o.fd = nil
- o.closed = true
+ o.fd = nil
+ o.closed = true
- return true
+ return true
end
local function nogc(o)
- sio_check_mt(o)
+ sio_check_mt(o)
- o.nogcfd = true
+ o.nogcfd = true
end
local function nonblocking(o, b)
- sio_check_mt(o)
-
- local fd = o.fd
- local flags, errno = l_fcntl(fd, l.F_GETFL)
-
- if flags == -1 then
- return err("fcntl", errno)
- end
-
- if b == nil then
- return l_bit_btest(flags, l.O_NONBLOCK)
- elseif b == false then
- flags = l_bit_bclear(flags, l.O_NONBLOCK)
- elseif b == true then
- flags = l_bit_bor(flags, l.O_NONBLOCK)
- else
- error "expected nil, true or false at #2"
- end
-
- flags, errno = l_fcntl(fd, l.F_SETFL, flags)
-
- if flags ~= 0 then
- return err("fcntl", errno)
- else
- return true
- end
+ sio_check_mt(o)
+
+ local fd = o.fd
+ local flags, errno = l_fcntl(fd, l.F_GETFL)
+
+ if flags == -1 then
+ return err("fcntl", errno)
+ end
+
+ if b == nil then
+ return l_bit_btest(flags, l.O_NONBLOCK)
+ elseif b == false then
+ flags = l_bit_bclear(flags, l.O_NONBLOCK)
+ elseif b == true then
+ flags = l_bit_bor(flags, l.O_NONBLOCK)
+ else
+ error "expected nil, true or false at #2"
+ end
+
+ flags, errno = l_fcntl(fd, l.F_SETFL, flags)
+
+ if flags ~= 0 then
+ return err("fcntl", errno)
+ else
+ return true
+ end
end
local function write(o, d, i)
- sio_check_mt(o)
- assert(type(d) == "string", "string expected at #2")
- local r, errno = l_write(o.fd, d, i)
+ sio_check_mt(o)
+ assert(type(d) == "string", "string expected at #2")
+ local r, errno = l_write(o.fd, d, i)
- if r < 0 then
- return err("write", errno)
- end
+ if r < 0 then
+ return err("write", errno)
+ end
- return r
+ return r
end
local function writev(o, ...)
- sio_check_mt(o)
- local r, errno = l_writev(o.fd, ...)
+ sio_check_mt(o)
+ local r, errno = l_writev(o.fd, ...)
- if r < 0 then
- return err("writev", errno)
- end
+ if r < 0 then
+ return err("writev", errno)
+ end
- return r
+ return r
end
local function read(o, ...)
- local res = { }
- for idx, patt in ipairs { ... } do
- if patt == "*a" then
- local c = { }
- repeat
- local r, errno = l_read(o.fd, l.BUFSIZ)
- if r == -1 then
- return err("read", errno)
- end
- c[#c + 1] = r
- until #r == 0
- if #c ~= 0 then
- res[#res + 1] = table.concat(c)
- end
- elseif patt == "*l" then
- local c = { }
- repeat
- local r, errno = l_read(o.fd, 1)
- if r == -1 then
- return err("read", errno)
- end
- if r ~= '\n' and #r ~= 0 then
+ local res = { }
+ for idx, patt in ipairs { ... } do
+ if patt == "*a" then
+ local c = { }
+ repeat
+ local r, errno = l_read(o.fd, l.BUFSIZ)
+ if r == -1 then
+ return err("read", errno)
+ end
c[#c + 1] = r
- else
- break
- end
- until #r == 0
- if #c ~= 0 then
- res[#res + 1] = table.concat(c)
- end
- elseif type(patt) == "number" then
- local r, errno = l_read(o.fd, patt)
- if r == -1 then
- return err("read", errno)
- end
- if #r ~= 0 then
- res[#res + 1] = r
- end
- else
- error(("unknown pattern specification '%s' at #%d"):format(patt, idx))
- end
- end
-
- if #res == 0 then
- return nil
- else
- return unpack(res)
- end
+ until #r == 0
+ if #c ~= 0 then
+ res[#res + 1] = table.concat(c)
+ end
+
+ elseif patt == "*l" then
+ local c = { }
+ repeat
+ local r, errno = l_read(o.fd, 1)
+ if r == -1 then
+ return err("read", errno)
+ end
+ if r ~= '\n' and #r ~= 0 then
+ c[#c + 1] = r
+ else
+ break
+ end
+ until #r == 0
+ if #c ~= 0 then
+ res[#res + 1] = table.concat(c)
+ end
+
+ elseif type(patt) == "number" then
+ local r, errno = l_read(o.fd, patt)
+ if r == -1 then
+ return err("read", errno)
+ end
+ if #r ~= 0 then
+ res[#res + 1] = r
+ end
+ else
+ error(("unknown pattern specification '%s' at #%d"):format(patt, idx))
+ end
+ end
+
+ if #res == 0 then
+ return nil
+ else
+ return unpack(res)
+ end
end
local function stat(p)
- local r, e = l_stat(p)
+ local r, e = l_stat(p)
- if r < 0 then
- return err("stat", e)
- end
+ if r < 0 then
+ return err("stat", e)
+ end
- return e
+ return e
end
local function lstat(p)
- local r, e = l_lstat(p)
+ local r, e = l_lstat(p)
- if r < 0 then
- return err("lstat", e)
- end
+ if r < 0 then
+ return err("lstat", e)
+ end
- return e
+ return e
end
local function fstat(o)
- sio_check_mt(o)
- local r, e = l_fstat(o.fd)
+ sio_check_mt(o)
+ local r, e = l_fstat(o.fd)
- if r < 0 then
- return err("fstat", e)
- end
+ if r < 0 then
+ return err("fstat", e)
+ end
- return e
+ return e
end
local seek_modes = {
- ["cur"] = l.SEEK_CUR,
- ["set"] = l.SEEK_SET,
- ["end"] = l.SEEK_END
+ ["cur"] = l.SEEK_CUR,
+ ["set"] = l.SEEK_SET,
+ ["end"] = l.SEEK_END
}
local function seek(o, whence, offset)
- sio_check_mt(o)
- assert(offset == nil or type(offset) == "number", "offset number expected at #2")
- whence = whence or "cur"
- offset = offset or 0
+ sio_check_mt(o)
+ assert(offset == nil or type(offset) == "number", "offset number expected at #2")
+ whence = whence or "cur"
+ offset = offset or 0
- whence = seek_modes[whence] or error "unknown seek whence"
- local r, errno = l_lseek(o.fd, offset, whence)
+ whence = seek_modes[whence] or error "unknown seek whence"
+ local r, errno = l_lseek(o.fd, offset, whence)
- if r < 0 then
- return err("lseek", errno)
- end
+ if r < 0 then
+ return err("lseek", errno)
+ end
- return r
+ return r
end
local function lock(o, rw, whence, offset, len, wait, test)