From 0e3968af0b537eb7c98fad2ab83120028ff69015 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tam=C3=A1s=20B=C3=A1lint=20Misius?= Date: Tue, 9 May 2023 22:31:12 +0200 Subject: [PATCH] Add fs.isLink --- src/common/platform/Platform.h | 1 + src/common/platform/Posix.cpp | 20 ++++++++++++++++++++ src/common/platform/Windows.cpp | 32 ++++++++++++++++++++++++++------ src/lua/LuaScriptInterface.cpp | 9 +++++++++ 4 files changed, 56 insertions(+), 6 deletions(-) diff --git a/src/common/platform/Platform.h b/src/common/platform/Platform.h index 591fe7a35..9016cd3c2 100644 --- a/src/common/platform/Platform.h +++ b/src/common/platform/Platform.h @@ -17,6 +17,7 @@ namespace Platform bool Stat(ByteString filename); bool FileExists(ByteString filename); bool DirectoryExists(ByteString directory); + bool IsLink(ByteString path); /** * @return true on success */ diff --git a/src/common/platform/Posix.cpp b/src/common/platform/Posix.cpp index af18bbf09..b514ce991 100644 --- a/src/common/platform/Posix.cpp +++ b/src/common/platform/Posix.cpp @@ -82,6 +82,26 @@ bool DirectoryExists(ByteString directory) } } +bool IsLink(ByteString path) +{ + struct stat s; + if (stat(path.c_str(), &s) == 0) + { + if (s.st_mode & S_IFLNK) + { + return true; // Is path + } + else + { + return false; // Is file or something else + } + } + else + { + return false; // Doesn't exist + } +} + bool RemoveFile(ByteString filename) { return remove(filename.c_str()) == 0; diff --git a/src/common/platform/Windows.cpp b/src/common/platform/Windows.cpp index 42864acb5..585af3a1d 100644 --- a/src/common/platform/Windows.cpp +++ b/src/common/platform/Windows.cpp @@ -11,18 +11,18 @@ #include #include #include +#include namespace Platform { ByteString GetCwd() { ByteString cwd; - wchar_t *cwdPtr = _wgetcwd(NULL, 0); + auto cwdPtr = std::unique_ptr(_wgetcwd(NULL, 0), free); if (cwdPtr) { - cwd = WinNarrow(cwdPtr); + cwd = WinNarrow(cwdPtr.get()); } - free(cwdPtr); return cwd; } @@ -47,7 +47,7 @@ long unsigned int GetTime() bool Stat(ByteString filename) { struct _stat s; - if (_stat(filename.c_str(), &s) == 0) + if (_wstat(WinWiden(filename).c_str(), &s) == 0) { return true; // Something exists, be it a file, directory, link, etc. } @@ -60,7 +60,7 @@ bool Stat(ByteString filename) bool FileExists(ByteString filename) { struct _stat s; - if (_stat(filename.c_str(), &s) == 0) + if (_wstat(WinWiden(filename).c_str(), &s) == 0) { if(s.st_mode & S_IFREG) { @@ -80,7 +80,7 @@ bool FileExists(ByteString filename) bool DirectoryExists(ByteString directory) { struct _stat s; - if (_stat(directory.c_str(), &s) == 0) + if (_wstat(WinWiden(directory).c_str(), &s) == 0) { if(s.st_mode & S_IFDIR) { @@ -97,6 +97,26 @@ bool DirectoryExists(ByteString directory) } } +bool IsLink(ByteString path) +{ + struct _stat s; + if (_wstat(WinWiden(path).c_str(), &s) == 0) + { + if (GetFileAttributesW(WinWiden(path).c_str()) & FILE_ATTRIBUTE_REPARSE_POINT) + { + return true; // Is directory + } + else + { + return false; // Is file or something else + } + } + else + { + return false; // Doesn't exist + } +} + bool RemoveFile(ByteString filename) { return _wremove(WinWiden(filename).c_str()) == 0; diff --git a/src/lua/LuaScriptInterface.cpp b/src/lua/LuaScriptInterface.cpp index 58fdb0dd3..23299cdc6 100644 --- a/src/lua/LuaScriptInterface.cpp +++ b/src/lua/LuaScriptInterface.cpp @@ -4002,6 +4002,14 @@ int LuaScriptInterface::graphics_setClipRect(lua_State * l) return 4; } +static int fsIsLink(lua_State * l) +{ + auto dirname = tpt_lua_checkByteString(l, 1); + bool ret = Platform::IsLink(dirname); + lua_pushboolean(l, ret); + return 1; +} + void LuaScriptInterface::initFileSystemAPI() { //Methods @@ -4010,6 +4018,7 @@ void LuaScriptInterface::initFileSystemAPI() {"exists", fileSystem_exists}, {"isFile", fileSystem_isFile}, {"isDirectory", fileSystem_isDirectory}, + {"isLink", fsIsLink}, {"makeDirectory", fileSystem_makeDirectory}, {"removeDirectory", fileSystem_removeDirectory}, {"removeFile", fileSystem_removeFile},