]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Provide 64-bit ftruncate() and lseek() on Windows.
authorThomas Munro <tmunro@postgresql.org>
Wed, 8 Jan 2025 23:10:26 +0000 (12:10 +1300)
committerThomas Munro <tmunro@postgresql.org>
Thu, 9 Jan 2025 02:00:23 +0000 (15:00 +1300)
Change our ftruncate() macro to use the 64-bit variant of chsize(), and
add a new macro to redirect lseek() to _lseeki64().

Back-patch to all supported releases, in preparation for a bug fix.

Tested-by: Davinder Singh <davinder.singh@enterprisedb.com>
Discussion: https://postgr.es/m/CAKZiRmyM4YnokK6Oenw5JKwAQ3rhP0YTz2T-tiw5dAQjGRXE3Q%40mail.gmail.com

src/include/port.h
src/include/port/win32_port.h
src/pl/plperl/plperl_system.h

index ae115d2d970d2ae6a7fdeb4c9a9de13ee90a678b..4d2d911cb619777a19f4b7f5405057ca0d57aaab 100644 (file)
@@ -306,6 +306,33 @@ extern bool rmtree(const char *path, bool rmtopdir);
 
 #if defined(WIN32) && !defined(__CYGWIN__)
 
+/*
+ * We want the 64-bit variant 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(a,b,c) _lseeki64((a),(b),(c))
+
+/*
+ * We want the 64-bit variant of chsize().  It sets errno and also returns it,
+ * so convert non-zero result to -1 to match POSIX.
+ *
+ * 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) (_chsize_s((a),(b)) == 0 ? 0 : -1)
+
 /*
  * open() and fopen() replacements to allow deletion of open files and
  * passing of other special options.
index 3d1de166cb06f630b7cda07d85805e8c62ac3c22..2d4d2c5868941649fc0dd2c43265483551af7075 100644 (file)
@@ -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)
 
index 48f6575e513b79dd6d838f4f6ea66d93d3a907d2..c3b66ee2623a8676a31602f02c9e8bd652c8e11c 100644 (file)
 #undef fstat
 #undef kill
 #undef listen
+#undef lseek
 #undef lstat
 #undef mkdir
 #undef open