Commit e3883cd2 authored by Tom Sutcliffe's avatar Tom Sutcliffe Committed by Johny Mattsson
Browse files

Fix file.list() zero sizes. Fixes #3549.

The modern spiffs backend doesn't like stat("./somefile") for something
on the root of the filesystem, and instead only accepts "somefile"
(it also doesn't like "/somefile"). The error from stat was being
ignored which is why the file sizes all appeared to be zero.

The fix is to change file.list() to pass just the filename, unless a
directory was passed to list(). Also improved error handling a bit.
parent cad125c4
...@@ -37,20 +37,33 @@ static int file_format( lua_State* L ) ...@@ -37,20 +37,33 @@ static int file_format( lua_State* L )
// Lua: list() // Lua: list()
static int file_list( lua_State* L ) static int file_list( lua_State* L )
{ {
const char *dirname = luaL_optstring(L, 1, "."); const char *dirname = luaL_optstring(L, 1, NULL);
DIR *dir; DIR *dir;
if ((dir = opendir(dirname))) { if ((dir = opendir(dirname ? dirname : "/"))) {
lua_newtable( L ); lua_newtable( L );
struct dirent *e; struct dirent *e;
while ((e = readdir(dir))) { while ((e = readdir(dir))) {
char *fname; char *fname = NULL;
if (dirname) {
asprintf(&fname, "%s/%s", dirname, e->d_name); asprintf(&fname, "%s/%s", dirname, e->d_name);
if (!fname) if (!fname) {
closedir(dir);
return luaL_error(L, "no memory"); return luaL_error(L, "no memory");
}
} else {
fname = e->d_name;
}
struct stat st = { 0, }; struct stat st = { 0, };
stat(fname, &st); int err = stat(fname, &st);
if (err) {
// We historically ignored this error, so just warn (although it
// shouldn't really happen now).
NODE_ERR("Failed to stat %s err=%d\n", fname, err);
}
if (dirname) {
free(fname); free(fname);
}
lua_pushinteger(L, st.st_size); lua_pushinteger(L, st.st_size);
lua_setfield(L, -2, e->d_name); lua_setfield(L, -2, e->d_name);
} }
......
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