summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEmil Renner Berthing <esmil@mailme.dk>2012-12-12 00:29:55 +0100
committerEmil Renner Berthing <esmil@mailme.dk>2012-12-17 10:11:06 +0100
commita252badcc741bf4e30677cd746f7792106c80dcd (patch)
tree28b83c43886a80e1f626a6809eedc333630fefd9
parentb368aaec1ac413ec107bcd8d2eca202bda301725 (diff)
downloadlem-a252badcc741bf4e30677cd746f7792106c80dcd.tar.gz
lem-a252badcc741bf4e30677cd746f7792106c80dcd.tar.xz
lem-a252badcc741bf4e30677cd746f7792106c80dcd.zip
io: factor out io_strerror()
-rw-r--r--lem/io/core.c12
-rw-r--r--lem/io/file.c15
-rw-r--r--lem/io/sendfile.c41
-rw-r--r--lem/io/server.c16
-rw-r--r--lem/io/stream.c45
5 files changed, 45 insertions, 84 deletions
diff --git a/lem/io/core.c b/lem/io/core.c
index 33be6e0..237f6eb 100644
--- a/lem/io/core.c
+++ b/lem/io/core.c
@@ -54,6 +54,14 @@ io_busy(lua_State *T)
return 2;
}
+static int
+io_strerror(lua_State *T, int err)
+{
+ lua_pushnil(T);
+ lua_pushstring(T, strerror(err));
+ return 2;
+}
+
#include "sendfile.c"
#include "file.c"
#include "stream.c"
@@ -155,9 +163,7 @@ io_open_reap(struct lem_async *a)
case 0: file_new(T, fd, 2); break;
case 1: stream_new(T, fd, 3); break;
default:
- lua_pushnil(T);
- lua_pushstring(T, strerror(-ret));
- lem_queue(T, 2);
+ lem_queue(T, io_strerror(T, -ret));
return;
}
diff --git a/lem/io/file.c b/lem/io/file.c
index 6bfc2f6..8c5052c 100644
--- a/lem/io/file.c
+++ b/lem/io/file.c
@@ -91,11 +91,8 @@ file_close(lua_State *T)
lem_debug("collecting %d", f->fd);
ret = close(f->fd);
f->fd = -1;
- if (ret) {
- lua_pushnil(T);
- lua_pushstring(T, strerror(errno));
- return 2;
- }
+ if (ret)
+ return io_strerror(T, errno);
lua_pushboolean(T, 1);
return 1;
@@ -214,9 +211,7 @@ file_write_reap(struct lem_async *a)
f->a.T = NULL;
if (f->ret) {
- lua_pushnil(T);
- lua_pushstring(T, strerror(f->ret));
- lem_queue(T, 2);
+ lem_queue(T, io_strerror(T, f->ret));
return;
}
@@ -274,9 +269,7 @@ file_seek_reap(struct lem_async *a)
f->a.T = NULL;
if (f->ret) {
- lua_pushnil(T);
- lua_pushstring(T, strerror(f->ret));
- lem_queue(T, 2);
+ lem_queue(T, io_strerror(T, f->ret));
return;
}
diff --git a/lem/io/sendfile.c b/lem/io/sendfile.c
index 16f128f..bd7d246 100644
--- a/lem/io/sendfile.c
+++ b/lem/io/sendfile.c
@@ -30,28 +30,13 @@ sendfile_open(lua_State *T)
struct lem_sendfile *f;
fd = open(path, O_RDONLY | O_NONBLOCK);
- if (fd < 0) {
- int err = errno;
-
- lua_pushnil(T);
- switch (err) {
- case ENOENT:
- lua_pushliteral(T, "not found");
- break;
- case EACCES:
- lua_pushliteral(T, "permission denied");
- break;
- default:
- lua_pushstring(T, strerror(err));
- }
- return 2;
- }
+ if (fd < 0)
+ return io_strerror(T, errno);
if (fstat(fd, &buf)) {
- lua_pushnil(T);
- lua_pushstring(T, strerror(errno));
- (void)close(fd);
- return 2;
+ int err = errno;
+ close(fd);
+ return io_strerror(T, err);
}
/* create userdata and set the metatable */
@@ -71,10 +56,9 @@ sendfile_gc(lua_State *T)
{
struct lem_sendfile *f = lua_touserdata(T, 1);
- if (f->fd < 0)
- return 0;
+ if (f->fd >= 0)
+ close(f->fd);
- (void)close(f->fd);
return 0;
}
@@ -82,19 +66,18 @@ static int
sendfile_close(lua_State *T)
{
struct lem_sendfile *f;
+ int ret;
luaL_checktype(T, 1, LUA_TUSERDATA);
f = lua_touserdata(T, 1);
if (f->fd < 0)
return io_closed(T);
- if (close(f->fd)) {
- lua_pushnil(T);
- lua_pushstring(T, strerror(errno));
- return 2;
- }
-
+ ret = close(f->fd);
f->fd = -1;
+ if (ret)
+ return io_strerror(T, errno);
+
lua_pushboolean(T, 1);
return 1;
}
diff --git a/lem/io/server.c b/lem/io/server.c
index df63593..b7655ef 100644
--- a/lem/io/server.c
+++ b/lem/io/server.c
@@ -64,17 +64,13 @@ server_close(lua_State *T)
lem_debug("closing server..");
- if (close(w->fd)) {
- lua_pushnil(T);
- lua_pushstring(T, strerror(errno));
- ret = 2;
- } else {
- lua_pushboolean(T, 1);
- ret = 1;
- }
-
+ ret = close(w->fd);
w->fd = -1;
- return ret;
+ if (ret)
+ return io_strerror(T, errno);
+
+ lua_pushboolean(T, 1);
+ return 1;
}
static int
diff --git a/lem/io/stream.c b/lem/io/stream.c
index a34856b..f84c91d 100644
--- a/lem/io/stream.c
+++ b/lem/io/stream.c
@@ -83,11 +83,8 @@ stream_close(lua_State *T)
ret = close(s->r.fd);
s->r.fd = s->w.fd = -1;
- if (ret) {
- lua_pushnil(T);
- lua_pushstring(T, strerror(errno));
- return 2;
- }
+ if (ret)
+ return io_strerror(T, errno);
lua_pushboolean(T, 1);
return 1;
@@ -132,9 +129,7 @@ stream__readp(lua_State *T, struct stream *s)
if (res == LEM_PCLOSED)
return io_closed(T);
- lua_pushnil(T);
- lua_pushstring(T, strerror(err));
- return 2;
+ return io_strerror(T, err);
}
static void
@@ -220,9 +215,7 @@ stream__write(lua_State *T, struct stream *s)
if (bytes == 0 || err == ECONNRESET || err == EPIPE)
return io_closed(T);
- lua_pushnil(T);
- lua_pushstring(T, strerror(err));
- return 2;
+ return io_strerror(T, err);
}
static void
@@ -290,11 +283,8 @@ stream_setcork(lua_State *T, int state)
if (s->w.data != NULL)
return io_busy(T);
- if (setsockopt(s->w.fd, IPPROTO_TCP, TCP_CORK, &state, sizeof(int))) {
- lua_pushnil(T);
- lua_pushstring(T, strerror(errno));
- return 2;
- }
+ if (setsockopt(s->w.fd, IPPROTO_TCP, TCP_CORK, &state, sizeof(int)))
+ return io_strerror(T, errno);
lua_pushboolean(T, 1);
return 1;
@@ -382,9 +372,7 @@ stream__sendfile(lua_State *T, struct stream *s, struct sendfile *sf)
if (errno == EAGAIN)
return 0;
- lua_pushnil(T);
- lua_pushstring(T, strerror(errno));
- return 2;
+ return io_strerror(T, errno);
}
static void
@@ -462,17 +450,16 @@ stream_popen(lua_State *T)
if (mode[0] != 'r' && mode[0] != 'w')
return luaL_error(T, "invalid mode string");
- if (pipe(fd)) {
- err = errno;
- goto error;
- }
+ if (pipe(fd))
+ return io_strerror(T, errno);
switch (fork()) {
case -1: /* error */
err = errno;
close(fd[0]);
close(fd[1]);
- goto error;
+ return io_strerror(T, err);
+
case 0: /* child */
if (mode[0] == 'r') {
close(fd[0]);
@@ -490,13 +477,13 @@ stream_popen(lua_State *T)
if (close(fd[1])) {
err = errno;
close(fd[0]);
- goto error;
+ return io_strerror(T, err);
}
} else {
if (close(fd[0])) {
err = errno;
close(fd[1]);
- goto error;
+ return io_strerror(T, err);
}
fd[0] = fd[1];
}
@@ -505,13 +492,9 @@ stream_popen(lua_State *T)
if (fcntl(fd[0], F_SETFL, O_NONBLOCK) < 0) {
err = errno;
close(fd[0]);
- goto error;
+ return io_strerror(T, err);
}
stream_new(T, fd[0], lua_upvalueindex(1));
return 1;
-error:
- lua_pushnil(T);
- lua_pushstring(T, strerror(err));
- return 2;
}