From 27e8aeb826b97963fa22ad386d35697558816fc7 Mon Sep 17 00:00:00 2001 From: Bart Trojanowski Date: Mon, 3 Sep 2007 21:52:15 -0400 Subject: [PATCH] added ixp.stat --- ixp/luaixp.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- ixp/test.lua | 11 +++++++++++ 2 files changed, 63 insertions(+), 2 deletions(-) diff --git a/ixp/luaixp.c b/ixp/luaixp.c index 5dbd876..4e14592 100644 --- a/ixp/luaixp.c +++ b/ixp/luaixp.c @@ -211,7 +211,7 @@ static int l_iread_gc (lua_State *L) return 0; } -static void init_iread_gc (lua_State *L) +static void init_iread_mt (lua_State *L) { luaL_newmetatable(L, "ixp.iread"); @@ -222,6 +222,53 @@ static void init_iread_gc (lua_State *L) } /* ------------------------------------------------------------------------ + * lua: stat = stat(file) -- returns a status table */ + +static int pushstat (lua_State *L, struct IxpStat *stat); + +static int l_stat (lua_State *L) +{ + struct IxpStat *stat; + const char *file; + int rc; + + file = luaL_checkstring (L, 1); + + stat = ixp_stat(client, (char*)file); + if(!stat) + return pusherror(L, "cannot stat file"); + + rc = pushstat (L, stat); + + ixp_freestat (stat); + + return rc; +} + +#define setfield(type,name,value) \ + lua_pushstring (L, name); \ + lua_push##type (L, value); \ + lua_settable (L, -3); +static int pushstat (lua_State *L, struct IxpStat *stat) +{ + lua_newtable (L); + + setfield(number, "type", stat->type); + setfield(number, "dev", stat->dev); + //setfield(Qid, "qid", stat->qid); + setfield(number, "mode", stat->mode); + setfield(number, "atime", stat->atime); + setfield(number, "mtime", stat->mtime); + setfield(number, "length", stat->length); + setfield(string, "name", stat->name); + setfield(string, "uid", stat->uid); + setfield(string, "gid", stat->gid); + setfield(string, "muid", stat->muid); + + return 1; +} + +/* ------------------------------------------------------------------------ * the table */ static const luaL_reg R[] = { @@ -230,6 +277,9 @@ static const luaL_reg R[] = { "write", l_write }, { "read", l_read }, { "iread", l_iread }, + + { "stat", l_stat }, + { NULL, NULL }, }; @@ -239,7 +289,7 @@ LUALIB_API int luaopen_ixp (lua_State *L) const char *address = "unix!/tmp/ns.bart.:0/wmii"; client = ixp_mount((char*)address); - init_iread_gc (L); + init_iread_mt (L); luaL_register (L, MYNAME, R); lua_pushliteral (L, "version"); diff --git a/ixp/test.lua b/ixp/test.lua index c580f3c..77e9830 100755 --- a/ixp/test.lua +++ b/ixp/test.lua @@ -12,6 +12,17 @@ print ("reading...") data = ixp.read ("/lbar/1") print (data) +print ("stating...") +data = ixp.stat ("/event") +for k,v in pairs (data) do + local hex = "" + if type(v) == "number" then + hex = string.format(" (0x%x)",v) + end + print (" "..k.." = " .. tostring(v) .. hex) +end + + print ("iterating...") for ev in ixp.iread("/event") do print (ev) -- 2.11.4.GIT