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

[PATCH] Add a new lstat implementation based on Win32 API, and make stat use that implementation too.

From
Marius Storm-Olsen <marius@trolltech.com>
Date
Sep 2, 2007, 19:31 UTC
Message-ID
<46DB0F9C.2090306@trolltech.com>
In-Reply-To
<46DB0478.8050402@trolltech.com>

This gives us a significant speedup when adding, committing and stat'ing files. (Also, since Windows doesn't really handle symlinks, it's fine that stat just uses lstat)

Signed-off-by: Marius Storm-Olsen <mstormo_git@storm-olsen.com>
---
 Revision #2 of the patch.
 For this one I change the filetime_to_time_t function to do the
 timestamp conversion inline in the FILETIME struct. That way we
 also avoid one assignment, bitshifting and addition.
 Sneaky, huh? ;-)
 New stats:
    -------------------------
    Command: git init
    -------------------------
    real    0m0.047s
    user    0m0.031s
    sys     0m0.000s
    -------------------------
    Command: git add .
    -------------------------
    real    0m12.016s
    user    0m0.015s
    sys     0m0.000s
    -------------------------
    Command: git commit -a...
    -------------------------
    real    0m17.031s
    user    0m0.015s
    sys     0m0.030s
    -------------------------
    3x Command: git-status
    -------------------------
    real    0m5.265s
    user    0m0.015s
    sys     0m0.015s
    real    0m5.297s
    user    0m0.015s
    sys     0m0.000s
    real    0m5.250s
    user    0m0.015s
    sys     0m0.016s
    -------------------------
    Command: git commit...
    (single file)
    -------------------------
    real    0m7.859s
    user    0m0.015s
    sys     0m0.015s
 compat/mingw.c    |   41 +++++++++++++++++++++++++++++++++--------
 git-compat-util.h |    4 ++++
 2 files changed, 37 insertions(+), 8 deletions(-)
diff --git a/compat/mingw.c b/compat/mingw.c
index 7711a3f..86a1419 100644
--- a/compat/mingw.c
+++ b/compat/mingw.c
@@ -23,19 +23,44 @@ int fchmod(int fildes, mode_t mode)
 	return -1;
 }

