Remove dependency on dirent.h on windows

Also fix a few bugs and other weirdness in Platform::DirectorySearch. Empty string paths would crash and filenames with 4 or fewer characters wouldn't register.
This commit is contained in:
Tamás Bálint Misius 2022-08-30 20:39:44 +02:00
parent 6ba5de6034
commit 9e712eba08
No known key found for this signature in database
GPG Key ID: 5B472A12F6ECA9F2
3 changed files with 19 additions and 61 deletions

View File

@ -8,7 +8,6 @@
#include <ctime>
#include <cstdio>
#include <fstream>
#include <dirent.h>
#ifdef MACOSX
# include "common/macosx.h"
@ -50,16 +49,6 @@
#include "client/http/RequestManager.h"
#include "gui/preview/Comment.h"
extern "C"
{
#if defined(WIN) && !defined(__GNUC__)
#include <io.h>
#else
#include <dirent.h>
#endif
}
Client::Client():
messageOfTheDay("Fetching the message of the day..."),
versionCheckRequest(nullptr),
@ -991,22 +980,13 @@ void Client::updateStamps()
void Client::RescanStamps()
{
DIR * directory;
struct dirent * entry;
directory = opendir("stamps");
if (directory != NULL)
{
stampIDs.clear();
while ((entry = readdir(directory)))
for (auto &stamp : Platform::DirectorySearch("stamps", "", { ".stm" }))
{
ByteString name = entry->d_name;
if(name != ".." && name != "." && name.EndsWith(".stm") && name.size() == 14)
stampIDs.push_front(name.Substr(0, 10));
stampIDs.push_front(stamp.Substr(0, 10));
}
closedir(directory);
stampIDs.sort(std::greater<ByteString>());
updateStamps();
}
}
int Client::GetStampsCount()

View File

@ -4,7 +4,6 @@
#include <cstring>
#include <cstdio>
#include <cassert>
#include <dirent.h>
#include <fstream>
#include <sys/stat.h>
@ -22,6 +21,7 @@
# include <unistd.h>
# include <ctime>
# include <sys/time.h>
# include <dirent.h>
#endif
#ifdef MACOSX
# include <mach-o/dyld.h>
@ -291,11 +291,10 @@ std::vector<ByteString> DirectorySearch(ByteString directory, ByteString search,
{
//Get full file listing
//Normalise directory string, ensure / or \ is present
if (*directory.rbegin() != '/' && *directory.rbegin() != '\\')
if (!directory.size() || (directory.back() != '/' && directory.back() != '\\'))
directory += PATH_SEP;
std::vector<ByteString> directoryList;
#if defined(WIN) && !defined(__GNUC__)
//Windows
#ifdef WIN
struct _wfinddata_t currentFile;
intptr_t findFileHandle;
ByteString fileMatch = directory + "*.*";
@ -306,14 +305,11 @@ std::vector<ByteString> DirectorySearch(ByteString directory, ByteString search,
}
do
{
ByteString currentFileName = Platform::WinNarrow(currentFile.name);
if (currentFileName.length() > 4)
directoryList.push_back(currentFileName);
directoryList.push_back(Platform::WinNarrow(currentFile.name));
}
while (_wfindnext(findFileHandle, &currentFile) == 0);
_findclose(findFileHandle);
#else
//Linux or MinGW
struct dirent * directoryEntry;
DIR *directoryHandle = opendir(directory.c_str());
if (!directoryHandle)
@ -322,9 +318,7 @@ std::vector<ByteString> DirectorySearch(ByteString directory, ByteString search,
}
while ((directoryEntry = readdir(directoryHandle)))
{
ByteString currentFileName = ByteString(directoryEntry->d_name);
if (currentFileName.length()>4)
directoryList.push_back(currentFileName);
directoryList.push_back(ByteString(directoryEntry->d_name));
}
closedir(directoryHandle);
#endif
@ -336,12 +330,12 @@ std::vector<ByteString> DirectorySearch(ByteString directory, ByteString search,
{
ByteString filename = *iter, tempfilename = *iter;
bool extensionMatch = !extensions.size();
for (std::vector<ByteString>::iterator extIter = extensions.begin(), extEnd = extensions.end(); extIter != extEnd; ++extIter)
for (auto &extension : extensions)
{
if (filename.EndsWith(*extIter))
if (filename.size() >= extension.size() && filename.EndsWith(extension))
{
extensionMatch = true;
tempfilename = filename.SubstrFromEnd(0, (*extIter).size()).ToLower();
tempfilename = filename.SubstrFromEnd(0, extension.size()).ToLower();
break;
}
}

View File

@ -59,7 +59,6 @@ extern "C"
#include <direct.h>
#endif
#include <sys/stat.h>
#include <dirent.h>
}
#include "eventcompat.lua.h"
@ -3783,32 +3782,17 @@ void LuaScriptInterface::initFileSystemAPI()
int LuaScriptInterface::fileSystem_list(lua_State * l)
{
auto directoryName = tpt_lua_checkByteString(l, 1);
int index = 1;
lua_newtable(l);
DIR * directory;
struct dirent * entry;
// FIXME: winapi
directory = opendir(directoryName.c_str());
if (directory != NULL)
int index = 0;
for (auto &name : Platform::DirectorySearch(directoryName, "", {}))
{
while ((entry = readdir(directory)))
if (name != "." && name != "..")
{
if(strncmp(entry->d_name, "..", 3) && strncmp(entry->d_name, ".", 2))
{
lua_pushstring(l, entry->d_name);
lua_rawseti(l, -2, index++);
index += 1;
lua_pushstring(l, name.c_str());
lua_rawseti(l, -2, index);
}
}
closedir(directory);
}
else
{
lua_pushnil(l);
}
return 1;
}