Skip to content

Commit 05a356c

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 2b9e7f8 commit 05a356c

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

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

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

2788-
int readlink(const char *path, char *buf, size_t bufsiz)
2793+
static int readlink_1(const WCHAR *wpath, BOOL fail_on_unknown_tag,
2794+
char *tmpbuf, int *plen, DWORD *ptag)
27892795
{
27902796
HANDLE handle;
2791-
WCHAR wpath[MAX_LONG_PATH], *wbuf;
2797+
WCHAR *wbuf;
27922798
REPARSE_DATA_BUFFER *b = alloca(MAXIMUM_REPARSE_DATA_BUFFER_SIZE);
27932799
DWORD dummy;
2794-
char tmpbuf[MAX_LONG_PATH];
2795-
int len;
2796-
2797-
if (xutftowcs_long_path(wpath, path) < 0)
2798-
return -1;
27992800

28002801
/* read reparse point data */
28012802
handle = CreateFileW(wpath, 0,
@@ -2815,7 +2816,7 @@ int readlink(const char *path, char *buf, size_t bufsiz)
28152816
CloseHandle(handle);
28162817

28172818
/* get target path for symlinks or mount points (aka 'junctions') */
2818-
switch (b->ReparseTag) {
2819+
switch ((*ptag = b->ReparseTag)) {
28192820
case IO_REPARSE_TAG_SYMLINK:
28202821
wbuf = (WCHAR*) (((char*) b->SymbolicLinkReparseBuffer.PathBuffer)
28212822
+ b->SymbolicLinkReparseBuffer.SubstituteNameOffset);
@@ -2829,19 +2830,41 @@ int readlink(const char *path, char *buf, size_t bufsiz)
28292830
+ b->MountPointReparseBuffer.SubstituteNameLength) = 0;
28302831
break;
28312832
default:
2832-
errno = EINVAL;
2833-
return -1;
2833+
if (fail_on_unknown_tag) {
2834+
errno = EINVAL;
2835+
return -1;
2836+
} else {
2837+
*plen = MAX_LONG_PATH;
2838+
return 0;
2839+
}
28342840
}
28352841

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

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)