diff options
Diffstat (limited to 'apps/plugins/lua/luadir.c')
-rw-r--r-- | apps/plugins/lua/luadir.c | 41 |
1 files changed, 27 insertions, 14 deletions
diff --git a/apps/plugins/lua/luadir.c b/apps/plugins/lua/luadir.c index c8c21d2c65..bea26661e1 100644 --- a/apps/plugins/lua/luadir.c +++ b/apps/plugins/lua/luadir.c | |||
@@ -47,19 +47,34 @@ static int remove_dir (lua_State *L) { | |||
47 | return 1; | 47 | return 1; |
48 | } | 48 | } |
49 | 49 | ||
50 | |||
50 | /* | 51 | /* |
51 | ** Directory iterator | 52 | ** Directory iterator |
52 | */ | 53 | */ |
53 | static int dir_iter (lua_State *L) { | 54 | static int dir_iter (lua_State *L) { |
54 | struct dirent *entry; | 55 | struct dirent *entry; |
55 | dir_data *d = (dir_data *)luaL_checkudata (L, 1, DIR_METATABLE); | 56 | dir_data *d = (dir_data *)luaL_checkudata (L, 1, DIR_METATABLE); |
57 | |||
56 | luaL_argcheck (L, !d->closed, 1, "closed directory"); | 58 | luaL_argcheck (L, !d->closed, 1, "closed directory"); |
57 | 59 | ||
58 | if ((entry = rb->readdir (d->dir)) != NULL) { | 60 | if ((entry = rb->readdir (d->dir)) != NULL) { |
59 | struct dirinfo info = rb->dir_get_info(d->dir, entry); | 61 | struct dirinfo info = rb->dir_get_info(d->dir, entry); |
60 | lua_pushstring (L, entry->d_name); | 62 | lua_pushstring (L, entry->d_name); |
61 | lua_pushboolean (L, info.attribute & ATTR_DIRECTORY); | 63 | lua_pushboolean (L, info.attribute & ATTR_DIRECTORY); |
62 | return 2; | 64 | if (lua_toboolean (L, lua_upvalueindex(1))) { |
65 | lua_createtable(L, 0, 3); | ||
66 | lua_pushnumber (L, info.attribute); | ||
67 | lua_setfield (L, -2, "attribute"); | ||
68 | lua_pushnumber (L, info.size); | ||
69 | lua_setfield (L, -2, "size"); | ||
70 | lua_pushnumber (L, info.mtime); | ||
71 | lua_setfield (L, -2, "time"); | ||
72 | } | ||
73 | else | ||
74 | { | ||
75 | lua_pushnil(L); | ||
76 | } | ||
77 | return 3; | ||
63 | } else { | 78 | } else { |
64 | /* no more entries => close directory */ | 79 | /* no more entries => close directory */ |
65 | rb->closedir (d->dir); | 80 | rb->closedir (d->dir); |
@@ -79,7 +94,6 @@ static int dir_close (lua_State *L) { | |||
79 | rb->closedir (d->dir); | 94 | rb->closedir (d->dir); |
80 | d->closed = 1; | 95 | d->closed = 1; |
81 | } | 96 | } |
82 | |||
83 | return 0; | 97 | return 0; |
84 | } | 98 | } |
85 | 99 | ||
@@ -90,7 +104,8 @@ static int dir_close (lua_State *L) { | |||
90 | static int dir_iter_factory (lua_State *L) { | 104 | static int dir_iter_factory (lua_State *L) { |
91 | const char *path = luaL_checkstring (L, 1); | 105 | const char *path = luaL_checkstring (L, 1); |
92 | dir_data *d; | 106 | dir_data *d; |
93 | lua_pushcfunction (L, dir_iter); | 107 | lua_settop(L, 2); /* index 2 (bool) return attribute table */ |
108 | lua_pushcclosure(L, &dir_iter, 1); | ||
94 | d = (dir_data *) lua_newuserdata (L, sizeof(dir_data)); | 109 | d = (dir_data *) lua_newuserdata (L, sizeof(dir_data)); |
95 | d->closed = 0; | 110 | d->closed = 0; |
96 | 111 | ||
@@ -98,8 +113,9 @@ static int dir_iter_factory (lua_State *L) { | |||
98 | lua_setmetatable (L, -2); | 113 | lua_setmetatable (L, -2); |
99 | d->dir = rb->opendir (path); | 114 | d->dir = rb->opendir (path); |
100 | if (d->dir == NULL) | 115 | if (d->dir == NULL) |
116 | { | ||
101 | luaL_error (L, "cannot open %s: %d", path, errno); | 117 | luaL_error (L, "cannot open %s: %d", path, errno); |
102 | 118 | } | |
103 | return 2; | 119 | return 2; |
104 | } | 120 | } |
105 | 121 | ||
@@ -109,19 +125,16 @@ static int dir_iter_factory (lua_State *L) { | |||
109 | */ | 125 | */ |
110 | static int dir_create_meta (lua_State *L) { | 126 | static int dir_create_meta (lua_State *L) { |
111 | luaL_newmetatable (L, DIR_METATABLE); | 127 | luaL_newmetatable (L, DIR_METATABLE); |
112 | /* set its __gc field */ | 128 | lua_createtable(L, 0, 2); |
113 | lua_pushstring (L, "__index"); | ||
114 | lua_newtable(L); | ||
115 | lua_pushstring (L, "next"); | ||
116 | lua_pushcfunction (L, dir_iter); | 129 | lua_pushcfunction (L, dir_iter); |
117 | lua_settable(L, -3); | 130 | lua_setfield (L, -2, "next"); |
118 | lua_pushstring (L, "close"); | ||
119 | lua_pushcfunction (L, dir_close); | 131 | lua_pushcfunction (L, dir_close); |
120 | lua_settable(L, -3); | 132 | lua_setfield (L, -2, "close"); |
121 | lua_settable (L, -3); | 133 | /* set its __index field */ |
122 | lua_pushstring (L, "__gc"); | 134 | lua_setfield (L, -2, "__index"); |
135 | /* set its __gc field */ | ||
123 | lua_pushcfunction (L, dir_close); | 136 | lua_pushcfunction (L, dir_close); |
124 | lua_settable (L, -3); | 137 | lua_setfield (L, -2, "__gc"); |
125 | return 1; | 138 | return 1; |
126 | } | 139 | } |
127 | 140 | ||