Skip to content

Commit 8e6d524

Browse files
committed
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 f835ff4 + 8b77f7b commit 8e6d524

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
@@ -919,7 +919,7 @@ int mingw_lstat(const char *file_name, struct stat *buf)
919919
buf->st_uid = 0;
920920
buf->st_nlink = 1;
921921
buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes,
922-
findbuf.dwReserved0);
922+
findbuf.dwReserved0, file_name);
923923
buf->st_size = S_ISLNK(buf->st_mode) ? MAX_LONG_PATH :
924924
fdata.nFileSizeLow | (((off_t) fdata.nFileSizeHigh) << 32);
925925
buf->st_dev = buf->st_rdev = 0; /* not used by Git */
@@ -970,7 +970,7 @@ static int get_file_info_by_handle(HANDLE hnd, struct stat *buf)
970970
buf->st_gid = 0;
971971
buf->st_uid = 0;
972972
buf->st_nlink = 1;
973-
buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes, 0);
973+
buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes, 0, NULL);
974974
buf->st_size = fdata.nFileSizeLow |
975975
(((off_t)fdata.nFileSizeHigh)<<32);
976976
buf->st_dev = buf->st_rdev = 0; /* not used by Git */
@@ -2180,6 +2180,13 @@ int mingw_rename(const char *pold, const char *pnew)
21802180
return 0;
21812181
gle = GetLastError();
21822182

2183+
if (gle == ERROR_ACCESS_DENIED && is_inside_windows_container()) {
2184+
/* Fall back to copy to destination & remove source */
2185+
if (CopyFileW(wpold, wpnew, FALSE) && !mingw_unlink(pold))
2186+
return 0;
2187+
gle = GetLastError();
2188+
}
2189+
21832190
/* revert file attributes on failure */
21842191
if (attrs != INVALID_FILE_ATTRIBUTES)
21852192
SetFileAttributesW(wpnew, attrs);
@@ -3137,3 +3144,62 @@ int uname(struct utsname *buf)
31373144
"%u", (v >> 16) & 0x7fff);
31383145
return 0;
31393146
}
3147+
3148+
/*
3149+
* Based on https://stackoverflow.com/questions/43002803
3150+
*
3151+
* [HKLM\SYSTEM\CurrentControlSet\Services\cexecsvc]
3152+
* "DisplayName"="@%systemroot%\\system32\\cexecsvc.exe,-100"
3153+
* "ErrorControl"=dword:00000001
3154+
* "ImagePath"=hex(2):25,00,73,00,79,00,73,00,74,00,65,00,6d,00,72,00,6f,00,
3155+
* 6f,00,74,00,25,00,5c,00,73,00,79,00,73,00,74,00,65,00,6d,00,33,00,32,00,
3156+
* 5c,00,63,00,65,00,78,00,65,00,63,00,73,00,76,00,63,00,2e,00,65,00,78,00,
3157+
* 65,00,00,00
3158+
* "Start"=dword:00000002
3159+
* "Type"=dword:00000010
3160+
* "Description"="@%systemroot%\\system32\\cexecsvc.exe,-101"
3161+
* "ObjectName"="LocalSystem"
3162+
* "ServiceSidType"=dword:00000001
3163+
*/
3164+
int is_inside_windows_container(void)
3165+
{
3166+
static int inside_container = -1; /* -1 uninitialized */
3167+
const char *key = "SYSTEM\\CurrentControlSet\\Services\\cexecsvc";
3168+
HKEY handle = NULL;
3169+
3170+
if (inside_container != -1)
3171+
return inside_container;
3172+
3173+
inside_container = ERROR_SUCCESS ==
3174+
RegOpenKeyExA(HKEY_LOCAL_MACHINE, key, 0, KEY_READ, &handle);
3175+
RegCloseKey(handle);
3176+
3177+
return inside_container;
3178+
}
3179+
3180+
int file_attr_to_st_mode (DWORD attr, DWORD tag, const char *path)
3181+
{
3182+
int fMode = S_IREAD;
3183+
if ((attr & FILE_ATTRIBUTE_REPARSE_POINT) &&
3184+
tag == IO_REPARSE_TAG_SYMLINK) {
3185+
int flag = S_IFLNK;
3186+
char buf[MAX_LONG_PATH];
3187+
3188+
/*
3189+
* Windows containers' mapped volumes are marked as reparse
3190+
* points and look like symbolic links, but they are not.
3191+
*/
3192+
if (path && is_inside_windows_container() &&
3193+
readlink(path, buf, sizeof(buf)) > 27 &&
3194+
starts_with(buf, "/ContainerMappedDirectories/"))
3195+
flag = S_IFDIR;
3196+
3197+
fMode |= flag;
3198+
} else if (attr & FILE_ATTRIBUTE_DIRECTORY)
3199+
fMode |= S_IFDIR;
3200+
else
3201+
fMode |= S_IFREG;
3202+
if (!(attr & FILE_ATTRIBUTE_READONLY))
3203+
fMode |= S_IWRITE;
3204+
return fMode;
3205+
}

compat/mingw.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,3 +679,8 @@ extern void open_in_gdb(void);
679679
* Used by Pthread API implementation for Windows
680680
*/
681681
int err_win_to_posix(DWORD winerr);
682+
683+
/*
684+
* Check current process is inside Windows Container.
685+
*/
686+
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
@@ -185,7 +185,29 @@ static struct fsentry *fseentry_create_entry(struct fscache *cache, struct fsent
185185
fdata->FileAttributes & FILE_ATTRIBUTE_REPARSE_POINT ?
186186
fdata->EaSize : 0;
187187

188-
fse->st_mode = file_attr_to_st_mode(fdata->FileAttributes, fdata->EaSize);
188+
/*
189+
* On certain Windows versions, host directories mapped into
190+
* Windows Containers ("Volumes", see https://docs.docker.com/storage/volumes/)
191+
* look like symbolic links, but their targets are paths that
192+
* are valid only in kernel mode.
193+
*
194+
* Let's work around this by detecting that situation and
195+
* telling Git that these are *not* symbolic links.
196+
*/
197+
if (fse->reparse_tag == IO_REPARSE_TAG_SYMLINK &&
198+
sizeof(buf) > (list ? list->len + 1 : 0) + fse->len + 1 &&
199+
is_inside_windows_container()) {
200+
size_t off = 0;
201+
if (list) {
202+
memcpy(buf, list->name, list->len);
203+
buf[list->len] = '/';
204+
off = list->len + 1;
205+
}
206+
memcpy(buf + off, fse->name, fse->len);
207+
buf[off + fse->len] = '\0';
208+
}
209+
210+
fse->st_mode = file_attr_to_st_mode(fdata->FileAttributes, fdata->EaSize, buf);
189211
fse->u.s.st_size = S_ISLNK(fse->st_mode) ? MAX_LONG_PATH :
190212
fdata->EndOfFile.LowPart | (((off_t)fdata->EndOfFile.HighPart) << 32);
191213
filetime_to_timespec((FILETIME *)&(fdata->LastAccessTime), &(fse->u.s.st_atim));

0 commit comments

Comments
 (0)