From 7ba146b4ff73ca85ece907917b4c7111e2e95b6b Mon Sep 17 00:00:00 2001 From: Alug Date: Sun, 5 Nov 2023 21:00:03 +0100 Subject: [PATCH] Improve file search performance on Linux and FreeBSD from: https://git.do.srb2.org/STJr/SRB2/-/merge_requests/2198 --- src/filesrch.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/filesrch.c b/src/filesrch.c index d51ef9516..8a213670c 100644 --- a/src/filesrch.c +++ b/src/filesrch.c @@ -396,9 +396,19 @@ filestatus_t filesearch(char *filename, const char *startpath, const UINT8 *want // 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) + if (lstat(searchpath,&fsstat) == 0 && S_ISDIR(fsstat.st_mode)) + dent->d_type = DT_DIR; + + // 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) +#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; dirhandle[depthleft] = opendir(searchpath);