Skip to content

Commit

Permalink
Win32: replace MSVCRT's fstat() with a Win32-based implementation
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
kblees authored and dscho committed Apr 4, 2016
1 parent c888bcd commit 881de0e
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions compat/mingw.c
Original file line number Diff line number Diff line change
Expand Up @@ -792,18 +792,31 @@ int mingw_stat(const char *file_name, struct stat *buf)
int mingw_fstat(int fd, struct stat *buf)
{
HANDLE fh = (HANDLE)_get_osfhandle(fd);
if (fh == INVALID_HANDLE_VALUE) {
DWORD avail, type = GetFileType(fh) & ~FILE_TYPE_REMOTE;

switch (type) {
case FILE_TYPE_DISK:
return get_file_info_by_handle(fh, buf);

case FILE_TYPE_CHAR:
case FILE_TYPE_PIPE:
/* initialize stat fields */
memset(buf, 0, sizeof(*buf));
buf->st_nlink = 1;

if (type == FILE_TYPE_CHAR) {
buf->st_mode = _S_IFCHR;
} else {
buf->st_mode = _S_IFIFO;
if (PeekNamedPipe(fh, NULL, 0, NULL, &avail, NULL))
buf->st_size = avail;
}
return 0;

default:
errno = EBADF;
return -1;
}
/* direct non-file handles to MS's fstat() */
if (GetFileType(fh) != FILE_TYPE_DISK)
return _fstati64(fd, buf);

if (!get_file_info_by_handle(fh, buf))
return 0;
errno = EBADF;
return -1;
}

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

0 comments on commit 881de0e

Please sign in to comment.