Skip to content

Commit 47a19a4

Browse files
committed
Create wrapper pgwin32_safestat() and redefine stat() to it
on win32, because the stat() function in the runtime cannot be trusted to always update the st_size field. Per report and research by Sergey Zubkovsky.
1 parent a57a1e6 commit 47a19a4

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

src/include/port.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
77
* Portions Copyright (c) 1994, Regents of the University of California
88
*
9-
* $PostgreSQL: pgsql/src/include/port.h,v 1.118 2008/02/29 15:31:33 mha Exp $
9+
* $PostgreSQL: pgsql/src/include/port.h,v 1.119 2008/04/10 16:58:51 mha Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -298,6 +298,19 @@ extern FILE *pgwin32_fopen(const char *, const char *);
298298
#define popen(a,b) _popen(a,b)
299299
#define pclose(a) _pclose(a)
300300

301+
/*
302+
* stat() is not guaranteed to set the st_size field on win32, so we
303+
* redefine it to our own implementation that is.
304+
*
305+
* We must pull in sys/stat.h here so the system header definition
306+
* goes in first, and we redefine that, and not the other way around.
307+
*/
308+
extern int pgwin32_safestat(const char *path, struct stat *buf);
309+
#if !defined(FRONTEND) && !defined(_DIRMOD_C)
310+
#include <sys/stat.h>
311+
#define stat(a,b) pgwin32_safestat(a,b)
312+
#endif
313+
301314
/* Missing rand functions */
302315
extern long lrand48(void);
303316
extern void srand48(long seed);

src/port/dirmod.c

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* Win32 (NT, Win2k, XP). replace() doesn't work on Win95/98/Me.
1111
*
1212
* IDENTIFICATION
13-
* $PostgreSQL: pgsql/src/port/dirmod.c,v 1.51 2008/01/01 19:46:00 momjian Exp $
13+
* $PostgreSQL: pgsql/src/port/dirmod.c,v 1.52 2008/04/10 16:58:51 mha Exp $
1414
*
1515
*-------------------------------------------------------------------------
1616
*/
@@ -447,3 +447,37 @@ rmtree(char *path, bool rmtopdir)
447447
pgfnames_cleanup(filenames);
448448
return false;
449449
}
450+
451+
452+
#ifdef WIN32
453+
/*
454+
* The stat() function in win32 is not guaranteed to update the st_size
455+
* field when run. So we define our own version that uses the Win32 API
456+
* to update this field.
457+
*/
458+
#undef stat
459+
int
460+
pgwin32_safestat(const char *path, struct stat *buf)
461+
{
462+
int r;
463+
WIN32_FILE_ATTRIBUTE_DATA attr;
464+
465+
r = stat(path, buf);
466+
if (r < 0)
467+
return r;
468+
469+
if (!GetFileAttributesEx(path, GetFileExInfoStandard, &attr))
470+
{
471+
_dosmaperr(GetLastError());
472+
return -1;
473+
}
474+
475+
/*
476+
* XXX no support for large files here, but we don't do that in
477+
* general on Win32 yet.
478+
*/
479+
buf->st_size = attr.nFileSizeLow;
480+
481+
return 0;
482+
}
483+
#endif

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy