Skip to content

Commit e6ee2f7

Browse files
authored
Merge pull request #1645 from ZCube/master
Support windows container.
2 parents 82f1b61 + bb654bd commit e6ee2f7

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
@@ -775,7 +775,7 @@ int mingw_lstat(const char *file_name, struct stat *buf)
775775
buf->st_uid = 0;
776776
buf->st_nlink = 1;
777777
buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes,
778-
findbuf.dwReserved0);
778+
findbuf.dwReserved0, file_name);
779779
buf->st_size = S_ISLNK(buf->st_mode) ? MAX_LONG_PATH :
780780
fdata.nFileSizeLow | (((off_t) fdata.nFileSizeHigh) << 32);
781781
buf->st_dev = buf->st_rdev = 0; /* not used by Git */
@@ -824,7 +824,7 @@ static int get_file_info_by_handle(HANDLE hnd, struct stat *buf)
824824
buf->st_dev = buf->st_rdev = 0; /* not used by Git */
825825
buf->st_gid = buf->st_uid = 0;
826826
buf->st_nlink = 1;
827-
buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes, 0);
827+
buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes, 0, NULL);
828828
buf->st_size = fdata.nFileSizeLow | (((off_t) fdata.nFileSizeHigh) << 32);
829829
filetime_to_timespec(&(fdata.ftLastAccessTime), &(buf->st_atim));
830830
filetime_to_timespec(&(fdata.ftLastWriteTime), &(buf->st_mtim));
@@ -2436,6 +2436,13 @@ int mingw_rename(const char *pold, const char *pnew)
24362436
return 0;
24372437
gle = GetLastError();
24382438

2439+
if (gle == ERROR_ACCESS_DENIED && is_inside_windows_container()) {
2440+
/* Fall back to copy to destination & remove source */
2441+
if (CopyFileW(wpold, wpnew, FALSE) && !mingw_unlink(pold))
2442+
return 0;
2443+
gle = GetLastError();
2444+
}
2445+
24392446
/* revert file attributes on failure */
24402447
if (attrs != INVALID_FILE_ATTRIBUTES)
24412448
SetFileAttributesW(wpnew, attrs);
@@ -3513,3 +3520,62 @@ const char *program_data_config(void)
35133520
}
35143521
return *path.buf ? path.buf : NULL;
35153522
}
3523+
3524+
/*
3525+
* Based on https://stackoverflow.com/questions/43002803
3526+
*
3527+
* [HKLM\SYSTEM\CurrentControlSet\Services\cexecsvc]
3528+
* "DisplayName"="@%systemroot%\\system32\\cexecsvc.exe,-100"
3529+
* "ErrorControl"=dword:00000001
3530+
* "ImagePath"=hex(2):25,00,73,00,79,00,73,00,74,00,65,00,6d,00,72,00,6f,00,
3531+
* 6f,00,74,00,25,00,5c,00,73,00,79,00,73,00,74,00,65,00,6d,00,33,00,32,00,
3532+
* 5c,00,63,00,65,00,78,00,65,00,63,00,73,00,76,00,63,00,2e,00,65,00,78,00,
3533+
* 65,00,00,00
3534+
* "Start"=dword:00000002
3535+
* "Type"=dword:00000010
3536+
* "Description"="@%systemroot%\\system32\\cexecsvc.exe,-101"
3537+
* "ObjectName"="LocalSystem"
3538+
* "ServiceSidType"=dword:00000001
3539+
*/
3540+
int is_inside_windows_container(void)
3541+
{
3542+
static int inside_container = -1; /* -1 uninitialized */
3543+
const char *key = "SYSTEM\\CurrentControlSet\\Services\\cexecsvc";
3544+
HKEY handle = NULL;
3545+
3546+
if (inside_container != -1)
3547+
return inside_container;
3548+
3549+
inside_container = ERROR_SUCCESS ==
3550+
RegOpenKeyEx(HKEY_LOCAL_MACHINE, key, 0, KEY_READ, &handle);
3551+
RegCloseKey(handle);
3552+
3553+
return inside_container;
3554+
}
3555+
3556+
int file_attr_to_st_mode (DWORD attr, DWORD tag, const char *path)
3557+
{
3558+
int fMode = S_IREAD;
3559+
if ((attr & FILE_ATTRIBUTE_REPARSE_POINT) &&
3560+
tag == IO_REPARSE_TAG_SYMLINK) {
3561+
int flag = S_IFLNK;
3562+
char buf[MAX_LONG_PATH];
3563+
3564+
/*
3565+
* Windows containers' mapped volumes are marked as reparse
3566+
* points and look like symbolic links, but they are not.
3567+
*/
3568+
if (path && is_inside_windows_container() &&
3569+
!readlink(path, buf, sizeof(buf)) &&
3570+
starts_with(buf, "/ContainerMappedDirectories/"))
3571+
flag = S_IFDIR;
3572+
3573+
fMode |= flag;
3574+
} else if (attr & FILE_ATTRIBUTE_DIRECTORY)
3575+
fMode |= S_IFDIR;
3576+
else
3577+
fMode |= S_IFREG;
3578+
if (!(attr & FILE_ATTRIBUTE_READONLY))
3579+
fMode |= S_IWRITE;
3580+
return fMode;
3581+
}

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)