fixed dir length bug in windows directory iterator

This commit is contained in:
Fabio Mascarenhas
2010-06-09 14:14:25 -03:00
parent fd028d3257
commit ae5a05deec
2 changed files with 8 additions and 9 deletions

View File

@@ -75,12 +75,11 @@
#endif #endif
#define DIR_METATABLE "directory metatable" #define DIR_METATABLE "directory metatable"
#define MAX_DIR_LENGTH 1023
typedef struct dir_data { typedef struct dir_data {
int closed; int closed;
#ifdef _WIN32 #ifdef _WIN32
long hFile; long hFile;
char pattern[MAX_DIR_LENGTH+1]; char pattern[MAX_PATH+1];
#else #else
DIR *dir; DIR *dir;
#endif #endif
@@ -410,12 +409,13 @@ static int dir_iter (lua_State *L) {
struct dirent *entry; struct dirent *entry;
#endif #endif
dir_data *d = (dir_data *)luaL_checkudata (L, 1, DIR_METATABLE); dir_data *d = (dir_data *)luaL_checkudata (L, 1, DIR_METATABLE);
luaL_argcheck (L, !d->closed, 1, "closed directory"); luaL_argcheck (L, d->closed == 0, 1, "closed directory");
#ifdef _WIN32 #ifdef _WIN32
if (d->hFile == 0L) { /* first entry */ if (d->hFile == 0L) { /* first entry */
if ((d->hFile = _findfirst (d->pattern, &c_file)) == -1L) { if ((d->hFile = _findfirst (d->pattern, &c_file)) == -1L) {
lua_pushnil (L); lua_pushnil (L);
lua_pushstring (L, strerror (errno)); lua_pushstring (L, strerror (errno));
d->closed = 1;
return 2; return 2;
} else { } else {
lua_pushstring (L, c_file.name); lua_pushstring (L, c_file.name);
@@ -454,14 +454,13 @@ static int dir_close (lua_State *L) {
#ifdef _WIN32 #ifdef _WIN32
if (!d->closed && d->hFile) { if (!d->closed && d->hFile) {
_findclose (d->hFile); _findclose (d->hFile);
d->closed = 1;
} }
#else #else
if (!d->closed && d->dir) { if (!d->closed && d->dir) {
closedir (d->dir); closedir (d->dir);
d->closed = 1;
} }
#endif #endif
d->closed = 1;
return 0; return 0;
} }
@@ -479,10 +478,10 @@ static int dir_iter_factory (lua_State *L) {
d->hFile = 0L; d->hFile = 0L;
luaL_getmetatable (L, DIR_METATABLE); luaL_getmetatable (L, DIR_METATABLE);
lua_setmetatable (L, -2); lua_setmetatable (L, -2);
if (strlen(path) > MAX_DIR_LENGTH) if (strlen(path) > MAX_PATH-2)
luaL_error (L, "path too long: %s", path); luaL_error (L, "path too long: %s", path);
else else
sprintf (d->pattern, "%s/*", path); sprintf (d->pattern, "%s/*", path);
#else #else
luaL_getmetatable (L, DIR_METATABLE); luaL_getmetatable (L, DIR_METATABLE);
lua_setmetatable (L, -2); lua_setmetatable (L, -2);

View File

@@ -1,5 +1,5 @@
LIBRARY lfs.dll LIBRARY lfs.dll
DESCRIPTION "LuaFileSystem" DESCRIPTION "LuaFileSystem"
VERSION 1.4.2 VERSION 1.5.0
EXPORTS EXPORTS
luaopen_lfs luaopen_lfs