Skip to content

Commit 3ae6eb7

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 28e8491 commit 3ae6eb7

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
@@ -847,10 +847,14 @@ static int has_valid_directory_prefix(wchar_t *wfilename)
847847
return 1;
848848
}
849849

850+
static int readlink_1(const WCHAR *wpath, BOOL fail_on_unknown_tag,
851+
char *tmpbuf, int *plen, DWORD *ptag);
852+
850853
int mingw_lstat(const char *file_name, struct stat *buf)
851854
{
852855
WIN32_FILE_ATTRIBUTE_DATA fdata;
853-
WIN32_FIND_DATAW findbuf = { 0 };
856+
DWORD reparse_tag = 0;
857+
int link_len = 0;
854858
wchar_t wfilename[MAX_LONG_PATH];
855859
int wlen = xutftowcs_long_path(wfilename, file_name);
856860
if (wlen < 0)
@@ -865,28 +869,29 @@ int mingw_lstat(const char *file_name, struct stat *buf)
865869
}
866870

867871
if (GetFileAttributesExW(wfilename, GetFileExInfoStandard, &fdata)) {
868-
/* for reparse points, use FindFirstFile to get the reparse tag */
872+
/* for reparse points, get the link tag and length */
869873
if (fdata.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
870-
HANDLE handle = FindFirstFileW(wfilename, &findbuf);
871-
if (handle == INVALID_HANDLE_VALUE)
872-
goto error;
873-
FindClose(handle);
874+
char tmpbuf[MAX_LONG_PATH];
875+
876+
if (readlink_1(wfilename, FALSE, tmpbuf, &link_len,
877+
&reparse_tag) < 0)
878+
return -1;
874879
}
875880
buf->st_ino = 0;
876881
buf->st_gid = 0;
877882
buf->st_uid = 0;
878883
buf->st_nlink = 1;
879884
buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes,
880-
findbuf.dwReserved0);
881-
buf->st_size = S_ISLNK(buf->st_mode) ? MAX_LONG_PATH :
885+
reparse_tag);
886+
buf->st_size = S_ISLNK(buf->st_mode) ? link_len :
882887
fdata.nFileSizeLow | (((off_t) fdata.nFileSizeHigh) << 32);
883888
buf->st_dev = buf->st_rdev = 0; /* not used by Git */
884889
filetime_to_timespec(&(fdata.ftLastAccessTime), &(buf->st_atim));
885890
filetime_to_timespec(&(fdata.ftLastWriteTime), &(buf->st_mtim));
886891
filetime_to_timespec(&(fdata.ftCreationTime), &(buf->st_ctim));
887892
return 0;
888893
}
889-
error:
894+
890895
switch (GetLastError()) {
891896
case ERROR_ACCESS_DENIED:
892897
case ERROR_SHARING_VIOLATION:
@@ -2728,17 +2733,13 @@ typedef struct _REPARSE_DATA_BUFFER {
27282733
} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
27292734
#endif
27302735

2731-
int readlink(const char *path, char *buf, size_t bufsiz)
2736+
static int readlink_1(const WCHAR *wpath, BOOL fail_on_unknown_tag,
2737+
char *tmpbuf, int *plen, DWORD *ptag)
27322738
{
27332739
HANDLE handle;
2734-
WCHAR wpath[MAX_LONG_PATH], *wbuf;
2740+
WCHAR *wbuf;
27352741
REPARSE_DATA_BUFFER *b = alloca(MAXIMUM_REPARSE_DATA_BUFFER_SIZE);
27362742
DWORD dummy;
2737-
char tmpbuf[MAX_LONG_PATH];
2738-
int len;
2739-
2740-
if (xutftowcs_long_path(wpath, path) < 0)
2741-
return -1;
27422743

27432744
/* read reparse point data */
27442745
handle = CreateFileW(wpath, 0,
@@ -2758,7 +2759,7 @@ int readlink(const char *path, char *buf, size_t bufsiz)
27582759
CloseHandle(handle);
27592760

27602761
/* get target path for symlinks or mount points (aka 'junctions') */
2761-
switch (b->ReparseTag) {
2762+
switch ((*ptag = b->ReparseTag)) {
27622763
case IO_REPARSE_TAG_SYMLINK:
27632764
wbuf = (WCHAR*) (((char*) b->SymbolicLinkReparseBuffer.PathBuffer)
27642765
+ b->SymbolicLinkReparseBuffer.SubstituteNameOffset);
@@ -2772,19 +2773,41 @@ int readlink(const char *path, char *buf, size_t bufsiz)
27722773
+ b->MountPointReparseBuffer.SubstituteNameLength) = 0;
27732774
break;
27742775
default:
2775-
errno = EINVAL;
2776-
return -1;
2776+
if (fail_on_unknown_tag) {
2777+
errno = EINVAL;
2778+
return -1;
2779+
} else {
2780+
*plen = MAX_LONG_PATH;
2781+
return 0;
2782+
}
27772783
}
27782784

2785+
if ((*plen =
2786+
xwcstoutf(tmpbuf, normalize_ntpath(wbuf), MAX_LONG_PATH)) < 0)
2787+
return -1;
2788+
return 0;
2789+
}
2790+
2791+
int readlink(const char *path, char *buf, size_t bufsiz)
2792+
{
2793+
WCHAR wpath[MAX_LONG_PATH];
2794+
char tmpbuf[MAX_LONG_PATH];
2795+
int len;
2796+
DWORD tag;
2797+
2798+
if (xutftowcs_long_path(wpath, path) < 0)
2799+
return -1;
2800+
2801+
if (readlink_1(wpath, TRUE, tmpbuf, &len, &tag) < 0)
2802+
return -1;
2803+
27792804
/*
27802805
* Adapt to strange readlink() API: Copy up to bufsiz *bytes*, potentially
27812806
* cutting off a UTF-8 sequence. Insufficient bufsize is *not* a failure
27822807
* condition. There is no conversion function that produces invalid UTF-8,
27832808
* so convert to a (hopefully large enough) temporary buffer, then memcpy
27842809
* the requested number of bytes (including '\0' for robustness).
27852810
*/
2786-
if ((len = xwcstoutf(tmpbuf, normalize_ntpath(wbuf), MAX_LONG_PATH)) < 0)
2787-
return -1;
27882811
memcpy(buf, tmpbuf, min(bufsiz, len + 1));
27892812
return min(bufsiz, len);
27902813
}

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)