Skip to content

Commit 53a836f

Browse files
billziss-ghGit for Windows Build Agent
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 b5cd688 commit 53a836f

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
@@ -986,10 +986,14 @@ static int has_valid_directory_prefix(wchar_t *wfilename)
986986
return 1;
987987
}
988988

989+
static int readlink_1(const WCHAR *wpath, BOOL fail_on_unknown_tag,
990+
char *tmpbuf, int *plen, DWORD *ptag);
991+
989992
int mingw_lstat(const char *file_name, struct stat *buf)
990993
{
991994
WIN32_FILE_ATTRIBUTE_DATA fdata;
992-
WIN32_FIND_DATAW findbuf = { 0 };
995+
DWORD reparse_tag = 0;
996+
int link_len = 0;
993997
wchar_t wfilename[MAX_LONG_PATH];
994998
int wlen = xutftowcs_long_path(wfilename, file_name);
995999
if (wlen < 0)
@@ -1004,28 +1008,29 @@ int mingw_lstat(const char *file_name, struct stat *buf)
10041008
}
10051009

10061010
if (GetFileAttributesExW(wfilename, GetFileExInfoStandard, &fdata)) {
1007-
/* for reparse points, use FindFirstFile to get the reparse tag */
1011+
/* for reparse points, get the link tag and length */
10081012
if (fdata.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1009-
HANDLE handle = FindFirstFileW(wfilename, &findbuf);
1010-
if (handle == INVALID_HANDLE_VALUE)
1011-
goto error;
1012-
FindClose(handle);
1013+
char tmpbuf[MAX_LONG_PATH];
1014+
1015+
if (readlink_1(wfilename, FALSE, tmpbuf, &link_len,
1016+
&reparse_tag) < 0)
1017+
return -1;
10131018
}
10141019
buf->st_ino = 0;
10151020
buf->st_gid = 0;
10161021
buf->st_uid = 0;
10171022
buf->st_nlink = 1;
10181023
buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes,
1019-
findbuf.dwReserved0);
1020-
buf->st_size = S_ISLNK(buf->st_mode) ? MAX_LONG_PATH :
1024+
reparse_tag);
1025+
buf->st_size = S_ISLNK(buf->st_mode) ? link_len :
10211026
fdata.nFileSizeLow | (((off_t) fdata.nFileSizeHigh) << 32);
10221027
buf->st_dev = buf->st_rdev = 0; /* not used by Git */
10231028
filetime_to_timespec(&(fdata.ftLastAccessTime), &(buf->st_atim));
10241029
filetime_to_timespec(&(fdata.ftLastWriteTime), &(buf->st_mtim));
10251030
filetime_to_timespec(&(fdata.ftCreationTime), &(buf->st_ctim));
10261031
return 0;
10271032
}
1028-
error:
1033+
10291034
switch (GetLastError()) {
10301035
case ERROR_ACCESS_DENIED:
10311036
case ERROR_SHARING_VIOLATION:
@@ -2990,17 +2995,13 @@ typedef struct _REPARSE_DATA_BUFFER {
29902995
} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
29912996
#endif
29922997

2993-
int readlink(const char *path, char *buf, size_t bufsiz)
2998+
static int readlink_1(const WCHAR *wpath, BOOL fail_on_unknown_tag,
2999+
char *tmpbuf, int *plen, DWORD *ptag)
29943000
{
29953001
HANDLE handle;
2996-
WCHAR wpath[MAX_LONG_PATH], *wbuf;
3002+
WCHAR *wbuf;
29973003
REPARSE_DATA_BUFFER *b = alloca(MAXIMUM_REPARSE_DATA_BUFFER_SIZE);
29983004
DWORD dummy;
2999-
char tmpbuf[MAX_LONG_PATH];
3000-
int len;
3001-
3002-
if (xutftowcs_long_path(wpath, path) < 0)
3003-
return -1;
30043005

30053006
/* read reparse point data */
30063007
handle = CreateFileW(wpath, 0,
@@ -3020,7 +3021,7 @@ int readlink(const char *path, char *buf, size_t bufsiz)
30203021
CloseHandle(handle);
30213022

30223023
/* get target path for symlinks or mount points (aka 'junctions') */
3023-
switch (b->ReparseTag) {
3024+
switch ((*ptag = b->ReparseTag)) {
30243025
case IO_REPARSE_TAG_SYMLINK:
30253026
wbuf = (WCHAR*) (((char*) b->SymbolicLinkReparseBuffer.PathBuffer)
30263027
+ b->SymbolicLinkReparseBuffer.SubstituteNameOffset);
@@ -3034,19 +3035,41 @@ int readlink(const char *path, char *buf, size_t bufsiz)
30343035
+ b->MountPointReparseBuffer.SubstituteNameLength) = 0;
30353036
break;
30363037
default:
3037-
errno = EINVAL;
3038-
return -1;
3038+
if (fail_on_unknown_tag) {
3039+
errno = EINVAL;
3040+
return -1;
3041+
} else {
3042+
*plen = MAX_LONG_PATH;
3043+
return 0;
3044+
}
30393045
}
30403046

3047+
if ((*plen =
3048+
xwcstoutf(tmpbuf, normalize_ntpath(wbuf), MAX_LONG_PATH)) < 0)
3049+
return -1;
3050+
return 0;
3051+
}
3052+
3053+
int readlink(const char *path, char *buf, size_t bufsiz)
3054+
{
3055+
WCHAR wpath[MAX_LONG_PATH];
3056+
char tmpbuf[MAX_LONG_PATH];
3057+
int len;
3058+
DWORD tag;
3059+
3060+
if (xutftowcs_long_path(wpath, path) < 0)
3061+
return -1;
3062+
3063+
if (readlink_1(wpath, TRUE, tmpbuf, &len, &tag) < 0)
3064+
return -1;
3065+
30413066
/*
30423067
* Adapt to strange readlink() API: Copy up to bufsiz *bytes*, potentially
30433068
* cutting off a UTF-8 sequence. Insufficient bufsize is *not* a failure
30443069
* condition. There is no conversion function that produces invalid UTF-8,
30453070
* so convert to a (hopefully large enough) temporary buffer, then memcpy
30463071
* the requested number of bytes (including '\0' for robustness).
30473072
*/
3048-
if ((len = xwcstoutf(tmpbuf, normalize_ntpath(wbuf), MAX_LONG_PATH)) < 0)
3049-
return -1;
30503073
memcpy(buf, tmpbuf, min(bufsiz, len + 1));
30513074
return min(bufsiz, len);
30523075
}

compat/win32/fscache.c

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

597+
/*
598+
* Special case symbolic links: FindFirstFile()/FindNextFile() did not
599+
* provide us with the length of the target path.
600+
*/
601+
if (fse->u.s.st_size == MAX_LONG_PATH && S_ISLNK(fse->st_mode)) {
602+
char buf[MAX_LONG_PATH];
603+
int len = readlink(filename, buf, sizeof(buf) - 1);
604+
605+
if (len > 0)
606+
fse->u.s.st_size = len;
607+
}
608+
597609
/* copy stat data */
598610
st->st_ino = 0;
599611
st->st_gid = 0;

0 commit comments

Comments
 (0)