Skip to content

Commit e6b4d99

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 e568585 commit e6b4d99

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
@@ -973,10 +973,14 @@ static int has_valid_directory_prefix(wchar_t *wfilename)
973973
return 1;
974974
}
975975

976+
static int readlink_1(const WCHAR *wpath, BOOL fail_on_unknown_tag,
977+
char *tmpbuf, int *plen, DWORD *ptag);
978+
976979
int mingw_lstat(const char *file_name, struct stat *buf)
977980
{
978981
WIN32_FILE_ATTRIBUTE_DATA fdata;
979-
WIN32_FIND_DATAW findbuf = { 0 };
982+
DWORD reparse_tag = 0;
983+
int link_len = 0;
980984
wchar_t wfilename[MAX_LONG_PATH];
981985
int wlen = xutftowcs_long_path(wfilename, file_name);
982986
if (wlen < 0)
@@ -991,28 +995,29 @@ int mingw_lstat(const char *file_name, struct stat *buf)
991995
}
992996

993997
if (GetFileAttributesExW(wfilename, GetFileExInfoStandard, &fdata)) {
994-
/* for reparse points, use FindFirstFile to get the reparse tag */
998+
/* for reparse points, get the link tag and length */
995999
if (fdata.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
996-
HANDLE handle = FindFirstFileW(wfilename, &findbuf);
997-
if (handle == INVALID_HANDLE_VALUE)
998-
goto error;
999-
FindClose(handle);
1000+
char tmpbuf[MAX_LONG_PATH];
1001+
1002+
if (readlink_1(wfilename, FALSE, tmpbuf, &link_len,
1003+
&reparse_tag) < 0)
1004+
return -1;
10001005
}
10011006
buf->st_ino = 0;
10021007
buf->st_gid = 0;
10031008
buf->st_uid = 0;
10041009
buf->st_nlink = 1;
10051010
buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes,
1006-
findbuf.dwReserved0);
1007-
buf->st_size = S_ISLNK(buf->st_mode) ? MAX_LONG_PATH :
1011+
reparse_tag);
1012+
buf->st_size = S_ISLNK(buf->st_mode) ? link_len :
10081013
fdata.nFileSizeLow | (((off_t) fdata.nFileSizeHigh) << 32);
10091014
buf->st_dev = buf->st_rdev = 0; /* not used by Git */
10101015
filetime_to_timespec(&(fdata.ftLastAccessTime), &(buf->st_atim));
10111016
filetime_to_timespec(&(fdata.ftLastWriteTime), &(buf->st_mtim));
10121017
filetime_to_timespec(&(fdata.ftCreationTime), &(buf->st_ctim));
10131018
return 0;
10141019
}
1015-
error:
1020+
10161021
switch (GetLastError()) {
10171022
case ERROR_ACCESS_DENIED:
10181023
case ERROR_SHARING_VIOLATION:
@@ -3027,17 +3032,13 @@ typedef struct _REPARSE_DATA_BUFFER {
30273032
} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
30283033
#endif
30293034

3030-
int readlink(const char *path, char *buf, size_t bufsiz)
3035+
static int readlink_1(const WCHAR *wpath, BOOL fail_on_unknown_tag,
3036+
char *tmpbuf, int *plen, DWORD *ptag)
30313037
{
30323038
HANDLE handle;
3033-
WCHAR wpath[MAX_LONG_PATH], *wbuf;
3039+
WCHAR *wbuf;
30343040
REPARSE_DATA_BUFFER *b = alloca(MAXIMUM_REPARSE_DATA_BUFFER_SIZE);
30353041
DWORD dummy;
3036-
char tmpbuf[MAX_LONG_PATH];
3037-
int len;
3038-
3039-
if (xutftowcs_long_path(wpath, path) < 0)
3040-
return -1;
30413042

30423043
/* read reparse point data */
30433044
handle = CreateFileW(wpath, 0,
@@ -3057,7 +3058,7 @@ int readlink(const char *path, char *buf, size_t bufsiz)
30573058
CloseHandle(handle);
30583059

30593060
/* get target path for symlinks or mount points (aka 'junctions') */
3060-
switch (b->ReparseTag) {
3061+
switch ((*ptag = b->ReparseTag)) {
30613062
case IO_REPARSE_TAG_SYMLINK:
30623063
wbuf = (WCHAR*) (((char*) b->SymbolicLinkReparseBuffer.PathBuffer)
30633064
+ b->SymbolicLinkReparseBuffer.SubstituteNameOffset);
@@ -3071,19 +3072,41 @@ int readlink(const char *path, char *buf, size_t bufsiz)
30713072
+ b->MountPointReparseBuffer.SubstituteNameLength) = 0;
30723073
break;
30733074
default:
3074-
errno = EINVAL;
3075-
return -1;
3075+
if (fail_on_unknown_tag) {
3076+
errno = EINVAL;
3077+
return -1;
3078+
} else {
3079+
*plen = MAX_LONG_PATH;
3080+
return 0;
3081+
}
30763082
}
30773083

3084+
if ((*plen =
3085+
xwcstoutf(tmpbuf, normalize_ntpath(wbuf), MAX_LONG_PATH)) < 0)
3086+
return -1;
3087+
return 0;
3088+
}
3089+
3090+
int readlink(const char *path, char *buf, size_t bufsiz)
3091+
{
3092+
WCHAR wpath[MAX_LONG_PATH];
3093+
char tmpbuf[MAX_LONG_PATH];
3094+
int len;
3095+
DWORD tag;
3096+
3097+
if (xutftowcs_long_path(wpath, path) < 0)
3098+
return -1;
3099+
3100+
if (readlink_1(wpath, TRUE, tmpbuf, &len, &tag) < 0)
3101+
return -1;
3102+
30783103
/*
30793104
* Adapt to strange readlink() API: Copy up to bufsiz *bytes*, potentially
30803105
* cutting off a UTF-8 sequence. Insufficient bufsize is *not* a failure
30813106
* condition. There is no conversion function that produces invalid UTF-8,
30823107
* so convert to a (hopefully large enough) temporary buffer, then memcpy
30833108
* the requested number of bytes (including '\0' for robustness).
30843109
*/
3085-
if ((len = xwcstoutf(tmpbuf, normalize_ntpath(wbuf), MAX_LONG_PATH)) < 0)
3086-
return -1;
30873110
memcpy(buf, tmpbuf, min(bufsiz, len + 1));
30883111
return min(bufsiz, len);
30893112
}

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)