Skip to content

Commit 22b23bf

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 <billziss@navimatics.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
1 parent fafe98a commit 22b23bf

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
@@ -963,10 +963,14 @@ static int has_valid_directory_prefix(wchar_t *wfilename)
963963
return 1;
964964
}
965965

966+
static int readlink_1(const WCHAR *wpath, BOOL fail_on_unknown_tag,
967+
char *tmpbuf, int *plen, DWORD *ptag);
968+
966969
int mingw_lstat(const char *file_name, struct stat *buf)
967970
{
968971
WIN32_FILE_ATTRIBUTE_DATA fdata;
969-
WIN32_FIND_DATAW findbuf = { 0 };
972+
DWORD reparse_tag = 0;
973+
int link_len = 0;
970974
wchar_t wfilename[MAX_LONG_PATH];
971975
int wlen = xutftowcs_long_path(wfilename, file_name);
972976
if (wlen < 0)
@@ -981,28 +985,29 @@ int mingw_lstat(const char *file_name, struct stat *buf)
981985
}
982986

983987
if (GetFileAttributesExW(wfilename, GetFileExInfoStandard, &fdata)) {
984-
/* for reparse points, use FindFirstFile to get the reparse tag */
988+
/* for reparse points, get the link tag and length */
985989
if (fdata.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
986-
HANDLE handle = FindFirstFileW(wfilename, &findbuf);
987-
if (handle == INVALID_HANDLE_VALUE)
988-
goto error;
989-
FindClose(handle);
990+
char tmpbuf[MAX_LONG_PATH];
991+
992+
if (readlink_1(wfilename, FALSE, tmpbuf, &link_len,
993+
&reparse_tag) < 0)
994+
return -1;
990995
}
991996
buf->st_ino = 0;
992997
buf->st_gid = 0;
993998
buf->st_uid = 0;
994999
buf->st_nlink = 1;
9951000
buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes,
996-
findbuf.dwReserved0);
997-
buf->st_size = S_ISLNK(buf->st_mode) ? MAX_LONG_PATH :
1001+
reparse_tag);
1002+
buf->st_size = S_ISLNK(buf->st_mode) ? link_len :
9981003
fdata.nFileSizeLow | (((off_t) fdata.nFileSizeHigh) << 32);
9991004
buf->st_dev = buf->st_rdev = 0; /* not used by Git */
10001005
filetime_to_timespec(&(fdata.ftLastAccessTime), &(buf->st_atim));
10011006
filetime_to_timespec(&(fdata.ftLastWriteTime), &(buf->st_mtim));
10021007
filetime_to_timespec(&(fdata.ftCreationTime), &(buf->st_ctim));
10031008
return 0;
10041009
}
1005-
error:
1010+
10061011
switch (GetLastError()) {
10071012
case ERROR_ACCESS_DENIED:
10081013
case ERROR_SHARING_VIOLATION:
@@ -2967,17 +2972,13 @@ typedef struct _REPARSE_DATA_BUFFER {
29672972
} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
29682973
#endif
29692974

2970-
int readlink(const char *path, char *buf, size_t bufsiz)
2975+
static int readlink_1(const WCHAR *wpath, BOOL fail_on_unknown_tag,
2976+
char *tmpbuf, int *plen, DWORD *ptag)
29712977
{
29722978
HANDLE handle;
2973-
WCHAR wpath[MAX_LONG_PATH], *wbuf;
2979+
WCHAR *wbuf;
29742980
REPARSE_DATA_BUFFER *b = alloca(MAXIMUM_REPARSE_DATA_BUFFER_SIZE);
29752981
DWORD dummy;
2976-
char tmpbuf[MAX_LONG_PATH];
2977-
int len;
2978-
2979-
if (xutftowcs_long_path(wpath, path) < 0)
2980-
return -1;
29812982

29822983
/* read reparse point data */
29832984
handle = CreateFileW(wpath, 0,
@@ -2997,7 +2998,7 @@ int readlink(const char *path, char *buf, size_t bufsiz)
29972998
CloseHandle(handle);
29982999

29993000
/* get target path for symlinks or mount points (aka 'junctions') */
3000-
switch (b->ReparseTag) {
3001+
switch ((*ptag = b->ReparseTag)) {
30013002
case IO_REPARSE_TAG_SYMLINK:
30023003
wbuf = (WCHAR*) (((char*) b->SymbolicLinkReparseBuffer.PathBuffer)
30033004
+ b->SymbolicLinkReparseBuffer.SubstituteNameOffset);
@@ -3011,19 +3012,41 @@ int readlink(const char *path, char *buf, size_t bufsiz)
30113012
+ b->MountPointReparseBuffer.SubstituteNameLength) = 0;
30123013
break;
30133014
default:
3014-
errno = EINVAL;
3015-
return -1;
3015+
if (fail_on_unknown_tag) {
3016+
errno = EINVAL;
3017+
return -1;
3018+
} else {
3019+
*plen = MAX_LONG_PATH;
3020+
return 0;
3021+
}
30163022
}
30173023

3024+
if ((*plen =
3025+
xwcstoutf(tmpbuf, normalize_ntpath(wbuf), MAX_LONG_PATH)) < 0)
3026+
return -1;
3027+
return 0;
3028+
}
3029+
3030+
int readlink(const char *path, char *buf, size_t bufsiz)
3031+
{
3032+
WCHAR wpath[MAX_LONG_PATH];
3033+
char tmpbuf[MAX_LONG_PATH];
3034+
int len;
3035+
DWORD tag;
3036+
3037+
if (xutftowcs_long_path(wpath, path) < 0)
3038+
return -1;
3039+
3040+
if (readlink_1(wpath, TRUE, tmpbuf, &len, &tag) < 0)
3041+
return -1;
3042+
30183043
/*
30193044
* Adapt to strange readlink() API: Copy up to bufsiz *bytes*, potentially
30203045
* cutting off a UTF-8 sequence. Insufficient bufsize is *not* a failure
30213046
* condition. There is no conversion function that produces invalid UTF-8,
30223047
* so convert to a (hopefully large enough) temporary buffer, then memcpy
30233048
* the requested number of bytes (including '\0' for robustness).
30243049
*/
3025-
if ((len = xwcstoutf(tmpbuf, normalize_ntpath(wbuf), MAX_LONG_PATH)) < 0)
3026-
return -1;
30273050
memcpy(buf, tmpbuf, min(bufsiz, len + 1));
30283051
return min(bufsiz, len);
30293052
}

compat/win32/fscache.c

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

597+
/*
598+
* Special case symbolic links: FindFirstFile()/FindNextFile() did not
599+
* provide us with the length of the target path.
600+
*/
601+
if (fse->u.s.st_size == MAX_LONG_PATH && S_ISLNK(fse->st_mode)) {
602+
char buf[MAX_LONG_PATH];
603+
int len = readlink(filename, buf, sizeof(buf) - 1);
604+
605+
if (len > 0)
606+
fse->u.s.st_size = len;
607+
}
608+
597609
/* copy stat data */
598610
st->st_ino = 0;
599611
st->st_gid = 0;

0 commit comments

Comments
 (0)