Skip to content

Commit b497937

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 724b591 commit b497937

File tree

3 files changed

+44
-8
lines changed

3 files changed

+44
-8
lines changed

compat/mingw.c

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -898,7 +898,7 @@ int mingw_lstat(const char *file_name, struct stat *buf)
898898
buf->st_uid = 0;
899899
buf->st_nlink = 1;
900900
buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes,
901-
findbuf.dwReserved0);
901+
findbuf.dwReserved0, file_name);
902902
buf->st_size = S_ISLNK(buf->st_mode) ? MAX_LONG_PATH :
903903
fdata.nFileSizeLow | (((off_t) fdata.nFileSizeHigh) << 32);
904904
buf->st_dev = buf->st_rdev = 0; /* not used by Git */
@@ -949,7 +949,7 @@ static int get_file_info_by_handle(HANDLE hnd, struct stat *buf)
949949
buf->st_gid = 0;
950950
buf->st_uid = 0;
951951
buf->st_nlink = 1;
952-
buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes, 0);
952+
buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes, 0, NULL);
953953
buf->st_size = fdata.nFileSizeLow |
954954
(((off_t)fdata.nFileSizeHigh)<<32);
955955
buf->st_dev = buf->st_rdev = 0; /* not used by Git */
@@ -3146,12 +3146,25 @@ int is_inside_windows_container(void)
31463146
return inside_container;
31473147
}
31483148

3149-
int file_attr_to_st_mode (DWORD attr, DWORD tag)
3149+
int file_attr_to_st_mode (DWORD attr, DWORD tag, const char *path)
31503150
{
31513151
int fMode = S_IREAD;
3152-
if ((attr & FILE_ATTRIBUTE_REPARSE_POINT) && tag == IO_REPARSE_TAG_SYMLINK)
3153-
fMode |= S_IFLNK;
3154-
else if (attr & FILE_ATTRIBUTE_DIRECTORY)
3152+
if ((attr & FILE_ATTRIBUTE_REPARSE_POINT) &&
3153+
tag == IO_REPARSE_TAG_SYMLINK) {
3154+
int flag = S_IFLNK;
3155+
char buf[MAX_LONG_PATH];
3156+
3157+
/*
3158+
* Windows containers' mapped volumes are marked as reparse
3159+
* points and look like symbolic links, but they are not.
3160+
*/
3161+
if (path && is_inside_windows_container() &&
3162+
readlink(path, buf, sizeof(buf)) > 27 &&
3163+
starts_with(buf, "/ContainerMappedDirectories/"))
3164+
flag = S_IFDIR;
3165+
3166+
fMode |= flag;
3167+
} else if (attr & FILE_ATTRIBUTE_DIRECTORY)
31553168
fMode |= S_IFDIR;
31563169
else
31573170
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: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,30 @@ static struct fsentry *fseentry_create_entry(struct fscache *cache, struct fsent
180180

181181
fse = fsentry_alloc(cache, list, buf, len);
182182

183-
fse->st_mode = file_attr_to_st_mode(fdata->FileAttributes, fdata->EaSize);
183+
/*
184+
* On certain Windows versions, host directories mapped into
185+
* Windows Containers ("Volumes", see https://docs.docker.com/storage/volumes/)
186+
* look like symbolic links, but their targets are paths that
187+
* are valid only in kernel mode.
188+
*
189+
* Let's work around this by detecting that situation and
190+
* telling Git that these are *not* symbolic links.
191+
*/
192+
if (fdata->FileAttributes & FILE_ATTRIBUTE_REPARSE_POINT &&
193+
fdata->EaSize == IO_REPARSE_TAG_SYMLINK &&
194+
sizeof(buf) > (list ? list->len + 1 : 0) + fse->len + 1 &&
195+
is_inside_windows_container()) {
196+
size_t off = 0;
197+
if (list) {
198+
memcpy(buf, list->name, list->len);
199+
buf[list->len] = '/';
200+
off = list->len + 1;
201+
}
202+
memcpy(buf + off, fse->name, fse->len);
203+
buf[off + fse->len] = '\0';
204+
}
205+
206+
fse->st_mode = file_attr_to_st_mode(fdata->FileAttributes, fdata->EaSize, buf);
184207
fse->u.s.st_size = S_ISLNK(fse->st_mode) ? MAX_LONG_PATH :
185208
fdata->EndOfFile.LowPart | (((off_t)fdata->EndOfFile.HighPart) << 32);
186209
filetime_to_timespec((FILETIME *)&(fdata->LastAccessTime), &(fse->u.s.st_atim));

0 commit comments

Comments
 (0)