Skip to content

Commit d578ab4

Browse files
committed
Merge 'docker-volumes-are-no-symlinks'
This was pull request git-for-windows#1645 from ZCube/master Support windows container. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
2 parents 3fd1723 + a922a70 commit d578ab4

4 files changed

Lines changed: 88 additions & 16 deletions

File tree

compat/mingw.c

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -930,7 +930,7 @@ int mingw_lstat(const char *file_name, struct stat *buf)
930930
buf->st_uid = 0;
931931
buf->st_nlink = 1;
932932
buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes,
933-
findbuf.dwReserved0);
933+
findbuf.dwReserved0, file_name);
934934
buf->st_size = S_ISLNK(buf->st_mode) ? MAX_LONG_PATH :
935935
fdata.nFileSizeLow | (((off_t) fdata.nFileSizeHigh) << 32);
936936
buf->st_dev = buf->st_rdev = 0; /* not used by Git */
@@ -981,7 +981,7 @@ static int get_file_info_by_handle(HANDLE hnd, struct stat *buf)
981981
buf->st_gid = 0;
982982
buf->st_uid = 0;
983983
buf->st_nlink = 1;
984-
buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes, 0);
984+
buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes, 0, NULL);
985985
buf->st_size = fdata.nFileSizeLow |
986986
(((off_t)fdata.nFileSizeHigh)<<32);
987987
buf->st_dev = buf->st_rdev = 0; /* not used by Git */
@@ -2233,6 +2233,13 @@ int mingw_rename(const char *pold, const char *pnew)
22332233
return 0;
22342234
gle = GetLastError();
22352235

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

compat/mingw.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,3 +695,8 @@ int main(int argc, const char **argv);
695695
* Used by Pthread API implementation for Windows
696696
*/
697697
extern int err_win_to_posix(DWORD winerr);
698+
699+
/*
700+
* Check current process is inside Windows Container.
701+
*/
702+
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: 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)