From 6000b7606167fe18b73d63a744d57d013d6793eb Mon Sep 17 00:00:00 2001
From: "Rob Swindell (on Debian Linux)" <rob@synchro.net>
Date: Wed, 4 Dec 2024 16:09:00 -0800
Subject: [PATCH] Clean up the OFD check/decision, make use of fcntl() locks
 easier to opt-in

---
 src/xpdev/filewrap.c | 34 +++++++++++++++++++---------------
 1 file changed, 19 insertions(+), 15 deletions(-)

diff --git a/src/xpdev/filewrap.c b/src/xpdev/filewrap.c
index 35ebe13eab..c7415dea0b 100644
--- a/src/xpdev/filewrap.c
+++ b/src/xpdev/filewrap.c
@@ -70,24 +70,28 @@ off_t filelength(int fd)
 	return(st.st_size);
 }
 
-// See https://patchwork.kernel.org/patch/9289177/
-#if defined(F_OFD_SETLK) && _FILE_OFFSET_BITS != 64
-	#undef F_OFD_SETLK
-#endif
-
-#if defined(__linux__) && !defined(F_OFD_SETLK)
-	#warning Linux OFD locks not enabled!
-#endif
-
-#if defined(F_OFD_SETLK)
-	#undef F_SETLK
-	#define F_SETLK F_OFD_SETLK
+/*************************************/
+/* Use OFD fcntl() locks when we can */
+/*************************************/
+#if defined __linux__
+	#define USE_FCNTL_LOCKS
+	// See https://patchwork.kernel.org/patch/9289177/
+	#if defined F_OFD_SETLK && _FILE_OFFSET_BITS != 64
+		#undef F_OFD_SETLK
+	#endif
+
+	#if defined F_OFD_SETLK
+		#undef F_SETLK
+		#define F_SETLK F_OFD_SETLK
+	#else
+		#warning Linux OFD locks not enabled!
+	#endif
 #endif
 
 /* Sets a lock on a portion of a file */
 int lock(int fd, off_t pos, off_t len)
 {
-#if !defined(BSD)
+#if defined USE_FCNTL_LOCKS
 	struct flock alock = {0};
 
 	// fcntl() will return EBADF if we try to set a write lock a file opened O_RDONLY
@@ -117,7 +121,7 @@ int lock(int fd, off_t pos, off_t len)
 int unlock(int fd, off_t pos, off_t len)
 {
 
-#if !defined(BSD)
+#if defined USE_FCNTL_LOCKS
 	struct flock alock = {0};
 
 	alock.l_type = F_UNLCK;   /* remove the lock */
@@ -195,7 +199,7 @@ int sopen(const char *fn, int sh_access, int share, ...)
 	if (share == SH_DENYNO || share == SH_COMPAT) /* no lock needed */
 		return fd;
 
-#if !defined(BSD)
+#if defined USE_FCNTL_LOCKS
 
 	struct flock alock = {0}; // lock entire file from offset 0
 
-- 
GitLab