Skip to content

Commit a018e35

Browse files
billziss-ghvdye
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 b482a4b commit a018e35

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

820+
static int readlink_1(const WCHAR *wpath, BOOL fail_on_unknown_tag,
821+
char *tmpbuf, int *plen, DWORD *ptag);
822+
820823
int mingw_lstat(const char *file_name, struct stat *buf)
821824
{
822825
WIN32_FILE_ATTRIBUTE_DATA fdata;
823-
WIN32_FIND_DATAW findbuf = { 0 };
826+
DWORD reparse_tag = 0;
827+
int link_len = 0;
824828
wchar_t wfilename[MAX_LONG_PATH];
825829
int wlen = xutftowcs_long_path(wfilename, file_name);
826830
if (wlen < 0)
@@ -835,28 +839,29 @@ int mingw_lstat(const char *file_name, struct stat *buf)
835839
}
836840

837841
if (GetFileAttributesExW(wfilename, GetFileExInfoStandard, &fdata)) {
838-
/* for reparse points, use FindFirstFile to get the reparse tag */
842+
/* for reparse points, get the link tag and length */
839843
if (fdata.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
840-
HANDLE handle = FindFirstFileW(wfilename, &findbuf);
841-
if (handle == INVALID_HANDLE_VALUE)
842-
goto error;
843-
FindClose(handle);
844+
char tmpbuf[MAX_LONG_PATH];
845+
846+
if (readlink_1(wfilename, FALSE, tmpbuf, &link_len,
847+
&reparse_tag) < 0)
848+
return -1;
844849
}
845850
buf->st_ino = 0;
846851
buf->st_gid = 0;
847852
buf->st_uid = 0;
848853
buf->st_nlink = 1;
849854
buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes,
850-
findbuf.dwReserved0);
851-
buf->st_size = S_ISLNK(buf->st_mode) ? MAX_LONG_PATH :
855+
reparse_tag);
856+
buf->st_size = S_ISLNK(buf->st_mode) ? link_len :
852857
fdata.nFileSizeLow | (((off_t) fdata.nFileSizeHigh) << 32);
853858
buf->st_dev = buf->st_rdev = 0; /* not used by Git */
854859
filetime_to_timespec(&(fdata.ftLastAccessTime), &(buf->st_atim));
855860
filetime_to_timespec(&(fdata.ftLastWriteTime), &(buf->st_mtim));
856861
filetime_to_timespec(&(fdata.ftCreationTime), &(buf->st_ctim));
857862
return 0;
858863
}
859-
error:
864+
860865
switch (GetLastError()) {
861866
case ERROR_ACCESS_DENIED:
862867
case ERROR_SHARING_VIOLATION:
@@ -2698,17 +2703,13 @@ typedef struct _REPARSE_DATA_BUFFER {
26982703
} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
26992704
#endif
27002705

2701-
int readlink(const char *path, char *buf, size_t bufsiz)
2706+
static int readlink_1(const WCHAR *wpath, BOOL fail_on_unknown_tag,
2707+
char *tmpbuf, int *plen, DWORD *ptag)
27022708
{
27032709
HANDLE handle;
2704-
WCHAR wpath[MAX_LONG_PATH], *wbuf;
2710+
WCHAR *wbuf;
27052711
REPARSE_DATA_BUFFER *b = alloca(MAXIMUM_REPARSE_DATA_BUFFER_SIZE);
27062712
DWORD dummy;
2707-
char tmpbuf[MAX_LONG_PATH];
2708-
int len;
2709-
2710-
if (xutftowcs_long_path(wpath, path) < 0)
2711-
return -1;
27122713

27132714
/* read reparse point data */
27142715
handle = CreateFileW(wpath, 0,
@@ -2728,7 +2729,7 @@ int readlink(const char *path, char *buf, size_t bufsiz)
27282729
CloseHandle(handle);
27292730

27302731
/* get target path for symlinks or mount points (aka 'junctions') */
2731-
switch (b->ReparseTag) {
2732+
switch ((*ptag = b->ReparseTag)) {
27322733
case IO_REPARSE_TAG_SYMLINK:
27332734
wbuf = (WCHAR*) (((char*) b->SymbolicLinkReparseBuffer.PathBuffer)
27342735
+ b->SymbolicLinkReparseBuffer.SubstituteNameOffset);
@@ -2742,19 +2743,41 @@ int readlink(const char *path, char *buf, size_t bufsiz)
27422743
+ b->MountPointReparseBuffer.SubstituteNameLength) = 0;
27432744
break;
27442745
default:
2745-
errno = EINVAL;
2746-
return -1;
2746+
if (fail_on_unknown_tag) {
2747+
errno = EINVAL;
2748+
return -1;
2749+
} else {
2750+
*plen = MAX_LONG_PATH;
2751+
return 0;
2752+
}
27472753
}
27482754

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

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)