diff options
author | Emil Renner Berthing <esmil@mailme.dk> | 2012-12-11 16:36:15 +0100 |
---|---|---|
committer | Emil Renner Berthing <esmil@mailme.dk> | 2012-12-17 10:11:06 +0100 |
commit | b738f523cc7a56602e07ff54bd11203355a64af9 (patch) | |
tree | 81d80589cd6de351aaff385da3969ec22d4062f6 | |
parent | cd9662eee3d728512527f794f1c7f576bd84b2fd (diff) | |
download | lem-b738f523cc7a56602e07ff54bd11203355a64af9.tar.gz lem-b738f523cc7a56602e07ff54bd11203355a64af9.tar.xz lem-b738f523cc7a56602e07ff54bd11203355a64af9.zip |
io: factor out closed and busy errors
-rw-r--r-- | README.markdown | 4 | ||||
-rw-r--r-- | lem/io/core.c | 16 | ||||
-rw-r--r-- | lem/io/file.c | 60 | ||||
-rw-r--r-- | lem/io/sendfile.c | 14 | ||||
-rw-r--r-- | lem/io/server.c | 37 | ||||
-rw-r--r-- | lem/io/stream.c | 67 |
6 files changed, 66 insertions, 132 deletions
diff --git a/README.markdown b/README.markdown index f671f2d..59d907c 100644 --- a/README.markdown +++ b/README.markdown @@ -212,7 +212,7 @@ The following methods are available on streams. action on the stream. Returns `true` on succes or otherwise `nil` followed by an error message. - If the stream is already closed the error message will be `'already closed'`. + If the stream is already closed the error message will be `'closed'`. * __stream:interrupt()__ @@ -321,7 +321,7 @@ The following methods are available on server objects. on the object it will be interrupted. Returns `true` on succes or otherwise `nil` followed by an error message. - If the server is already closed the error message will be `'already closed'`. + If the server is already closed the error message will be `'closed'`. * __server:interrupt()__ diff --git a/lem/io/core.c b/lem/io/core.c index a37a6ee..da3241e 100644 --- a/lem/io/core.c +++ b/lem/io/core.c @@ -37,6 +37,22 @@ #include <lem-io.h> +static int +io_closed(lua_State *T) +{ + lua_pushnil(T); + lua_pushliteral(T, "closed"); + return 2; +} + +static int +io_busy(lua_State *T) +{ + lua_pushnil(T); + lua_pushliteral(T, "busy"); + return 2; +} + #include "sendfile.c" #include "file.c" #include "stream.c" diff --git a/lem/io/file.c b/lem/io/file.c index 4fde951..6bfc2f6 100644 --- a/lem/io/file.c +++ b/lem/io/file.c @@ -83,17 +83,10 @@ file_close(lua_State *T) luaL_checktype(T, 1, LUA_TUSERDATA); f = lua_touserdata(T, 1); - if (f->fd < 0) { - lua_pushnil(T); - lua_pushliteral(T, "already closed"); - return 2; - } - - if (f->a.T != NULL) { - lua_pushnil(T); - lua_pushliteral(T, "busy"); - return 2; - } + if (f->fd < 0) + return io_closed(T); + if (f->a.T != NULL) + return io_busy(T); lem_debug("collecting %d", f->fd); ret = close(f->fd); @@ -180,17 +173,10 @@ file_readp(lua_State *T) return luaL_argerror(T, 2, "expected userdata"); f = lua_touserdata(T, 1); - if (f->fd < 0) { - lua_pushnil(T); - lua_pushliteral(T, "closed"); - return 2; - } - - if (f->a.T != NULL) { - lua_pushnil(T); - lua_pushliteral(T, "busy"); - return 2; - } + if (f->fd < 0) + return io_closed(T); + if (f->a.T != NULL) + return io_busy(T); p = lua_touserdata(T, 2); if (p->init) @@ -249,17 +235,10 @@ file_write(lua_State *T) out = luaL_checklstring(T, 2, &out_size); f = lua_touserdata(T, 1); - if (f->fd < 0) { - lua_pushnil(T); - lua_pushliteral(T, "closed"); - return 2; - } - - if (f->a.T != NULL) { - lua_pushnil(T); - lua_pushliteral(T, "busy"); - return 2; - } + if (f->fd < 0) + return io_closed(T); + if (f->a.T != NULL) + return io_busy(T); f->out = out; f->out_size = out_size; @@ -321,17 +300,10 @@ file_seek(lua_State *T) f->offset = (off_t)offset; luaL_argcheck(T, (lua_Number)f->offset == offset, 3, "not an integer in proper range"); - if (f->fd < 0) { - lua_pushnil(T); - lua_pushliteral(T, "closed"); - return 2; - } - - if (f->a.T != NULL) { - lua_pushnil(T); - lua_pushliteral(T, "busy"); - return 2; - } + if (f->fd < 0) + return io_closed(T); + if (f->a.T != NULL) + return io_busy(T); f->whence = mode[op]; lem_async_do(&f->a, T, file_seek_work, file_seek_reap); diff --git a/lem/io/sendfile.c b/lem/io/sendfile.c index c3395e3..16f128f 100644 --- a/lem/io/sendfile.c +++ b/lem/io/sendfile.c @@ -85,11 +85,8 @@ sendfile_close(lua_State *T) luaL_checktype(T, 1, LUA_TUSERDATA); f = lua_touserdata(T, 1); - if (f->fd < 0) { - lua_pushnil(T); - lua_pushliteral(T, "already closed"); - return 2; - } + if (f->fd < 0) + return io_closed(T); if (close(f->fd)) { lua_pushnil(T); @@ -109,11 +106,8 @@ sendfile_size(lua_State *T) luaL_checktype(T, 1, LUA_TUSERDATA); f = lua_touserdata(T, 1); - if (f->fd < 0) { - lua_pushnil(T); - lua_pushliteral(T, "closed"); - return 2; - } + if (f->fd < 0) + return io_closed(T); lua_pushnumber(T, (lua_Number)f->size); return 1; diff --git a/lem/io/server.c b/lem/io/server.c index c2c03cf..d318609 100644 --- a/lem/io/server.c +++ b/lem/io/server.c @@ -50,11 +50,8 @@ server_close(lua_State *T) luaL_checktype(T, 1, LUA_TUSERDATA); w = lua_touserdata(T, 1); - if (w->fd < 0) { - lua_pushnil(T); - lua_pushliteral(T, "already closed"); - return 2; - } + if (w->fd < 0) + return io_closed(T); if (w->data != NULL) { lem_debug("interrupting listen"); @@ -169,17 +166,10 @@ server_accept(lua_State *T) luaL_checktype(T, 1, LUA_TUSERDATA); w = lua_touserdata(T, 1); - if (w->fd < 0) { - lua_pushnil(T); - lua_pushliteral(T, "closed"); - return 2; - } - - if (w->data != NULL) { - lua_pushnil(T); - lua_pushliteral(T, "busy"); - return 2; - } + if (w->fd < 0) + return io_closed(T); + if (w->data != NULL) + return io_busy(T); switch (try_accept(T, w)) { case 0: @@ -273,17 +263,10 @@ server_autospawn(lua_State *T) luaL_checktype(T, 2, LUA_TFUNCTION); w = lua_touserdata(T, 1); - if (w->fd < 0) { - lua_pushnil(T); - lua_pushliteral(T, "closed"); - return 2; - } - - if (w->data != NULL) { - lua_pushnil(T); - lua_pushliteral(T, "busy"); - return 2; - } + if (w->fd < 0) + return io_closed(T); + if (w->data != NULL) + return io_busy(T); w->cb = server_autospawn_handler; w->data = T; diff --git a/lem/io/stream.c b/lem/io/stream.c index f499f1e..62a12fa 100644 --- a/lem/io/stream.c +++ b/lem/io/stream.c @@ -103,11 +103,8 @@ stream_interrupt(lua_State *T) static int stream_close_check(lua_State *T, struct ev_io *w) { - if (w->fd < 0) { - lua_pushnil(T); - lua_pushliteral(T, "already closed"); - return 2; - } + if (w->fd < 0) + return io_closed(T); if (w->data != NULL) { lua_State *S = w->data; @@ -255,17 +252,10 @@ stream_readp(lua_State *T) return luaL_argerror(T, 2, "expected userdata"); s = lua_touserdata(T, 1); - if (s->w.fd < 0) { - lua_pushnil(T); - lua_pushliteral(T, "closed"); - return 2; - } - - if (s->w.data != NULL) { - lua_pushnil(T); - lua_pushliteral(T, "busy"); - return 2; - } + if (s->w.fd < 0) + return io_closed(T); + if (s->w.data != NULL) + return io_busy(T); p = lua_touserdata(T, 2); if (p->init) @@ -388,17 +378,10 @@ stream_write(lua_State *T) luaL_checktype(T, 2, LUA_TSTRING); s = lua_touserdata(T, 1); - if (s->w.fd < 0) { - lua_pushnil(T); - lua_pushliteral(T, "closed"); - return 2; - } - - if (s->w.data != NULL) { - lua_pushnil(T); - lua_pushliteral(T, "busy"); - return 2; - } + if (s->w.fd < 0) + return io_closed(T); + if (s->w.data != NULL) + return io_busy(T); s->data = lua_tolstring(T, 2, &s->len); if (s->len == 0) { @@ -453,17 +436,10 @@ stream_setcork(lua_State *T, int state) luaL_checktype(T, 1, LUA_TUSERDATA); s = lua_touserdata(T, 1); - if (s->w.fd < 0) { - lua_pushnil(T); - lua_pushliteral(T, "closed"); - return 2; - } - - if (s->w.data != NULL) { - lua_pushnil(T); - lua_pushliteral(T, "busy"); - return 2; - } + if (s->w.fd < 0) + return io_closed(T); + if (s->w.data != NULL) + return io_busy(T); if (setsockopt(s->w.fd, IPPROTO_TCP, TCP_CORK, &state, sizeof(int))) { lua_pushnil(T); @@ -603,17 +579,10 @@ stream_sendfile(lua_State *T) offset = (off_t)luaL_optnumber(T, 3, 0); s = lua_touserdata(T, 1); - if (s->w.fd < 0) { - lua_pushnil(T); - lua_pushliteral(T, "closed"); - return 2; - } - - if (s->w.data != NULL) { - lua_pushnil(T); - lua_pushliteral(T, "busy"); - return 2; - } + if (s->w.fd < 0) + return io_closed(T); + if (s->w.data != NULL) + return io_busy(T); f = lua_touserdata(T, 2); if (f->fd < 0) { |