git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 2/3] mingw: replace MSVCRT's fstat() with a Win32-based implementation

From
Karsten Blees via GitGitGadget <gitgitgadget@gmail.com>
Date
Oct 23, 2018, 10:23 UTC
Message-ID
<f2ce9bdc01892b514f75c6c25c3393765593b1ca.1540290197.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.53.git.gitgitgadget@gmail.com>
From: Karsten Blees <blees@dcon.de>

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>
---
 compat/mingw.c | 31 +++++++++++++++++++++----------
 1 file changed, 21 insertions(+), 10 deletions(-)
diff --git a/compat/mingw.c b/compat/mingw.c
index d2e7d86db..07fc0b79a 100644
--- a/compat/mingw.c
+++ b/compat/mingw.c
@@ -771,20 +771,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);
+	DWORD avail, type = GetFileType(fh) & ~FILE_TYPE_REMOTE;
 
-	if (fh == INVALID_HANDLE_VALUE) {
-		errno = EBADF;
-		return -1;
-	}
-	/* direct non-file handles to MS's fstat() */
-	if (GetFileType(fh) != FILE_TYPE_DISK)
-		return _fstati64(fd, buf);
+	switch (type) {
+	case FILE_TYPE_DISK:
+		return get_file_info_by_handle(fh, buf);
 
-	if (!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;
 
-	errno = EBADF;
-	return -1;
+	default:
+		errno = EBADF;
+		return -1;
+	}
 }
 
 static inline void time_t_to_filetime(time_t t, FILETIME *ft)
-- 
gitgitgadget
Previous: Johannes Schindelin via GitGitGadgetNext: brian m. carlson
Message 3 of 8 in “Use nanosecond-precision file times on Windows”
  1. 0/3 Use nanosecond-precision file times on WindowsJohannes Schindelin via GitGitGadget, Oct 23, 2018
  2. 1/3 mingw: factor out code to set stat() dataJohannes Schindelin via GitGitGadget, Oct 23, 2018
  3. 2/3 mingw: replace MSVCRT's fstat() with a Win32-based implementationKarsten Blees via GitGitGadget, Oct 23, 2018
  4. brian m. carlsonOct 24, 2018
  5. Johannes SchindelinOct 24, 2018
  6. brian m. carlsonOct 24, 2018
  7. Junio C HamanoOct 25, 2018
  8. 3/3 mingw: implement nanosecond-precision file timesKarsten Blees via GitGitGadget, Oct 23, 2018

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.