Skip to content

Commit a4807b6

Browse files
committed
Merge branch 'docker-volumes-are-no-symlinks'
Signed-off-by: Johannes Schindelin <[email protected]>
2 parents 0024631 + adad93b commit a4807b6

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
@@ -946,7 +946,7 @@ int mingw_lstat(const char *file_name, struct stat *buf)
946946
buf->st_uid = 0;
947947
buf->st_nlink = 1;
948948
buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes,
949-
findbuf.dwReserved0);
949+
findbuf.dwReserved0, file_name);
950950
buf->st_size = S_ISLNK(buf->st_mode) ? MAX_LONG_PATH :
951951
fdata.nFileSizeLow | (((off_t) fdata.nFileSizeHigh) << 32);
952952
buf->st_dev = buf->st_rdev = 0; /* not used by Git */
@@ -997,7 +997,7 @@ static int get_file_info_by_handle(HANDLE hnd, struct stat *buf)
997997
buf->st_gid = 0;
998998
buf->st_uid = 0;
999999
buf->st_nlink = 1;
1000-
buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes, 0);
1000+
buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes, 0, NULL);
10011001
buf->st_size = fdata.nFileSizeLow |
10021002
(((off_t)fdata.nFileSizeHigh)<<32);
10031003
buf->st_dev = buf->st_rdev = 0; /* not used by Git */
@@ -2251,6 +2251,13 @@ int mingw_rename(const char *pold, const char *pnew)
22512251
return 0;
22522252
gle = GetLastError();
22532253

2254+
if (gle == ERROR_ACCESS_DENIED && is_inside_windows_container()) {
2255+
/* Fall back to copy to destination & remove source */
2256+
if (CopyFileW(wpold, wpnew, FALSE) && !mingw_unlink(pold))
2257+
return 0;
2258+
gle = GetLastError();
2259+
}
2260+
22542261
/* revert file attributes on failure */
22552262
if (attrs != INVALID_FILE_ATTRIBUTES)
22562263
SetFileAttributesW(wpnew, attrs);
@@ -3200,3 +3207,62 @@ const char *program_data_config(void)
32003207
}
32013208
return *path.buf ? path.buf : NULL;
32023209
}
3210+
3211+
/*
3212+
* Based on https://stackoverflow.com/questions/43002803
3213+
*
3214+
* [HKLM\SYSTEM\CurrentControlSet\Services\cexecsvc]
3215+
* "DisplayName"="@%systemroot%\\system32\\cexecsvc.exe,-100"
3216+
* "ErrorControl"=dword:00000001
3217+
* "ImagePath"=hex(2):25,00,73,00,79,00,73,00,74,00,65,00,6d,00,72,00,6f,00,
3218+
* 6f,00,74,00,25,00,5c,00,73,00,79,00,73,00,74,00,65,00,6d,00,33,00,32,00,
3219+
* 5c,00,63,00,65,00,78,00,65,00,63,00,73,00,76,00,63,00,2e,00,65,00,78,00,
3220+
* 65,00,00,00
3221+
* "Start"=dword:00000002
3222+
* "Type"=dword:00000010
3223+
* "Description"="@%systemroot%\\system32\\cexecsvc.exe,-101"
3224+
* "ObjectName"="LocalSystem"
3225+
* "ServiceSidType"=dword:00000001
3226+
*/
3227+
int is_inside_windows_container(void)
3228+
{
3229+
static int inside_container = -1; /* -1 uninitialized */
3230+
const char *key = "SYSTEM\\CurrentControlSet\\Services\\cexecsvc";
3231+
HKEY handle = NULL;
3232+
3233+
if (inside_container != -1)
3234+
return inside_container;
3235+
3236+
inside_container = ERROR_SUCCESS ==
3237+
RegOpenKeyExA(HKEY_LOCAL_MACHINE, key, 0, KEY_READ, &handle);
3238+
RegCloseKey(handle);
3239+
3240+
return inside_container;
3241+
}
3242+
3243+
int file_attr_to_st_mode (DWORD attr, DWORD tag, const char *path)
3244+
{
3245+
int fMode = S_IREAD;
3246+
if ((attr & FILE_ATTRIBUTE_REPARSE_POINT) &&
3247+
tag == IO_REPARSE_TAG_SYMLINK) {
3248+
int flag = S_IFLNK;
3249+
char buf[MAX_LONG_PATH];
3250+
3251+
/*
3252+
* Windows containers' mapped volumes are marked as reparse
3253+
* points and look like symbolic links, but they are not.
3254+
*/
3255+
if (path && is_inside_windows_container() &&
3256+
readlink(path, buf, sizeof(buf)) > 27 &&
3257+
starts_with(buf, "/ContainerMappedDirectories/"))
3258+
flag = S_IFDIR;
3259+
3260+
fMode |= flag;
3261+
} else if (attr & FILE_ATTRIBUTE_DIRECTORY)
3262+
fMode |= S_IFDIR;
3263+
else
3264+
fMode |= S_IFREG;
3265+
if (!(attr & FILE_ATTRIBUTE_READONLY))
3266+
fMode |= S_IWRITE;
3267+
return fMode;
3268+
}

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
@@ -158,8 +158,30 @@ static struct fsentry *fseentry_create_entry(struct fscache *cache, struct fsent
158158

159159
fse = fsentry_alloc(cache, list, buf, len);
160160

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

0 commit comments

Comments
 (0)