Skip to content

Commit adad93b

Browse files
committed
mingw: Windows Docker volumes are *not* symbolic links
... even if they may look like them. As looking up the target of the "symbolic link" (just to see whether it starts with `/ContainerMappedDirectories/`) is pretty expensive, we do it when we can be *really* sure that there is a possibility that this might be the case. Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: JiSeop Moon <[email protected]>
1 parent c05e652 commit adad93b

File tree

3 files changed

+43
-8
lines changed

3 files changed

+43
-8
lines changed

compat/mingw.c

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -895,7 +895,7 @@ int mingw_lstat(const char *file_name, struct stat *buf)
895895
buf->st_uid = 0;
896896
buf->st_nlink = 1;
897897
buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes,
898-
findbuf.dwReserved0);
898+
findbuf.dwReserved0, file_name);
899899
buf->st_size = S_ISLNK(buf->st_mode) ? MAX_LONG_PATH :
900900
fdata.nFileSizeLow | (((off_t) fdata.nFileSizeHigh) << 32);
901901
buf->st_dev = buf->st_rdev = 0; /* not used by Git */
@@ -946,7 +946,7 @@ static int get_file_info_by_handle(HANDLE hnd, struct stat *buf)
946946
buf->st_gid = 0;
947947
buf->st_uid = 0;
948948
buf->st_nlink = 1;
949-
buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes, 0);
949+
buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes, 0, NULL);
950950
buf->st_size = fdata.nFileSizeLow |
951951
(((off_t)fdata.nFileSizeHigh)<<32);
952952
buf->st_dev = buf->st_rdev = 0; /* not used by Git */
@@ -3013,12 +3013,25 @@ int is_inside_windows_container(void)
30133013
return inside_container;
30143014
}
30153015

3016-
int file_attr_to_st_mode (DWORD attr, DWORD tag)
3016+
int file_attr_to_st_mode (DWORD attr, DWORD tag, const char *path)
30173017
{
30183018
int fMode = S_IREAD;
3019-
if ((attr & FILE_ATTRIBUTE_REPARSE_POINT) && tag == IO_REPARSE_TAG_SYMLINK)
3020-
fMode |= S_IFLNK;
3021-
else if (attr & FILE_ATTRIBUTE_DIRECTORY)
3019+
if ((attr & FILE_ATTRIBUTE_REPARSE_POINT) &&
3020+
tag == IO_REPARSE_TAG_SYMLINK) {
3021+
int flag = S_IFLNK;
3022+
char buf[MAX_LONG_PATH];
3023+
3024+
/*
3025+
* Windows containers' mapped volumes are marked as reparse
3026+
* points and look like symbolic links, but they are not.
3027+
*/
3028+
if (path && is_inside_windows_container() &&
3029+
readlink(path, buf, sizeof(buf)) > 27 &&
3030+
starts_with(buf, "/ContainerMappedDirectories/"))
3031+
flag = S_IFDIR;
3032+
3033+
fMode |= flag;
3034+
} else if (attr & FILE_ATTRIBUTE_DIRECTORY)
30223035
fMode |= S_IFDIR;
30233036
else
30243037
fMode |= S_IFREG;

compat/win32.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include <windows.h>
77
#endif
88

9-
extern int file_attr_to_st_mode (DWORD attr, DWORD tag);
9+
extern int file_attr_to_st_mode (DWORD attr, DWORD tag, const char *path);
1010

1111
static inline int get_file_attr(const char *fname, WIN32_FILE_ATTRIBUTE_DATA *fdata)
1212
{

compat/win32/fscache.c

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,30 @@ static struct fsentry *fseentry_create_entry(struct fsentry *list,
149149

150150
fse = fsentry_alloc(list, buf, len);
151151

152+
/*
153+
* On certain Windows versions, host directories mapped into
154+
* Windows Containers ("Volumes", see https://docs.docker.com/storage/volumes/)
155+
* look like symbolic links, but their targets are paths that
156+
* are valid only in kernel mode.
157+
*
158+
* Let's work around this by detecting that situation and
159+
* telling Git that these are *not* symbolic links.
160+
*/
161+
if (fdata->dwReserved0 == IO_REPARSE_TAG_SYMLINK &&
162+
sizeof(buf) > (list ? list->len + 1 : 0) + fse->len + 1 &&
163+
is_inside_windows_container()) {
164+
size_t off = 0;
165+
if (list) {
166+
memcpy(buf, list->name, list->len);
167+
buf[list->len] = '/';
168+
off = list->len + 1;
169+
}
170+
memcpy(buf + off, fse->name, fse->len);
171+
buf[off + fse->len] = '\0';
172+
}
173+
152174
fse->st_mode = file_attr_to_st_mode(fdata->dwFileAttributes,
153-
fdata->dwReserved0);
175+
fdata->dwReserved0, buf);
154176
fse->st_size = S_ISLNK(fse->st_mode) ? MAX_LONG_PATH :
155177
fdata->nFileSizeLow | (((off_t) fdata->nFileSizeHigh) << 32);
156178
filetime_to_timespec(&(fdata->ftLastAccessTime), &(fse->st_atim));

0 commit comments

Comments
 (0)