From c5947043953c2be0897ec43c7de025f97da39886 Mon Sep 17 00:00:00 2001 From: Konstantin Knizhnik Date: Mon, 27 May 2019 18:00:59 +0300 Subject: [PATCH] [Issue #62] Replace pgwin32_safestat with fio_safestat --- src/utils/file.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/utils/file.c b/src/utils/file.c index 07a646d4..34b2b18f 100644 --- a/src/utils/file.c +++ b/src/utils/file.c @@ -51,6 +51,41 @@ static bool fio_is_remote_fd(int fd) } #ifdef WIN32 + +#undef stat + +/* + * The stat() function in win32 is not guaranteed to update the st_size + * field when run. So we define our own version that uses the Win32 API + * to update this field. + */ +static int +fio_safestat(const char *path, struct stat *buf) +{ + int r; + WIN32_FILE_ATTRIBUTE_DATA attr; + + r = stat(path, buf); + if (r < 0) + return r; + + if (!GetFileAttributesEx(path, GetFileExInfoStandard, &attr)) + { + errno = ENOENT; + return -1; + } + + /* + * XXX no support for large files here, but we don't do that in general on + * Win32 yet. + */ + buf->st_size = attr.nFileSizeLow; + + return 0; +} + +#define stat(x) fio_safestat(x) + static ssize_t pread(int fd, void* buf, size_t size, off_t off) { off_t rc = lseek(fd, off, SEEK_SET);