- D_AddFile(): Check the existence of the file before enumerating entire directory to find it as input might be coming in correctly.

This commit is contained in:
Mitchell Richters 2020-10-15 06:50:41 +11:00
parent ec5762f024
commit 38c0af4d34

View file

@ -42,6 +42,7 @@
#include <unistd.h>
#include <fnmatch.h>
#include <sys/stat.h>
#include "cmdlib.h"
@ -204,43 +205,49 @@ bool D_AddFile(TArray<FString>& wadfiles, const char* file, bool check, int posi
return false;
}
#ifdef __unix__
// Test case sensitively, pure lowercase and pure uppercase.
FString fullpath = file;
auto lastindex = fullpath.LastIndexOf("/");
FString basepath = fullpath.Left(lastindex);
FString filename = fullpath.Right(fullpath.Len() - lastindex - 1);
if (filename.IsNotEmpty())
// Confirm file exists in filesystem.
struct stat info;
bool found = stat(file, &info) == 0;
if (!found)
{
bool found = false;
DIR *d;
struct dirent *dir;
d = opendir(basepath.GetChars());
if (d)
// File not found, so split file into path and filename so we can enumerate the path for the file.
FString fullpath = file;
auto lastindex = fullpath.LastIndexOf("/");
FString basepath = fullpath.Left(lastindex);
FString filename = fullpath.Right(fullpath.Len() - lastindex - 1);
// Proceed only if locating a file (i.e. `file` isn't a path to just a directory.)
if (filename.IsNotEmpty())
{
while ((dir = readdir(d)) != NULL)
DIR *d;
struct dirent *dir;
d = opendir(basepath.GetChars());
if (d)
{
if (filename.CompareNoCase(dir->d_name) == 0)
while ((dir = readdir(d)) != NULL)
{
found = true;
filename = dir->d_name;
fullpath = basepath << "/" << filename;
file = fullpath.GetChars();
break;
if (filename.CompareNoCase(dir->d_name) == 0)
{
found = true;
filename = dir->d_name;
fullpath = basepath << "/" << filename;
file = fullpath.GetChars();
break;
}
}
closedir(d);
if (!found)
{
Printf("Can't find file '%s' in '%s'\n", filename.GetChars(), basepath.GetChars());
return false;
}
}
closedir(d);
if (!found)
else
{
Printf("Can't find file '%s'\n", filename.GetChars());
Printf("Can't open directory '%s'\n", basepath.GetChars());
return false;
}
}
else
{
Printf("Can't open directory '%s'\n", basepath.GetChars());
return false;
}
}
#endif