From 31587f7c869c75d1a37aa269739618232490cef2 Mon Sep 17 00:00:00 2001 From: emaste Date: Fri, 13 Mar 2020 15:40:35 +0000 Subject: [PATCH] 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 --- libexec/flua/linit_flua.c | 1 + libexec/flua/modules/lposix.c | 38 +++++++++++++++++++++++++++++++++++ libexec/flua/modules/lposix.h | 1 + 3 files changed, 40 insertions(+) diff --git a/libexec/flua/linit_flua.c b/libexec/flua/linit_flua.c index 4b4069e9ad6..99818c3b4db 100644 --- a/libexec/flua/linit_flua.c +++ b/libexec/flua/linit_flua.c @@ -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} }; diff --git a/libexec/flua/modules/lposix.c b/libexec/flua/modules/lposix.c index 208802456fe..adf3a7bb9a1 100644 --- a/libexec/flua/modules/lposix.c +++ b/libexec/flua/modules/lposix.c @@ -27,6 +27,10 @@ #include __FBSDID("$FreeBSD$"); +#include + +#include +#include #include #include @@ -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) { diff --git a/libexec/flua/modules/lposix.h b/libexec/flua/modules/lposix.h index 4c771d79769..d2d9ec0cd67 100644 --- a/libexec/flua/modules/lposix.h +++ b/libexec/flua/modules/lposix.h @@ -8,4 +8,5 @@ #include +int luaopen_posix_sys_stat(lua_State *L); int luaopen_posix_unistd(lua_State *L); -- 2.45.0