Skip to content

Commit a833d4b

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 63a2744 commit a833d4b

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
@@ -818,7 +818,7 @@ int mingw_lstat(const char *file_name, struct stat *buf)
818818
buf->st_uid = 0;
819819
buf->st_nlink = 1;
820820
buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes,
821-
findbuf.dwReserved0);
821+
findbuf.dwReserved0, file_name);
822822
buf->st_size = S_ISLNK(buf->st_mode) ? MAX_LONG_PATH :
823823
fdata.nFileSizeLow | (((off_t) fdata.nFileSizeHigh) << 32);
824824
buf->st_dev = buf->st_rdev = 0; /* not used by Git */
@@ -867,7 +867,7 @@ static int get_file_info_by_handle(HANDLE hnd, struct stat *buf)
867867
buf->st_dev = buf->st_rdev = 0; /* not used by Git */
868868
buf->st_gid = buf->st_uid = 0;
869869
buf->st_nlink = 1;
870-
buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes, 0);
870+
buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes, 0, NULL);
871871
buf->st_size = fdata.nFileSizeLow | (((off_t) fdata.nFileSizeHigh) << 32);
872872
filetime_to_timespec(&(fdata.ftLastAccessTime), &(buf->st_atim));
873873
filetime_to_timespec(&(fdata.ftLastWriteTime), &(buf->st_mtim));
@@ -3584,12 +3584,25 @@ int is_inside_windows_container(void)
35843584
return inside_container;
35853585
}
35863586

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