diff --git a/source/common/utility/findfile.cpp b/source/common/utility/findfile.cpp index c76b9bf8c..ac0ca8fdf 100644 --- a/source/common/utility/findfile.cpp +++ b/source/common/utility/findfile.cpp @@ -42,6 +42,7 @@ #include #include +#include #include "cmdlib.h" @@ -204,43 +205,49 @@ bool D_AddFile(TArray& 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