From e1413764f62c0a417935ec39b3e73b119fe178ec Mon Sep 17 00:00:00 2001 From: Alug Date: Sat, 9 Aug 2025 17:10:41 +0200 Subject: [PATCH 01/11] speedup addon search by adding a blacklist of folders logs, luafiles, replay etc shouldnt ever contain addons so those can be skipped entirely this massively speeds up searching for addons on windows also will make findfile try to search in the designated addon folders first so it doesent look in the home or kart folders before it this is probably not the best way to do things but idk what im even doing, im not a coder just some chao who likes to sleep all day --- src/d_netfil.c | 66 +++++++++++++++++++++++++------------- src/dedicated/i_system.c | 4 +-- src/filesrch.c | 68 +++++++++++++++++++++++++++++++++++----- src/filesrch.h | 4 +-- src/sdl/i_system.cpp | 2 +- src/sdl/i_ttf.c | 2 +- 6 files changed, 110 insertions(+), 36 deletions(-) diff --git a/src/d_netfil.c b/src/d_netfil.c index 76459caef..8a37a2dac 100644 --- a/src/d_netfil.c +++ b/src/d_netfil.c @@ -1173,28 +1173,32 @@ filestatus_t findfile(char *filename, const UINT8 *wantedmd5sum, boolean complet filestatus_t homecheck; // store result of last file search boolean badmd5 = false; // store whether md5 was bad from either of the first two searches (if nothing was found in the third) - // first, check SRB2's "home" directory - homecheck = filesearch(filename, srb2home, wantedmd5sum, completepath, 10); - - if (homecheck == FS_FOUND) // we found the file, so return that we have :) - return FS_FOUND; - else if (homecheck == FS_MD5SUMBAD) // file has a bad md5; move on and look for a file with the right md5 - badmd5 = true; - // if not found at all, just move on without doing anything - - // next, check SRB2's "path" directory - homecheck = filesearch(filename, srb2path, wantedmd5sum, completepath, 10); - - if (homecheck == FS_FOUND) // we found the file, so return that we have :) - return FS_FOUND; - else if (homecheck == FS_MD5SUMBAD) // file has a bad md5; move on and look for a file with the right md5 - badmd5 = true; - // if not found at all, just move on without doing anything - - if (cv_addons_option.value == 3 && *cv_addons_folder.string != '\0') + // skip for startup, our mainwads wont be in there + if (loaded_config) { - // next, check any custom directory if specified - homecheck = filesearch(filename, cv_addons_folder.string, wantedmd5sum, completepath, 10); + if (cv_addons_option.value == 3 && *cv_addons_folder.string != '\0') + { + // first, check any custom directory if specified + homecheck = filesearch(filename, cv_addons_folder.string, wantedmd5sum, completepath, 10, false); + + if (homecheck == FS_FOUND) // we found the file, so return that we have :) + return FS_FOUND; + else if (homecheck == FS_MD5SUMBAD) // file has a bad md5; move on and look for a file with the right md5 + badmd5 = true; + // if not found at all, just move on without doing anything + } + + // next, check "DOWNLOAD" directory + homecheck = filesearch(filename, "DOWNLOAD", wantedmd5sum, completepath, 10, false); + + if (homecheck == FS_FOUND) // we found the file, so return that we have :) + return FS_FOUND; + else if (homecheck == FS_MD5SUMBAD) // file has a bad md5; move on and look for a file with the right md5 + badmd5 = true; + // if not found at all, just move on without doing anything + + // next, check "addons" directory + homecheck = filesearch(filename, "addons", wantedmd5sum, completepath, 10, false); if (homecheck == FS_FOUND) // we found the file, so return that we have :) return FS_FOUND; @@ -1203,8 +1207,26 @@ filestatus_t findfile(char *filename, const UINT8 *wantedmd5sum, boolean complet // if not found at all, just move on without doing anything } + // next, check SRB2's "home" directory + homecheck = filesearch(filename, srb2home, wantedmd5sum, completepath, 10, true); + + if (homecheck == FS_FOUND) // we found the file, so return that we have :) + return FS_FOUND; + else if (homecheck == FS_MD5SUMBAD) // file has a bad md5; move on and look for a file with the right md5 + badmd5 = true; + // if not found at all, just move on without doing anything + + // next, check SRB2's "path" directory + homecheck = filesearch(filename, srb2path, wantedmd5sum, completepath, 10, true); + + if (homecheck == FS_FOUND) // we found the file, so return that we have :) + return FS_FOUND; + else if (homecheck == FS_MD5SUMBAD) // file has a bad md5; move on and look for a file with the right md5 + badmd5 = true; + // if not found at all, just move on without doing anything + // finally check "." directory - homecheck = filesearch(filename, ".", wantedmd5sum, completepath, 10); + homecheck = filesearch(filename, ".", wantedmd5sum, completepath, 10, true); if (homecheck != FS_NOTFOUND) // if not found this time, fall back on the below return statement return homecheck; // otherwise return the result we got diff --git a/src/dedicated/i_system.c b/src/dedicated/i_system.c index 6ab748f86..9c3a9f2d2 100644 --- a/src/dedicated/i_system.c +++ b/src/dedicated/i_system.c @@ -1899,7 +1899,7 @@ static const char *searchWad(const char *searchDir) filestatus_t fstemp; strcpy(tempsw, WADKEYWORD1); - fstemp = filesearch(tempsw,searchDir,NULL,true,20); + fstemp = filesearch(tempsw, searchDir, NULL, true, 20, true); if (fstemp == FS_FOUND) { pathonly(tempsw); @@ -1907,7 +1907,7 @@ static const char *searchWad(const char *searchDir) } strcpy(tempsw, WADKEYWORD2); - fstemp = filesearch(tempsw, searchDir, NULL, true, 20); + fstemp = filesearch(tempsw, searchDir, NULL, true, 20, true); if (fstemp == FS_FOUND) { pathonly(tempsw); diff --git a/src/filesrch.c b/src/filesrch.c index 060e57d21..69c1fce68 100644 --- a/src/filesrch.c +++ b/src/filesrch.c @@ -12,6 +12,7 @@ /// FS_MD5SUMBAD; /// FS_FOUND +#include "d_main.h" #include #ifdef __GNUC__ #include @@ -353,34 +354,58 @@ INT32 pathisdirectory(const char *path) return 0; } -filestatus_t filesearch(char *filename, const char *startpath, const UINT8 *wantedmd5sum, boolean completepath, int maxsearchdepth) +// skip those folders, they will not have any addons +#ifdef _WIN32 +static const char *exclude_paths[] = { + "\\logs\\", + "\\luafiles\\", + "\\replay\\", + "\\mdls\\", + "\\gifs\\", + "\\screenshots\\", + NULL +}; +#else +static const char *exclude_paths[] = { + "/logs/", + "/luafiles/", + "/replay/", + "/mdls/", + "/gifs/", + "/screenshots/", + NULL +}; +#endif + +filestatus_t filesearch(char *filename, const char *startpath, const UINT8 *wantedmd5sum, boolean completepath, int maxsearchdepth, boolean skipexclude) { filestatus_t retval = FS_NOTFOUND; DIR **dirhandle; struct dirent *dent; struct stat fsstat; int found = 0; - char *searchname = strdup(filename); + char *searchname; int depthleft = maxsearchdepth; char searchpath[1024]; size_t *searchpathindex; - dirhandle = (DIR**) malloc(maxsearchdepth * sizeof(DIR*)); - searchpathindex = (size_t *) malloc(maxsearchdepth * sizeof(size_t)); + dirhandle = (DIR**)malloc(maxsearchdepth * sizeof(DIR*)); + searchpathindex = (size_t *)malloc(maxsearchdepth * sizeof(size_t)); - strcpy(searchpath,startpath); + strcpy(searchpath, startpath); searchpathindex[--depthleft] = strlen(searchpath) + 1; dirhandle[depthleft] = opendir(searchpath); if (dirhandle[depthleft] == NULL) { - free(searchname); free(dirhandle); free(searchpathindex); return FS_NOTFOUND; } + searchname = strdup(filename); + if (searchpath[searchpathindex[depthleft]-2] != PATHSEP[0]) { searchpath[searchpathindex[depthleft]-1] = PATHSEP[0]; @@ -409,6 +434,33 @@ filestatus_t filesearch(char *filename, const char *startpath, const UINT8 *want continue; } + // skip some folders that wont have addons in them + if (skipexclude) + { + boolean skipfolder = false; + + for (const char **path = exclude_paths; *path != NULL; path++) + { + if (**path == '\0') + continue; +#ifdef _WIN32 + if (strstr(searchpath, *path)) // windows doesent care if lower or uppercase +#else + if (strcasestr(searchpath, *path)) +#endif + { + skipfolder = true; + break; + } + } + + if (skipfolder) + { + closedir(dirhandle[depthleft++]); + continue; + } + } + // okay, now we actually want searchpath to incorporate d_name strcpy(&searchpath[searchpathindex[depthleft]], dent->d_name); @@ -423,7 +475,7 @@ filestatus_t filesearch(char *filename, const char *startpath, const UINT8 *want // Symlinks aren't always directory symlinks. Dunno if this would resolve recursive // symlinks, but i think stat already does that - if (dent->d_type == DT_LNK && stat(searchpath,&fsstat) == 0 && !S_ISDIR(fsstat.st_mode)) + if (dent->d_type == DT_LNK && stat(searchpath, &fsstat) == 0 && !S_ISDIR(fsstat.st_mode)) { dent->d_type = DT_UNKNOWN; } @@ -432,7 +484,7 @@ filestatus_t filesearch(char *filename, const char *startpath, const UINT8 *want // FIXME: should we also follow symlinks? if ((dent->d_type == DT_DIR && depthleft) || (dent->d_type == DT_LNK && depthleft)) #else - if (stat(searchpath,&fsstat) < 0) // do we want to follow symlinks? if not: change it to lstat + if (stat(searchpath, &fsstat) < 0) // do we want to follow symlinks? if not: change it to lstat ; // was the file (re)moved? can't stat it else if (S_ISDIR(fsstat.st_mode) && depthleft) #endif diff --git a/src/filesrch.h b/src/filesrch.h index 075895cb0..d2cba7272 100644 --- a/src/filesrch.h +++ b/src/filesrch.h @@ -24,14 +24,14 @@ extern consvar_t cv_addons_option, cv_addons_folder, cv_addons_md5, cv_addons_sh \param wantedmd5sum want to check with MD5 \param completepath want to return the complete path of the file? \param maxsearchdepth the max depth to search for the file + \param skipexclude when true this skips certain named folders \return filestatus_t */ -filestatus_t filesearch(char *filename, const char *startpath, const UINT8 *wantedmd5sum, - boolean completepath, int maxsearchdepth); +filestatus_t filesearch(char *filename, const char *startpath, const UINT8 *wantedmd5sum, boolean completepath, int maxsearchdepth, boolean skipexclude); #define menudepth 20 diff --git a/src/sdl/i_system.cpp b/src/sdl/i_system.cpp index 535f519bd..2dc371015 100644 --- a/src/sdl/i_system.cpp +++ b/src/sdl/i_system.cpp @@ -2486,7 +2486,7 @@ static const char *searchWad(const char *searchDir) filestatus_t fstemp; strcpy(tempsw, WADKEYWORD1); - fstemp = filesearch(tempsw,searchDir,NULL,true,20); + fstemp = filesearch(tempsw, searchDir, NULL, true, 20, true); if (fstemp == FS_FOUND) { pathonly(tempsw); diff --git a/src/sdl/i_ttf.c b/src/sdl/i_ttf.c index f2cd497ee..ebfc50d4a 100644 --- a/src/sdl/i_ttf.c +++ b/src/sdl/i_ttf.c @@ -58,7 +58,7 @@ static char *searchFont(const char *fontsearchDir) filestatus_t fstemp; strcpy(tempsw, FONTFILE); - fstemp = filesearch(tempsw, fontsearchDir, NULL, true, 20); + fstemp = filesearch(tempsw, fontsearchDir, NULL, true, 20, true); if (fstemp == FS_FOUND) { return tempsw; From 0d9d176415d7c9a3a8fd3b1c1b4f6fa46ba17a01 Mon Sep 17 00:00:00 2001 From: Alug Date: Sat, 9 Aug 2025 21:26:15 +0200 Subject: [PATCH 02/11] use GetFileAttributes instead of stat for filesearch on windows this is slightly faster and the recommended way of doing this apparently stat on windows has to emulate some POSIX stuff for that which may slow it down --- src/filesrch.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/filesrch.c b/src/filesrch.c index 69c1fce68..35fbefc8a 100644 --- a/src/filesrch.c +++ b/src/filesrch.c @@ -484,9 +484,11 @@ filestatus_t filesearch(char *filename, const char *startpath, const UINT8 *want // FIXME: should we also follow symlinks? if ((dent->d_type == DT_DIR && depthleft) || (dent->d_type == DT_LNK && depthleft)) #else - if (stat(searchpath, &fsstat) < 0) // do we want to follow symlinks? if not: change it to lstat + // if we wanna follow symlinks we can check with FILE_ATTRIBUTE_REPARSE_POINT + DWORD fileattr = GetFileAttributes(searchpath); + if (fileattr == INVALID_FILE_ATTRIBUTES) ; // was the file (re)moved? can't stat it - else if (S_ISDIR(fsstat.st_mode) && depthleft) + else if ((fileattr & FILE_ATTRIBUTE_DIRECTORY) && depthleft) #endif { searchpathindex[--depthleft] = strlen(searchpath) + 1; From c8d9ff6575f1c3da0bebc87d1294b9b6d4dfb84f Mon Sep 17 00:00:00 2001 From: Alug Date: Sat, 9 Aug 2025 22:05:28 +0200 Subject: [PATCH 03/11] use GetFileAttributes also for preparefilemenu untested as of now --- src/filesrch.c | 64 ++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 49 insertions(+), 15 deletions(-) diff --git a/src/filesrch.c b/src/filesrch.c index 35fbefc8a..9aa1aee55 100644 --- a/src/filesrch.c +++ b/src/filesrch.c @@ -734,30 +734,44 @@ boolean preparefilemenu(boolean samedepth, boolean replayhut) if (!dent) break; else if (dent->d_name[0]=='.' && - (dent->d_name[1]=='\0' || - (dent->d_name[1]=='.' && - dent->d_name[2]=='\0'))) + (dent->d_name[1]=='\0' || + (dent->d_name[1]=='.' && + dent->d_name[2]=='\0'))) continue; // we don't want to scan uptree strcpy(&menupath[menupathindex[menudepthleft]],dent->d_name); - if (stat(menupath,&fsstat) < 0) // do we want to follow symlinks? if not: change it to lstat +#if defined(__linux__) || defined(__FreeBSD__) + if (stat(menupath, &fsstat) < 0) +#else + // if we wanna follow symlinks we can check with FILE_ATTRIBUTE_REPARSE_POINT + DWORD fileattr = GetFileAttributes(searchpath); + if (fileattr == INVALID_FILE_ATTRIBUTES) +#endif ; // was the file (re)moved? can't stat it else // is a file or directory { +#if defined(__linux__) || defined(__FreeBSD__) if (!S_ISDIR(fsstat.st_mode)) // file +#else + if (!(fileattr & FILE_ATTRIBUTE_DIRECTORY)) +#endif { size_t len = strlen(dent->d_name)+1; if (replayhut) { - if (strcasecmp(".lmp", dent->d_name+len-5)) continue; // Not a replay + if (strcasecmp(".lmp", dent->d_name+len-5)) + continue; // Not a replay } else if (!cv_addons_showall.value) { UINT8 ext; for (ext = 0; ext < NUM_EXT_TABLE; ext++) - if (!strcasecmp(exttable[ext]+1, dent->d_name+len-(exttable[ext][0]))) break; // extension comparison - if (ext == NUM_EXT_TABLE) continue; // not an addfile-able (or exec-able) file + if (!strcasecmp(exttable[ext]+1, dent->d_name+len-(exttable[ext][0]))) + break; // extension comparison + + if (ext == NUM_EXT_TABLE) + continue; // not an addfile-able (or exec-able) file } } else // directory @@ -802,14 +816,20 @@ boolean preparefilemenu(boolean samedepth, boolean replayhut) if (!dent) break; else if (dent->d_name[0]=='.' && - (dent->d_name[1]=='\0' || - (dent->d_name[1]=='.' && - dent->d_name[2]=='\0'))) + (dent->d_name[1]=='\0' || + (dent->d_name[1]=='.' && + dent->d_name[2]=='\0'))) continue; // we don't want to scan uptree strcpy(&menupath[menupathindex[menudepthleft]],dent->d_name); - if (stat(menupath,&fsstat) < 0) // do we want to follow symlinks? if not: change it to lstat +#if defined(__linux__) || defined(__FreeBSD__) + if (stat(menupath, &fsstat) < 0) +#else + // if we wanna follow symlinks we can check with FILE_ATTRIBUTE_REPARSE_POINT + DWORD fileattr = GetFileAttributes(searchpath); + if (fileattr == INVALID_FILE_ATTRIBUTES) +#endif ; // was the file (re)moved? can't stat it else // is a file or directory { @@ -818,20 +838,31 @@ boolean preparefilemenu(boolean samedepth, boolean replayhut) UINT8 ext = EXT_FOLDER; UINT8 folder; +#if defined(__linux__) || defined(__FreeBSD__) if (!S_ISDIR(fsstat.st_mode)) // file +#else + if (!(fileattr & FILE_ATTRIBUTE_DIRECTORY)) +#endif { - if (!((numfolders+pos) < sizecoredirmenu)) continue; // crash prevention + if (!((numfolders+pos) < sizecoredirmenu)) + continue; // crash prevention if (replayhut) { - if (strcasecmp(".lmp", dent->d_name+len-5)) continue; // Not a replay + if (strcasecmp(".lmp", dent->d_name+len-5)) + continue; // Not a replay + ext = EXT_TXT; // This isn't used anywhere but better safe than sorry for messing with this... } else { for (; ext < NUM_EXT_TABLE; ext++) - if (!strcasecmp(exttable[ext]+1, dent->d_name+len-(exttable[ext][0]))) break; // extension comparison - if (ext == NUM_EXT_TABLE && !cv_addons_showall.value) continue; // not an addfile-able (or exec-able) file + if (!strcasecmp(exttable[ext]+1, dent->d_name+len-(exttable[ext][0]))) + break; // extension comparison + + if (ext == NUM_EXT_TABLE && !cv_addons_showall.value) + continue; // not an addfile-able (or exec-able) file + ext += EXT_START; // moving to be appropriate position if (ext >= EXT_LOADSTART) @@ -848,6 +879,7 @@ boolean preparefilemenu(boolean samedepth, boolean replayhut) if (strcmp(dent->d_name, filenamebuf[i])) continue; + if (cv_addons_md5.value && !checkfilemd5(menupath, wadfiles[i]->md5sum)) continue; @@ -874,9 +906,11 @@ boolean preparefilemenu(boolean samedepth, boolean replayhut) if (!(temp = Z_Malloc((len+DIR_STRING+folder) * sizeof (char), PU_STATIC, NULL))) I_Error("preparefilemenu(): could not create file entry."); + temp[DIR_TYPE] = ext; temp[DIR_LEN] = (UINT8)(len); strlcpy(temp+DIR_STRING, dent->d_name, len); + if (folder) { strcpy(temp+len, PATHSEP); From e6a0b4f58b41f79ec1c2c591e69c844205dd0b8d Mon Sep 17 00:00:00 2001 From: Alug Date: Sat, 9 Aug 2025 22:32:13 +0100 Subject: [PATCH 04/11] fix oopsie copy paste --- src/filesrch.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/filesrch.c b/src/filesrch.c index 9aa1aee55..6966f12eb 100644 --- a/src/filesrch.c +++ b/src/filesrch.c @@ -745,7 +745,7 @@ boolean preparefilemenu(boolean samedepth, boolean replayhut) if (stat(menupath, &fsstat) < 0) #else // if we wanna follow symlinks we can check with FILE_ATTRIBUTE_REPARSE_POINT - DWORD fileattr = GetFileAttributes(searchpath); + DWORD fileattr = GetFileAttributes(menupath); if (fileattr == INVALID_FILE_ATTRIBUTES) #endif ; // was the file (re)moved? can't stat it @@ -827,7 +827,7 @@ boolean preparefilemenu(boolean samedepth, boolean replayhut) if (stat(menupath, &fsstat) < 0) #else // if we wanna follow symlinks we can check with FILE_ATTRIBUTE_REPARSE_POINT - DWORD fileattr = GetFileAttributes(searchpath); + DWORD fileattr = GetFileAttributes(menupath); if (fileattr == INVALID_FILE_ATTRIBUTES) #endif ; // was the file (re)moved? can't stat it From 2cad7f721018532d727c3912f2d0bea4612e970c Mon Sep 17 00:00:00 2001 From: Alug Date: Mon, 25 Aug 2025 12:55:53 +0200 Subject: [PATCH 05/11] dont recheck if we should skip the folder if folder did not change saves a whole lotta string comparisons and speeds this up even further --- src/filesrch.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/filesrch.c b/src/filesrch.c index 6966f12eb..c5b1cd8b6 100644 --- a/src/filesrch.c +++ b/src/filesrch.c @@ -388,6 +388,7 @@ filestatus_t filesearch(char *filename, const char *startpath, const UINT8 *want int depthleft = maxsearchdepth; char searchpath[1024]; size_t *searchpathindex; + boolean folderchanged = true; dirhandle = (DIR**)malloc(maxsearchdepth * sizeof(DIR*)); searchpathindex = (size_t *)malloc(maxsearchdepth * sizeof(size_t)); @@ -422,6 +423,7 @@ filestatus_t filesearch(char *filename, const char *startpath, const UINT8 *want if (!dent) { closedir(dirhandle[depthleft++]); + folderchanged = true; continue; } @@ -435,7 +437,7 @@ filestatus_t filesearch(char *filename, const char *startpath, const UINT8 *want } // skip some folders that wont have addons in them - if (skipexclude) + if (skipexclude && folderchanged) { boolean skipfolder = false; @@ -457,6 +459,7 @@ filestatus_t filesearch(char *filename, const char *startpath, const UINT8 *want if (skipfolder) { closedir(dirhandle[depthleft++]); + folderchanged = true; continue; } } @@ -503,6 +506,8 @@ filestatus_t filesearch(char *filename, const char *startpath, const UINT8 *want searchpath[searchpathindex[depthleft]-1] = PATHSEP[0]; searchpath[searchpathindex[depthleft]] = 0; + folderchanged = true; + continue; } else if (!strcasecmp(searchname, dent->d_name)) { @@ -523,6 +528,8 @@ filestatus_t filesearch(char *filename, const char *startpath, const UINT8 *want break; } } + + folderchanged = false; } for (; depthleft < maxsearchdepth; closedir(dirhandle[depthleft++])); From 4f534c5ce308216f323c83415da41c55fe5b0499 Mon Sep 17 00:00:00 2001 From: Alug Date: Mon, 25 Aug 2025 16:10:24 +0200 Subject: [PATCH 06/11] remove d_main include from filesrch --- src/filesrch.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/filesrch.c b/src/filesrch.c index c5b1cd8b6..c76c9b7a9 100644 --- a/src/filesrch.c +++ b/src/filesrch.c @@ -12,7 +12,6 @@ /// FS_MD5SUMBAD; /// FS_FOUND -#include "d_main.h" #include #ifdef __GNUC__ #include From 6e123b0732d8f0565de1728c24227f42a02752ee Mon Sep 17 00:00:00 2001 From: Alug Date: Tue, 26 Aug 2025 08:34:57 +0100 Subject: [PATCH 07/11] fix filesearch portability --- src/filesrch.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/filesrch.c b/src/filesrch.c index c76c9b7a9..9592c63c2 100644 --- a/src/filesrch.c +++ b/src/filesrch.c @@ -381,7 +381,9 @@ filestatus_t filesearch(char *filename, const char *startpath, const UINT8 *want filestatus_t retval = FS_NOTFOUND; DIR **dirhandle; struct dirent *dent; +#ifndef _WIN32 struct stat fsstat; +#endif int found = 0; char *searchname; int depthleft = maxsearchdepth; @@ -485,12 +487,16 @@ filestatus_t filesearch(char *filename, const char *startpath, const UINT8 *want // Linux and FreeBSD has a special field for file type on dirent, so use that to speed up lookups. // FIXME: should we also follow symlinks? if ((dent->d_type == DT_DIR && depthleft) || (dent->d_type == DT_LNK && depthleft)) -#else +#elif defined (_WIN32) // if we wanna follow symlinks we can check with FILE_ATTRIBUTE_REPARSE_POINT DWORD fileattr = GetFileAttributes(searchpath); if (fileattr == INVALID_FILE_ATTRIBUTES) ; // was the file (re)moved? can't stat it else if ((fileattr & FILE_ATTRIBUTE_DIRECTORY) && depthleft) +#else + if (stat(searchpath,&fsstat) < 0) // do we want to follow symlinks? if not: change it to lstat + ; // was the file (re)moved? can't stat it + else if (S_ISDIR(fsstat.st_mode) && depthleft) #endif { searchpathindex[--depthleft] = strlen(searchpath) + 1; @@ -704,7 +710,9 @@ boolean preparefilemenu(boolean samedepth, boolean replayhut) { DIR *dirhandle; struct dirent *dent; +#ifndef _WIN32 struct stat fsstat; +#endif size_t pos = 0, folderpos = 0, numfolders = 0; char *tempname = NULL; @@ -747,7 +755,7 @@ boolean preparefilemenu(boolean samedepth, boolean replayhut) strcpy(&menupath[menupathindex[menudepthleft]],dent->d_name); -#if defined(__linux__) || defined(__FreeBSD__) +#ifndef _WIN32 if (stat(menupath, &fsstat) < 0) #else // if we wanna follow symlinks we can check with FILE_ATTRIBUTE_REPARSE_POINT @@ -757,7 +765,7 @@ boolean preparefilemenu(boolean samedepth, boolean replayhut) ; // was the file (re)moved? can't stat it else // is a file or directory { -#if defined(__linux__) || defined(__FreeBSD__) +#ifndef _WIN32 if (!S_ISDIR(fsstat.st_mode)) // file #else if (!(fileattr & FILE_ATTRIBUTE_DIRECTORY)) @@ -829,7 +837,7 @@ boolean preparefilemenu(boolean samedepth, boolean replayhut) strcpy(&menupath[menupathindex[menudepthleft]],dent->d_name); -#if defined(__linux__) || defined(__FreeBSD__) +#ifndef _WIN32 if (stat(menupath, &fsstat) < 0) #else // if we wanna follow symlinks we can check with FILE_ATTRIBUTE_REPARSE_POINT @@ -844,7 +852,7 @@ boolean preparefilemenu(boolean samedepth, boolean replayhut) UINT8 ext = EXT_FOLDER; UINT8 folder; -#if defined(__linux__) || defined(__FreeBSD__) +#ifndef _WIN32 if (!S_ISDIR(fsstat.st_mode)) // file #else if (!(fileattr & FILE_ATTRIBUTE_DIRECTORY)) From 6f891dd2351a881aba15a1180c61231a348cce1f Mon Sep 17 00:00:00 2001 From: Alug Date: Tue, 26 Aug 2025 08:36:00 +0100 Subject: [PATCH 08/11] use GetFileAttributes for pathisdirectory on Windows should slightly speedup gamestartup with autoloaded files --- src/filesrch.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/filesrch.c b/src/filesrch.c index 9592c63c2..74ff1ef53 100644 --- a/src/filesrch.c +++ b/src/filesrch.c @@ -341,6 +341,7 @@ char *refreshdirname = NULL; // direrror is set if there was an error. INT32 pathisdirectory(const char *path) { +#ifndef _WIN32 struct stat fsstat; if (stat(path, &fsstat) < 0) @@ -348,7 +349,21 @@ INT32 pathisdirectory(const char *path) return -1; } else if (S_ISDIR(fsstat.st_mode)) + { return 1; + } + +#else + DWORD fileattr = GetFileAttributes(path); + if (fileattr == INVALID_FILE_ATTRIBUTES) + { + return -1; + } + else if (fileattr & FILE_ATTRIBUTE_DIRECTORY) + { + return 1; + } +#endif return 0; } From 74c9c09fcdd2a27c029916a61cba39af57b2d093 Mon Sep 17 00:00:00 2001 From: Alug Date: Tue, 26 Aug 2025 12:04:00 +0200 Subject: [PATCH 09/11] remove skipexclude variable with the folderchange check this becomes erronous --- src/d_netfil.c | 12 ++++++------ src/dedicated/i_system.c | 4 ++-- src/filesrch.c | 7 +++---- src/filesrch.h | 3 +-- src/sdl/i_system.cpp | 2 +- src/sdl/i_ttf.c | 2 +- 6 files changed, 14 insertions(+), 16 deletions(-) diff --git a/src/d_netfil.c b/src/d_netfil.c index 8a37a2dac..356fb6854 100644 --- a/src/d_netfil.c +++ b/src/d_netfil.c @@ -1179,7 +1179,7 @@ filestatus_t findfile(char *filename, const UINT8 *wantedmd5sum, boolean complet if (cv_addons_option.value == 3 && *cv_addons_folder.string != '\0') { // first, check any custom directory if specified - homecheck = filesearch(filename, cv_addons_folder.string, wantedmd5sum, completepath, 10, false); + homecheck = filesearch(filename, cv_addons_folder.string, wantedmd5sum, completepath, 10); if (homecheck == FS_FOUND) // we found the file, so return that we have :) return FS_FOUND; @@ -1189,7 +1189,7 @@ filestatus_t findfile(char *filename, const UINT8 *wantedmd5sum, boolean complet } // next, check "DOWNLOAD" directory - homecheck = filesearch(filename, "DOWNLOAD", wantedmd5sum, completepath, 10, false); + homecheck = filesearch(filename, "DOWNLOAD", wantedmd5sum, completepath, 10); if (homecheck == FS_FOUND) // we found the file, so return that we have :) return FS_FOUND; @@ -1198,7 +1198,7 @@ filestatus_t findfile(char *filename, const UINT8 *wantedmd5sum, boolean complet // if not found at all, just move on without doing anything // next, check "addons" directory - homecheck = filesearch(filename, "addons", wantedmd5sum, completepath, 10, false); + homecheck = filesearch(filename, "addons", wantedmd5sum, completepath, 10); if (homecheck == FS_FOUND) // we found the file, so return that we have :) return FS_FOUND; @@ -1208,7 +1208,7 @@ filestatus_t findfile(char *filename, const UINT8 *wantedmd5sum, boolean complet } // next, check SRB2's "home" directory - homecheck = filesearch(filename, srb2home, wantedmd5sum, completepath, 10, true); + homecheck = filesearch(filename, srb2home, wantedmd5sum, completepath, 10); if (homecheck == FS_FOUND) // we found the file, so return that we have :) return FS_FOUND; @@ -1217,7 +1217,7 @@ filestatus_t findfile(char *filename, const UINT8 *wantedmd5sum, boolean complet // if not found at all, just move on without doing anything // next, check SRB2's "path" directory - homecheck = filesearch(filename, srb2path, wantedmd5sum, completepath, 10, true); + homecheck = filesearch(filename, srb2path, wantedmd5sum, completepath, 10); if (homecheck == FS_FOUND) // we found the file, so return that we have :) return FS_FOUND; @@ -1226,7 +1226,7 @@ filestatus_t findfile(char *filename, const UINT8 *wantedmd5sum, boolean complet // if not found at all, just move on without doing anything // finally check "." directory - homecheck = filesearch(filename, ".", wantedmd5sum, completepath, 10, true); + homecheck = filesearch(filename, ".", wantedmd5sum, completepath, 10); if (homecheck != FS_NOTFOUND) // if not found this time, fall back on the below return statement return homecheck; // otherwise return the result we got diff --git a/src/dedicated/i_system.c b/src/dedicated/i_system.c index 9c3a9f2d2..1fe81e165 100644 --- a/src/dedicated/i_system.c +++ b/src/dedicated/i_system.c @@ -1899,7 +1899,7 @@ static const char *searchWad(const char *searchDir) filestatus_t fstemp; strcpy(tempsw, WADKEYWORD1); - fstemp = filesearch(tempsw, searchDir, NULL, true, 20, true); + fstemp = filesearch(tempsw, searchDir, NULL, true, 20); if (fstemp == FS_FOUND) { pathonly(tempsw); @@ -1907,7 +1907,7 @@ static const char *searchWad(const char *searchDir) } strcpy(tempsw, WADKEYWORD2); - fstemp = filesearch(tempsw, searchDir, NULL, true, 20, true); + fstemp = filesearch(tempsw, searchDir, NULL, true, 20); if (fstemp == FS_FOUND) { pathonly(tempsw); diff --git a/src/filesrch.c b/src/filesrch.c index 74ff1ef53..55b04aadc 100644 --- a/src/filesrch.c +++ b/src/filesrch.c @@ -391,7 +391,7 @@ static const char *exclude_paths[] = { }; #endif -filestatus_t filesearch(char *filename, const char *startpath, const UINT8 *wantedmd5sum, boolean completepath, int maxsearchdepth, boolean skipexclude) +filestatus_t filesearch(char *filename, const char *startpath, const UINT8 *wantedmd5sum, boolean completepath, int maxsearchdepth) { filestatus_t retval = FS_NOTFOUND; DIR **dirhandle; @@ -404,7 +404,7 @@ filestatus_t filesearch(char *filename, const char *startpath, const UINT8 *want int depthleft = maxsearchdepth; char searchpath[1024]; size_t *searchpathindex; - boolean folderchanged = true; + boolean folderchanged = true; // tells us if we changed folders during traversal dirhandle = (DIR**)malloc(maxsearchdepth * sizeof(DIR*)); searchpathindex = (size_t *)malloc(maxsearchdepth * sizeof(size_t)); @@ -453,7 +453,7 @@ filestatus_t filesearch(char *filename, const char *startpath, const UINT8 *want } // skip some folders that wont have addons in them - if (skipexclude && folderchanged) + if (folderchanged) { boolean skipfolder = false; @@ -500,7 +500,6 @@ filestatus_t filesearch(char *filename, const char *startpath, const UINT8 *want } // Linux and FreeBSD has a special field for file type on dirent, so use that to speed up lookups. - // FIXME: should we also follow symlinks? if ((dent->d_type == DT_DIR && depthleft) || (dent->d_type == DT_LNK && depthleft)) #elif defined (_WIN32) // if we wanna follow symlinks we can check with FILE_ATTRIBUTE_REPARSE_POINT diff --git a/src/filesrch.h b/src/filesrch.h index d2cba7272..697020c54 100644 --- a/src/filesrch.h +++ b/src/filesrch.h @@ -24,14 +24,13 @@ extern consvar_t cv_addons_option, cv_addons_folder, cv_addons_md5, cv_addons_sh \param wantedmd5sum want to check with MD5 \param completepath want to return the complete path of the file? \param maxsearchdepth the max depth to search for the file - \param skipexclude when true this skips certain named folders \return filestatus_t */ -filestatus_t filesearch(char *filename, const char *startpath, const UINT8 *wantedmd5sum, boolean completepath, int maxsearchdepth, boolean skipexclude); +filestatus_t filesearch(char *filename, const char *startpath, const UINT8 *wantedmd5sum, boolean completepath, int maxsearchdepth); #define menudepth 20 diff --git a/src/sdl/i_system.cpp b/src/sdl/i_system.cpp index 2dc371015..e2c7801f2 100644 --- a/src/sdl/i_system.cpp +++ b/src/sdl/i_system.cpp @@ -2486,7 +2486,7 @@ static const char *searchWad(const char *searchDir) filestatus_t fstemp; strcpy(tempsw, WADKEYWORD1); - fstemp = filesearch(tempsw, searchDir, NULL, true, 20, true); + fstemp = filesearch(tempsw, searchDir, NULL, true, 20); if (fstemp == FS_FOUND) { pathonly(tempsw); diff --git a/src/sdl/i_ttf.c b/src/sdl/i_ttf.c index ebfc50d4a..f2cd497ee 100644 --- a/src/sdl/i_ttf.c +++ b/src/sdl/i_ttf.c @@ -58,7 +58,7 @@ static char *searchFont(const char *fontsearchDir) filestatus_t fstemp; strcpy(tempsw, FONTFILE); - fstemp = filesearch(tempsw, fontsearchDir, NULL, true, 20, true); + fstemp = filesearch(tempsw, fontsearchDir, NULL, true, 20); if (fstemp == FS_FOUND) { return tempsw; From ef9472097d019bfd82ad32fed896031e67c99bd8 Mon Sep 17 00:00:00 2001 From: Alug Date: Sun, 14 Sep 2025 20:47:57 +0200 Subject: [PATCH 10/11] clean up faster filesearch MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit -use parts of RR´s implementation to get rid of the windows specific code and generally simplify this - also use SRB2´s symlink detection as its simpler and saves one lstat call per searched file --- src/filesrch.c | 167 +++++++++++++++++++++---------------------------- 1 file changed, 71 insertions(+), 96 deletions(-) diff --git a/src/filesrch.c b/src/filesrch.c index 55b04aadc..f3a94bb88 100644 --- a/src/filesrch.c +++ b/src/filesrch.c @@ -369,27 +369,15 @@ INT32 pathisdirectory(const char *path) } // skip those folders, they will not have any addons -#ifdef _WIN32 static const char *exclude_paths[] = { - "\\logs\\", - "\\luafiles\\", - "\\replay\\", - "\\mdls\\", - "\\gifs\\", - "\\screenshots\\", + "logs", + "luafiles", + "replay", + "mdls", + "gifs", + "screenshots", NULL }; -#else -static const char *exclude_paths[] = { - "/logs/", - "/luafiles/", - "/replay/", - "/mdls/", - "/gifs/", - "/screenshots/", - NULL -}; -#endif filestatus_t filesearch(char *filename, const char *startpath, const UINT8 *wantedmd5sum, boolean completepath, int maxsearchdepth) { @@ -404,7 +392,6 @@ filestatus_t filesearch(char *filename, const char *startpath, const UINT8 *want int depthleft = maxsearchdepth; char searchpath[1024]; size_t *searchpathindex; - boolean folderchanged = true; // tells us if we changed folders during traversal dirhandle = (DIR**)malloc(maxsearchdepth * sizeof(DIR*)); searchpathindex = (size_t *)malloc(maxsearchdepth * sizeof(size_t)); @@ -439,7 +426,6 @@ filestatus_t filesearch(char *filename, const char *startpath, const UINT8 *want if (!dent) { closedir(dirhandle[depthleft++]); - folderchanged = true; continue; } @@ -452,103 +438,92 @@ filestatus_t filesearch(char *filename, const char *startpath, const UINT8 *want continue; } - // skip some folders that wont have addons in them - if (folderchanged) - { - boolean skipfolder = false; - - for (const char **path = exclude_paths; *path != NULL; path++) - { - if (**path == '\0') - continue; -#ifdef _WIN32 - if (strstr(searchpath, *path)) // windows doesent care if lower or uppercase -#else - if (strcasestr(searchpath, *path)) -#endif - { - skipfolder = true; - break; - } - } - - if (skipfolder) - { - closedir(dirhandle[depthleft++]); - folderchanged = true; - continue; - } - } - // okay, now we actually want searchpath to incorporate d_name strcpy(&searchpath[searchpathindex[depthleft]], dent->d_name); -#if defined(__linux__) || defined(__FreeBSD__) - if (dent->d_type == DT_UNKNOWN && lstat(searchpath, &fsstat) == 0) - { - if (S_ISDIR(fsstat.st_mode)) +#if defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) + if (dent->d_type == DT_UNKNOWN || dent->d_type == DT_LNK) + if (stat(searchpath, &fsstat) == 0 && S_ISDIR(fsstat.st_mode)) dent->d_type = DT_DIR; - else if (S_ISLNK(fsstat.st_mode)) - dent->d_type = DT_LNK; - } - - // Symlinks aren't always directory symlinks. Dunno if this would resolve recursive - // symlinks, but i think stat already does that - if (dent->d_type == DT_LNK && stat(searchpath, &fsstat) == 0 && !S_ISDIR(fsstat.st_mode)) - { - dent->d_type = DT_UNKNOWN; - } // Linux and FreeBSD has a special field for file type on dirent, so use that to speed up lookups. - if ((dent->d_type == DT_DIR && depthleft) || (dent->d_type == DT_LNK && depthleft)) + if (dent->d_type == DT_DIR) #elif defined (_WIN32) // if we wanna follow symlinks we can check with FILE_ATTRIBUTE_REPARSE_POINT DWORD fileattr = GetFileAttributes(searchpath); if (fileattr == INVALID_FILE_ATTRIBUTES) - ; // was the file (re)moved? can't stat it - else if ((fileattr & FILE_ATTRIBUTE_DIRECTORY) && depthleft) + continue; // was the file (re)moved? can't stat it + + if (fileattr & FILE_ATTRIBUTE_DIRECTORY) #else if (stat(searchpath,&fsstat) < 0) // do we want to follow symlinks? if not: change it to lstat - ; // was the file (re)moved? can't stat it - else if (S_ISDIR(fsstat.st_mode) && depthleft) + continue; // was the file (re)moved? can't stat it + + if (S_ISDIR(fsstat.st_mode)) #endif { - searchpathindex[--depthleft] = strlen(searchpath) + 1; - dirhandle[depthleft] = opendir(searchpath); + // I am a folder! - if (!dirhandle[depthleft]) + if (!depthleft) + continue; // No additional folder delving permitted... + + const char **path = exclude_paths; + + if (depthleft == maxsearchdepth-1) { - // can't open it... maybe no read-permissions - // go back to previous dir - depthleft++; + // When we're at the root of the search, we exclude certain folders. + + boolean skipfolder = false; + + for (; *path != NULL; path++) + { + if (strcasecmp(*path, dent->d_name)) + { + skipfolder = true; + break; + } + } + + if (skipfolder) + { + continue; + } + } + + if (strcasecmp(".git", dent->d_name) // sanity if you're weird like me + && (dirhandle[depthleft-1] = opendir(searchpath)) != NULL) + { + // Got read permissions! + searchpathindex[--depthleft] = strlen(searchpath) + 1; + + searchpath[searchpathindex[depthleft]-1] = PATHSEP[0]; + searchpath[searchpathindex[depthleft]] = 0; } - searchpath[searchpathindex[depthleft]-1] = PATHSEP[0]; - searchpath[searchpathindex[depthleft]] = 0; - folderchanged = true; continue; } - else if (!strcasecmp(searchname, dent->d_name)) - { - switch (checkfilemd5(searchpath, wantedmd5sum)) - { - case FS_FOUND: - if (completepath) - strcpy(filename, searchpath); - else - strcpy(filename, dent->d_name); - retval = FS_FOUND; - found = 1; - break; - case FS_MD5SUMBAD: - retval = FS_MD5SUMBAD; - break; - default: // prevent some compiler warnings - break; - } - } - folderchanged = false; + // I am a file! + + if (strcasecmp(searchname, dent->d_name)) + continue; // Not what we're looking for! + + switch (checkfilemd5(searchpath, wantedmd5sum)) + { + case FS_FOUND: + if (completepath) + strcpy(filename, searchpath); + else + strcpy(filename, dent->d_name); + retval = FS_FOUND; + found = 1; + break; + case FS_MD5SUMBAD: + retval = FS_MD5SUMBAD; + break; + default: // prevent some compiler warnings + break; + } } for (; depthleft < maxsearchdepth; closedir(dirhandle[depthleft++])); From 36c1369041f3eccaaebbe10e5e96446aeea1ae2f Mon Sep 17 00:00:00 2001 From: Alug Date: Mon, 15 Sep 2025 15:04:51 +0200 Subject: [PATCH 11/11] fix filesearch oopsie lulul --- src/filesrch.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/filesrch.c b/src/filesrch.c index f3a94bb88..b7f1a141f 100644 --- a/src/filesrch.c +++ b/src/filesrch.c @@ -477,7 +477,7 @@ filestatus_t filesearch(char *filename, const char *startpath, const UINT8 *want for (; *path != NULL; path++) { - if (strcasecmp(*path, dent->d_name)) + if (strcasecmp(*path, dent->d_name) == 0) { skipfolder = true; break;