Skip to content

Commit 216b024

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 b898c79 commit 216b024

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
@@ -790,10 +790,14 @@ static int has_valid_directory_prefix(wchar_t *wfilename)
790790
return 1;
791791
}
792792

793+
static int readlink_1(const WCHAR *wpath, BOOL fail_on_unknown_tag,
794+
char *tmpbuf, int *plen, DWORD *ptag);
795+
793796
int mingw_lstat(const char *file_name, struct stat *buf)
794797
{
795798
WIN32_FILE_ATTRIBUTE_DATA fdata;
796-
WIN32_FIND_DATAW findbuf = { 0 };
799+
DWORD reparse_tag = 0;
800+
int link_len = 0;
797801
wchar_t wfilename[MAX_LONG_PATH];
798802
int wlen = xutftowcs_long_path(wfilename, file_name);
799803
if (wlen < 0)
@@ -808,28 +812,29 @@ int mingw_lstat(const char *file_name, struct stat *buf)
808812
}
809813

810814
if (GetFileAttributesExW(wfilename, GetFileExInfoStandard, &fdata)) {
811-
/* for reparse points, use FindFirstFile to get the reparse tag */
815+
/* for reparse points, get the link tag and length */
812816
if (fdata.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
813-
HANDLE handle = FindFirstFileW(wfilename, &findbuf);
814-
if (handle == INVALID_HANDLE_VALUE)
815-
goto error;
816-
FindClose(handle);
817+
char tmpbuf[MAX_LONG_PATH];
818+
819+
if (readlink_1(wfilename, FALSE, tmpbuf, &link_len,
820+
&reparse_tag) < 0)
821+
return -1;
817822
}
818823
buf->st_ino = 0;
819824
buf->st_gid = 0;
820825
buf->st_uid = 0;
821826
buf->st_nlink = 1;
822827
buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes,
823-
findbuf.dwReserved0);
824-
buf->st_size = S_ISLNK(buf->st_mode) ? MAX_LONG_PATH :
828+
reparse_tag);
829+
buf->st_size = S_ISLNK(buf->st_mode) ? link_len :
825830
fdata.nFileSizeLow | (((off_t) fdata.nFileSizeHigh) << 32);
826831
buf->st_dev = buf->st_rdev = 0; /* not used by Git */
827832
filetime_to_timespec(&(fdata.ftLastAccessTime), &(buf->st_atim));
828833
filetime_to_timespec(&(fdata.ftLastWriteTime), &(buf->st_mtim));
829834
filetime_to_timespec(&(fdata.ftCreationTime), &(buf->st_ctim));
830835
return 0;
831836
}
832-
error:
837+
833838
switch (GetLastError()) {
834839
case ERROR_ACCESS_DENIED:
835840
case ERROR_SHARING_VIOLATION:
@@ -2650,17 +2655,13 @@ typedef struct _REPARSE_DATA_BUFFER {
26502655
} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
26512656
#endif
26522657

2653-
int readlink(const char *path, char *buf, size_t bufsiz)
2658+
static int readlink_1(const WCHAR *wpath, BOOL fail_on_unknown_tag,
2659+
char *tmpbuf, int *plen, DWORD *ptag)
26542660
{
26552661
HANDLE handle;
2656-
WCHAR wpath[MAX_LONG_PATH], *wbuf;
2662+
WCHAR *wbuf;
26572663
REPARSE_DATA_BUFFER *b = alloca(MAXIMUM_REPARSE_DATA_BUFFER_SIZE);
26582664
DWORD dummy;
2659-
char tmpbuf[MAX_LONG_PATH];
2660-
int len;
2661-
2662-
if (xutftowcs_long_path(wpath, path) < 0)
2663-
return -1;
26642665

26652666
/* read reparse point data */
26662667
handle = CreateFileW(wpath, 0,
@@ -2680,7 +2681,7 @@ int readlink(const char *path, char *buf, size_t bufsiz)
26802681
CloseHandle(handle);
26812682

26822683
/* get target path for symlinks or mount points (aka 'junctions') */
2683-
switch (b->ReparseTag) {
2684+
switch ((*ptag = b->ReparseTag)) {
26842685
case IO_REPARSE_TAG_SYMLINK:
26852686
wbuf = (WCHAR*) (((char*) b->SymbolicLinkReparseBuffer.PathBuffer)
26862687
+ b->SymbolicLinkReparseBuffer.SubstituteNameOffset);
@@ -2694,19 +2695,41 @@ int readlink(const char *path, char *buf, size_t bufsiz)
26942695
+ b->MountPointReparseBuffer.SubstituteNameLength) = 0;
26952696
break;
26962697
default:
2697-
errno = EINVAL;
2698-
return -1;
2698+
if (fail_on_unknown_tag) {
2699+
errno = EINVAL;
2700+
return -1;
2701+
} else {
2702+
*plen = MAX_LONG_PATH;
2703+
return 0;
2704+
}
26992705
}
27002706

2707+
if ((*plen =
2708+
xwcstoutf(tmpbuf, normalize_ntpath(wbuf), MAX_LONG_PATH)) < 0)
2709+
return -1;
2710+
return 0;
2711+
}
2712+
2713+
int readlink(const char *path, char *buf, size_t bufsiz)
2714+
{
2715+
WCHAR wpath[MAX_LONG_PATH];
2716+
char tmpbuf[MAX_LONG_PATH];
2717+
int len;
2718+
DWORD tag;
2719+
2720+
if (xutftowcs_long_path(wpath, path) < 0)
2721+
return -1;
2722+
2723+
if (readlink_1(wpath, TRUE, tmpbuf, &len, &tag) < 0)
2724+
return -1;
2725+
27012726
/*
27022727
* Adapt to strange readlink() API: Copy up to bufsiz *bytes*, potentially
27032728
* cutting off a UTF-8 sequence. Insufficient bufsize is *not* a failure
27042729
* condition. There is no conversion function that produces invalid UTF-8,
27052730
* so convert to a (hopefully large enough) temporary buffer, then memcpy
27062731
* the requested number of bytes (including '\0' for robustness).
27072732
*/
2708-
if ((len = xwcstoutf(tmpbuf, normalize_ntpath(wbuf), MAX_LONG_PATH)) < 0)
2709-
return -1;
27102733
memcpy(buf, tmpbuf, min(bufsiz, len + 1));
27112734
return min(bufsiz, len);
27122735
}

compat/win32/fscache.c

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

577+
/*
578+
* Special case symbolic links: FindFirstFile()/FindNextFile() did not
579+
* provide us with the length of the target path.
580+
*/
581+
if (fse->u.s.st_size == MAX_LONG_PATH && S_ISLNK(fse->st_mode)) {
582+
char buf[MAX_LONG_PATH];
583+
int len = readlink(filename, buf, sizeof(buf) - 1);
584+
585+
if (len > 0)
586+
fse->u.s.st_size = len;
587+
}
588+
577589
/* copy stat data */
578590
st->st_ino = 0;
579591
st->st_gid = 0;

0 commit comments

Comments
 (0)