diff --git a/src/luaengineos/luaengine/LuaEngineOS.cpp b/src/luaengineos/luaengine/LuaEngineOS.cpp index 1335c29..379c03f 100644 --- a/src/luaengineos/luaengine/LuaEngineOS.cpp +++ b/src/luaengineos/luaengine/LuaEngineOS.cpp @@ -20,7 +20,14 @@ #include #include #include -#include +#include +#include +#ifdef Q_OS_WIN +#include "windows.h" +#include +#else +#include "unistd.h" +#endif LuaEngineOS::LuaEngineOS(QObject *parent, bool loadBaseLibraries) : LuaEngine(parent, loadBaseLibraries) { @@ -33,6 +40,11 @@ void LuaEngineOS::pushClass(lua_State *L_p) // Directory pushFunction(L_p, "directoryListFiles", directoryListFiles); + // File + pushFunction(L_p, "linkFile", linkFile); + pushVariant(L_p, "Symlink", (int)0); + pushVariant(L_p, "Hardlink", (int)1); + // Process pushFunction(L_p, "executeProcess", executeProcess); } @@ -61,6 +73,39 @@ int LuaEngineOS::directoryListFiles(lua_State *L_p) return 0; } +int LuaEngineOS::linkFile(lua_State *L_p) +{ + if (getArgumentCount(L_p) >= 2) { + bool symlinkMode = true; + if (getArgumentCount(L_p) >= 3 && getVariant(L_p, 3).toInt() == 1) { + symlinkMode = false; + } + bool linkSucceeded = false; + if (symlinkMode) { +#ifdef Q_OS_WIN + QString targetFile = getVariant(L_p, 2).toString(); + if (targetFile.right(4) != ".lnk") { + targetFile += ".lnk"; + } + linkSucceeded = QFile::link(getVariant(L_p, 1).toString(), targetFile); +#else + linkSucceeded = QFile::link(getVariant(L_p, 1).toString(), getVariant(L_p, 2).toString()); +#endif + } + else { +#ifdef Q_OS_WIN + linkSucceeded = CreateHardLinkW(getVariant(L_p, 2).toString().toStdWString().c_str(), getVariant(L_p, 1).toString().toStdWString().c_str(), NULL); +#else + linkSucceeded = link(getVariant(L_p, 1).toString().toUtf8().data(), getVariant(L_p, 2).toString().toUtf8().data()); +#endif + } + getVariant(L_p, linkSucceeded); + return 1; + } + getVariant(L_p, false); + return 1; +} + int LuaEngineOS::executeProcess(lua_State *L_p) { if (getArgumentCount(L_p) >= 1) { diff --git a/src/luaengineos/luaengine/LuaEngineOS.h b/src/luaengineos/luaengine/LuaEngineOS.h index ba54718..a67d4de 100644 --- a/src/luaengineos/luaengine/LuaEngineOS.h +++ b/src/luaengineos/luaengine/LuaEngineOS.h @@ -31,6 +31,7 @@ public: static void pushClass(lua_State *L_p); static void pushClass(LuaEngine *luaEngine); static int directoryListFiles(lua_State *L_p); + static int linkFile(lua_State *L_p); static int executeProcess(lua_State *L_p); private: