Skip to content

Commit af109e3

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 45aef9f commit af109e3

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
@@ -306,6 +306,33 @@ extern bool rmtree(const char *path, bool rmtopdir);
306306

307307
#if defined(WIN32) && !defined(__CYGWIN__)
308308

309+
/*
310+
* We want the 64-bit variant of lseek().
311+
*
312+
* For Visual Studio, this must be after <io.h> to avoid messing up its
313+
* lseek() and _lseeki64() function declarations.
314+
*
315+
* For MinGW there is already a macro, so we have to undefine it (depending on
316+
* _FILE_OFFSET_BITS, it may point at its own lseek64, but we don't want to
317+
* count on that being set).
318+
*/
319+
#undef lseek
320+
#define lseek(a,b,c) _lseeki64((a),(b),(c))
321+
322+
/*
323+
* We want the 64-bit variant of chsize(). It sets errno and also returns it,
324+
* so convert non-zero result to -1 to match POSIX.
325+
*
326+
* Prevent MinGW from declaring functions, and undefine its macro before we
327+
* define our own.
328+
*/
329+
#ifndef _MSC_VER
330+
#define FTRUNCATE_DEFINED
331+
#include <unistd.h>
332+
#undef ftruncate
333+
#endif
334+
#define ftruncate(a,b) (_chsize_s((a),(b)) == 0 ? 0 : -1)
335+
309336
/*
310337
* open() and fopen() replacements to allow deletion of open files and
311338
* passing of other special options.

src/include/port/win32_port.h

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

82-
#define ftruncate(a,b) chsize(a,b)
83-
8482
/* Windows doesn't have fsync() as such, use _commit() */
8583
#define fsync(fd) _commit(fd)
8684

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)