Skip to content

Commit f715757

Browse files
billziss-ghdscho
authored andcommitted
mingw: lstat: compute correct size for symlinks
This commit fixes mingw_lstat by computing the proper size for symlinks according to POSIX. POSIX specifies that upon successful return from lstat: "the value of the st_size member shall be set to the length of the pathname contained in the symbolic link not including any terminating null byte". Prior to this commit the mingw_lstat function returned a fixed size of 4096. This caused problems in git repositories that were accessed by git for Cygwin or git for WSL. For example, doing `git reset --hard` using git for Windows would update the size of symlinks in the index to be 4096; at a later time git for Cygwin or git for WSL would find that symlinks have changed size during `git status`. Vice versa doing `git reset --hard` in git for Cygwin or git for WSL would update the size of symlinks in the index with the correct value, only for git for Windows to find incorrectly at a later time that the size had changed. Signed-off-by: Bill Zissimopoulos <[email protected]> Signed-off-by: Johannes Schindelin <[email protected]>
1 parent c1c3110 commit f715757

File tree

2 files changed

+56
-21
lines changed

2 files changed

+56
-21
lines changed

compat/mingw.c

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -862,10 +862,14 @@ static int has_valid_directory_prefix(wchar_t *wfilename)
862862
return 1;
863863
}
864864

865+
static int readlink_1(const WCHAR *wpath, BOOL fail_on_unknown_tag,
866+
char *tmpbuf, int *plen, DWORD *ptag);
867+
865868
int mingw_lstat(const char *file_name, struct stat *buf)
866869
{
867870
WIN32_FILE_ATTRIBUTE_DATA fdata;
868-
WIN32_FIND_DATAW findbuf = { 0 };
871+
DWORD reparse_tag = 0;
872+
int link_len = 0;
869873
wchar_t wfilename[MAX_LONG_PATH];
870874
int wlen = xutftowcs_long_path(wfilename, file_name);
871875
if (wlen < 0)
@@ -880,28 +884,29 @@ int mingw_lstat(const char *file_name, struct stat *buf)
880884
}
881885

