From 3bd7c3f7d9e812fcbb5fd536b36ca1268b0c0c29 Mon Sep 17 00:00:00 2001 From: Michael Liebmann Date: Sun, 4 Sep 2016 18:54:45 +0200 Subject: [PATCH] lua_filehelpers.cpp: Add lua script function 'chmod()' - Set Lua api version to 1.49 Origin commit data ------------------ Commit: https://github.com/neutrino-images/ni-neutrino/commit/ab7d90de3f20a22bb5f6f7af5972304a5b67a26c Author: Michael Liebmann Date: 2016-09-04 (Sun, 04 Sep 2016) --- src/gui/lua/lua_api_version.h | 2 +- src/gui/lua/lua_filehelpers.cpp | 39 +++++++++++++++++++++++++++++++++ src/gui/lua/lua_filehelpers.h | 1 + 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/gui/lua/lua_api_version.h b/src/gui/lua/lua_api_version.h index e2a052eab..54b49c965 100644 --- a/src/gui/lua/lua_api_version.h +++ b/src/gui/lua/lua_api_version.h @@ -4,4 +4,4 @@ * to luainstance.h changes */ #define LUA_API_VERSION_MAJOR 1 -#define LUA_API_VERSION_MINOR 48 +#define LUA_API_VERSION_MINOR 49 diff --git a/src/gui/lua/lua_filehelpers.cpp b/src/gui/lua/lua_filehelpers.cpp index 4b11c3bdf..e27d46fc9 100644 --- a/src/gui/lua/lua_filehelpers.cpp +++ b/src/gui/lua/lua_filehelpers.cpp @@ -23,6 +23,7 @@ #include #include #include +#include #include #include @@ -51,6 +52,7 @@ void CLuaInstFileHelpers::LuaFileHelpersRegister(lua_State *L) luaL_Reg meth[] = { { "new", CLuaInstFileHelpers::FileHelpersNew }, { "cp", CLuaInstFileHelpers::FileHelpersCp }, + { "chmod", CLuaInstFileHelpers::FileHelpersChmod }, { "__gc", CLuaInstFileHelpers::FileHelpersDelete }, { NULL, NULL } }; @@ -122,6 +124,43 @@ int CLuaInstFileHelpers::FileHelpersCp(lua_State *L) return 1; } +int CLuaInstFileHelpers::FileHelpersChmod(lua_State *L) +{ + CLuaFileHelpers *D = FileHelpersCheckData(L, 1); + if (!D) return 0; + + int numargs = lua_gettop(L) - 1; + int min_numargs = 2; + if (numargs < min_numargs) { + printf("luascript chmod: not enough arguments (%d, expected %d)\n", numargs, min_numargs); + lua_pushboolean(L, false); + return 1; + } + + const char *file = ""; + file = luaL_checkstring(L, 2); + + int mode_i = luaL_checkint(L, 3); + /* Hack for convert lua number to octal */ + std::string mode_s = itoa(mode_i, 10); + mode_t mode = (mode_t)(strtol(mode_s.c_str(), (char **)NULL, 8) & 0x0FFF); + //printf("\n##### [%s:%d] str: %s, okt: %o \n \n", __func__, __LINE__, mode_s.c_str(), (int)mode); + + bool ret = true; + if (chmod(file, mode) != 0) { + ret = false; + lua_Debug ar; + lua_getstack(L, 1, &ar); + lua_getinfo(L, "Sl", &ar); + const char* s = strerror(errno); + printf(">>> Lua script error [%s:%d] %s\n (error from neutrino: [%s:%d])\n", + ar.short_src, ar.currentline, s, __path_file__, __LINE__); + } + + lua_pushboolean(L, ret); + return 1; +} + diff --git a/src/gui/lua/lua_filehelpers.h b/src/gui/lua/lua_filehelpers.h index 7bdd8b642..72dc9c871 100644 --- a/src/gui/lua/lua_filehelpers.h +++ b/src/gui/lua/lua_filehelpers.h @@ -40,6 +40,7 @@ class CLuaInstFileHelpers static CLuaFileHelpers *FileHelpersCheckData(lua_State *L, int n); static int FileHelpersNew(lua_State *L); static int FileHelpersCp(lua_State *L); + static int FileHelpersChmod(lua_State *L); static int FileHelpersDelete(lua_State *L); };