Skip to content

Commit d02486c

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 a722252 commit d02486c

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

src/include/port.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,33 @@ extern bool rmtree(const char *path, bool rmtopdir);
275275

276276
#if defined(WIN32) && !defined(__CYGWIN__)
277277

278+
/*
279+
* We want the 64-bit variant of lseek().
280+
*
281+
* For Visual Studio, this must be after <io.h> to avoid messing up its
282+
* lseek() and _lseeki64() function declarations.
283+
*
284+
* For MinGW there is already a macro, so we have to undefine it (depending on
285+
* _FILE_OFFSET_BITS, it may point at its own lseek64, but we don't want to
286+
* count on that being set).
287+
*/
288+
#undef lseek
289+
#define lseek(a,b,c) _lseeki64((a),(b),(c))
290+
291+
/*
292+
* We want the 64-bit variant of chsize(). It sets errno and also returns it,
293+
* so convert non-zero result to -1 to match POSIX.
294+
*
295+
* Prevent MinGW from declaring functions, and undefine its macro before we
296+
* define our own.
297+
*/
298+
#ifndef _MSC_VER
299+
#define FTRUNCATE_DEFINED
300+
#include <unistd.h>
301+
#undef ftruncate
302+
#endif
303+
#define ftruncate(a,b) (_chsize_s((a),(b)) == 0 ? 0 : -1)
304+
278305
/*
279306
* open() and fopen() replacements to allow deletion of open files and
280307
* passing of other special options.

src/include/port/win32_port.h

Lines changed: 0 additions & 2 deletions
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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
#undef fopen
109109
#undef kill
110110
#undef listen
111+
#undef lseek
111112
#undef lstat
112113
#undef mkdir
113114
#undef open

0 commit comments

Comments
 (0)