882886
if (GetFileAttributesExW(wfilename, GetFileExInfoStandard, &fdata)) {
883-
/* for reparse points, use FindFirstFile to get the reparse tag */
887+
/* for reparse points, get the link tag and length */
884888
if (fdata.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
885-
HANDLE handle = FindFirstFileW(wfilename, &findbuf);
886-
if (handle == INVALID_HANDLE_VALUE)
887-
goto error;
888-
FindClose(handle);
889+
char tmpbuf[MAX_LONG_PATH];
890+
891+
if (readlink_1(wfilename, FALSE, tmpbuf, &link_len,
892+
&reparse_tag) < 0)
893+
return -1;
889894
}
890895
buf->st_ino = 0;
891896
buf->st_gid = 0;
892897
buf->st_uid = 0;
893898
buf->st_nlink = 1;
894899
buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes,
895-
findbuf.dwReserved0);
896-
buf->st_size = S_ISLNK(buf->st_mode) ? MAX_LONG_PATH :
900+
reparse_tag);
901+
buf->st_size = S_ISLNK(buf->st_mode) ? link_len :
897902
fdata.nFileSizeLow | (((off_t) fdata.nFileSizeHigh) << 32);
898903
buf->st_dev = buf->st_rdev = 0; /* not used by Git */
899904
filetime_to_timespec(&(fdata.ftLastAccessTime), &(buf->st_atim));
900905
filetime_to_timespec(&(fdata.ftLastWriteTime), &(buf->st_mtim));
901906
filetime_to_timespec(&(fdata.ftCreationTime), &(buf->st_ctim));
902907
return 0;
903908
}
904-
error:
909+
905910
switch (GetLastError()) {
906911
case ERROR_ACCESS_DENIED:
907912
case ERROR_SHARING_VIOLATION:
@@ -2740,17 +2745,13 @@ typedef struct _REPARSE_DATA_BUFFER {
27402745
} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
27412746
#endif
27422747

2743-
int readlink(const char *path, char *buf, size_t bufsiz)
2748+
static int readlink_1(const WCHAR *wpath, BOOL fail_on_unknown_tag,
2749+
char *tmpbuf, int *plen, DWORD *ptag)
27442750
{
27452751
HANDLE handle;
2746-
WCHAR wpath[MAX_LONG_PATH], *wbuf;
2752+
WCHAR *wbuf;
27472753
REPARSE_DATA_BUFFER *b = alloca(MAXIMUM_REPARSE_DATA_BUFFER_SIZE);
27482754
DWORD dummy;
2749-
char tmpbuf[MAX_LONG_PATH];
2750-
int len;
2751-
2752-
if (xutftowcs_long_path(wpath, path) < 0)
2753-
return -1;
27542755

27552756
/* read reparse point data */
27562757
handle = CreateFileW(wpath, 0,
@@ -2770,7 +2771,7 @@ int readlink(const char *path, char *buf, size_t bufsiz)
27702771
CloseHandle(handle);
27712772

27722773
/* get target path for symlinks or mount points (aka 'junctions') */
2773-
switch (b->ReparseTag) {
2774+
switch ((*ptag = b->ReparseTag)) {
27742775
case IO_REPARSE_TAG_SYMLINK:
27752776
wbuf = (WCHAR*) (((char*) b->SymbolicLinkReparseBuffer.PathBuffer)
27762777
+ b->SymbolicLinkReparseBuffer.SubstituteNameOffset);
@@ -2784,19 +2785,41 @@ int readlink(const char *path, char *buf, size_t bufsiz)
27842785
+ b->MountPointReparseBuffer.SubstituteNameLength) = 0;
27852786
break;
27862787
default:
2787-
errno = EINVAL;
2788-
return -1;
2788+
if (fail_on_unknown_tag) {
2789+
errno = EINVAL;
2790+
return -1;
2791+
} else {
2792+
*plen = MAX_LONG_PATH;
2793+
return 0;
2794+
}
27892795
}
27902796

2797+
if ((*plen =
2798+
xwcstoutf(tmpbuf, normalize_ntpath(wbuf), MAX_LONG_PATH)) < 0)
2799+
return -1;
2800+
return 0;
2801+
}
2802+
2803+
int readlink(const char *path, char *buf, size_t bufsiz)
2804+
{
2805+
WCHAR wpath[MAX_LONG_PATH];
2806+
char tmpbuf[MAX_LONG_PATH];
2807+
int len;
2808+
DWORD tag;
2809+
2810+
if (xutftowcs_long_path(wpath, path) < 0)
2811+
return -1;
2812+
2813+
if (readlink_1(wpath, TRUE, tmpbuf, &len, &tag) < 0)
2814+
return -1;
2815+
27912816
/*
27922817
* Adapt to strange readlink() API: Copy up to bufsiz *bytes*, potentially
27932818
* cutting off a UTF-8 sequence. Insufficient bufsize is *not* a failure
27942819
* condition. There is no conversion function that produces invalid UTF-8,
27952820
* so convert to a (hopefully large enough) temporary buffer, then memcpy
27962821
* the requested number of bytes (including '\0' for robustness).
27972822
*/
2798-
if ((len = xwcstoutf(tmpbuf, normalize_ntpath(wbuf), MAX_LONG_PATH)) < 0)
2799-
return -1;
28002823
memcpy(buf, tmpbuf, min(bufsiz, len + 1));
28012824
return min(bufsiz, len);
28022825
}

compat/win32/fscache.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,18 @@ int fscache_lstat(const char *filename, struct stat *st)
582582
return -1;
583583
}
584584

585+
/*
586+
* Special case symbolic links: FindFirstFile()/FindNextFile() did not
587+
* provide us with the length of the target path.
588+
*/
589+
if (fse->u.s.st_size == MAX_LONG_PATH && S_ISLNK(fse->st_mode)) {
590+
char buf[MAX_LONG_PATH];
591+
int len = readlink(filename, buf, sizeof(buf) - 1);
592+
593+
if (len > 0)
594+
fse->u.s.st_size = len;
595+
}
596+
585597
/* copy stat data */
586598
st->st_ino = 0;
587599
st->st_gid = 0;

0 commit comments

Comments
 (0)