Skip to content

Commit 3b22d56

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 <[email protected]>
2 parents 9a8203e + c27ae17 commit 3b22d56

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
@@ -895,7 +895,7 @@ int mingw_lstat(const char *file_name, struct stat *buf)
895895
buf->st_uid = 0;
896896
buf->st_nlink = 1;
897897
buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes,
898-
findbuf.dwReserved0);
898+
findbuf.dwReserved0, file_name);
899899
buf->st_size = S_ISLNK(buf->st_mode) ? MAX_LONG_PATH :
900900
fdata.nFileSizeLow | (((off_t) fdata.nFileSizeHigh) << 32);
901901
buf->st_dev = buf->st_rdev = 0; /* not used by Git */
@@ -946,7 +946,7 @@ static int get_file_info_by_handle(HANDLE hnd, struct stat *buf)
946946
buf->st_gid = 0;
947947
buf->st_uid = 0;
948948
buf->st_nlink = 1;
949-
buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes, 0);
949+
buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes, 0, NULL);
950950
buf->st_size = fdata.nFileSizeLow |
951951
(((off_t)fdata.nFileSizeHigh)<<32);
952952
buf->st_dev = buf->st_rdev = 0; /* not used by Git */
@@ -2044,6 +2044,13 @@ int mingw_rename(const char *pold, const char *pnew)
20442044
return 0;
20452045
gle = GetLastError();
20462046

2047+
if (gle == ERROR_ACCESS_DENIED && is_inside_windows_container()) {
2048+
/* Fall back to copy to destination & remove source */
2049+
if (CopyFileW(wpold, wpnew, FALSE) && !mingw_unlink(pold))
2050+
return 0;
2051+
gle = GetLastError();
2052+
}
2053+
20472054
/* revert file attributes on failure */
20482055
if (attrs != INVALID_FILE_ATTRIBUTES)
20492056
SetFileAttributesW(wpnew, attrs);
@@ -2981,3 +2988,62 @@ const char *program_data_config(void)
29812988
}
29822989
return *path.buf ? path.buf : NULL;
29832990
}
2991+
2992+
/*
2993+
* Based on https://stackoverflow.com/questions/43002803
2994+
*
2995+
* [HKLM\SYSTEM\CurrentControlSet\Services\cexecsvc]
2996+
* "DisplayName"="@%systemroot%\\system32\\cexecsvc.exe,-100"
2997+
* "ErrorControl"=dword:00000001
2998+
* "ImagePath"=hex(2):25,00,73,00,79,00,73,00,74,00,65,00,6d,00,72,00,6f,00,
2999+
* 6f,00,74,00,25,00,5c,00,73,00,79,00,73,00,74,00,65,00,6d,00,33,00,32,00,
3000+
* 5c,00,63,00,65,00,78,00,65,00,63,00,73,00,76,00,63,00,2e,00,65,00,78,00,
3001+
* 65,00,00,00
3002+
* "Start"=dword:00000002
3003+
* "Type"=dword:00000010
3004+
* "Description"="@%systemroot%\\system32\\cexecsvc.exe,-101"
3005+
* "ObjectName"="LocalSystem"
3006+
* "ServiceSidType"=dword:00000001
3007+
*/
3008+
int is_inside_windows_container(void)
3009+
{
3010+
static int inside_container = -1; /* -1 uninitialized */
3011+
const char *key = "SYSTEM\\CurrentControlSet\\Services\\cexecsvc";
3012+
HKEY handle = NULL;
3013+
3014+
if (inside_container != -1)
3015+
return inside_container;
3016+
3017+
inside_container = ERROR_SUCCESS ==
3018+
RegOpenKeyExA(HKEY_LOCAL_MACHINE, key, 0, KEY_READ, &handle);
3019+
RegCloseKey(handle);
3020+
3021+
return inside_container;
3022+
}
3023+
3024+
int file_attr_to_st_mode (DWORD attr, DWORD tag, const char *path)
3025+
{
3026+
int fMode = S_IREAD;
3027+
if ((attr & FILE_ATTRIBUTE_REPARSE_POINT) &&
3028+
tag == IO_REPARSE_TAG_SYMLINK) {
3029+
int flag = S_IFLNK;
3030+
char buf[MAX_LONG_PATH];
3031+
3032+
/*
3033+
* Windows containers' mapped volumes are marked as reparse
3034+
* points and look like symbolic links, but they are not.
3035+
*/
3036+
if (path && is_inside_windows_container() &&
3037+
readlink(path, buf, sizeof(buf)) > 27 &&
3038+
starts_with(buf, "/ContainerMappedDirectories/"))
3039+
flag = S_IFDIR;
3040+
3041+
fMode |= flag;
3042+
} else if (attr & FILE_ATTRIBUTE_DIRECTORY)
3043+
fMode |= S_IFDIR;
3044+
else
3045+
fMode |= S_IFREG;
3046+
if (!(attr & FILE_ATTRIBUTE_READONLY))
3047+
fMode |= S_IWRITE;
3048+
return fMode;
3049+
}

compat/mingw.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,3 +677,8 @@ extern void open_in_gdb(void);
677677
* Used by Pthread API implementation for Windows
678678
*/
679679
extern int err_win_to_posix(DWORD winerr);
680+
681+
/*
682+
* Check current process is inside Windows Container.
683+
*/
684+
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
@@ -149,8 +149,30 @@ static struct fsentry *fseentry_create_entry(struct fsentry *list,
149149

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

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

0 commit comments

Comments
 (0)