Browse Source

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 <blees@dcon.de>
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
maint
Karsten Blees 6 years ago committed by Junio C Hamano
parent
commit
d75e697353
  1. 27
      compat/mingw.c

27
compat/mingw.c

@ -771,21 +771,32 @@ int mingw_stat(const char *file_name, struct stat *buf)
int mingw_fstat(int fd, struct stat *buf) int mingw_fstat(int fd, struct stat *buf)
{ {
HANDLE fh = (HANDLE)_get_osfhandle(fd); HANDLE fh = (HANDLE)_get_osfhandle(fd);
DWORD avail, type = GetFileType(fh) & ~FILE_TYPE_REMOTE;


if (fh == INVALID_HANDLE_VALUE) { switch (type) {
errno = EBADF; case FILE_TYPE_DISK:
return -1; return get_file_info_by_handle(fh, buf);
}
/* direct non-file handles to MS's fstat() */ case FILE_TYPE_CHAR:
if (GetFileType(fh) != FILE_TYPE_DISK) case FILE_TYPE_PIPE:
return _fstati64(fd, buf); /* initialize stat fields */
memset(buf, 0, sizeof(*buf));
buf->st_nlink = 1;


if (!get_file_info_by_handle(fh, buf)) 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; return 0;


default:
errno = EBADF; errno = EBADF;
return -1; return -1;
} }
}


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

Loading…
Cancel
Save