Author: emaste
Date: Fri Mar 13 15:40:35 2020
New Revision: 358960
URL: https://svnweb.freebsd.org/changeset/base/358960

Log:
  flua: implement chmod
  
  Lua does not provide a native way to change the permission of a file.
  
  Submitted by: Yang Wang <2...@outlook.jp>
  Reviewed by:  kevans
  Sponsored by: The FreeBSD Foundation
  Differential Revision:        https://reviews.freebsd.org/D24036

Modified:
  head/libexec/flua/linit_flua.c
  head/libexec/flua/modules/lposix.c
  head/libexec/flua/modules/lposix.h

Modified: head/libexec/flua/linit_flua.c
==============================================================================
--- head/libexec/flua/linit_flua.c      Fri Mar 13 14:51:11 2020        
(r358959)
+++ head/libexec/flua/linit_flua.c      Fri Mar 13 15:40:35 2020        
(r358960)
@@ -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}
 };

Modified: head/libexec/flua/modules/lposix.c
==============================================================================
--- head/libexec/flua/modules/lposix.c  Fri Mar 13 14:51:11 2020        
(r358959)
+++ head/libexec/flua/modules/lposix.c  Fri Mar 13 15:40:35 2020        
(r358960)
@@ -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>
@@ -38,6 +42,28 @@ __FBSDID("$FreeBSD$");
  */
 
 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)
 {
        int n;
@@ -49,11 +75,23 @@ 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)

Modified: head/libexec/flua/modules/lposix.h
==============================================================================
--- head/libexec/flua/modules/lposix.h  Fri Mar 13 14:51:11 2020        
(r358959)
+++ head/libexec/flua/modules/lposix.h  Fri Mar 13 15:40:35 2020        
(r358960)
@@ -8,4 +8,5 @@
 
 #include <lua.h>
 
+int luaopen_posix_sys_stat(lua_State *L);
 int luaopen_posix_unistd(lua_State *L);
_______________________________________________
svn-src-head@freebsd.org mailing list
https://lists.freebsd.org/mailman/listinfo/svn-src-head
To unsubscribe, send any mail to "svn-src-head-unsubscr...@freebsd.org"

Reply via email to