Skip to content

Commit 1fd0690

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 53d4ae0 commit 1fd0690

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
@@ -811,18 +811,31 @@ int mingw_stat(const char *file_name, struct stat *buf)
811811
int mingw_fstat(int fd, struct stat *buf)
812812
{
813813
HANDLE fh = (HANDLE)_get_osfhandle(fd);
814-
if (fh == INVALID_HANDLE_VALUE) {
814+
DWORD avail, type = GetFileType(fh) & ~FILE_TYPE_REMOTE;
815+
816+
switch (type) {
817+
case FILE_TYPE_DISK:
818+
return get_file_info_by_handle(fh, buf);
819+
820+
case FILE_TYPE_CHAR:
821+
case FILE_TYPE_PIPE:
822+
/* initialize stat fields */
823+
memset(buf, 0, sizeof(*buf));
824+
buf->st_nlink = 1;
825+
826+
if (type == FILE_TYPE_CHAR) {
827+
buf->st_mode = _S_IFCHR;
828+
} else {
829+
buf->st_mode = _S_IFIFO;
830+
if (PeekNamedPipe(fh, NULL, 0, NULL, &avail, NULL))
831+
buf->st_size = avail;
832+
}
833+
return 0;
834+
835+
default:
815836
errno = EBADF;
816837
return -1;
817838
}
818-
/* direct non-file handles to MS's fstat() */
819-
if (GetFileType(fh) != FILE_TYPE_DISK)
820-
return _fstati64(fd, buf);
821-
822-
if (!get_file_info_by_handle(fh, buf))
823-
return 0;
824-
errno = EBADF;
825-
return -1;
826839
}
827840

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

0 commit comments

Comments
 (0)