Skip to content

Commit 250218a

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 dbef513 commit 250218a

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
@@ -888,10 +888,14 @@ static int has_valid_directory_prefix(wchar_t *wfilename)
888888
return 1;
889889
}
890890

891+
static int readlink_1(const WCHAR *wpath, BOOL fail_on_unknown_tag,
892+
char *tmpbuf, int *plen, DWORD *ptag);
893+
891894
int mingw_lstat(const char *file_name, struct stat *buf)
892895
{
893896
WIN32_FILE_ATTRIBUTE_DATA fdata;
894-
WIN32_FIND_DATAW findbuf = { 0 };
897+
DWORD reparse_tag = 0;
898+
int link_len = 0;
895899
wchar_t wfilename[MAX_LONG_PATH];
896900
int wlen = xutftowcs_long_path(wfilename, file_name);
897901
if (wlen < 0)
@@ -906,28 +910,29 @@ int mingw_lstat(const char *file_name, struct stat *buf)
906910
}
907911

908912
if (GetFileAttributesExW(wfilename, GetFileExInfoStandard, &fdata)) {
909-
/* for reparse points, use FindFirstFile to get the reparse tag */
913+
/* for reparse points, get the link tag and length */
910914
if (fdata.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
911-
HANDLE handle = FindFirstFileW(wfilename, &findbuf);
912-
if (handle == INVALID_HANDLE_VALUE)
913-
goto error;
914-
FindClose(handle);
915+
char tmpbuf[MAX_LONG_PATH];
916+
917+
if (readlink_1(wfilename, FALSE, tmpbuf, &link_len,
918+
&reparse_tag) < 0)
919+
return -1;
915920
}
916921
buf->st_ino = 0;
917922
buf->st_gid = 0;
918923
buf->st_uid = 0;
919924
buf->st_nlink = 1;
920925
buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes,
921-
findbuf.dwReserved0);
922-
buf->st_size = S_ISLNK(buf->st_mode) ? MAX_LONG_PATH :
926+
reparse_tag);
927+
buf->st_size = S_ISLNK(buf->st_mode) ? link_len :
923928
fdata.nFileSizeLow | (((off_t) fdata.nFileSizeHigh) << 32);
924929
buf->st_dev = buf->st_rdev = 0; /* not used by Git */
925930
filetime_to_timespec(&(fdata.ftLastAccessTime), &(buf->st_atim));
926931
filetime_to_timespec(&(fdata.ftLastWriteTime), &(buf->st_mtim));
927932
filetime_to_timespec(&(fdata.ftCreationTime), &(buf->st_ctim));
928933
return 0;
929934
}
930-
error:
935+
931936
switch (GetLastError()) {
932937
case ERROR_ACCESS_DENIED:
933938
case ERROR_SHARING_VIOLATION:
@@ -2780,17 +2785,13 @@ typedef struct _REPARSE_DATA_BUFFER {
27802785
} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
27812786
#endif
27822787

2783-
int readlink(const char *path, char *buf, size_t bufsiz)
2788+
static int readlink_1(const WCHAR *wpath, BOOL fail_on_unknown_tag,
2789+
char *tmpbuf, int *plen, DWORD *ptag)
27842790
{
27852791
HANDLE handle;
2786-
WCHAR wpath[MAX_LONG_PATH], *wbuf;
2792+
WCHAR *wbuf;
27872793
REPARSE_DATA_BUFFER *b = alloca(MAXIMUM_REPARSE_DATA_BUFFER_SIZE);
27882794
DWORD dummy;
2789-
char tmpbuf[MAX_LONG_PATH];
2790-
int len;
2791-
2792-
if (xutftowcs_long_path(wpath, path) < 0)
2793-
return -1;
27942795

27952796
/* read reparse point data */
27962797
handle = CreateFileW(wpath, 0,
@@ -2810,7 +2811,7 @@ int readlink(const char *path, char *buf, size_t bufsiz)
28102811
CloseHandle(handle);
28112812

28122813
/* get target path for symlinks or mount points (aka 'junctions') */
2813-
switch (b->ReparseTag) {
2814+
switch ((*ptag = b->ReparseTag)) {
28142815
case IO_REPARSE_TAG_SYMLINK:
28152816
wbuf = (WCHAR*) (((char*) b->SymbolicLinkReparseBuffer.PathBuffer)
28162817
+ b->SymbolicLinkReparseBuffer.SubstituteNameOffset);
@@ -2824,19 +2825,41 @@ int readlink(const char *path, char *buf, size_t bufsiz)
28242825
+ b->MountPointReparseBuffer.SubstituteNameLength) = 0;
28252826
break;
28262827
default:
2827-
errno = EINVAL;
2828-
return -1;
2828+
if (fail_on_unknown_tag) {
2829+
errno = EINVAL;
2830+
return -1;
2831+
} else {
2832+
*plen = MAX_LONG_PATH;
2833+
return 0;
2834+
}
28292835
}
28302836

2837+
if ((*plen =
2838+
xwcstoutf(tmpbuf, normalize_ntpath(wbuf), MAX_LONG_PATH)) < 0)
2839+
return -1;
2840+
return 0;
2841+
}
2842+
2843+
int readlink(const char *path, char *buf, size_t bufsiz)
2844+
{
2845+
WCHAR wpath[MAX_LONG_PATH];
2846+
char tmpbuf[MAX_LONG_PATH];
2847+
int len;
2848+
DWORD tag;
2849+
2850+
if (xutftowcs_long_path(wpath, path) < 0)
2851+
return -1;
2852+
2853+
if (readlink_1(wpath, TRUE, tmpbuf, &len, &tag) < 0)
2854+
return -1;
2855+
28312856
/*
28322857
* Adapt to strange readlink() API: Copy up to bufsiz *bytes*, potentially
28332858
* cutting off a UTF-8 sequence. Insufficient bufsize is *not* a failure
28342859
* condition. There is no conversion function that produces invalid UTF-8,
28352860
* so convert to a (hopefully large enough) temporary buffer, then memcpy
28362861
* the requested number of bytes (including '\0' for robustness).
28372862
*/
2838-
if ((len = xwcstoutf(tmpbuf, normalize_ntpath(wbuf), MAX_LONG_PATH)) < 0)
2839-
return -1;
28402863
memcpy(buf, tmpbuf, min(bufsiz, len + 1));
28412864
return min(bufsiz, len);
28422865
}

compat/win32/fscache.c

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

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

0 commit comments

Comments
 (0)