Skip to content

Commit 881de0e

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 c888bcd commit 881de0e

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
@@ -792,18 +792,31 @@ int mingw_stat(const char *file_name, struct stat *buf)
792792
int mingw_fstat(int fd, struct stat *buf)
793793
{
794794
HANDLE fh = (HANDLE)_get_osfhandle(fd);
795-
if (fh == INVALID_HANDLE_VALUE) {
795+
DWORD avail, type = GetFileType(fh) & ~FILE_TYPE_REMOTE;
796+
797+
switch (type) {
798+
case FILE_TYPE_DISK:
799+
return get_file_info_by_handle(fh, buf);
800+
801+
case FILE_TYPE_CHAR:
802+
case FILE_TYPE_PIPE:
803+
/* initialize stat fields */
804+
memset(buf, 0, sizeof(*buf));
805+
buf->st_nlink = 1;
806+
807+
if (type == FILE_TYPE_CHAR) {
808+
buf->st_mode = _S_IFCHR;
809+
} else {
810+
buf->st_mode = _S_IFIFO;
811+
if (PeekNamedPipe(fh, NULL, 0, NULL, &avail, NULL))
812+
buf->st_size = avail;
813+
}
814+
return 0;
815+
816+
default:
796817
errno = EBADF;
797818
return -1;
798819
}
799-
/* direct non-file handles to MS's fstat() */
800-
if (GetFileType(fh) != FILE_TYPE_DISK)
801-
return _fstati64(fd, buf);
802-
803-
if (!get_file_info_by_handle(fh, buf))
804-
return 0;
805-
errno = EBADF;
806-
return -1;
807820
}
808821

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

0 commit comments

Comments
 (0)