-int lstat(const char *file_name, struct stat *buf)
+static inline time_t filetime_to_time_t(const FILETIME *ft)
+{
+	long long *winTime = (long long*)ft;
+	*winTime -= 116444736000000000LL; /* Windows to Unix Epoch conversion */
+	*winTime /= 10000000;		  /* Nano to seconds resolution */
+	return (time_t)ft->dwLowDateTime;
+}
+
+extern int _getdrive( void );
+int git_lstat(const char *file_name, struct stat *buf)
 {
 	int namelen;
 	static char alt_name[PATH_MAX];
-
-	if (!stat(file_name, buf))
+	WIN32_FILE_ATTRIBUTE_DATA fdata;
+
+	if (GetFileAttributesExA(file_name, GetFileExInfoStandard, &fdata)) {
+		int fMode = S_IREAD;
+		if (fdata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
+			fMode |= S_IFDIR;
+		else
+			fMode |= S_IFREG;
+		if (!(fdata.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
+			fMode |= S_IWRITE;
+
+		buf->st_ino = 0;
+		buf->st_gid = 0;
+		buf->st_uid = 0;
+		buf->st_nlink = 1;
+		buf->st_mode = fMode;
+		buf->st_size = fdata.nFileSizeLow; /* Can't use nFileSizeHigh, since it's not a stat64 */
+		buf->st_dev = buf->st_rdev = (_getdrive() - 1);
+		buf->st_atime = filetime_to_time_t(&(fdata.ftLastAccessTime));
+		buf->st_mtime = filetime_to_time_t(&(fdata.ftLastWriteTime));
+		buf->st_ctime = filetime_to_time_t(&(fdata.ftCreationTime));
 		return 0;
+	}
+	errno = ENOENT;

-	/* if file_name ended in a '/', Windows returned ENOENT;
-	 * try again without trailing slashes
-	 */
-	if (errno != ENOENT)
-		return -1;
 	namelen = strlen(file_name);
 	if (namelen && file_name[namelen-1] != '/')
 		return -1;
diff --git a/git-compat-util.h b/git-compat-util.h
index 1ba499f..4122465 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -488,6 +488,10 @@ int mingw_rename(const char*, const char*);
 extern void quote_argv(const char **dst, const char **src);
 extern const char *parse_interpreter(const char *cmd);

+/* Make git on Windows use git_lstat instead of lstat and stat */
+int git_lstat(const char *file_name, struct stat *buf);
+#define lstat(x,y) git_lstat(x,y)
+#define stat(x,y) git_lstat(x,y)
 #endif /* __MINGW32__ */

 #endif
--
1.5.3.GIT-dirty
Previous: Johannes SixtNext: Robin Rosenberg
Message 16 of 86 in “Stats in Git”
  1. Marius Storm-OlsenSep 2, 2007
  2. Add a new lstat implementation based on Win32 API, and make stat use that implementation too.Marius Storm-Olsen, Sep 2, 2007
  3. Marius Storm-OlsenSep 2, 2007
  4. Reece DunnSep 2, 2007
  5. Marius Storm-OlsenSep 2, 2007
  6. Reece DunnSep 2, 2007
  7. Brian GernhardtSep 2, 2007
  8. Reece DunnSep 2, 2007
  9. Marius Storm-OlsenSep 2, 2007
  10. Johannes SchindelinSep 2, 2007
  11. David KastrupSep 2, 2007
  12. Marius Storm-OlsenSep 2, 2007
  13. Johannes SixtSep 2, 2007
  14. Marius Storm-OlsenSep 2, 2007
  15. Johannes SixtSep 2, 2007
  16. Add a new lstat implementation based on Win32 API, and make stat use that implementation too.Marius Storm-Olsen, Sep 2, 2007
  17. Robin RosenbergSep 2, 2007
  18. Johannes SchindelinSep 2, 2007
  19. Robin RosenbergSep 2, 2007
  20. Johannes SchindelinSep 2, 2007
  21. Johannes SixtSep 3, 2007
  22. Miklos VajnaSep 3, 2007
  23. David KastrupSep 3, 2007
  24. Miklos VajnaSep 5, 2007
  25. David KastrupSep 5, 2007
  26. Miklos VajnaSep 6, 2007
  27. David KastrupSep 6, 2007
  28. Douglas StockwellSep 6, 2007
  29. David KastrupSep 7, 2007
  30. Alex RiesenSep 2, 2007
  31. Robin RosenbergSep 2, 2007
  32. Marius Storm-OlsenSep 3, 2007
  33. Johannes SchindelinSep 3, 2007
  34. David KastrupSep 3, 2007
  35. Marius Storm-OlsenSep 3, 2007
  36. Johannes SchindelinSep 3, 2007
  37. Alex RiesenSep 2, 2007
  38. Marius Storm-OlsenSep 3, 2007
  39. Johannes SixtSep 3, 2007
  40. Marius Storm-OlsenSep 3, 2007
  41. Alex RiesenSep 2, 2007
  42. Marius Storm-OlsenSep 2, 2007
  43. Matthieu MoySep 3, 2007
  44. Marius Storm-OlsenSep 3, 2007
  45. Johannes SchindelinSep 3, 2007
  46. Marius Storm-OlsenSep 3, 2007
  47. Johannes SchindelinSep 3, 2007
  48. Marius Storm-OlsenSep 3, 2007
  49. Johannes SchindelinSep 3, 2007
  50. Johannes SixtSep 3, 2007
  51. Johannes SchindelinSep 3, 2007
  52. Marius Storm-OlsenSep 3, 2007
  53. Johannes SchindelinSep 4, 2007
  54. Johannes SixtSep 4, 2007
  55. David KastrupSep 4, 2007
  56. Marius Storm-OlsenSep 4, 2007
  57. Johannes SixtSep 4, 2007
  58. Marius Storm-OlsenSep 4, 2007
  59. Johannes SixtSep 4, 2007
  60. David KastrupSep 4, 2007
  61. Johannes SchindelinSep 4, 2007
  62. Johannes SixtSep 4, 2007
  63. Marius Storm-OlsenSep 4, 2007
  64. Marius Storm-OlsenSep 4, 2007
  65. Johannes SixtSep 4, 2007
  66. Johannes SchindelinSep 4, 2007
  67. Johannes SixtSep 4, 2007
  68. Johannes SchindelinSep 4, 2007
  69. Johannes SchindelinSep 4, 2007
  70. Marius Storm-OlsenSep 4, 2007
  71. Johannes SchindelinSep 4, 2007
  72. David KastrupSep 4, 2007
  73. Marius Storm-OlsenSep 4, 2007
  74. Johannes SchindelinSep 4, 2007
  75. Johannes SchindelinSep 4, 2007
  76. Rutger NijlunsingSep 4, 2007
  77. Reece DunnSep 4, 2007
  78. Marius Storm-OlsenSep 5, 2007
  79. Johannes SchindelinSep 5, 2007
  80. Johannes SixtSep 4, 2007
  81. Johannes SixtSep 6, 2007
  82. Marius Storm-OlsenSep 6, 2007
  83. Johannes SixtSep 3, 2007
  84. Johannes SchindelinSep 3, 2007
  85. Marius Storm-OlsenSep 3, 2007
  86. Johannes SchindelinSep 3, 2007

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.