diff options
author | Stefan Hoffmann <stefan991@gmail.com> | 2014-08-09 12:33:33 +0200 |
---|---|---|
committer | Stefan Hoffmann <stefan991@gmail.com> | 2014-08-09 12:33:33 +0200 |
commit | 4437e194558279b31878965d1c4b2188b1a8cf39 (patch) | |
tree | f8db6b102a3ac4226fbfbb52310e1896dbae2ad2 | |
parent | 6b178640f0c83cfcd74e7fbc81d4a4cab5fe4748 (diff) | |
download | luafilesystem-4437e194558279b31878965d1c4b2188b1a8cf39.tar.gz luafilesystem-4437e194558279b31878965d1c4b2188b1a8cf39.tar.bz2 luafilesystem-4437e194558279b31878965d1c4b2188b1a8cf39.zip |
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.
-rw-r--r-- | src/lfs.c | 39 | ||||
-rw-r--r-- | tests/test.lua | 7 |
2 files changed, 22 insertions, 24 deletions
@@ -716,12 +716,6 @@ static void push_st_blksize (lua_State *L, STAT_STRUCT *info) { | |||
716 | lua_pushnumber (L, (lua_Number)info->st_blksize); | 716 | lua_pushnumber (L, (lua_Number)info->st_blksize); |
717 | } | 717 | } |
718 | #endif | 718 | #endif |
719 | static void push_invalid (lua_State *L, STAT_STRUCT *info) { | ||
720 | luaL_error(L, "invalid attribute name"); | ||
721 | #ifndef _WIN32 | ||
722 | info->st_blksize = 0; /* never reached */ | ||
723 | #endif | ||
724 | } | ||
725 | 719 | ||
726 | /* | 720 | /* |
727 | ** Convert the inode protection mode to a permission list. | 721 | ** Convert the inode protection mode to a permission list. |
@@ -787,14 +781,13 @@ struct _stat_members members[] = { | |||
787 | { "blocks", push_st_blocks }, | 781 | { "blocks", push_st_blocks }, |
788 | { "blksize", push_st_blksize }, | 782 | { "blksize", push_st_blksize }, |
789 | #endif | 783 | #endif |
790 | { NULL, push_invalid } | 784 | { NULL, NULL } |
791 | }; | 785 | }; |
792 | 786 | ||
793 | /* | 787 | /* |
794 | ** Get file or symbolic link information | 788 | ** Get file or symbolic link information |
795 | */ | 789 | */ |
796 | static int _file_info_ (lua_State *L, int (*st)(const char*, STAT_STRUCT*)) { | 790 | static int _file_info_ (lua_State *L, int (*st)(const char*, STAT_STRUCT*)) { |
797 | int i; | ||
798 | STAT_STRUCT info; | 791 | STAT_STRUCT info; |
799 | const char *file = luaL_checkstring (L, 1); | 792 | const char *file = luaL_checkstring (L, 1); |
800 | 793 | ||
@@ -804,25 +797,23 @@ static int _file_info_ (lua_State *L, int (*st)(const char*, STAT_STRUCT*)) { | |||
804 | return 2; | 797 | return 2; |
805 | } | 798 | } |
806 | if (lua_isstring (L, 2)) { | 799 | if (lua_isstring (L, 2)) { |
807 | int v; | ||
808 | const char *member = lua_tostring (L, 2); | 800 | const char *member = lua_tostring (L, 2); |
809 | if (strcmp (member, "mode") == 0) v = 0; | 801 | for (int i = 0; members[i].name; i++) { |
810 | #ifndef _WIN32 | 802 | if (strcmp(members[i].name, member) == 0) { |
811 | else if (strcmp (member, "blocks") == 0) v = 11; | 803 | /* push member value and return */ |
812 | else if (strcmp (member, "blksize") == 0) v = 12; | 804 | members[i].push (L, &info); |
813 | #endif | 805 | return 1; |
814 | else /* look for member */ | 806 | } |
815 | for (v = 1; members[v].name; v++) | 807 | } |
816 | if (*members[v].name == *member) | 808 | /* member not found */ |
817 | break; | 809 | return luaL_error(L, "invalid attribute name"); |
818 | /* push member value and return */ | 810 | } |
819 | members[v].push (L, &info); | 811 | /* creates a table if none is given */ |
820 | return 1; | 812 | if (!lua_istable (L, 2)) { |
821 | } else if (!lua_istable (L, 2)) | ||
822 | /* creates a table if none is given */ | ||
823 | lua_newtable (L); | 813 | lua_newtable (L); |
814 | } | ||
824 | /* stores all members in table on top of the stack */ | 815 | /* stores all members in table on top of the stack */ |
825 | for (i = 0; members[i].name; i++) { | 816 | for (int i = 0; members[i].name; i++) { |
826 | lua_pushstring (L, members[i].name); | 817 | lua_pushstring (L, members[i].name); |
827 | members[i].push (L, &info); | 818 | members[i].push (L, &info); |
828 | lua_rawset (L, -3); | 819 | 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) | |||
120 | io.write(".") | 120 | io.write(".") |
121 | io.flush() | 121 | io.flush() |
122 | 122 | ||
123 | -- Check consistency of lfs.attributes values | ||
124 | local attr = lfs.attributes (tmpfile) | ||
125 | for key, value in pairs(attr) do | ||
126 | assert (value == lfs.attributes (tmpfile, key), | ||
127 | "lfs.attributes values not consistent") | ||
128 | end | ||
129 | |||
123 | -- Remove new file and directory | 130 | -- Remove new file and directory |
124 | assert (os.remove (tmpfile), "could not remove new file") | 131 | assert (os.remove (tmpfile), "could not remove new file") |
125 | assert (lfs.rmdir (tmpdir), "could not remove new directory") | 132 | assert (lfs.rmdir (tmpdir), "could not remove new directory") |