mirror of
https://github.com/tuxbox-neutrino/neutrino.git
synced 2025-08-27 15:32:59 +02:00
lua_filehelpers.cpp: Add lua script function 'ln()'
- Set Lua api version to 1.54
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 53
|
#define LUA_API_VERSION_MINOR 54
|
||||||
|
@@ -58,6 +58,7 @@ void CLuaInstFileHelpers::LuaFileHelpersRegister(lua_State *L)
|
|||||||
{ "rmdir", CLuaInstFileHelpers::FileHelpersRmdir },
|
{ "rmdir", CLuaInstFileHelpers::FileHelpersRmdir },
|
||||||
{ "mkdir", CLuaInstFileHelpers::FileHelpersMkdir },
|
{ "mkdir", CLuaInstFileHelpers::FileHelpersMkdir },
|
||||||
{ "readlink", CLuaInstFileHelpers::FileHelpersReadlink },
|
{ "readlink", CLuaInstFileHelpers::FileHelpersReadlink },
|
||||||
|
{ "ln", CLuaInstFileHelpers::FileHelpersLn },
|
||||||
{ "__gc", CLuaInstFileHelpers::FileHelpersDelete },
|
{ "__gc", CLuaInstFileHelpers::FileHelpersDelete },
|
||||||
{ NULL, NULL }
|
{ NULL, NULL }
|
||||||
};
|
};
|
||||||
@@ -339,6 +340,65 @@ int CLuaInstFileHelpers::FileHelpersReadlink(lua_State *L)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int CLuaInstFileHelpers::FileHelpersLn(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 ln: not enough arguments (%d, expected %d)\n", numargs, min_numargs);
|
||||||
|
lua_pushboolean(L, false);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
const char *src = "";
|
||||||
|
src = luaL_checkstring(L, 2);
|
||||||
|
const char *link = "";
|
||||||
|
link = luaL_checkstring(L, 3);
|
||||||
|
|
||||||
|
const char *flags = "";
|
||||||
|
if (numargs > min_numargs)
|
||||||
|
flags = luaL_checkstring(L, 4);
|
||||||
|
|
||||||
|
bool symlnk = (strchr(flags, 's') != NULL);
|
||||||
|
bool force = (strchr(flags, 'f') != NULL);
|
||||||
|
lua_Debug ar;
|
||||||
|
|
||||||
|
if (!symlnk) {
|
||||||
|
lua_getstack(L, 1, &ar);
|
||||||
|
lua_getinfo(L, "Sl", &ar);
|
||||||
|
const char* s = "Currently only supports symlinks.";
|
||||||
|
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, false);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ret = true;
|
||||||
|
if (symlink(src, link) != 0) {
|
||||||
|
if (force && (errno == EEXIST)) {
|
||||||
|
if (unlink(link) == 0) {
|
||||||
|
if (symlink(src, link) == 0) {
|
||||||
|
lua_pushboolean(L, ret);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int CLuaInstFileHelpers::FileHelpersDelete(lua_State *L)
|
int CLuaInstFileHelpers::FileHelpersDelete(lua_State *L)
|
||||||
|
@@ -45,6 +45,7 @@ class CLuaInstFileHelpers
|
|||||||
static int FileHelpersRmdir(lua_State *L);
|
static int FileHelpersRmdir(lua_State *L);
|
||||||
static int FileHelpersMkdir(lua_State *L);
|
static int FileHelpersMkdir(lua_State *L);
|
||||||
static int FileHelpersReadlink(lua_State *L);
|
static int FileHelpersReadlink(lua_State *L);
|
||||||
|
static int FileHelpersLn(lua_State *L);
|
||||||
static int FileHelpersDelete(lua_State *L);
|
static int FileHelpersDelete(lua_State *L);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user