Skip to content

Commit b317680

Browse files
dschoGit for Windows Build Agent
authored andcommitted
Merge 'docker-volumes-are-no-symlinks'
This was pull request #1645 from ZCube/master Support windows container. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
2 parents 2997946 + 5f58061 commit b317680

File tree

4 files changed

+97
-16
lines changed

4 files changed

+97
-16
lines changed

compat/mingw.c

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -944,7 +944,7 @@ int mingw_lstat(const char *file_name, struct stat *buf)
944944
buf->st_uid = 0;
945945
buf->st_nlink = 1;
946946
buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes,
947-
findbuf.dwReserved0);
947+
findbuf.dwReserved0, file_name);
948948
buf->st_size = S_ISLNK(buf->st_mode) ? MAX_LONG_PATH :
949949
fdata.nFileSizeLow | (((off_t) fdata.nFileSizeHigh) << 32);
950950
buf->st_dev = buf->st_rdev = 0; /* not used by Git */
@@ -995,7 +995,7 @@ static int get_file_info_by_handle(HANDLE hnd, struct stat *buf)
995995
buf->st_gid = 0;
996996
buf->st_uid = 0;
997997
buf->st_nlink = 1;
998-
buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes, 0);
998+
buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes, 0, NULL);
999999
buf->st_size = fdata.nFileSizeLow |
10001000
(((off_t)fdata.nFileSizeHigh)<<32);
10011001
buf->st_dev = buf->st_rdev = 0; /* not used by Git */
@@ -2097,6 +2097,13 @@ int mingw_rename(const char *pold, const char *pnew)
20972097
return 0;
20982098
gle = GetLastError();
20992099

2100+
if (gle == ERROR_ACCESS_DENIED && is_inside_windows_container()) {
2101+
/* Fall back to copy to destination & remove source */
2102+
if (CopyFileW(wpold, wpnew, FALSE) && !mingw_unlink(pold))
2103+
return 0;
2104+
gle = GetLastError();
2105+
}
2106+
21002107
/* revert file attributes on failure */
21012108
if (attrs != INVALID_FILE_ATTRIBUTES)
21022109
SetFileAttributesW(wpnew, attrs);
@@ -3043,3 +3050,62 @@ const char *program_data_config(void)
30433050
}
30443051
return *path.buf ? path.buf : NULL;
30453052
}
3053+
3054+
/*
3055+
* Based on https://stackoverflow.com/questions/43002803
3056+
*
3057+
* [HKLM\SYSTEM\CurrentControlSet\Services\cexecsvc]
3058+
* "DisplayName"="@%systemroot%\\system32\\cexecsvc.exe,-100"
3059+
* "ErrorControl"=dword:00000001
3060+
* "ImagePath"=hex(2):25,00,73,00,79,00,73,00,74,00,65,00,6d,00,72,00,6f,00,
3061+
* 6f,00,74,00,25,00,5c,00,73,00,79,00,73,00,74,00,65,00,6d,00,33,00,32,00,
3062+
* 5c,00,63,00,65,00,78,00,65,00,63,00,73,00,76,00,63,00,2e,00,65,00,78,00,
3063+
* 65,00,00,00
3064+
* "Start"=dword:00000002
3065+
* "Type"=dword:00000010
3066+
* "Description"="@%systemroot%\\system32\\cexecsvc.exe,-101"
3067+
* "ObjectName"="LocalSystem"
3068+
* "ServiceSidType"=dword:00000001
3069+
*/
3070+
int is_inside_windows_container(void)
3071+
{
3072+
static int inside_container = -1; /* -1 uninitialized */
3073+
const char *key = "SYSTEM\\CurrentControlSet\\Services\\cexecsvc";
3074+
HKEY handle = NULL;
3075+
3076+
if (inside_container != -1)
3077+
return inside_container;
3078+
3079+
inside_container = ERROR_SUCCESS ==
3080+
RegOpenKeyExA(HKEY_LOCAL_MACHINE, key, 0, KEY_READ, &handle);
3081+
RegCloseKey(handle);
3082+
3083+
return inside_container;
3084+
}
3085+
3086+
int file_attr_to_st_mode (DWORD attr, DWORD tag, const char *path)
3087+
{
3088+
int fMode = S_IREAD;
3089+
if ((attr & FILE_ATTRIBUTE_REPARSE_POINT) &&
3090+
tag == IO_REPARSE_TAG_SYMLINK) {
3091+
int flag = S_IFLNK;
3092+
char buf[MAX_LONG_PATH];
3093+
3094+
/*
3095+
* Windows containers' mapped volumes are marked as reparse
3096+
* points and look like symbolic links, but they are not.
3097+
*/
3098+
if (path && is_inside_windows_container() &&
3099+
readlink(path, buf, sizeof(buf)) > 27 &&
3100+
starts_with(buf, "/ContainerMappedDirectories/"))
3101+
flag = S_IFDIR;
3102+
3103+
fMode |= flag;
3104+
} else if (attr & FILE_ATTRIBUTE_DIRECTORY)
3105+
fMode |= S_IFDIR;
3106+
else
3107+
fMode |= S_IFREG;
3108+
if (!(attr & FILE_ATTRIBUTE_READONLY))
3109+
fMode |= S_IWRITE;
3110+
return fMode;
3111+
}

