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

View File

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

View File

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