Merge pull request #345 from tritao/symbolic-links

Fixed os.isdir to work with Windows symbolic links.
This commit is contained in:
Samuel Surtees 2015-11-25 13:00:18 +10:00
commit 026daa9b68

View File

@ -8,20 +8,35 @@
#include <sys/stat.h> #include <sys/stat.h>
#include "premake.h" #include "premake.h"
#ifdef _WIN32
#include <Windows.h>
#endif
int os_isdir(lua_State* L) int os_isdir(lua_State* L)
{ {
struct stat buf; struct stat buf;
const char* path = luaL_checkstring(L, 1); const char* path = luaL_checkstring(L, 1);
#ifdef _WIN32
int attr;
#endif
/* empty path is equivalent to ".", must be true */ /* empty path is equivalent to ".", must be true */
if (strlen(path) == 0) if (strlen(path) == 0)
{ {
lua_pushboolean(L, 1); lua_pushboolean(L, 1);
} }
#ifdef _WIN32
// Use Windows-specific GetFileAttributes since it deals with symbolic links.
else if ((attr = GetFileAttributes(path)) != INVALID_FILE_ATTRIBUTES)
{
int isdir = (attr & FILE_ATTRIBUTE_DIRECTORY) != 0;
lua_pushboolean(L, isdir);
}
#endif
else if (stat(path, &buf) == 0) else if (stat(path, &buf) == 0)
{ {
lua_pushboolean(L, buf.st_mode & S_IFDIR); int isdir = (buf.st_mode & S_IFDIR) != 0;
lua_pushboolean(L, isdir);
} }
else else
{ {