Skip to content

Commit 8e80e95

Browse files
kbleesdscho
authored andcommitted
mingw: 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. This allows us also to implement some clever code to handle pipes and character devices in our own way. Signed-off-by: Karsten Blees <[email protected]> Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 0552f2b commit 8e80e95

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

compat/mingw.c

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -741,20 +741,31 @@ int mingw_stat(const char *file_name, struct stat *buf)
741741
int mingw_fstat(int fd, struct stat *buf)
742742
{
743743
HANDLE fh = (HANDLE)_get_osfhandle(fd);
744+
DWORD avail, type = GetFileType(fh) & ~FILE_TYPE_REMOTE;
744745

745-
if (fh == INVALID_HANDLE_VALUE) {
746-
errno = EBADF;
747-
return -1;
748-
}
749-
/* direct non-file handles to MS's fstat() */
750-
if (GetFileType(fh) != FILE_TYPE_DISK)
751-
return _fstati64(fd, buf);
746+
switch (type) {
747+
case FILE_TYPE_DISK:
748+
return get_file_info_by_handle(fh, buf);
752749

753-
if (!get_file_info_by_handle(fh, buf))
750+
case FILE_TYPE_CHAR:
751+
case FILE_TYPE_PIPE:
752+
/* initialize stat fields */
753+
memset(buf, 0, sizeof(*buf));
754+
buf->st_nlink = 1;
755+
756+
if (type == FILE_TYPE_CHAR) {
757+
buf->st_mode = _S_IFCHR;
758+
} else {
759+
buf->st_mode = _S_IFIFO;
760+
if (PeekNamedPipe(fh, NULL, 0, NULL, &avail, NULL))
761+
buf->st_size = avail;
762+
}
754763
return 0;
755764

756-
errno = EBADF;
757-
return -1;
765+
default:
766+
errno = EBADF;
767+
return -1;
768+
}
758769
}
759770

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

0 commit comments

Comments
 (0)