From a956b1c01fa1052dc68cc8cee9965dec4264e2d0 Mon Sep 17 00:00:00 2001 From: Michael Liebmann Date: Sun, 4 Sep 2016 18:54:50 +0200 Subject: [PATCH] lua_filehelpers.cpp: Add lua script function 'touch()' - Set Lua api version to 1.50 Origin commit data ------------------ Commit: https://github.com/neutrino-images/ni-neutrino/commit/ab322416dc124cf1dc51170198e9f017c131fdee 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 | 64 +++++++++++++++++++++++++++++++++ src/gui/lua/lua_filehelpers.h | 1 + 3 files changed, 66 insertions(+), 1 deletion(-) diff --git a/src/gui/lua/lua_api_version.h b/src/gui/lua/lua_api_version.h index 54b49c965..26835c5c1 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 49 +#define LUA_API_VERSION_MINOR 50 diff --git a/src/gui/lua/lua_filehelpers.cpp b/src/gui/lua/lua_filehelpers.cpp index e27d46fc9..5a5c5a31f 100644 --- a/src/gui/lua/lua_filehelpers.cpp +++ b/src/gui/lua/lua_filehelpers.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include #include @@ -53,6 +54,7 @@ void CLuaInstFileHelpers::LuaFileHelpersRegister(lua_State *L) { "new", CLuaInstFileHelpers::FileHelpersNew }, { "cp", CLuaInstFileHelpers::FileHelpersCp }, { "chmod", CLuaInstFileHelpers::FileHelpersChmod }, + { "touch", CLuaInstFileHelpers::FileHelpersTouch }, { "__gc", CLuaInstFileHelpers::FileHelpersDelete }, { NULL, NULL } }; @@ -161,6 +163,68 @@ int CLuaInstFileHelpers::FileHelpersChmod(lua_State *L) return 1; } +int CLuaInstFileHelpers::FileHelpersTouch(lua_State *L) +{ + CLuaFileHelpers *D = FileHelpersCheckData(L, 1); + if (!D) return 0; + + int numargs = lua_gettop(L) - 1; + int min_numargs = 1; + if (numargs < min_numargs) { + printf("luascript touch: not enough arguments (%d, expected %d)\n", numargs, min_numargs); + lua_pushboolean(L, false); + return 1; + } + + const char *file = ""; + file = luaL_checkstring(L, 2); + + bool ret = true; + lua_Debug ar; + + if (!file_exists(file)) { + FILE *f = fopen(file, "w"); + if (f == NULL) { + ret = false; + 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; + } + fclose(f); + if (numargs == min_numargs) { + lua_pushboolean(L, ret); + return 1; + } + } + + time_t modTime; + if (numargs == min_numargs) + /* current time */ + modTime = time(NULL); + else + /* new time */ + modTime = (time_t)luaL_checkint(L, 3); + + utimbuf utb; + utb.actime = modTime; + utb.modtime = modTime; + if (utime(file, &utb) != 0) { + ret = false; + 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 72dc9c871..94dbff985 100644 --- a/src/gui/lua/lua_filehelpers.h +++ b/src/gui/lua/lua_filehelpers.h @@ -41,6 +41,7 @@ class CLuaInstFileHelpers static int FileHelpersNew(lua_State *L); static int FileHelpersCp(lua_State *L); static int FileHelpersChmod(lua_State *L); + static int FileHelpersTouch(lua_State *L); static int FileHelpersDelete(lua_State *L); };