From aa952051fdcf088a559bdfdb8f341a0596338090 Mon Sep 17 00:00:00 2001
From: Sara Sparks <flarn2006@gmail.com>
Date: Sat, 6 May 2023 20:46:46 -0400
Subject: [PATCH] Made filesearch aware of symbolic links

(cherry picked from commit cbcbda1586d5a19cf17aabedb49bca3e5328fa4f)
---
 src/filesrch.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

diff --git a/src/filesrch.c b/src/filesrch.c
index 3f901b6958..2104d6af5f 100644
--- a/src/filesrch.c
+++ b/src/filesrch.c
@@ -23,6 +23,11 @@
 #include <windows.h>
 #endif
 #include <sys/stat.h>
+#ifndef IGNORE_SYMLINKS
+#include <unistd.h>
+#include <libgen.h>
+#include <limits.h>
+#endif
 #include <string.h>
 
 #include "filesrch.h"
@@ -417,6 +422,9 @@ filestatus_t filesearch(char *filename, const char *startpath, const UINT8 *want
 		}
 		else if (!strcasecmp(searchname, dent->d_name))
 		{
+#ifndef IGNORE_SYMLINKS
+			struct stat statbuf;
+#endif
 			switch (checkfilemd5(searchpath, wantedmd5sum))
 			{
 				case FS_FOUND:
@@ -424,6 +432,17 @@ filestatus_t filesearch(char *filename, const char *startpath, const UINT8 *want
 						strcpy(filename,searchpath);
 					else
 						strcpy(filename,dent->d_name);
+#ifndef IGNORE_SYMLINKS
+					if (lstat(filename, &statbuf) != -1) {
+						if (S_ISLNK(statbuf.st_mode)) {
+							char *tempbuf = realpath(filename, NULL);
+							if (!tempbuf)
+								I_Error("Error parsing link %s: %s", filename, strerror(errno));
+							strncpy(filename, tempbuf, MAX_WADPATH);
+							free(tempbuf);
+						}
+					}
+#endif
 					retval = FS_FOUND;
 					found = 1;
 					break;
-- 
GitLab