From d7fc7684659a9e5a0a6174d7b53dda9226348b4a Mon Sep 17 00:00:00 2001 From: BPanther Date: Sat, 13 Jul 2024 01:08:07 +0200 Subject: [PATCH] Add method to check for empty files in neutrino Lua (thx dbt) --- src/gui/lua/lua_api_version.h | 2 +- src/gui/lua/lua_filehelpers.cpp | 74 +++++++++++++++++++++++++++++++++ src/gui/lua/lua_filehelpers.h | 1 + 3 files changed, 76 insertions(+), 1 deletion(-) diff --git a/src/gui/lua/lua_api_version.h b/src/gui/lua/lua_api_version.h index 665eb04de..272eb7d22 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 90 +#define LUA_API_VERSION_MINOR 97 diff --git a/src/gui/lua/lua_filehelpers.cpp b/src/gui/lua/lua_filehelpers.cpp index 520b6d10f..d6f158a48 100644 --- a/src/gui/lua/lua_filehelpers.cpp +++ b/src/gui/lua/lua_filehelpers.cpp @@ -30,6 +30,7 @@ #include #include #include +#include #include "luainstance.h" #include "lua_filehelpers.h" @@ -61,6 +62,7 @@ void CLuaInstFileHelpers::LuaFileHelpersRegister(lua_State *L) { "readlink", CLuaInstFileHelpers::FileHelpersReadlink }, { "ln", CLuaInstFileHelpers::FileHelpersLn }, { "exist", CLuaInstFileHelpers::FileHelpersExist }, + { "empty", CLuaInstFileHelpers::FileHelpersIsEmpty }, { "__gc", CLuaInstFileHelpers::FileHelpersDelete }, { NULL, NULL } }; @@ -529,6 +531,78 @@ int CLuaInstFileHelpers::FileHelpersExist(lua_State *L) return 1; } +int CLuaInstFileHelpers::FileHelpersIsEmpty(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 isempty: not enough arguments (%d, expected %d)\n", numargs, min_numargs); + lua_pushnil(L); + return 1; + } + + if (!lua_isstring(L, 2)) { + printf("%s: argument 1 is not a string.\n", __func__); + lua_pushboolean(L, false); + return 1; + } + + bool ret = false; + bool err = false; + int errLine = 0; + std::string errMsg = ""; + + const char *file = luaL_checkstring(L, 2); + + if (file_exists(file)) { + struct stat FileInfo; + if (lstat(file, &FileInfo) == -1) { + err = true; + errLine = __LINE__; + errMsg = (std::string)strerror(errno); + } else if (S_ISLNK(FileInfo.st_mode)) { + printf("Following symlink: %s\n", file); + if (stat(file, &FileInfo) == -1) { + err = true; + errLine = __LINE__; + errMsg = "Symlink target does not exist: " + std::string(strerror(errno)); + } else if (S_ISREG(FileInfo.st_mode)) { + ret = (FileInfo.st_size == 0); + } else { + err = true; + errLine = __LINE__; + errMsg = "Target of symlink is not a regular file."; + } + } else if (S_ISREG(FileInfo.st_mode)) { + ret = (FileInfo.st_size == 0); + } else { + err = true; + errLine = __LINE__; + errMsg = std::string(file) + " is not a regular file."; + } + } else { + err = true; + errLine = __LINE__; + errMsg = std::string(file) + " does not exist."; + } + + if (err) { + lua_Debug ar; + lua_getstack(L, 1, &ar); + lua_getinfo(L, "Sl", &ar); + printf(">>> Lua script error [%s:%d] %s\n (error from neutrino: [%s:%d])\n", + ar.short_src, ar.currentline, errMsg.c_str(), __path_file__, errLine); + DisplayErrorMessage(errMsg.c_str(), "Lua Script Error:"); + lua_pushnil(L); + return 1; + } + + lua_pushboolean(L, ret); + return 1; +} int CLuaInstFileHelpers::FileHelpersDelete(lua_State *L) { diff --git a/src/gui/lua/lua_filehelpers.h b/src/gui/lua/lua_filehelpers.h index 0f119f615..035951f76 100644 --- a/src/gui/lua/lua_filehelpers.h +++ b/src/gui/lua/lua_filehelpers.h @@ -48,6 +48,7 @@ class CLuaInstFileHelpers static int FileHelpersLn(lua_State *L); static int FileHelpersExist(lua_State *L); static int FileHelpersDelete(lua_State *L); + static int FileHelpersIsEmpty(lua_State *L); }; #endif //_LUAFILEHELPERS_H