Skip to content

Commit 53ffa01

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 8a8209a commit 53ffa01

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
@@ -934,18 +934,31 @@ int mingw_stat(const char *file_name, struct stat *buf)
934934
int mingw_fstat(int fd, struct stat *buf)
935935
{
936936
HANDLE fh = (HANDLE)_get_osfhandle(fd);
937-
if (fh == INVALID_HANDLE_VALUE) {
937+
DWORD avail, type = GetFileType(fh) & ~FILE_TYPE_REMOTE;
938+
939+
switch (type) {
940+
case FILE_TYPE_DISK:
941+
return get_file_info_by_handle(fh, buf);
942+
943+
case FILE_TYPE_CHAR:
944+
case FILE_TYPE_PIPE:
945+
/* initialize stat fields */
946+
memset(buf, 0, sizeof(*buf));
947+
buf->st_nlink = 1;
948+
949+
if (type == FILE_TYPE_CHAR) {
950+
buf->st_mode = _S_IFCHR;
951+
} else {
952+
buf->st_mode = _S_IFIFO;
953+
if (PeekNamedPipe(fh, NULL, 0, NULL, &avail, NULL))
954+
buf->st_size = avail;
955+
}
956+
return 0;
957+
958+
default:
938959
errno = EBADF;
939960
return -1;
940961
}
941-
/* direct non-file handles to MS's fstat() */
942-
if (GetFileType(fh) != FILE_TYPE_DISK)
943-
return _fstati64(fd, buf);
944-
945-
if (!get_file_info_by_handle(fh, buf))
946-
return 0;
947-
errno = EBADF;
948-
return -1;
949962
}
950963

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

0 commit comments

Comments
 (0)