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,29 +298,23 @@ fs_exists(lua_State *L)
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int fs_type(lua_State *);
|
||||||
|
|
||||||
static int
|
static int
|
||||||
ismode(lua_State *L, mode_t mode)
|
istype(lua_State *L, char *type)
|
||||||
{
|
{
|
||||||
struct stat sb;
|
luaL_checkstring(L, 1);
|
||||||
const char *path; /* parameter 1 (string) */
|
|
||||||
|
|
||||||
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_pushstring(L, type);
|
||||||
lua_pushboolean(L, sb.st_mode & mode);
|
|
||||||
|
lua_pushboolean(L, lua_compare(L, -1, -2, LUA_OPEQ));
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (errno) {
|
|
||||||
case ENOTDIR:
|
|
||||||
case ENOENT:
|
|
||||||
lua_pushboolean(L, 0);
|
|
||||||
return 1;
|
|
||||||
default:
|
|
||||||
return lfail(L);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/***
|
/***
|
||||||
* Returns true if the given path specifies a directory.
|
* Returns true if the given path specifies a directory.
|
||||||
* Will return false if either the given path does not
|
* Will return false if either the given path does not
|
||||||
|
@ -339,7 +333,7 @@ end
|
||||||
static int
|
static int
|
||||||
fs_isdirectory(lua_State *L)
|
fs_isdirectory(lua_State *L)
|
||||||
{
|
{
|
||||||
return ismode(L, S_IFDIR);
|
return istype(L, "directory");
|
||||||
}
|
}
|
||||||
|
|
||||||
/***
|
/***
|
||||||
|
@ -361,7 +355,7 @@ end
|
||||||
static int
|
static int
|
||||||
fs_isfile(lua_State *L)
|
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