Skip to content

Commit bb654bd

Browse files
dschoZCube
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 <johannes.schindelin@gmx.de> Signed-off-by: JiSeop Moon <zcube@zcube.kr>
1 parent e66b66b commit bb654bd

File tree

3 files changed

+34
-8
lines changed

3 files changed

+34
-8
lines changed

Diff for: compat/mingw.c

+19-6
Original file line numberDiff line numberDiff line change
@@ -775,7 +775,7 @@ int mingw_lstat(const char *file_name, struct stat *buf)
775775
buf->st_uid = 0;
776776
buf->st_nlink = 1;
777777
buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes,
778-
findbuf.dwReserved0);
778+
findbuf.dwReserved0, file_name);
779779
buf->st_size = S_ISLNK(buf->st_mode) ? MAX_LONG_PATH :
780780
fdata.nFileSizeLow | (((off_t) fdata.nFileSizeHigh) << 32);
781781
buf->st_dev = buf->st_rdev = 0; /* not used by Git */
@@ -824,7 +824,7 @@ static int get_file_info_by_handle(HANDLE hnd, struct stat *buf)
824824
buf->st_dev = buf->st_rdev = 0; /* not used by Git */
825825
buf->st_gid = buf->st_uid = 0;
826826
buf->st_nlink = 1;
827-
buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes, 0);
827+
buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes, 0, NULL);
828828
buf->st_size = fdata.nFileSizeLow | (((off_t) fdata.nFileSizeHigh) << 32);
829829
filetime_to_timespec(&(fdata.ftLastAccessTime), &(buf->st_atim));
830830
filetime_to_timespec(&(fdata.ftLastWriteTime), &(buf->st_mtim));
@@ -3585,12 +3585,25 @@ int is_inside_windows_container(void)
35853585
return inside_container;
35863586
}
35873587

3588-
int file_attr_to_st_mode (DWORD attr, DWORD tag)
3588+
int file_attr_to_st_mode (DWORD attr, DWORD tag, const char *path)
35893589
{
35903590
int fMode = S_IREAD;
3591-
if ((attr & FILE_ATTRIBUTE_REPARSE_POINT) && tag == IO_REPARSE_TAG_SYMLINK)
3592-
fMode |= S_IFLNK;
3593-
else if (attr & FILE_ATTRIBUTE_DIRECTORY)
3591+
if ((attr & FILE_ATTRIBUTE_REPARSE_POINT) &&
3592+
tag == IO_REPARSE_TAG_SYMLINK) {
3593+
int flag = S_IFLNK;
3594+
char buf[MAX_LONG_PATH];
3595+
3596+
/*
3597+
* Windows containers' mapped volumes are marked as reparse
3598+
* points and look like symbolic links, but they are not.
3599+
*/
3600+
if (path && is_inside_windows_container() &&
3601+
!readlink(path, buf, sizeof(buf)) &&
3602+
starts_with(buf, "/ContainerMappedDirectories/"))
3603+
flag = S_IFDIR;
3604+
3605+
fMode |= flag;
3606+
} else if (attr & FILE_ATTRIBUTE_DIRECTORY)
35943607
fMode |= S_IFDIR;
35953608
else
35963609
fMode |= S_IFREG;

Diff for: compat/win32.h

+1-1
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
{

Diff for: compat/win32/fscache.c

+14-1
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)