Skip to content

Commit 4236f8c

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 585b4bb commit 4236f8c

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

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

982986
if (GetFileAttributesExW(wfilename, GetFileExInfoStandard, &fdata)) {
983-
/* for reparse points, use FindFirstFile to get the reparse tag */
987+
/* for reparse points, get the link tag and length */
984988
if (fdata.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
985-
HANDLE handle = FindFirstFileW(wfilename, &findbuf);
986-
if (handle == INVALID_HANDLE_VALUE)
987-
goto error;
988-
FindClose(handle);
989+
char tmpbuf[MAX_LONG_PATH];
990+
991+
if (readlink_1(wfilename, FALSE, tmpbuf, &link_len,
992+
&reparse_tag) < 0)
993+
return -1;
989994
}
990995
buf->st_ino = 0;
991996
buf->st_gid = 0;
992997
buf->st_uid = 0;
993998
buf->st_nlink = 1;
994999
buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes,
995-
findbuf.dwReserved0);
996-
buf->st_size = S_ISLNK(buf->st_mode) ? MAX_LONG_PATH :
1000+
reparse_tag);
1001+
buf->st_size = S_ISLNK(buf->st_mode) ? link_len :
9971002
fdata.nFileSizeLow | (((off_t) fdata.nFileSizeHigh) << 32);
9981003
buf->st_dev = buf->st_rdev = 0; /* not used by Git */
9991004
filetime_to_timespec(&(fdata.ftLastAccessTime), &(buf->st_atim));
10001005
filetime_to_timespec(&(fdata.ftLastWriteTime), &(buf->st_mtim));
10011006
filetime_to_timespec(&(fdata.ftCreationTime), &(buf->st_ctim));
10021007
return 0;
10031008
}
1004-
error:
1009+
10051010
switch (GetLastError()) {
10061011
case ERROR_ACCESS_DENIED:
10071012
case ERROR_SHARING_VIOLATION:
@@ -2938,17 +2943,13 @@ typedef struct _REPARSE_DATA_BUFFER {
29382943
} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
29392944
#endif
29402945

2941-
int readlink(const char *path, char *buf, size_t bufsiz)
2946+
static int readlink_1(const WCHAR *wpath, BOOL fail_on_unknown_tag,
2947+
char *tmpbuf, int *plen, DWORD *ptag)
29422948
{
29432949
HANDLE handle;
2944-
WCHAR wpath[MAX_LONG_PATH], *wbuf;
2950+
WCHAR *wbuf;
29452951
REPARSE_DATA_BUFFER *b = alloca(MAXIMUM_REPARSE_DATA_BUFFER_SIZE);
29462952
DWORD dummy;
2947-
char tmpbuf[MAX_LONG_PATH];
2948-
int len;
2949-
2950-
if (xutftowcs_long_path(wpath, path) < 0)
2951-
return -1;
29522953

29532954
/* read reparse point data */
29542955
handle = CreateFileW(wpath, 0,
@@ -2968,7 +2969,7 @@ int readlink(const char *path, char *buf, size_t bufsiz)
29682969
CloseHandle(handle);
29692970

29702971
/* get target path for symlinks or mount points (aka 'junctions') */
2971-
switch (b->ReparseTag) {
2972+
switch ((*ptag = b->ReparseTag)) {
29722973
case IO_REPARSE_TAG_SYMLINK:
29732974
wbuf = (WCHAR*) (((char*) b->SymbolicLinkReparseBuffer.PathBuffer)
29742975
+ b->SymbolicLinkReparseBuffer.SubstituteNameOffset);
@@ -2982,19 +2983,41 @@ int readlink(const char *path, char *buf, size_t bufsiz)
29822983
+ b->MountPointReparseBuffer.SubstituteNameLength) = 0;
29832984
break;
29842985
default:
2985-
errno = EINVAL;
2986-
return -1;
2986+
if (fail_on_unknown_tag) {
2987+
errno = EINVAL;
2988+
return -1;
2989+
} else {
2990+
*plen = MAX_LONG_PATH;
2991+
return 0;
2992+
}
29872993
}
29882994

2995+
if ((*plen =
2996+
xwcstoutf(tmpbuf, normalize_ntpath(wbuf), MAX_LONG_PATH)) < 0)
2997+
return -1;
2998+
return 0;
2999+
}
3000+
3001+
int readlink(const char *path, char *buf, size_t bufsiz)
3002+
{
3003+
WCHAR wpath[MAX_LONG_PATH];
3004+
char tmpbuf[MAX_LONG_PATH];
3005+
int len;
3006+
DWORD tag;
3007+
3008+
if (xutftowcs_long_path(wpath, path) < 0)
3009+
return -1;
3010+
3011+
if (readlink_1(wpath, TRUE, tmpbuf, &len, &tag) < 0)
3012+
return -1;
3013+
29893014
/*
29903015
* Adapt to strange readlink() API: Copy up to bufsiz *bytes*, potentially
29913016
* cutting off a UTF-8 sequence. Insufficient bufsize is *not* a failure
29923017
* condition. There is no conversion function that produces invalid UTF-8,
29933018
* so convert to a (hopefully large enough) temporary buffer, then memcpy
29943019
* the requested number of bytes (including '\0' for robustness).
29953020
*/
2996-
if ((len = xwcstoutf(tmpbuf, normalize_ntpath(wbuf), MAX_LONG_PATH)) < 0)
2997-
return -1;
29983021
memcpy(buf, tmpbuf, min(bufsiz, len + 1));
29993022
return min(bufsiz, len);
30003023
}

compat/win32/fscache.c

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

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

0 commit comments

Comments
 (0)