Skip to content

Commit 6223f29

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 472e9a0 commit 6223f29

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

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

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

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

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

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

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

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)