Skip to content

Commit f213f4d

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 2b2f28e commit f213f4d

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

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

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

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

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

compat/win32/fscache.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,18 @@ int fscache_lstat(const char *filename, struct stat *st)
579579
if (!fse)
580580
return -1;
581581

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

0 commit comments

Comments
 (0)