Add fs.isLink

This commit is contained in:
Tamás Bálint Misius
2023-05-09 22:31:12 +02:00
parent 65cf8021c9
commit 0e3968af0b
4 changed files with 56 additions and 6 deletions

View File

@@ -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
*/

View File

@@ -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;

View File

@@ -11,18 +11,18 @@
#include <shlwapi.h>
#include <windows.h>
#include <crtdbg.h>
#include <memory>
namespace Platform
{
ByteString GetCwd()
{
ByteString cwd;
wchar_t *cwdPtr = _wgetcwd(NULL, 0);
auto cwdPtr = std::unique_ptr<wchar_t, decltype(&free)>(_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;

View File

@@ -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},