Skip to content

Commit 03a2e41

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 c090e6a commit 03a2e41

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
@@ -894,10 +894,14 @@ static int has_valid_directory_prefix(wchar_t *wfilename)
894894
return 1;
895895
}
896896

897+
static int readlink_1(const WCHAR *wpath, BOOL fail_on_unknown_tag,
898+
char *tmpbuf, int *plen, DWORD *ptag);
899+
897900
int mingw_lstat(const char *file_name, struct stat *buf)
898901
{
899902
WIN32_FILE_ATTRIBUTE_DATA fdata;
900-
WIN32_FIND_DATAW findbuf = { 0 };
903+
DWORD reparse_tag = 0;
904+
int link_len = 0;
901905
wchar_t wfilename[MAX_LONG_PATH];
902906
int wlen = xutftowcs_long_path(wfilename, file_name);
903907
if (wlen < 0)
@@ -912,28 +916,29 @@ int mingw_lstat(const char *file_name, struct stat *buf)
912916
}
913917

914918
if (GetFileAttributesExW(wfilename, GetFileExInfoStandard, &fdata)) {
915-
/* for reparse points, use FindFirstFile to get the reparse tag */
919+
/* for reparse points, get the link tag and length */
916920
if (fdata.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
917-
HANDLE handle = FindFirstFileW(wfilename, &findbuf);
918-
if (handle == INVALID_HANDLE_VALUE)
919-
goto error;
920-
FindClose(handle);
921+
char tmpbuf[MAX_LONG_PATH];
922+
923+
if (readlink_1(wfilename, FALSE, tmpbuf, &link_len,
924+
&reparse_tag) < 0)
925+
return -1;
921926
}
922927
buf->st_ino = 0;
923928
buf->st_gid = 0;
924929
buf->st_uid = 0;
925930
buf->st_nlink = 1;
926931
buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes,
927-
findbuf.dwReserved0);
928-
buf->st_size = S_ISLNK(buf->st_mode) ? MAX_LONG_PATH :
932+
reparse_tag);
933+
buf->st_size = S_ISLNK(buf->st_mode) ? link_len :
929934
fdata.nFileSizeLow | (((off_t) fdata.nFileSizeHigh) << 32);
930935
buf->st_dev = buf->st_rdev = 0; /* not used by Git */
931936
filetime_to_timespec(&(fdata.ftLastAccessTime), &(buf->st_atim));
932937
filetime_to_timespec(&(fdata.ftLastWriteTime), &(buf->st_mtim));
933938
filetime_to_timespec(&(fdata.ftCreationTime), &(buf->st_ctim));
934939
return 0;
935940
}
936-
error:
941+
937942
switch (GetLastError()) {
938943
case ERROR_ACCESS_DENIED:
939944
case ERROR_SHARING_VIOLATION:
@@ -2790,17 +2795,13 @@ typedef struct _REPARSE_DATA_BUFFER {
27902795
} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
27912796
#endif
27922797

2793-
int readlink(const char *path, char *buf, size_t bufsiz)
2798+
static int readlink_1(const WCHAR *wpath, BOOL fail_on_unknown_tag,
2799+
char *tmpbuf, int *plen, DWORD *ptag)
27942800
{
27952801
HANDLE handle;
2796-
WCHAR wpath[MAX_LONG_PATH], *wbuf;
2802+
WCHAR *wbuf;
27972803
REPARSE_DATA_BUFFER *b = alloca(MAXIMUM_REPARSE_DATA_BUFFER_SIZE);
27982804
DWORD dummy;
2799-
char tmpbuf[MAX_LONG_PATH];
2800-
int len;
2801-
2802-
if (xutftowcs_long_path(wpath, path) < 0)
2803-
return -1;
28042805

28052806
/* read reparse point data */
28062807
handle = CreateFileW(wpath, 0,
@@ -2820,7 +2821,7 @@ int readlink(const char *path, char *buf, size_t bufsiz)
28202821
CloseHandle(handle);
28212822

28222823
/* get target path for symlinks or mount points (aka 'junctions') */
2823-
switch (b->ReparseTag) {
2824+
switch ((*ptag = b->ReparseTag)) {
28242825
case IO_REPARSE_TAG_SYMLINK:
28252826
wbuf = (WCHAR*) (((char*) b->SymbolicLinkReparseBuffer.PathBuffer)
28262827
+ b->SymbolicLinkReparseBuffer.SubstituteNameOffset);
@@ -2834,19 +2835,41 @@ int readlink(const char *path, char *buf, size_t bufsiz)
28342835
+ b->MountPointReparseBuffer.SubstituteNameLength) = 0;
28352836
break;
28362837
default:
2837-
errno = EINVAL;
2838-
return -1;
2838+
if (fail_on_unknown_tag) {
2839+
errno = EINVAL;
2840+
return -1;
2841+
} else {
2842+
*plen = MAX_LONG_PATH;
2843+
return 0;
2844+
}
28392845
}
28402846

2847+
if ((*plen =
2848+
xwcstoutf(tmpbuf, normalize_ntpath(wbuf), MAX_LONG_PATH)) < 0)
2849+
return -1;
2850+
return 0;
2851+
}
2852+
2853+
int readlink(const char *path, char *buf, size_t bufsiz)
2854+
{
2855+
WCHAR wpath[MAX_LONG_PATH];
2856+
char tmpbuf[MAX_LONG_PATH];
2857+
int len;
2858+
DWORD tag;
2859+
2860+
if (xutftowcs_long_path(wpath, path) < 0)
2861+
return -1;
2862+
2863+
if (readlink_1(wpath, TRUE, tmpbuf, &len, &tag) < 0)
2864+
return -1;
2865+
28412866
/*
28422867
* Adapt to strange readlink() API: Copy up to bufsiz *bytes*, potentially
28432868
* cutting off a UTF-8 sequence. Insufficient bufsize is *not* a failure
28442869
* condition. There is no conversion function that produces invalid UTF-8,
28452870
* so convert to a (hopefully large enough) temporary buffer, then memcpy
28462871
* the requested number of bytes (including '\0' for robustness).
28472872
*/
2848-
if ((len = xwcstoutf(tmpbuf, normalize_ntpath(wbuf), MAX_LONG_PATH)) < 0)
2849-
return -1;
28502873
memcpy(buf, tmpbuf, min(bufsiz, len + 1));
28512874
return min(bufsiz, len);
28522875
}

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)