fs: use fs.type backend for isdirectory and isfile
This commit is contained in:
parent
1c156e6f74
commit
1c408c914e
1 changed files with 12 additions and 18 deletions
30
lfs.c
30
lfs.c
|
@ -298,27 +298,21 @@ fs_exists(lua_State *L)
|
|||
return 1;
|
||||
}
|
||||
|
||||
static int fs_type(lua_State *);
|
||||
|
||||
static int
|
||||
ismode(lua_State *L, mode_t mode)
|
||||
istype(lua_State *L, char *type)
|
||||
{
|
||||
struct stat sb;
|
||||
const char *path; /* parameter 1 (string) */
|
||||
luaL_checkstring(L, 1);
|
||||
|
||||
path = luaL_checkstring(L, 1);
|
||||
lua_pushcfunction(L, fs_type);
|
||||
lua_pushvalue(L, 1);
|
||||
lua_call(L, 1, 1);
|
||||
|
||||
if (lstat(path, &sb) != -1) {
|
||||
lua_pushboolean(L, sb.st_mode & mode);
|
||||
return 1;
|
||||
}
|
||||
lua_pushstring(L, type);
|
||||
|
||||
switch (errno) {
|
||||
case ENOTDIR:
|
||||
case ENOENT:
|
||||
lua_pushboolean(L, 0);
|
||||
return 1;
|
||||
default:
|
||||
return lfail(L);
|
||||
}
|
||||
lua_pushboolean(L, lua_compare(L, -1, -2, LUA_OPEQ));
|
||||
return 1;
|
||||
}
|
||||
|
||||
/***
|
||||
|
@ -339,7 +333,7 @@ end
|
|||
static int
|
||||
fs_isdirectory(lua_State *L)
|
||||
{
|
||||
return ismode(L, S_IFDIR);
|
||||
return istype(L, "directory");
|
||||
}
|
||||
|
||||
/***
|
||||
|
@ -361,7 +355,7 @@ end
|
|||
static int
|
||||
fs_isfile(lua_State *L)
|
||||
{
|
||||
return ismode(L, S_IFREG);
|
||||
return istype(L, "file");
|
||||
}
|
||||
|
||||
/***
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue