From 97ed09b3f951f90117f16e48c0b4ba344d66e945 Mon Sep 17 00:00:00 2001 From: Syping Date: Tue, 1 Oct 2019 05:59:28 +0200 Subject: [PATCH] add linkFile --- src/luaengineos/luaengine/LuaEngineOS.cpp | 47 ++++++++++++++++++++++- src/luaengineos/luaengine/LuaEngineOS.h | 1 + 2 files changed, 47 insertions(+), 1 deletion(-) 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: