Skip to content

Commit 2ee9d5e

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 2ff8eaf commit 2ee9d5e

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
@@ -819,10 +819,14 @@ static int has_valid_directory_prefix(wchar_t *wfilename)
819819
return 1;
820820
}
821821

822+
static int readlink_1(const WCHAR *wpath, BOOL fail_on_unknown_tag,
823+
char *tmpbuf, int *plen, DWORD *ptag);
824+
822825
int mingw_lstat(const char *file_name, struct stat *buf)
823826
{
824827
WIN32_FILE_ATTRIBUTE_DATA fdata;
825-
WIN32_FIND_DATAW findbuf = { 0 };
828+
DWORD reparse_tag = 0;
829+
int link_len = 0;
826830
wchar_t wfilename[MAX_LONG_PATH];
827831
int wlen = xutftowcs_long_path(wfilename, file_name);
828832
if (wlen < 0)
@@ -837,28 +841,29 @@ int mingw_lstat(const char *file_name, struct stat *buf)
837841
}
838842

839843
if (GetFileAttributesExW(wfilename, GetFileExInfoStandard, &fdata)) {
840-
/* for reparse points, use FindFirstFile to get the reparse tag */
844+
/* for reparse points, get the link tag and length */
841845
if (fdata.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
842-
HANDLE handle = FindFirstFileW(wfilename, &findbuf);
843-
if (handle == INVALID_HANDLE_VALUE)
844-
goto error;
845-
FindClose(handle);
846+
char tmpbuf[MAX_LONG_PATH];
847+
848+
if (readlink_1(wfilename, FALSE, tmpbuf, &link_len,
849+
&reparse_tag) < 0)
850+
return -1;
846851
}
847852
buf->st_ino = 0;
848853
buf->st_gid = 0;
849854
buf->st_uid = 0;
850855
buf->st_nlink = 1;
851856
buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes,
852-
findbuf.dwReserved0);
853-
buf->st_size = S_ISLNK(buf->st_mode) ? MAX_LONG_PATH :
857+
reparse_tag);
858+
buf->st_size = S_ISLNK(buf->st_mode) ? link_len :
854859
fdata.nFileSizeLow | (((off_t) fdata.nFileSizeHigh) << 32);
855860
buf->st_dev = buf->st_rdev = 0; /* not used by Git */
856861
filetime_to_timespec(&(fdata.ftLastAccessTime), &(buf->st_atim));
857862
filetime_to_timespec(&(fdata.ftLastWriteTime), &(buf->st_mtim));
858863
filetime_to_timespec(&(fdata.ftCreationTime), &(buf->st_ctim));
859864
return 0;
860865
}
861-
error:
866+
862867
switch (GetLastError()) {
863868
case ERROR_ACCESS_DENIED:
864869
case ERROR_SHARING_VIOLATION:
@@ -2700,17 +2705,13 @@ typedef struct _REPARSE_DATA_BUFFER {
27002705
} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
27012706
#endif
27022707

2703-
int readlink(const char *path, char *buf, size_t bufsiz)
2708+
static int readlink_1(const WCHAR *wpath, BOOL fail_on_unknown_tag,
2709+
char *tmpbuf, int *plen, DWORD *ptag)
27042710
{
27052711
HANDLE handle;
2706-
WCHAR wpath[MAX_LONG_PATH], *wbuf;
2712+
WCHAR *wbuf;
27072713
REPARSE_DATA_BUFFER *b = alloca(MAXIMUM_REPARSE_DATA_BUFFER_SIZE);
27082714
DWORD dummy;
2709-
char tmpbuf[MAX_LONG_PATH];
2710-
int len;
2711-
2712-
if (xutftowcs_long_path(wpath, path) < 0)
2713-
return -1;
27142715

27152716
/* read reparse point data */
27162717
handle = CreateFileW(wpath, 0,
@@ -2730,7 +2731,7 @@ int readlink(const char *path, char *buf, size_t bufsiz)
27302731
CloseHandle(handle);
27312732

27322733
/* get target path for symlinks or mount points (aka 'junctions') */
2733-
switch (b->ReparseTag) {
2734+
switch ((*ptag = b->ReparseTag)) {
27342735
case IO_REPARSE_TAG_SYMLINK:
27352736
wbuf = (WCHAR*) (((char*) b->SymbolicLinkReparseBuffer.PathBuffer)
27362737
+ b->SymbolicLinkReparseBuffer.SubstituteNameOffset);
@@ -2744,19 +2745,41 @@ int readlink(const char *path, char *buf, size_t bufsiz)
27442745
+ b->MountPointReparseBuffer.SubstituteNameLength) = 0;
27452746
break;
27462747
default:
2747-
errno = EINVAL;
2748-
return -1;
2748+
if (fail_on_unknown_tag) {
2749+
errno = EINVAL;
2750+
return -1;
2751+
} else {
2752+
*plen = MAX_LONG_PATH;
2753+
return 0;
2754+
}
27492755
}
27502756

2757+
if ((*plen =
2758+
xwcstoutf(tmpbuf, normalize_ntpath(wbuf), MAX_LONG_PATH)) < 0)
2759+
return -1;
2760+
return 0;
2761+
}
2762+
2763+
int readlink(const char *path, char *buf, size_t bufsiz)
2764+
{
2765+
WCHAR wpath[MAX_LONG_PATH];
2766+
char tmpbuf[MAX_LONG_PATH];
2767+
int len;
2768+
DWORD tag;
2769+
2770+
if (xutftowcs_long_path(wpath, path) < 0)
2771+
return -1;
2772+
2773+
if (readlink_1(wpath, TRUE, tmpbuf, &len, &tag) < 0)
2774+
return -1;
2775+
27512776
/*
27522777
* Adapt to strange readlink() API: Copy up to bufsiz *bytes*, potentially
27532778
* cutting off a UTF-8 sequence. Insufficient bufsize is *not* a failure
27542779
* condition. There is no conversion function that produces invalid UTF-8,
27552780
* so convert to a (hopefully large enough) temporary buffer, then memcpy
27562781
* the requested number of bytes (including '\0' for robustness).
27572782
*/
2758-
if ((len = xwcstoutf(tmpbuf, normalize_ntpath(wbuf), MAX_LONG_PATH)) < 0)
2759-
return -1;
27602783
memcpy(buf, tmpbuf, min(bufsiz, len + 1));
27612784
return min(bufsiz, len);
27622785
}

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)