compat/mingw.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,3 +678,8 @@ extern void open_in_gdb(void);
678678
* Used by Pthread API implementation for Windows
679679
*/
680680
extern int err_win_to_posix(DWORD winerr);
681+
682+
/*
683+
* Check current process is inside Windows Container.
684+
*/
685+
extern int is_inside_windows_container(void);

compat/win32.h

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,7 @@
66
#include <windows.h>
77
#endif
88

9-
static inline int file_attr_to_st_mode (DWORD attr, DWORD tag)
10-
{
11-
int fMode = S_IREAD;
12-
if ((attr & FILE_ATTRIBUTE_REPARSE_POINT) && tag == IO_REPARSE_TAG_SYMLINK)
13-
fMode |= S_IFLNK;
14-
else if (attr & FILE_ATTRIBUTE_DIRECTORY)
15-
fMode |= S_IFDIR;
16-
else
17-
fMode |= S_IFREG;
18-
if (!(attr & FILE_ATTRIBUTE_READONLY))
19-
fMode |= S_IWRITE;
20-
return fMode;
21-
}
9+
extern int file_attr_to_st_mode (DWORD attr, DWORD tag, const char *path);
2210

2311
static inline int get_file_attr(const char *fname, WIN32_FILE_ATTRIBUTE_DATA *fdata)
2412
{

compat/win32/fscache.c

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,30 @@ static struct fsentry *fseentry_create_entry(struct fsentry *list,
150150

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

153+
/*
154+
* On certain Windows versions, host directories mapped into
155+
* Windows Containers ("Volumes", see https://docs.docker.com/storage/volumes/)
156+
* look like symbolic links, but their targets are paths that
157+
* are valid only in kernel mode.
158+
*
159+
* Let's work around this by detecting that situation and
160+
* telling Git that these are *not* symbolic links.
161+
*/
162+
if (fdata->dwReserved0 == IO_REPARSE_TAG_SYMLINK &&
163+
sizeof(buf) > (list ? list->len + 1 : 0) + fse->len + 1 &&
164+
is_inside_windows_container()) {
165+
size_t off = 0;
166+
if (list) {
167+
memcpy(buf, list->name, list->len);
168+
buf[list->len] = '/';
169+
off = list->len + 1;
170+
}
171+
memcpy(buf + off, fse->name, fse->len);
172+
buf[off + fse->len] = '\0';
173+
}
174+
153175
fse->st_mode = file_attr_to_st_mode(fdata->dwFileAttributes,
154-
fdata->dwReserved0);
176+
fdata->dwReserved0, buf);
155177
fse->st_size = S_ISLNK(fse->st_mode) ? MAX_LONG_PATH :
156178
fdata->nFileSizeLow | (((off_t) fdata->nFileSizeHigh) << 32);
157179
filetime_to_timespec(&(fdata->ftLastAccessTime), &(fse->st_atim));

0 commit comments

Comments
 (0)