Skip to content

Commit 9ea40e3

Browse files
dschoGit for Windows Build Agent
authored andcommitted
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 c933e05 commit 9ea40e3

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
@@ -896,7 +896,7 @@ int mingw_lstat(const char *file_name, struct stat *buf)
896896
buf->st_uid = 0;
897897
buf->st_nlink = 1;
898898
buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes,
899-
findbuf.dwReserved0);
899+
findbuf.dwReserved0, file_name);
900900
buf->st_size = S_ISLNK(buf->st_mode) ? MAX_LONG_PATH :
901901
fdata.nFileSizeLow | (((off_t) fdata.nFileSizeHigh) << 32);
902902
buf->st_dev = buf->st_rdev = 0; /* not used by Git */
@@ -947,7 +947,7 @@ static int get_file_info_by_handle(HANDLE hnd, struct stat *buf)
947947
buf->st_gid = 0;
948948
buf->st_uid = 0;
949949
buf->st_nlink = 1;
950-
buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes, 0);
950+
buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes, 0, NULL);
951951
buf->st_size = fdata.nFileSizeLow |
952952
(((off_t)fdata.nFileSizeHigh)<<32);
953953
buf->st_dev = buf->st_rdev = 0; /* not used by Git */
@@ -3041,12 +3041,25 @@ int is_inside_windows_container(void)
30413041
return inside_container;
30423042
}
30433043

3044-
int file_attr_to_st_mode (DWORD attr, DWORD tag)
3044+
int file_attr_to_st_mode (DWORD attr, DWORD tag, const char *path)
30453045
{
30463046
int fMode = S_IREAD;
3047-
if ((attr & FILE_ATTRIBUTE_REPARSE_POINT) && tag == IO_REPARSE_TAG_SYMLINK)
3048-
fMode |= S_IFLNK;
3049-
else if (attr & FILE_ATTRIBUTE_DIRECTORY)
3047+
if ((attr & FILE_ATTRIBUTE_REPARSE_POINT) &&
3048+
tag == IO_REPARSE_TAG_SYMLINK) {
3049+
int flag = S_IFLNK;
3050+
char buf[MAX_LONG_PATH];
3051+
3052+
/*
3053+
* Windows containers' mapped volumes are marked as reparse
3054+
* points and look like symbolic links, but they are not.
3055+
*/
3056+
if (path && is_inside_windows_container() &&
3057+
readlink(path, buf, sizeof(buf)) > 27 &&
3058+
starts_with(buf, "/ContainerMappedDirectories/"))
3059+
flag = S_IFDIR;
3060+
3061+
fMode |= flag;
3062+
} else if (attr & FILE_ATTRIBUTE_DIRECTORY)
30503063
fMode |= S_IFDIR;
30513064
else
30523065
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)