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