mirror of
https://github.com/tuxbox-neutrino/neutrino.git
synced 2025-08-26 15:02:56 +02:00
neutrino: Add method to check for empty files in neutrino Lua API
doc: -https://wiki.tuxbox-neutrino.org/wiki/Lua:Neutrino-API:empty:de -https://wiki.tuxbox-neutrino.org/wiki/Lua:Neutrino-API:empty:en
This commit is contained in:
@@ -4,4 +4,4 @@
|
|||||||
* to luainstance.h changes
|
* to luainstance.h changes
|
||||||
*/
|
*/
|
||||||
#define LUA_API_VERSION_MAJOR 1
|
#define LUA_API_VERSION_MAJOR 1
|
||||||
#define LUA_API_VERSION_MINOR 96
|
#define LUA_API_VERSION_MINOR 97
|
||||||
|
@@ -30,6 +30,7 @@
|
|||||||
#include <system/debug.h>
|
#include <system/debug.h>
|
||||||
#include <system/helpers.h>
|
#include <system/helpers.h>
|
||||||
#include <neutrino.h>
|
#include <neutrino.h>
|
||||||
|
#include <gui/widget/msgbox.h>
|
||||||
|
|
||||||
#include "luainstance.h"
|
#include "luainstance.h"
|
||||||
#include "lua_filehelpers.h"
|
#include "lua_filehelpers.h"
|
||||||
@@ -60,6 +61,7 @@ void CLuaInstFileHelpers::LuaFileHelpersRegister(lua_State *L)
|
|||||||
{ "readlink", CLuaInstFileHelpers::FileHelpersReadlink },
|
{ "readlink", CLuaInstFileHelpers::FileHelpersReadlink },
|
||||||
{ "ln", CLuaInstFileHelpers::FileHelpersLn },
|
{ "ln", CLuaInstFileHelpers::FileHelpersLn },
|
||||||
{ "exist", CLuaInstFileHelpers::FileHelpersExist },
|
{ "exist", CLuaInstFileHelpers::FileHelpersExist },
|
||||||
|
{ "empty", CLuaInstFileHelpers::FileHelpersIsEmpty },
|
||||||
{ "__gc", CLuaInstFileHelpers::FileHelpersDelete },
|
{ "__gc", CLuaInstFileHelpers::FileHelpersDelete },
|
||||||
{ NULL, NULL }
|
{ NULL, NULL }
|
||||||
};
|
};
|
||||||
@@ -486,6 +488,78 @@ int CLuaInstFileHelpers::FileHelpersExist(lua_State *L)
|
|||||||
return 1;
|
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)
|
int CLuaInstFileHelpers::FileHelpersDelete(lua_State *L)
|
||||||
{
|
{
|
||||||
|
@@ -48,6 +48,7 @@ class CLuaInstFileHelpers
|
|||||||
static int FileHelpersLn(lua_State *L);
|
static int FileHelpersLn(lua_State *L);
|
||||||
static int FileHelpersExist(lua_State *L);
|
static int FileHelpersExist(lua_State *L);
|
||||||
static int FileHelpersDelete(lua_State *L);
|
static int FileHelpersDelete(lua_State *L);
|
||||||
|
static int FileHelpersIsEmpty(lua_State *L);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif //_LUAFILEHELPERS_H
|
#endif //_LUAFILEHELPERS_H
|
||||||
|
Reference in New Issue
Block a user