Skip to content

Commit 0f32e2e

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 e0e8554 commit 0f32e2e

File tree

3 files changed

+34
-8
lines changed

3 files changed

+34
-8
lines changed

compat/mingw.c

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -855,7 +855,7 @@ int mingw_lstat(const char *file_name, struct stat *buf)
855855
buf->st_uid = 0;
856856
buf->st_nlink = 1;
857857
buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes,
858-
findbuf.dwReserved0);
858+
findbuf.dwReserved0, file_name);
859859
buf->st_size = S_ISLNK(buf->st_mode) ? MAX_LONG_PATH :
860860
fdata.nFileSizeLow | (((off_t) fdata.nFileSizeHigh) << 32);
861861
buf->st_dev = buf->st_rdev = 0; /* not used by Git */
@@ -904,7 +904,7 @@ static int get_file_info_by_handle(HANDLE hnd, struct stat *buf)
904904
buf->st_dev = buf->st_rdev = 0; /* not used by Git */
905905
buf->st_gid = buf->st_uid = 0;
906906
buf->st_nlink = 1;
907-
buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes, 0);
907+
buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes, 0, NULL);
908908
buf->st_size = fdata.nFileSizeLow | (((off_t) fdata.nFileSizeHigh) << 32);
909909
filetime_to_timespec(&(fdata.ftLastAccessTime), &(buf->st_atim));
910910
filetime_to_timespec(&(fdata.ftLastWriteTime), &(buf->st_mtim));
@@ -3621,12 +3621,25 @@ int is_inside_windows_container(void)
36213621
return inside_container;
36223622
}
36233623

3624-
int file_attr_to_st_mode (DWORD attr, DWORD tag)
3624+
int file_attr_to_st_mode (DWORD attr, DWORD tag, const char *path)
36253625
{
36263626
int fMode = S_IREAD;
3627-
if ((attr & FILE_ATTRIBUTE_REPARSE_POINT) && tag == IO_REPARSE_TAG_SYMLINK)
3628-
fMode |= S_IFLNK;
3629-
else if (attr & FILE_ATTRIBUTE_DIRECTORY)
3627+
if ((attr & FILE_ATTRIBUTE_REPARSE_POINT) &&
3628+
tag == IO_REPARSE_TAG_SYMLINK) {
3629+
int flag = S_IFLNK;
3630+
char buf[MAX_LONG_PATH];
3631+
3632+
/*
3633+
* Windows containers' mapped volumes are marked as reparse
3634+
* points and look like symbolic links, but they are not.
3635+
*/
3636+
if (path && is_inside_windows_container() &&
3637+
readlink(path, buf, sizeof(buf)) > 27 &&
3638+
starts_with(buf, "/ContainerMappedDirectories/"))
3639+
flag = S_IFDIR;
3640+
3641+
fMode |= flag;
3642+
} else if (attr & FILE_ATTRIBUTE_DIRECTORY)
36303643
fMode |= S_IFDIR;
36313644
else
36323645
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: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,21 @@ static struct fsentry *fseentry_create_entry(struct fsentry *list,
150150

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

153+
if (fdata->dwReserved0 == IO_REPARSE_TAG_SYMLINK &&
154+
sizeof(buf) > (list ? list->len + 1 : 0) + fse->len + 1 &&
155+
is_inside_windows_container()) {
156+
size_t off = 0;
157+
if (list) {
158+
memcpy(buf, list->name, list->len);
159+
buf[list->len] = '/';
160+
off = list->len + 1;
161+
}
162+
memcpy(buf + off, fse->name, fse->len);
163+
buf[off + fse->len] = '\0';
164+
}
165+
153166
fse->st_mode = file_attr_to_st_mode(fdata->dwFileAttributes,
154-
fdata->dwReserved0);
167+
fdata->dwReserved0, buf);
155168
fse->st_size = S_ISLNK(fse->st_mode) ? MAX_LONG_PATH :
156169
fdata->nFileSizeLow | (((off_t) fdata->nFileSizeHigh) << 32);
157170
filetime_to_timespec(&(fdata->ftLastAccessTime), &(fse->st_atim));

0 commit comments

Comments
 (0)