Skip to content

Commit fb3fca1

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 7fee55e + 1625ef4 commit fb3fca1

File tree

4 files changed

+88
-16
lines changed

4 files changed

+88
-16
lines changed

compat/mingw.c

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -853,7 +853,7 @@ int mingw_lstat(const char *file_name, struct stat *buf)
853853
buf->st_uid = 0;
854854
buf->st_nlink = 1;
855855
buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes,
856-
findbuf.dwReserved0);
856+
findbuf.dwReserved0, file_name);
857857
buf->st_size = S_ISLNK(buf->st_mode) ? MAX_LONG_PATH :
858858
fdata.nFileSizeLow | (((off_t) fdata.nFileSizeHigh) << 32);
859859
buf->st_dev = buf->st_rdev = 0; /* not used by Git */
@@ -902,7 +902,7 @@ static int get_file_info_by_handle(HANDLE hnd, struct stat *buf)
902902
buf->st_dev = buf->st_rdev = 0; /* not used by Git */
903903
buf->st_gid = buf->st_uid = 0;
904904
buf->st_nlink = 1;
905-
buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes, 0);
905+
buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes, 0, NULL);
906906
buf->st_size = fdata.nFileSizeLow | (((off_t) fdata.nFileSizeHigh) << 32);
907907
filetime_to_timespec(&(fdata.ftLastAccessTime), &(buf->st_atim));
908908
filetime_to_timespec(&(fdata.ftLastWriteTime), &(buf->st_mtim));
@@ -2130,6 +2130,13 @@ int mingw_rename(const char *pold, const char *pnew)
21302130
return 0;
21312131
gle = GetLastError();
21322132

2133+
if (gle == ERROR_ACCESS_DENIED && is_inside_windows_container()) {
2134+
/* Fall back to copy to destination & remove source */
2135+
if (CopyFileW(wpold, wpnew, FALSE) && !mingw_unlink(pold))
2136+
return 0;
2137+
gle = GetLastError();
2138+
}
2139+
21332140
/* revert file attributes on failure */
21342141
if (attrs != INVALID_FILE_ATTRIBUTES)
21352142
SetFileAttributesW(wpnew, attrs);
@@ -3061,3 +3068,62 @@ const char *program_data_config(void)
30613068
}
30623069
return *path.buf ? path.buf : NULL;
30633070
}
3071+
3072+
/*
3073+
* Based on https://stackoverflow.com/questions/43002803
3074+
*
3075+
* [HKLM\SYSTEM\CurrentControlSet\Services\cexecsvc]
3076+
* "DisplayName"="@%systemroot%\\system32\\cexecsvc.exe,-100"
3077+
* "ErrorControl"=dword:00000001
3078+
* "ImagePath"=hex(2):25,00,73,00,79,00,73,00,74,00,65,00,6d,00,72,00,6f,00,
3079+
* 6f,00,74,00,25,00,5c,00,73,00,79,00,73,00,74,00,65,00,6d,00,33,00,32,00,
3080+
* 5c,00,63,00,65,00,78,00,65,00,63,00,73,00,76,00,63,00,2e,00,65,00,78,00,
3081+
* 65,00,00,00
3082+
* "Start"=dword:00000002
3083+
* "Type"=dword:00000010
3084+
* "Description"="@%systemroot%\\system32\\cexecsvc.exe,-101"
3085+
* "ObjectName"="LocalSystem"
3086+
* "ServiceSidType"=dword:00000001
3087+
*/
3088+
int is_inside_windows_container(void)
3089+
{
3090+
static int inside_container = -1; /* -1 uninitialized */
3091+
const char *key = "SYSTEM\\CurrentControlSet\\Services\\cexecsvc";
3092+
HKEY handle = NULL;
3093+
3094+
if (inside_container != -1)
3095+
return inside_container;
3096+
3097+
inside_container = ERROR_SUCCESS ==
3098+
RegOpenKeyEx(HKEY_LOCAL_MACHINE, key, 0, KEY_READ, &handle);
3099+
RegCloseKey(handle);
3100+
3101+
return inside_container;
3102+
}
3103+
3104+
int file_attr_to_st_mode (DWORD attr, DWORD tag, const char *path)
3105+
{
3106+
int fMode = S_IREAD;
3107+
if ((attr & FILE_ATTRIBUTE_REPARSE_POINT) &&
3108+
tag == IO_REPARSE_TAG_SYMLINK) {
3109+
int flag = S_IFLNK;
3110+
char buf[MAX_LONG_PATH];
3111+
3112+
/*
3113+
* Windows containers' mapped volumes are marked as reparse
3114+
* points and look like symbolic links, but they are not.
3115+
*/
3116+
if (path && is_inside_windows_container() &&
3117+
readlink(path, buf, sizeof(buf)) > 27 &&
3118+
starts_with(buf, "/ContainerMappedDirectories/"))
3119+
flag = S_IFDIR;
3120+
3121+
fMode |= flag;
3122+
} else if (attr & FILE_ATTRIBUTE_DIRECTORY)
3123+
fMode |= S_IFDIR;
3124+
else
3125+
fMode |= S_IFREG;
3126+
if (!(attr & FILE_ATTRIBUTE_READONLY))
3127+
fMode |= S_IWRITE;
3128+
return fMode;
3129+
}

compat/mingw.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,3 +673,8 @@ static int mingw_main(c,v)
673673
* Used by Pthread API implementation for Windows
674674
*/
675675
extern int err_win_to_posix(DWORD winerr);
676+
677+
/*
678+
* Check current process is inside Windows Container.
679+
*/
680+
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)