Read about our upcoming Code of Conduct on this issue

Commit 1358e1ca authored by alex@thinkpad's avatar alex@thinkpad
Browse files

Lua dryos.directory.children: exclude "." and ".." from directory listings.

Added API test for recursively listing directory contents.

--HG--
branch : lua_fix
parent bb023917bbac
...@@ -358,7 +358,7 @@ static int luaCB_directory_create(lua_State * L) ...@@ -358,7 +358,7 @@ static int luaCB_directory_create(lua_State * L)
} }
/*** /***
Get a table (of @{directory} objects) containing this directory's child directories. Get a list of subdirectories, as table of @{directory} objects.
@treturn {directory,...} @treturn {directory,...}
@function children @function children
*/ */
...@@ -377,11 +377,14 @@ static int luaCB_directory_children(lua_State * L) ...@@ -377,11 +377,14 @@ static int luaCB_directory_children(lua_State * L)
{ {
if (file.mode & ATTR_DIRECTORY) if (file.mode & ATTR_DIRECTORY)
{ {
//call the directory constructor if (!streq(file.name, ".") && !streq(file.name, ".."))
lua_pushcfunction(L, luaCB_dryos_directory); {
lua_pushfstring(L, "%s%s/", path, file.name); //call the directory constructor
lua_call(L, 1, 1); lua_pushcfunction(L, luaCB_dryos_directory);
lua_seti(L, -2, index++); lua_pushfstring(L, "%s%s/", path, file.name);
lua_call(L, 1, 1);
lua_seti(L, -2, index++);
}
} }
} }
while(FIO_FindNextEx(dirent, &file) == 0); while(FIO_FindNextEx(dirent, &file) == 0);
...@@ -396,7 +399,7 @@ static int luaCB_directory_children(lua_State * L) ...@@ -396,7 +399,7 @@ static int luaCB_directory_children(lua_State * L)
} }
/*** /***
Get a table (of @{string}s) that are the file names of this directory's files. Get a list of file names (with full path) in this directory, as table of @{string}s.
@treturn {string,...} @treturn {string,...}
@function files @function files
*/ */
......
...@@ -227,6 +227,18 @@ function rename_test(src, dst) ...@@ -227,6 +227,18 @@ function rename_test(src, dst)
printf("Rename test OK\n") printf("Rename test OK\n")
end end
function list_dir(dir, prefix)
prefix = prefix or "- "
local i, d, f
for i,d in pairs(dir:children()) do
printf("%s%s\n", prefix, d)
list_dir(d, " " .. prefix)
end
for i,f in pairs(dir:files()) do
printf("%s%s\n", prefix, f)
end
end
function card_test() function card_test()
if dryos.cf_card then if dryos.cf_card then
printf("CF card (%s) present\n", dryos.cf_card.path) printf("CF card (%s) present\n", dryos.cf_card.path)
...@@ -235,6 +247,8 @@ function card_test() ...@@ -235,6 +247,8 @@ function card_test()
printf("- DCIM dir. : %s\n", dryos.cf_card.dcim_dir) printf("- DCIM dir. : %s\n", dryos.cf_card.dcim_dir)
assert(dryos.cf_card.path == "A:/") assert(dryos.cf_card.path == "A:/")
assert(dryos.cf_card.type == "CF") assert(dryos.cf_card.type == "CF")
list_dir(dryos.cf_card.dcim_dir)
list_dir(dryos.directory(dryos.cf_card.path))
end end
if dryos.sd_card then if dryos.sd_card then
printf("SD card (%s) present\n", dryos.sd_card.path) printf("SD card (%s) present\n", dryos.sd_card.path)
...@@ -243,6 +257,8 @@ function card_test() ...@@ -243,6 +257,8 @@ function card_test()
printf("- DCIM dir. : %s\n", dryos.sd_card.dcim_dir) printf("- DCIM dir. : %s\n", dryos.sd_card.dcim_dir)
assert(dryos.sd_card.path == "B:/") assert(dryos.sd_card.path == "B:/")
assert(dryos.sd_card.type == "SD") assert(dryos.sd_card.type == "SD")
list_dir(dryos.sd_card.dcim_dir)
list_dir(dryos.directory(dryos.sd_card.path))
end end
end end
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment