Skip to content

Commit ff22f8c

Browse files
committed
Merge pull request git-for-windows#1645 from ZCube/master
Support windows container.
2 parents dc9551f + f14fd6c commit ff22f8c

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
@@ -855,7 +855,7 @@ int mingw_lstat(const char *file_name, struct stat *buf)
855855
buf->st_uid = 0;
856856
buf->st_nlink = 1;
857857
buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes,
858-
findbuf.dwReserved0);
858+
findbuf.dwReserved0, file_name);
859859
buf->st_size = S_ISLNK(buf->st_mode) ? MAX_LONG_PATH :
860860
fdata.nFileSizeLow | (((off_t) fdata.nFileSizeHigh) << 32);
861861
buf->st_dev = buf->st_rdev = 0; /* not used by Git */
@@ -904,7 +904,7 @@ static int get_file_info_by_handle(HANDLE hnd, struct stat *buf)
904904
buf->st_dev = buf->st_rdev = 0; /* not used by Git */
905905
buf->st_gid = buf->st_uid = 0;
906906
buf->st_nlink = 1;
907-
buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes, 0);
907+
buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes, 0, NULL);
908908
buf->st_size = fdata.nFileSizeLow | (((off_t) fdata.nFileSizeHigh) << 32);
909909
filetime_to_timespec(&(fdata.ftLastAccessTime), &(buf->st_atim));
910910
filetime_to_timespec(&(fdata.ftLastWriteTime), &(buf->st_mtim));
@@ -2516,6 +2516,13 @@ int mingw_rename(const char *pold, const char *pnew)
25162516
return 0;
25172517
gle = GetLastError();
25182518

2519+
if (gle == ERROR_ACCESS_DENIED && is_inside_windows_container()) {
2520+
/* Fall back to copy to destination & remove source */
2521+
if (CopyFileW(wpold, wpnew, FALSE) && !mingw_unlink(pold))
2522+
return 0;
2523+
gle = GetLastError();
2524+
}
2525+
25192526
/* revert file attributes on failure */
25202527
if (attrs != INVALID_FILE_ATTRIBUTES)
25212528
SetFileAttributesW(wpnew, attrs);
@@ -3593,3 +3600,62 @@ const char *program_data_config(void)
35933600
}
35943601
return *path.buf ? path.buf : NULL;
35953602
}
3603+
3604+
/*
3605+
* Based on https://stackoverflow.com/questions/43002803
3606+
*
3607+
* [HKLM\SYSTEM\CurrentControlSet\Services\cexecsvc]
3608+
* "DisplayName"="@%systemroot%\\system32\\cexecsvc.exe,-100"
3609+
* "ErrorControl"=dword:00000001
3610+
* "ImagePath"=hex(2):25,00,73,00,79,00,73,00,74,00,65,00,6d,00,72,00,6f,00,
3611+
* 6f,00,74,00,25,00,5c,00,73,00,79,00,73,00,74,00,65,00,6d,00,33,00,32,00,
3612+
* 5c,00,63,00,65,00,78,00,65,00,63,00,73,00,76,00,63,00,2e,00,65,00,78,00,
3613+
* 65,00,00,00
3614+
* "Start"=dword:00000002
3615+
* "Type"=dword:00000010
3616+
* "Description"="@%systemroot%\\system32\\cexecsvc.exe,-101"
3617+
* "ObjectName"="LocalSystem"
3618+
* "ServiceSidType"=dword:00000001
3619+
*/
3620+
int is_inside_windows_container(void)
3621+
{
3622+
static int inside_container = -1; /* -1 uninitialized */
3623+
const char *key = "SYSTEM\\CurrentControlSet\\Services\\cexecsvc";
3624+
HKEY handle = NULL;
3625+
3626+
if (inside_container != -1)
3627+
return inside_container;
3628+
3629+
inside_container = ERROR_SUCCESS ==
3630+
RegOpenKeyEx(HKEY_LOCAL_MACHINE, key, 0, KEY_READ, &handle);
3631+
RegCloseKey(handle);
3632+
3633+
return inside_container;
3634+
}
3635+
3636+
int file_attr_to_st_mode (DWORD attr, DWORD tag, const char *path)
3637+
{
3638+
int fMode = S_IREAD;
3639+
if ((attr & FILE_ATTRIBUTE_REPARSE_POINT) &&
3640+
tag == IO_REPARSE_TAG_SYMLINK) {
3641+
int flag = S_IFLNK;
3642+
char buf[MAX_LONG_PATH];
3643+
3644+
/*
3645+
* Windows containers' mapped volumes are marked as reparse
3646+
* points and look like symbolic links, but they are not.
3647+
*/
3648+
if (path && is_inside_windows_container() &&
3649+
readlink(path, buf, sizeof(buf)) > 27 &&
3650+
starts_with(buf, "/ContainerMappedDirectories/"))
3651+
flag = S_IFDIR;
3652+
3653+
fMode |= flag;
3654+
} else if (attr & FILE_ATTRIBUTE_DIRECTORY)
3655+
fMode |= S_IFDIR;
3656+
else
3657+
fMode |= S_IFREG;
3658+
if (!(attr & FILE_ATTRIBUTE_READONLY))
3659+
fMode |= S_IWRITE;
3660+
return fMode;
3661+
}

compat/mingw.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,3 +737,8 @@ static int mingw_main(c,v)
737737
* Used by Pthread API implementation for Windows
738738
*/
739739
extern int err_win_to_posix(DWORD winerr);
740+
741+
/*
742+
* Check current process is inside Windows Container.
743+
*/
744+
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)