Skip to content

Commit b0d9c5a

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 1d147f6 commit b0d9c5a

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
@@ -874,10 +874,14 @@ static int has_valid_directory_prefix(wchar_t *wfilename)
874874
return 1;
875875
}
876876

877+
static int readlink_1(const WCHAR *wpath, BOOL fail_on_unknown_tag,
878+
char *tmpbuf, int *plen, DWORD *ptag);
879+
877880
int mingw_lstat(const char *file_name, struct stat *buf)
878881
{
879882
WIN32_FILE_ATTRIBUTE_DATA fdata;
880-
WIN32_FIND_DATAW findbuf = { 0 };
883+
DWORD reparse_tag = 0;
884+
int link_len = 0;
881885
wchar_t wfilename[MAX_LONG_PATH];
882886
int wlen = xutftowcs_long_path(wfilename, file_name);
883887
if (wlen < 0)
@@ -892,28 +896,29 @@ int mingw_lstat(const char *file_name, struct stat *buf)
892896
}
893897

894898
if (GetFileAttributesExW(wfilename, GetFileExInfoStandard, &fdata)) {
895-
/* for reparse points, use FindFirstFile to get the reparse tag */
899+
/* for reparse points, get the link tag and length */
896900
if (fdata.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
897-
HANDLE handle = FindFirstFileW(wfilename, &findbuf);
898-
if (handle == INVALID_HANDLE_VALUE)
899-
goto error;
900-
FindClose(handle);
901+
char tmpbuf[MAX_LONG_PATH];
902+
903+
if (readlink_1(wfilename, FALSE, tmpbuf, &link_len,
904+
&reparse_tag) < 0)
905+
return -1;
901906
}
902907
buf->st_ino = 0;
903908
buf->st_gid = 0;
904909
buf->st_uid = 0;
905910
buf->st_nlink = 1;
906911
buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes,
907-
findbuf.dwReserved0);
908-
buf->st_size = S_ISLNK(buf->st_mode) ? MAX_LONG_PATH :
912+
reparse_tag);
913+
buf->st_size = S_ISLNK(buf->st_mode) ? link_len :
909914
fdata.nFileSizeLow | (((off_t) fdata.nFileSizeHigh) << 32);
910915
buf->st_dev = buf->st_rdev = 0; /* not used by Git */
911916
filetime_to_timespec(&(fdata.ftLastAccessTime), &(buf->st_atim));
912917
filetime_to_timespec(&(fdata.ftLastWriteTime), &(buf->st_mtim));
913918
filetime_to_timespec(&(fdata.ftCreationTime), &(buf->st_ctim));
914919
return 0;
915920
}
916-
error:
921+
917922
switch (GetLastError()) {
918923
case ERROR_ACCESS_DENIED:
919924
case ERROR_SHARING_VIOLATION:
@@ -2766,17 +2771,13 @@ typedef struct _REPARSE_DATA_BUFFER {
27662771
} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
27672772
#endif
27682773

2769-
int readlink(const char *path, char *buf, size_t bufsiz)
2774+
static int readlink_1(const WCHAR *wpath, BOOL fail_on_unknown_tag,
2775+
char *tmpbuf, int *plen, DWORD *ptag)
27702776
{
27712777
HANDLE handle;
2772-
WCHAR wpath[MAX_LONG_PATH], *wbuf;
2778+
WCHAR *wbuf;
27732779
REPARSE_DATA_BUFFER *b = alloca(MAXIMUM_REPARSE_DATA_BUFFER_SIZE);
27742780
DWORD dummy;
2775-
char tmpbuf[MAX_LONG_PATH];
2776-
int len;
2777-
2778-
if (xutftowcs_long_path(wpath, path) < 0)
2779-
return -1;
27802781

27812782
/* read reparse point data */
27822783
handle = CreateFileW(wpath, 0,
@@ -2796,7 +2797,7 @@ int readlink(const char *path, char *buf, size_t bufsiz)
27962797
CloseHandle(handle);
27972798

27982799
/* get target path for symlinks or mount points (aka 'junctions') */
2799-
switch (b->ReparseTag) {
2800+
switch ((*ptag = b->ReparseTag)) {
28002801
case IO_REPARSE_TAG_SYMLINK:
28012802
wbuf = (WCHAR*) (((char*) b->SymbolicLinkReparseBuffer.PathBuffer)
28022803
+ b->SymbolicLinkReparseBuffer.SubstituteNameOffset);
@@ -2810,19 +2811,41 @@ int readlink(const char *path, char *buf, size_t bufsiz)
28102811
+ b->MountPointReparseBuffer.SubstituteNameLength) = 0;
28112812
break;
28122813
default:
2813-
errno = EINVAL;
2814-
return -1;
2814+
if (fail_on_unknown_tag) {
2815+
errno = EINVAL;
2816+
return -1;
2817+
} else {
2818+
*plen = MAX_LONG_PATH;
2819+
return 0;
2820+
}
28152821
}
28162822

2823+
if ((*plen =
2824+
xwcstoutf(tmpbuf, normalize_ntpath(wbuf), MAX_LONG_PATH)) < 0)
2825+
return -1;
2826+
return 0;
2827+
}
2828+
2829+
int readlink(const char *path, char *buf, size_t bufsiz)
2830+
{
2831+
WCHAR wpath[MAX_LONG_PATH];
2832+
char tmpbuf[MAX_LONG_PATH];
2833+
int len;
2834+
DWORD tag;
2835+
2836+
if (xutftowcs_long_path(wpath, path) < 0)
2837+
return -1;
2838+
2839+
if (readlink_1(wpath, TRUE, tmpbuf, &len, &tag) < 0)
2840+
return -1;
2841+
28172842
/*
28182843
* Adapt to strange readlink() API: Copy up to bufsiz *bytes*, potentially
28192844
* cutting off a UTF-8 sequence. Insufficient bufsize is *not* a failure
28202845
* condition. There is no conversion function that produces invalid UTF-8,
28212846
* so convert to a (hopefully large enough) temporary buffer, then memcpy
28222847
* the requested number of bytes (including '\0' for robustness).
28232848
*/
2824-
if ((len = xwcstoutf(tmpbuf, normalize_ntpath(wbuf), MAX_LONG_PATH)) < 0)
2825-
return -1;
28262849
memcpy(buf, tmpbuf, min(bufsiz, len + 1));
28272850
return min(bufsiz, len);
28282851
}

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)