Skip to content

Commit 2583ad3

Browse files
kbleesdscho
authored andcommitted
Win32: replace MSVCRT's fstat() with a Win32-based implementation
fstat() is the only stat-related CRT function for which we don't have a full replacement yet (and thus the only reason to stick with MSVCRT's 'struct stat' definition). Fully implement fstat(), in preparation of implementing a POSIX 2013 compatible 'struct stat' with nanosecond-precision file times. Signed-off-by: Karsten Blees <[email protected]>
1 parent b1bcead commit 2583ad3

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

compat/mingw.c

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -895,18 +895,31 @@ int mingw_stat(const char *file_name, struct stat *buf)
895895
int mingw_fstat(int fd, struct stat *buf)
896896
{
897897
HANDLE fh = (HANDLE)_get_osfhandle(fd);
898-
if (fh == INVALID_HANDLE_VALUE) {
898+
DWORD avail, type = GetFileType(fh) & ~FILE_TYPE_REMOTE;
899+
900+
switch (type) {
901+
case FILE_TYPE_DISK:
902+
return get_file_info_by_handle(fh, buf);
903+
904+
case FILE_TYPE_CHAR:
905+
case FILE_TYPE_PIPE:
906+
/* initialize stat fields */
907+
memset(buf, 0, sizeof(*buf));
908+
buf->st_nlink = 1;
909+
910+
if (type == FILE_TYPE_CHAR) {
911+
buf->st_mode = _S_IFCHR;
912+
} else {
913+
buf->st_mode = _S_IFIFO;
914+
if (PeekNamedPipe(fh, NULL, 0, NULL, &avail, NULL))
915+
buf->st_size = avail;
916+
}
917+
return 0;
918+
919+
default:
899920
errno = EBADF;
900921
return -1;
901922
}
902-
/* direct non-file handles to MS's fstat() */
903-
if (GetFileType(fh) != FILE_TYPE_DISK)
904-
return _fstati64(fd, buf);
905-
906-
if (!get_file_info_by_handle(fh, buf))
907-
return 0;
908-
errno = EBADF;
909-
return -1;
910923
}
911924

912925
static inline void time_t_to_filetime(time_t t, FILETIME *ft)

0 commit comments

Comments
 (0)