mirror of
https://github.com/lunarmodules/luafilesystem.git
synced 2026-07-25 13:13:07 +00:00
Dynamically size getcwd() buffer in get_dir function (#84)
* Dynamically size getcwd() buffer in get_dir function This should fix issue 42. * Fixup: Properly respect NO_GETCWD * Fixup: Get rid of getcwd_error, handle NO_GETCWD in a single place
This commit is contained in:
committed by
Hisham Muhammad
parent
50919ed69f
commit
3c4e563d9c
87
src/lfs.c
87
src/lfs.c
@@ -41,22 +41,26 @@
|
|||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#include <direct.h>
|
#include <direct.h>
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
#include <io.h>
|
#include <io.h>
|
||||||
#include <sys/locking.h>
|
#include <sys/locking.h>
|
||||||
#ifdef __BORLANDC__
|
#ifdef __BORLANDC__
|
||||||
#include <utime.h>
|
#include <utime.h>
|
||||||
|
#else
|
||||||
|
#include <sys/utime.h>
|
||||||
|
#endif
|
||||||
|
#include <fcntl.h>
|
||||||
|
/* MAX_PATH seems to be 260. Seems kind of small. Is there a better one? */
|
||||||
|
#define LFS_MAXPATHLEN MAX_PATH
|
||||||
#else
|
#else
|
||||||
#include <sys/utime.h>
|
#include <unistd.h>
|
||||||
#endif
|
#include <dirent.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#else
|
#include <sys/types.h>
|
||||||
#include <unistd.h>
|
#include <utime.h>
|
||||||
#include <dirent.h>
|
#include <sys/param.h> /* for MAXPATHLEN */
|
||||||
#include <fcntl.h>
|
#define LFS_MAXPATHLEN MAXPATHLEN
|
||||||
#include <sys/types.h>
|
|
||||||
#include <utime.h>
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <lua.h>
|
#include <lua.h>
|
||||||
@@ -85,22 +89,6 @@
|
|||||||
#define strerror(_) "System unable to describe the error"
|
#define strerror(_) "System unable to describe the error"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* Define 'getcwd' for systems that do not implement it */
|
|
||||||
#ifdef NO_GETCWD
|
|
||||||
#define getcwd(p,s) NULL
|
|
||||||
#define getcwd_error "Function 'getcwd' not provided by system"
|
|
||||||
#else
|
|
||||||
#define getcwd_error strerror(errno)
|
|
||||||
#ifdef _WIN32
|
|
||||||
/* MAX_PATH seems to be 260. Seems kind of small. Is there a better one? */
|
|
||||||
#define LFS_MAXPATHLEN MAX_PATH
|
|
||||||
#else
|
|
||||||
/* For MAXPATHLEN: */
|
|
||||||
#include <sys/param.h>
|
|
||||||
#define LFS_MAXPATHLEN MAXPATHLEN
|
|
||||||
#endif
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#define DIR_METATABLE "directory metatable"
|
#define DIR_METATABLE "directory metatable"
|
||||||
typedef struct dir_data {
|
typedef struct dir_data {
|
||||||
int closed;
|
int closed;
|
||||||
@@ -178,18 +166,35 @@ static int change_dir (lua_State *L) {
|
|||||||
** and a string describing the error
|
** and a string describing the error
|
||||||
*/
|
*/
|
||||||
static int get_dir (lua_State *L) {
|
static int get_dir (lua_State *L) {
|
||||||
char *path;
|
#ifdef NO_GETCWD
|
||||||
/* Passing (NULL, 0) is not guaranteed to work. Use a temp buffer and size instead. */
|
|
||||||
char buf[LFS_MAXPATHLEN];
|
|
||||||
if ((path = getcwd(buf, LFS_MAXPATHLEN)) == NULL) {
|
|
||||||
lua_pushnil(L);
|
lua_pushnil(L);
|
||||||
lua_pushstring(L, getcwd_error);
|
lua_pushstring(L, "Function 'getcwd' not provided by system");
|
||||||
return 2;
|
return 2;
|
||||||
}
|
#else
|
||||||
else {
|
char *path = NULL;
|
||||||
lua_pushstring(L, path);
|
/* Passing (NULL, 0) is not guaranteed to work. Use a temp buffer and size instead. */
|
||||||
return 1;
|
size_t size = LFS_MAXPATHLEN; /* initial buffer size */
|
||||||
}
|
int result;
|
||||||
|
while (1) {
|
||||||
|
path = realloc(path, size);
|
||||||
|
if (!path) /* failed to allocate */
|
||||||
|
return pusherror(L, "get_dir realloc() failed");
|
||||||
|
if (getcwd(path, size) != NULL) {
|
||||||
|
/* success, push the path to the Lua stack */
|
||||||
|
lua_pushstring(L, path);
|
||||||
|
result = 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (errno != ERANGE) { /* unexpected error */
|
||||||
|
result = pusherror(L, "get_dir getcwd() failed");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
/* ERANGE = insufficient buffer capacity, double size and retry */
|
||||||
|
size *= 2;
|
||||||
|
}
|
||||||
|
free(path);
|
||||||
|
return result;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|||||||
Reference in New Issue
Block a user