- consolidation of 'stat' calls.

Since this is a non-standard function it's better kept to as few places as possible, so now DirEntryExists returns an additional flag to say what type an entry is and is being used nearly everywhere where stat was used, excluding a few low level parts in the POSIX code.
This commit is contained in:
Christoph Oelckers 2017-12-02 16:07:09 +01:00
commit 8627a48b34
10 changed files with 31 additions and 44 deletions

View file

@ -155,13 +155,9 @@ void ReplaceString (char **ptr, const char *str)
bool FileExists (const char *filename)
{
struct stat buff;
// [RH] Empty filenames are never there
if (filename == NULL || *filename == 0)
return false;
return stat(filename, &buff) == 0 && !(buff.st_mode & S_IFDIR);
bool isdir;
bool res = DirEntryExists(filename, &isdir);
return res && !isdir;
}
//==========================================================================
@ -174,13 +170,9 @@ bool FileExists (const char *filename)
bool DirExists(const char *filename)
{
struct stat buff;
// [RH] Empty filenames are never there
if (filename == NULL || *filename == 0)
return false;
return stat(filename, &buff) == 0 && (buff.st_mode & S_IFDIR);
bool isdir;
bool res = DirEntryExists(filename, &isdir);
return res && isdir;
}
//==========================================================================
@ -191,13 +183,16 @@ bool DirExists(const char *filename)
//
//==========================================================================
bool DirEntryExists(const char *pathname)
bool DirEntryExists(const char *pathname, bool *isdir)
{
if (isdir) *isdir = false;
if (pathname == NULL || *pathname == 0)
return false;
struct stat info;
return stat(pathname, &info) == 0;
bool res = stat(pathname, &info) == 0;
if (isdir) *isdir = !!(info.st_mode & S_IFDIR);
return res;
}
//==========================================================================
@ -550,7 +545,7 @@ void CreatePath(const char *fn)
*p = '\0';
}
struct stat info;
if (stat(copy, &info) == 0)
if (DirEntryExists(copy))
{
if (info.st_mode & S_IFDIR)
goto exists;
@ -1009,10 +1004,7 @@ void ScanDirectory(TArray<FFileList> &list, const char *dirpath)
FFileList *fl = &list[list.Reserve(1)];
fl->Filename << dirpath << file->d_name;
struct stat fileStat;
stat(fl->Filename, &fileStat);
fl->isDirectory = S_ISDIR(fileStat.st_mode);
fl->isDirectory = DirExists(fl->Filename);
if(fl->isDirectory)
{
FString newdir = fl->Filename;