From 8627a48b343d72f7941a554079c00f150c94a1ff Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Sat, 2 Dec 2017 16:07:09 +0100 Subject: [PATCH] - 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. --- src/c_bind.cpp | 2 +- src/cmdlib.cpp | 34 +++++++++++----------------- src/cmdlib.h | 2 +- src/d_main.cpp | 4 +--- src/m_misc.cpp | 1 - src/posix/cocoa/i_system.mm | 7 +++--- src/posix/i_steam.cpp | 3 ++- src/posix/sdl/i_system.cpp | 11 +++++---- src/resourcefiles/file_directory.cpp | 5 +--- src/w_wad.cpp | 6 ++--- 10 files changed, 31 insertions(+), 44 deletions(-) diff --git a/src/c_bind.cpp b/src/c_bind.cpp index b1a5575149..5098b9a95a 100644 --- a/src/c_bind.cpp +++ b/src/c_bind.cpp @@ -744,7 +744,7 @@ bool C_DoKey (event_t *ev, FKeyBindings *binds, FKeyBindings *doublebinds) dclick = false; // 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) { // Key pressed for a double click diff --git a/src/cmdlib.cpp b/src/cmdlib.cpp index f2d4389d1e..bccb4afcd6 100644 --- a/src/cmdlib.cpp +++ b/src/cmdlib.cpp @@ -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 &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; diff --git a/src/cmdlib.h b/src/cmdlib.h index f168cb9102..76a2798dab 100644 --- a/src/cmdlib.h +++ b/src/cmdlib.h @@ -20,7 +20,7 @@ bool FileExists (const char *filename); bool DirExists(const char *filename); -bool DirEntryExists (const char *pathname); +bool DirEntryExists (const char *pathname, bool *isdir = nullptr); extern FString progdir; diff --git a/src/d_main.cpp b/src/d_main.cpp index 301be20f8b..9d8055b0d8 100644 --- a/src/d_main.cpp +++ b/src/d_main.cpp @@ -1946,13 +1946,11 @@ static FString CheckGameInfo(TArray & pwads) const char *filename = pwads[i]; // Does this exist? If so, is it a directory? - struct stat info; - if (stat(pwads[i], &info) != 0) + if (!DirEntryExists(pwads[i], &isdir)) { Printf(TEXTCOLOR_RED "Could not stat %s\n", filename); continue; } - isdir = (info.st_mode & S_IFDIR) != 0; if (!isdir) { diff --git a/src/m_misc.cpp b/src/m_misc.cpp index d074152482..c2fa021f6d 100644 --- a/src/m_misc.cpp +++ b/src/m_misc.cpp @@ -112,7 +112,6 @@ void M_FindResponseFile (void) char **argv; char *file = NULL; int argc = 0; - FILE *handle; int size; long argsize = 0; int index; diff --git a/src/posix/cocoa/i_system.mm b/src/posix/cocoa/i_system.mm index bb1ea2e1b9..3be9a067ce 100644 --- a/src/posix/cocoa/i_system.mm +++ b/src/posix/cocoa/i_system.mm @@ -49,6 +49,7 @@ #include "st_console.h" #include "v_text.h" #include "x86.h" +#include "cmdlib.h" EXTERN_CVAR(String, language) @@ -336,11 +337,11 @@ int I_FindClose(void* const handle) int I_FindAttr(findstate_t* const fileinfo) { 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; diff --git a/src/posix/i_steam.cpp b/src/posix/i_steam.cpp index 9542bdd4aa..f233f4ce81 100644 --- a/src/posix/i_steam.cpp +++ b/src/posix/i_steam.cpp @@ -42,6 +42,7 @@ #include "d_main.h" #include "zstring.h" #include "sc_man.h" +#include "cmdlib.h" static void PSR_FindEndBlock(FScanner &sc) { @@ -224,7 +225,7 @@ TArray I_GetSteamPath() { struct stat st; FString candidate(SteamInstallFolders[i] + "/" + AppInfo[app].BasePath); - if(stat(candidate, &st) == 0 && S_ISDIR(st.st_mode)) + if(DirExists(candidate)) result.Push(candidate); } } diff --git a/src/posix/sdl/i_system.cpp b/src/posix/sdl/i_system.cpp index 7bf23af117..b71acb8f8d 100644 --- a/src/posix/sdl/i_system.cpp +++ b/src/posix/sdl/i_system.cpp @@ -397,15 +397,16 @@ int I_FindClose (void *handle) return 0; } -int I_FindAttr (findstate_t *fileinfo) +int I_FindAttr(findstate_t* const fileinfo) { - dirent *ent = fileinfo->namelist[fileinfo->current]; - struct stat buf; + dirent* const ent = fileinfo->namelist[fileinfo->current]; + 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; } diff --git a/src/resourcefiles/file_directory.cpp b/src/resourcefiles/file_directory.cpp index 77e5d0141e..02c70a21e3 100644 --- a/src/resourcefiles/file_directory.cpp +++ b/src/resourcefiles/file_directory.cpp @@ -209,10 +209,7 @@ int FDirectory::AddDirectory(const char *dirpath) FString fullFileName = scanDirectories[i] + file->d_name; - struct stat fileStat; - stat(fullFileName.GetChars(), &fileStat); - - if(S_ISDIR(fileStat.st_mode)) + if(DirExists(fullFileName.GetChars())) { scanDirectories.Push(scanDirectories[i] + file->d_name + "/"); continue; diff --git a/src/w_wad.cpp b/src/w_wad.cpp index ae52725d21..1e1de76f0b 100644 --- a/src/w_wad.cpp +++ b/src/w_wad.cpp @@ -233,14 +233,12 @@ void FWadCollection::AddFile (const char *filename, FileReader *wadinfo) if (wadinfo == NULL) { // Does this exist? If so, is it a directory? - struct stat info; - if (stat(filename, &info) != 0) + if (!DirEntryExists(filename, &isdir)) { - Printf(TEXTCOLOR_RED "Could not stat %s\n", filename); + Printf(TEXTCOLOR_RED "%s: File or Directory not found\n", filename); PrintLastError(); return; } - isdir = (info.st_mode & S_IFDIR) != 0; if (!isdir) {