- 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
parent 1afc3b48a1
commit 8627a48b34
10 changed files with 31 additions and 44 deletions

View file

@ -744,7 +744,7 @@ bool C_DoKey (event_t *ev, FKeyBindings *binds, FKeyBindings *doublebinds)
dclick = false; dclick = false;
// This used level.time which didn't work outside a level. // This used level.time which didn't work outside a level.
nowtime = I_msTime(); nowtime = (unsigned)I_msTime();
if (doublebinds != NULL && int(DClickTime[ev->data1] - nowtime) > 0 && ev->type == EV_KeyDown) if (doublebinds != NULL && int(DClickTime[ev->data1] - nowtime) > 0 && ev->type == EV_KeyDown)
{ {
// Key pressed for a double click // Key pressed for a double click

View file

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

View file

@ -20,7 +20,7 @@
bool FileExists (const char *filename); bool FileExists (const char *filename);
bool DirExists(const char *filename); bool DirExists(const char *filename);
bool DirEntryExists (const char *pathname); bool DirEntryExists (const char *pathname, bool *isdir = nullptr);
extern FString progdir; extern FString progdir;

View file

@ -1946,13 +1946,11 @@ static FString CheckGameInfo(TArray<FString> & pwads)
const char *filename = pwads[i]; const char *filename = pwads[i];
// Does this exist? If so, is it a directory? // Does this exist? If so, is it a directory?
struct stat info; if (!DirEntryExists(pwads[i], &isdir))
if (stat(pwads[i], &info) != 0)
{ {
Printf(TEXTCOLOR_RED "Could not stat %s\n", filename); Printf(TEXTCOLOR_RED "Could not stat %s\n", filename);
continue; continue;
} }
isdir = (info.st_mode & S_IFDIR) != 0;
if (!isdir) if (!isdir)
{ {

View file

@ -112,7 +112,6 @@ void M_FindResponseFile (void)
char **argv; char **argv;
char *file = NULL; char *file = NULL;
int argc = 0; int argc = 0;
FILE *handle;
int size; int size;
long argsize = 0; long argsize = 0;
int index; int index;

View file

@ -49,6 +49,7 @@
#include "st_console.h" #include "st_console.h"
#include "v_text.h" #include "v_text.h"
#include "x86.h" #include "x86.h"
#include "cmdlib.h"
EXTERN_CVAR(String, language) EXTERN_CVAR(String, language)
@ -336,11 +337,11 @@ int I_FindClose(void* const handle)
int I_FindAttr(findstate_t* const fileinfo) int I_FindAttr(findstate_t* const fileinfo)
{ {
dirent* const ent = fileinfo->namelist[fileinfo->current]; dirent* const ent = fileinfo->namelist[fileinfo->current];
struct stat buf; bool isdir;
if (stat(ent->d_name, &buf) == 0) if (DirEntryExists(ent->d_name, &isdir))
{ {
return S_ISDIR(buf.st_mode) ? FA_DIREC : 0; return isdir ? FA_DIREC : 0;
} }
return 0; return 0;

View file

@ -42,6 +42,7 @@
#include "d_main.h" #include "d_main.h"
#include "zstring.h" #include "zstring.h"
#include "sc_man.h" #include "sc_man.h"
#include "cmdlib.h"
static void PSR_FindEndBlock(FScanner &sc) static void PSR_FindEndBlock(FScanner &sc)
{ {
@ -224,7 +225,7 @@ TArray<FString> I_GetSteamPath()
{ {
struct stat st; struct stat st;
FString candidate(SteamInstallFolders[i] + "/" + AppInfo[app].BasePath); FString candidate(SteamInstallFolders[i] + "/" + AppInfo[app].BasePath);
if(stat(candidate, &st) == 0 && S_ISDIR(st.st_mode)) if(DirExists(candidate))
result.Push(candidate); result.Push(candidate);
} }
} }

View file

@ -397,15 +397,16 @@ int I_FindClose (void *handle)
return 0; return 0;
} }
int I_FindAttr (findstate_t *fileinfo) int I_FindAttr(findstate_t* const fileinfo)
{ {
dirent *ent = fileinfo->namelist[fileinfo->current]; dirent* const ent = fileinfo->namelist[fileinfo->current];
struct stat buf; bool isdir;
if (stat(ent->d_name, &buf) == 0) if (DirEntryExists(ent->d_name, &isdir))
{ {
return S_ISDIR(buf.st_mode) ? FA_DIREC : 0; return isdir ? FA_DIREC : 0;
} }
return 0; return 0;
} }

View file

@ -209,10 +209,7 @@ int FDirectory::AddDirectory(const char *dirpath)
FString fullFileName = scanDirectories[i] + file->d_name; FString fullFileName = scanDirectories[i] + file->d_name;
struct stat fileStat; if(DirExists(fullFileName.GetChars()))
stat(fullFileName.GetChars(), &fileStat);
if(S_ISDIR(fileStat.st_mode))
{ {
scanDirectories.Push(scanDirectories[i] + file->d_name + "/"); scanDirectories.Push(scanDirectories[i] + file->d_name + "/");
continue; continue;

View file

@ -233,14 +233,12 @@ void FWadCollection::AddFile (const char *filename, FileReader *wadinfo)
if (wadinfo == NULL) if (wadinfo == NULL)
{ {
// Does this exist? If so, is it a directory? // Does this exist? If so, is it a directory?
struct stat info; if (!DirEntryExists(filename, &isdir))
if (stat(filename, &info) != 0)
{ {
Printf(TEXTCOLOR_RED "Could not stat %s\n", filename); Printf(TEXTCOLOR_RED "%s: File or Directory not found\n", filename);
PrintLastError(); PrintLastError();
return; return;
} }
isdir = (info.st_mode & S_IFDIR) != 0;
if (!isdir) if (!isdir)
{ {