From 6b178640f0c83cfcd74e7fbc81d4a4cab5fe4748 Mon Sep 17 00:00:00 2001 From: Stefan Hoffmann Date: Sat, 9 Aug 2014 12:11:20 +0200 Subject: [PATCH 1/2] Add test target to Makefile easy way to run the tests without installing the library --- Makefile | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Makefile b/Makefile index 6b54f2c..b834a4d 100644 --- a/Makefile +++ b/Makefile @@ -14,6 +14,9 @@ lib: src/lfs.so src/lfs.so: $(OBJS) MACOSX_DEPLOYMENT_TARGET="10.3"; export MACOSX_DEPLOYMENT_TARGET; $(CC) $(CFLAGS) $(LIB_OPTION) -o src/lfs.so $(OBJS) +test: lib + LUA_CPATH=./src/?.so lua tests/test.lua + install: mkdir -p $(LUA_LIBDIR) cp src/lfs.so $(LUA_LIBDIR) From 4437e194558279b31878965d1c4b2188b1a8cf39 Mon Sep 17 00:00:00 2001 From: Stefan Hoffmann Date: Sat, 9 Aug 2014 12:33:33 +0200 Subject: [PATCH 2/2] Fix lfs.attributes(file, 'blksize') fs.attributes(file, 'blksize') and fs.attributes(file, 'blocks) return the wrong values. Compare the whole attribute name instead of the first char and remove buggy special casing with wrong indexes into the member array. --- src/lfs.c | 39 +++++++++++++++------------------------ tests/test.lua | 7 +++++++ 2 files changed, 22 insertions(+), 24 deletions(-) diff --git a/src/lfs.c b/src/lfs.c index ccbba5e..b2322c9 100644 --- a/src/lfs.c +++ b/src/lfs.c @@ -716,12 +716,6 @@ static void push_st_blksize (lua_State *L, STAT_STRUCT *info) { lua_pushnumber (L, (lua_Number)info->st_blksize); } #endif -static void push_invalid (lua_State *L, STAT_STRUCT *info) { - luaL_error(L, "invalid attribute name"); -#ifndef _WIN32 - info->st_blksize = 0; /* never reached */ -#endif -} /* ** Convert the inode protection mode to a permission list. @@ -787,14 +781,13 @@ struct _stat_members members[] = { { "blocks", push_st_blocks }, { "blksize", push_st_blksize }, #endif - { NULL, push_invalid } + { NULL, NULL } }; /* ** Get file or symbolic link information */ static int _file_info_ (lua_State *L, int (*st)(const char*, STAT_STRUCT*)) { - int i; STAT_STRUCT info; const char *file = luaL_checkstring (L, 1); @@ -804,25 +797,23 @@ static int _file_info_ (lua_State *L, int (*st)(const char*, STAT_STRUCT*)) { return 2; } if (lua_isstring (L, 2)) { - int v; const char *member = lua_tostring (L, 2); - if (strcmp (member, "mode") == 0) v = 0; -#ifndef _WIN32 - else if (strcmp (member, "blocks") == 0) v = 11; - else if (strcmp (member, "blksize") == 0) v = 12; -#endif - else /* look for member */ - for (v = 1; members[v].name; v++) - if (*members[v].name == *member) - break; - /* push member value and return */ - members[v].push (L, &info); - return 1; - } else if (!lua_istable (L, 2)) - /* creates a table if none is given */ + for (int i = 0; members[i].name; i++) { + if (strcmp(members[i].name, member) == 0) { + /* push member value and return */ + members[i].push (L, &info); + return 1; + } + } + /* member not found */ + return luaL_error(L, "invalid attribute name"); + } + /* creates a table if none is given */ + if (!lua_istable (L, 2)) { lua_newtable (L); + } /* stores all members in table on top of the stack */ - for (i = 0; members[i].name; i++) { + for (int i = 0; members[i].name; i++) { lua_pushstring (L, members[i].name); members[i].push (L, &info); lua_rawset (L, -3); diff --git a/tests/test.lua b/tests/test.lua index 4990aec..abfbd4d 100644 --- a/tests/test.lua +++ b/tests/test.lua @@ -120,6 +120,13 @@ assert (new_att.modification == attrib.modification) io.write(".") io.flush() +-- Check consistency of lfs.attributes values +local attr = lfs.attributes (tmpfile) +for key, value in pairs(attr) do + assert (value == lfs.attributes (tmpfile, key), + "lfs.attributes values not consistent") +end + -- Remove new file and directory assert (os.remove (tmpfile), "could not remove new file") assert (lfs.rmdir (tmpdir), "could not remove new directory")