1
0
mirror of https://github.com/postgres/postgres.git synced 2025-10-24 01:29:19 +03:00

Fix our Windows stat() emulation to handle file sizes > 4GB.

Hack things so that our idea of "struct stat" is equivalent to Windows'
struct __stat64, allowing it to have a wide enough st_size field.

Instead of relying on native stat(), use GetFileInformationByHandle().
This avoids a number of issues with Microsoft's multiple and rather
slipshod emulations of stat().  We still need to jump through hoops
to deal with ERROR_DELETE_PENDING, though :-(

Pull the relevant support code out of dirmod.c and put it into
its own file, win32stat.c.

Still TODO: do we need to do something different with lstat(),
rather than treating it identically to stat()?

Juan José Santamaría Flecha, reviewed by Emil Iggland;
based on prior work by Michael Paquier, Sergey Zubkovsky, and others

Discussion: https://postgr.es/m/1803D792815FC24D871C00D17AE95905CF5099@g01jpexmbkw24
Discussion: https://postgr.es/m/15858-9572469fd3b73263@postgresql.org
This commit is contained in:
Tom Lane
2020-10-09 16:20:12 -04:00
parent f13f2e4841
commit bed90759fc
6 changed files with 340 additions and 66 deletions

View File

@@ -51,7 +51,13 @@
#include <signal.h>
#include <direct.h>
#undef near
#include <sys/stat.h> /* needed before sys/stat hacking below */
/* needed before sys/stat hacking below: */
#define fstat microsoft_native_fstat
#define stat microsoft_native_stat
#include <sys/stat.h>
#undef fstat
#undef stat
/* Must be here to avoid conflicting with prototype in windows.h */
#define mkdir(a,b) mkdir(a)
@@ -240,20 +246,34 @@ typedef int pid_t;
* Supplement to <sys/stat.h>.
*
* We must pull in sys/stat.h before this part, else our overrides lose.
*/
#define lstat(path, sb) stat(path, sb)
/*
* stat() is not guaranteed to set the st_size field on win32, so we
* redefine it to our own implementation that is.
*
* Some frontends don't need the size from stat, so if UNSAFE_STAT_OK
* is defined we don't bother with this.
* stat() is not guaranteed to set the st_size field on win32, so we
* redefine it to our own implementation. See src/port/win32stat.c.
*
* The struct stat is 32 bit in MSVC, so we redefine it as a copy of
* struct __stat64. This also fixes the struct size for MINGW builds.
*/
#ifndef UNSAFE_STAT_OK
extern int pgwin32_safestat(const char *path, struct stat *buf);
#define stat(a,b) pgwin32_safestat(a,b)
#endif
struct stat /* This should match struct __stat64 */
{
_dev_t st_dev;
_ino_t st_ino;
unsigned short st_mode;
short st_nlink;
short st_uid;
short st_gid;
_dev_t st_rdev;
__int64 st_size;
__time64_t st_atime;
__time64_t st_mtime;
__time64_t st_ctime;
};
extern int _pgfstat64(int fileno, struct stat *buf);
extern int _pgstat64(const char *name, struct stat *buf);
#define fstat(fileno, sb) _pgfstat64(fileno, sb)
#define stat(path, sb) _pgstat64(path, sb)
#define lstat(path, sb) _pgstat64(path, sb)
/* These macros are not provided by older MinGW, nor by MSVC */
#ifndef S_IRUSR