v3-0001-Redirect-to-64-bit-ftruncate-and-lseek-on-Windows.patch

application/x-patch

Filename: v3-0001-Redirect-to-64-bit-ftruncate-and-lseek-on-Windows.patch
Type: application/x-patch
Part: 0
Message: Re: Windows pg_basebackup unable to create >2GB pg_wal.tar tarballs ("could not close file: Invalid argument" when creating pg_wal.tar of size ~ 2^31 bytes)

Patch

Format: format-patch
Series: patch v3-0001
Subject: Redirect to 64-bit ftruncate and lseek on Windows.
File+
src/include/port.h 27 0
src/include/port/win32_port.h 0 2
From 4dbd9f4b794dbeac2c01b978d712a43e33bbfb70 Mon Sep 17 00:00:00 2001
From: Thomas Munro <thomas.munro@gmail.com>
Date: Fri, 6 Dec 2024 14:32:06 +1300
Subject: [PATCH v3] Redirect to 64-bit ftruncate and lseek on Windows.

In preparation for a later patch, change our ftruncate() and lseek()
macros to use the 64-bit versions.

Discussion: https://postgr.es/m/CAKZiRmyM4YnokK6Oenw5JKwAQ3rhP0YTz2T-tiw5dAQjGRXE3Q%40mail.gmail.com
---
 src/include/port.h            | 27 +++++++++++++++++++++++++++
 src/include/port/win32_port.h |  2 --
 2 files changed, 27 insertions(+), 2 deletions(-)

diff --git a/src/include/port.h b/src/include/port.h
index ba9ab0d34f4..5535811005d 100644
--- a/src/include/port.h
+++ b/src/include/port.h
@@ -306,6 +306,33 @@ extern bool rmtree(const char *path, bool rmtopdir);
 
 #if defined(WIN32) && !defined(__CYGWIN__)
 
+/*
+ * We want the 64-bit version of lseek().
+ *
+ * For Visual Studio, this must be after <io.h> to avoid messing up its
+ * lseek() and _lseeki64() function declarations.
+ *
+ * For MinGW there is already a macro, so we have to undefine it (depending on
+ * _FILE_OFFSET_BITS, it may point at its own lseek64, but we don't want to
+ * count on that being set).
+ */
+#undef lseek
+#define lseek _lseeki64
+
+/*
+ * We want the 64-bit version of chsize().
+ *
+ * Prevent MinGW from declaring functions, and undefine its macro before we
+ * define our own.
+ */
+#ifndef _MSC_VER
+#define FTRUNCATE_DEFINED
+#include <unistd.h>
+#undef ftruncate
+#endif
+
+#define ftruncate(a,b) ((errno = _chsize_s((a), (b))) == 0 ? 0 : -1)
+
 /*
  * open() and fopen() replacements to allow deletion of open files and
  * passing of other special options.
diff --git a/src/include/port/win32_port.h b/src/include/port/win32_port.h
index 7789e0431aa..c03747393e4 100644
--- a/src/include/port/win32_port.h
+++ b/src/include/port/win32_port.h
@@ -79,8 +79,6 @@
 /* Must be here to avoid conflicting with prototype in windows.h */
 #define mkdir(a,b)	mkdir(a)
 
-#define ftruncate(a,b)	chsize(a,b)
-
 /* Windows doesn't have fsync() as such, use _commit() */
 #define fsync(fd) _commit(fd)
 
-- 
2.47.0