From 11af4bd45489d4a180fb7e6f85e701cbf8d4d9a9 Mon Sep 17 00:00:00 2001
From: "Rob Swindell (on Windows 11)" <rob@synchro.net>
Date: Tue, 3 Dec 2024 09:06:14 -0800
Subject: [PATCH] On Windows, lock the opened mutex file to insure *nix sopen()
 will fail

Even though we use O_EXCL, networked file systems still sometimes allow the
secondary open() to succeed. We use record locking in xpdev's sopen()
implementation for *nix, so this lock will insure such opens (and locks) will
now fail.

This is an attempt to address occassional error on Vertrauen:
ERROR 2 (No such file or directory) renaming *.rep to *.bad
---
 src/sbbs3/nopen.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/src/sbbs3/nopen.c b/src/sbbs3/nopen.c
index f25572ede3..fd2306ec0b 100644
--- a/src/sbbs3/nopen.c
+++ b/src/sbbs3/nopen.c
@@ -154,6 +154,15 @@ bool _fmutex_open(fmutex_t* fm, const char* text, long max_age, bool auto_remove
 		);
 	if(h == INVALID_HANDLE_VALUE)
 		return false;
+	if(!LockFile(h,
+		0,	// dwFileOffsetLow
+		0,	// dwFileOffsetHigh
+		1,	// nNumberOfBytesToLockLow
+		0	// nNumberOfBytesToLockHigh
+		)) {
+		CloseHandle(h);
+		return false;
+	}
 	if((fm->fd = _open_osfhandle((intptr_t)h, O_WRONLY)) == -1) {
 		CloseHandle(h);
 		return false;
-- 
GitLab