flua: implement chmod

Lua does not provide a native way to change the permission of a file.

Submitted by:	Yang Wang <2333@outlook.jp>
Reviewed by:	kevans
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D24036
This commit is contained in:
emaste 2020-03-13 15:40:35 +00:00
parent 30aa1312f7
commit 31587f7c86
3 changed files with 40 additions and 0 deletions

View File

@ -57,6 +57,7 @@ static const luaL_Reg loadedlibs[] = {
#endif
/* FreeBSD Extensions */
{"lfs", luaopen_lfs},
{"posix.sys.stat", luaopen_posix_sys_stat},
{"posix.unistd", luaopen_posix_unistd},
{NULL, NULL}
};

View File

@ -27,6 +27,10 @@
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <sys/stat.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <lua.h>
@ -37,6 +41,28 @@ __FBSDID("$FreeBSD$");
* Minimal implementation of luaposix needed for internal FreeBSD bits.
*/
static int
lua_chmod(lua_State *L)
{
int n;
const char *path;
mode_t mode;
n = lua_gettop(L);
luaL_argcheck(L, n == 2, n > 2 ? 3 : n,
"chmod takes exactly two arguments");
path = luaL_checkstring(L, 1);
mode = (mode_t)luaL_checkinteger(L, 2);
if (chmod(path, mode) == -1) {
lua_pushnil(L);
lua_pushstring(L, strerror(errno));
lua_pushinteger(L, errno);
return 3;
}
lua_pushinteger(L, 0);
return 1;
}
static int
lua_getpid(lua_State *L)
{
@ -49,12 +75,24 @@ lua_getpid(lua_State *L)
}
#define REG_SIMPLE(n) { #n, lua_ ## n }
static const struct luaL_Reg sys_statlib[] = {
REG_SIMPLE(chmod),
{ NULL, NULL },
};
static const struct luaL_Reg unistdlib[] = {
REG_SIMPLE(getpid),
{ NULL, NULL },
};
#undef REG_SIMPLE
int
luaopen_posix_sys_stat(lua_State *L)
{
luaL_newlib(L, sys_statlib);
return 1;
}
int
luaopen_posix_unistd(lua_State *L)
{

View File

@ -8,4 +8,5 @@
#include <lua.h>
int luaopen_posix_sys_stat(lua_State *L);
int luaopen_posix_unistd(lua_State *L);