]> granicus.if.org Git - apache/commitdiff
Added function to get all entries of a directory.
authorGuenter Knauf <fuankg@apache.org>
Sat, 27 Apr 2013 15:26:08 +0000 (15:26 +0000)
committerGuenter Knauf <fuankg@apache.org>
Sat, 27 Apr 2013 15:26:08 +0000 (15:26 +0000)
git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@1476601 13f79535-47bb-0310-9956-ffa450edef68

modules/lua/lua_request.c

index 9a14a5de2b22b1e3b67018343bbede65bd784c1e..5943000bac7d2de79bf9c8e3d60f487826b070d5 100644 (file)
@@ -1192,6 +1192,46 @@ static int lua_ap_set_document_root(lua_State *L)
     return 0;
 }
 
+/*
+ * lua_ap_getdir; r:get_direntries(directory) - Gets all entries of a
+ * directory and returns the directory info as a table
+ */
+static int lua_ap_getdir(lua_State *L)
+{
+    request_rec    *r;
+    apr_dir_t      *thedir;
+    apr_finfo_t    file_info;
+    apr_status_t   status;
+    const char     *directory;
+
+    luaL_checktype(L, 1, LUA_TUSERDATA);
+    luaL_checktype(L, 2, LUA_TSTRING);
+    r = ap_lua_check_request_rec(L, 1);
+    directory = lua_tostring(L, 2);
+    if (apr_dir_open(&thedir, directory, r->pool) == APR_SUCCESS) {
+        int i = 0;
+        lua_newtable(L);
+        do {
+            status = apr_dir_read(&file_info, APR_FINFO_NAME, thedir);
+            if (APR_STATUS_IS_INCOMPLETE(status)) {
+                continue; /* ignore un-stat()able files */
+            }
+            else if (status != APR_SUCCESS) {
+                break;
+            }
+            lua_pushinteger(L, ++i);
+            lua_pushstring(L, file_info.name);
+            lua_settable(L, -3);
+
+        } while (1);
+        apr_dir_close(thedir);
+        return 1;
+    }
+    else {
+        return 0;
+    }
+}
+
 /*
  * lua_ap_stat; r:stat(filename) - Runs stat on a file and returns the file
  * info as a table
@@ -1985,6 +2025,8 @@ AP_LUA_DECLARE(void) ap_lua_load_request_lmodule(lua_State *L, apr_pool_t *p)
                  makefun(&lua_db_acquire, APL_REQ_FUNTYPE_LUACFUN, p));
     apr_hash_set(dispatch, "stat", APR_HASH_KEY_STRING,
                  makefun(&lua_ap_stat, APL_REQ_FUNTYPE_LUACFUN, p));
+    apr_hash_set(dispatch, "get_direntries", APR_HASH_KEY_STRING,
+                 makefun(&lua_ap_getdir, APL_REQ_FUNTYPE_LUACFUN, p));
     apr_hash_set(dispatch, "regex", APR_HASH_KEY_STRING,
                  makefun(&lua_ap_regex, APL_REQ_FUNTYPE_LUACFUN, p));
     apr_hash_set(dispatch, "sleep", APR_HASH_KEY_STRING,