Skip to content

Commit d0f5019

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 3b2de86 commit d0f5019

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:
@@ -2684,17 +2689,13 @@ typedef struct _REPARSE_DATA_BUFFER {
26842689
} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
26852690
#endif
26862691

2687-
int readlink(const char *path, char *buf, size_t bufsiz)
2692+
static int readlink_1(const WCHAR *wpath, BOOL fail_on_unknown_tag,
2693+
char *tmpbuf, int *plen, DWORD *ptag)
26882694
{
26892695
HANDLE handle;
2690-
WCHAR wpath[MAX_LONG_PATH], *wbuf;
2696+
WCHAR *wbuf;
26912697
REPARSE_DATA_BUFFER *b = alloca(MAXIMUM_REPARSE_DATA_BUFFER_SIZE);
26922698
DWORD dummy;
2693-
char tmpbuf[MAX_LONG_PATH];
2694-
int len;
2695-
2696-
if (xutftowcs_long_path(wpath, path) < 0)
2697-
return -1;
26982699

26992700
/* read reparse point data */
27002701
handle = CreateFileW(wpath, 0,
@@ -2714,7 +2715,7 @@ int readlink(const char *path, char *buf, size_t bufsiz)
27142715
CloseHandle(handle);
27152716

27162717
/* get target path for symlinks or mount points (aka 'junctions') */
2717-
switch (b->ReparseTag) {
2718+
switch ((*ptag = b->ReparseTag)) {
27182719
case IO_REPARSE_TAG_SYMLINK:
27192720
wbuf = (WCHAR*) (((char*) b->SymbolicLinkReparseBuffer.PathBuffer)
27202721
+ b->SymbolicLinkReparseBuffer.SubstituteNameOffset);
@@ -2728,19 +2729,41 @@ int readlink(const char *path, char *buf, size_t bufsiz)
27282729
+ b->MountPointReparseBuffer.SubstituteNameLength) = 0;
27292730
break;
27302731
default:
2731-
errno = EINVAL;
2732-
return -1;
2732+
if (fail_on_unknown_tag) {
2733+
errno = EINVAL;
2734+
return -1;
2735+
} else {
2736+
*plen = MAX_LONG_PATH;
2737+
return 0;
2738+
}
27332739
}
27342740

2741+
if ((*plen =
2742+
xwcstoutf(tmpbuf, normalize_ntpath(wbuf), MAX_LONG_PATH)) < 0)
2743+
return -1;
2744+
return 0;
2745+
}
2746+
2747+
int readlink(const char *path, char *buf, size_t bufsiz)
2748+
{
2749+
WCHAR wpath[MAX_LONG_PATH];
2750+
char tmpbuf[MAX_LONG_PATH];
2751+
int len;
2752+
DWORD tag;
2753+
2754+
if (xutftowcs_long_path(wpath, path) < 0)
2755+
return -1;
2756+
2757+
if (readlink_1(wpath, TRUE, tmpbuf, &len, &tag) < 0)
2758+
return -1;
2759+
27352760
/*
27362761
* Adapt to strange readlink() API: Copy up to bufsiz *bytes*, potentially
27372762
* cutting off a UTF-8 sequence. Insufficient bufsize is *not* a failure
27382763
* condition. There is no conversion function that produces invalid UTF-8,
27392764
* so convert to a (hopefully large enough) temporary buffer, then memcpy
27402765
* the requested number of bytes (including '\0' for robustness).
27412766
*/
2742-
if ((len = xwcstoutf(tmpbuf, normalize_ntpath(wbuf), MAX_LONG_PATH)) < 0)
2743-
return -1;
27442767
memcpy(buf, tmpbuf, min(bufsiz, len + 1));
27452768
return min(bufsiz, len);
27462769
}

compat/win32/fscache.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,18 @@ int fscache_lstat(const char *filename, struct stat *st)
581581
return -1;
582582
}
583583

584+
/*
585+
* Special case symbolic links: FindFirstFile()/FindNextFile() did not
586+
* provide us with the length of the target path.
587+
*/
588+
if (fse->u.s.st_size == MAX_LONG_PATH && S_ISLNK(fse->st_mode)) {
589+
char buf[MAX_LONG_PATH];
590+
int len = readlink(filename, buf, sizeof(buf) - 1);
591+
592+
if (len > 0)
593+
fse->u.s.st_size = len;
594+
}
595+
584596
/* copy stat data */
585597
st->st_ino = 0;
586598
st->st_gid = 0;

0 commit comments

Comments
 (0)