Skip to content

Commit 1636c5e

Browse files
committed
Provide 64-bit ftruncate() and lseek() on Windows.
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
1 parent 049c8cb commit 1636c5e

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

src/include/port.h

+27
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,33 @@ extern bool rmtree(const char *path, bool rmtopdir);
293293

294294
#if defined(WIN32) && !defined(__CYGWIN__)
295295

296+
/*
297+
* We want the 64-bit variant of lseek().
298+
*
299+
* For Visual Studio, this must be after <io.h> to avoid messing up its
300+
* lseek() and _lseeki64() function declarations.
301+
*
302+
* For MinGW there is already a macro, so we have to undefine it (depending on
303+
* _FILE_OFFSET_BITS, it may point at its own lseek64, but we don't want to
304+
* count on that being set).
305+
*/
306+
#undef lseek
307+
#define lseek(a,b,c) _lseeki64((a),(b),(c))
308+
309+
/*
310+
* We want the 64-bit variant of chsize(). It sets errno and also returns it,
311+
* so convert non-zero result to -1 to match POSIX.
312+
*
313+
* Prevent MinGW from declaring functions, and undefine its macro before we
314+
* define our own.
315+
*/
316+
#ifndef _MSC_VER
317+
#define FTRUNCATE_DEFINED
318+
#include <unistd.h>
319+
#undef ftruncate
320+
#endif
321+
#define ftruncate(a,b) (_chsize_s((a),(b)) == 0 ? 0 : -1)
322+
296323
/*
297324
* open() and fopen() replacements to allow deletion of open files and
298325
* passing of other special options.

src/include/port/win32_port.h

-2
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,6 @@
6262
/* Must be here to avoid conflicting with prototype in windows.h */
6363
#define mkdir(a,b) mkdir(a)
6464

65-
#define ftruncate(a,b) chsize(a,b)
66-
6765
/* Windows doesn't have fsync() as such, use _commit() */
6866
#define fsync(fd) _commit(fd)
6967

src/pl/plperl/plperl_system.h

+1
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@
109109
#undef fstat
110110
#undef kill
111111
#undef listen
112+
#undef lseek
112113
#undef lstat
113114
#undef mkdir
114115
#undef open

0 commit comments

Comments
 (0)