Some better new: 1. _chsize_s does in fact seem to work in an msvcrt.dll build. Thanks to Andres for testing that for me on Windows with a standalone 2 line program on ucrt and msvcrt runtimes.
2. MinGW always has _chsize_s, but it does more or less what I had been proposing already if it can't find the function by the Windows thing like dlsym, so there's no point in writing another thing like that. The test was with a 3G file though so it was the real function. https://github.com/mingw-w64/mingw-w64/blob/master/mingw-w64-crt/secapi/_chsize_s.c 3. After a long time trying various ways to make it tidy and getting incomprehensible results I eventually realised that precompiled headers were causing strange results on MinGW/Meson builds (on CI), due to fighting over macros with this guy: https://github.com/mingw-w64/mingw-w64/blob/master/mingw-w64-headers/crt/unistd.h I found it easiest to completely stop it from getting in our way completely, with the attached. Thoughts, anyone? Davinder, Jakub, are you in a position repro the issue and make versions of the patch for the 13, 15 and master branches on top of that?
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