Skip to content

Commit bb52f38

Browse files
committed
Merge pull request #1645 from ZCube/master
Support windows container.
2 parents e052b45 + a833d4b commit bb52f38

File tree

4 files changed

+88
-16
lines changed

4 files changed

+88
-16
lines changed

compat/mingw.c

+68-2
Original file line numberDiff line numberDiff line change
@@ -818,7 +818,7 @@ int mingw_lstat(const char *file_name, struct stat *buf)
818818
buf->st_uid = 0;
819819
buf->st_nlink = 1;
820820
buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes,
821-
findbuf.dwReserved0);
821+
findbuf.dwReserved0, file_name);
822822
buf->st_size = S_ISLNK(buf->st_mode) ? MAX_LONG_PATH :
823823
fdata.nFileSizeLow | (((off_t) fdata.nFileSizeHigh) << 32);
824824
buf->st_dev = buf->st_rdev = 0; /* not used by Git */
@@ -867,7 +867,7 @@ static int get_file_info_by_handle(HANDLE hnd, struct stat *buf)
867867
buf->st_dev = buf->st_rdev = 0; /* not used by Git */
868868
buf->st_gid = buf->st_uid = 0;
869869
buf->st_nlink = 1;
870-
buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes, 0);
870+
buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes, 0, NULL);
871871
buf->st_size = fdata.nFileSizeLow | (((off_t) fdata.nFileSizeHigh) << 32);
872872
filetime_to_timespec(&(fdata.ftLastAccessTime), &(buf->st_atim));
873873
filetime_to_timespec(&(fdata.ftLastWriteTime), &(buf->st_mtim));
@@ -2479,6 +2479,13 @@ int mingw_rename(const char *pold, const char *pnew)
24792479
return 0;
24802480
gle = GetLastError();
24812481

2482+
if (gle == ERROR_ACCESS_DENIED && is_inside_windows_container()) {
2483+
/* Fall back to copy to destination & remove source */
2484+
if (CopyFileW(wpold, wpnew, FALSE) && !mingw_unlink(pold))
2485+
return 0;
2486+
gle = GetLastError();
2487+
}
2488+
24822489
/* revert file attributes on failure */
24832490
if (attrs != INVALID_FILE_ATTRIBUTES)
24842491
SetFileAttributesW(wpnew, attrs);
@@ -3556,3 +3563,62 @@ const char *program_data_config(void)
35563563
}
35573564
return *path.buf ? path.buf : NULL;
35583565
}
3566+
3567+
/*
3568+
* Based on https://stackoverflow.com/questions/43002803
3569+
*
3570+
* [HKLM\SYSTEM\CurrentControlSet\Services\cexecsvc]
3571+
* "DisplayName"="@%systemroot%\\system32\\cexecsvc.exe,-100"
3572+
* "ErrorControl"=dword:00000001
3573+
* "ImagePath"=hex(2):25,00,73,00,79,00,73,00,74,00,65,00,6d,00,72,00,6f,00,
3574+
* 6f,00,74,00,25,00,5c,00,73,00,79,00,73,00,74,00,65,00,6d,00,33,00,32,00,
3575+
* 5c,00,63,00,65,00,78,00,65,00,63,00,73,00,76,00,63,00,2e,00,65,00,78,00,
3576+
* 65,00,00,00
3577+
* "Start"=dword:00000002
3578+
* "Type"=dword:00000010
3579+
* "Description"="@%systemroot%\\system32\\cexecsvc.exe,-101"
3580+
* "ObjectName"="LocalSystem"
3581+
* "ServiceSidType"=dword:00000001
3582+
*/
3583+
int is_inside_windows_container(void)
3584+
{
3585+
static int inside_container = -1; /* -1 uninitialized */
3586+
const char *key = "SYSTEM\\CurrentControlSet\\Services\\cexecsvc";
3587+
HKEY handle = NULL;
3588+
3589+
if (inside_container != -1)
3590+
return inside_container;
3591+
3592+
inside_container = ERROR_SUCCESS ==
3593+
RegOpenKeyEx(HKEY_LOCAL_MACHINE, key, 0, KEY_READ, &handle);
3594+
RegCloseKey(handle);
3595+
3596+
return inside_container;
3597+
}
3598+
3599+
int file_attr_to_st_mode (DWORD attr, DWORD tag, const char *path)
3600+
{
3601+
int fMode = S_IREAD;
3602+
if ((attr & FILE_ATTRIBUTE_REPARSE_POINT) &&
3603+
tag == IO_REPARSE_TAG_SYMLINK) {
3604+
int flag = S_IFLNK;
3605+
char buf[MAX_LONG_PATH];
3606+
3607+
/*
3608+
* Windows containers' mapped volumes are marked as reparse
3609+
* points and look like symbolic links, but they are not.
3610+
*/
3611+
if (path && is_inside_windows_container() &&
3612+
readlink(path, buf, sizeof(buf)) > 27 &&
3613+
starts_with(buf, "/ContainerMappedDirectories/"))
3614+
flag = S_IFDIR;
3615+
3616+
fMode |= flag;
3617+
} else if (attr & FILE_ATTRIBUTE_DIRECTORY)
3618+
fMode |= S_IFDIR;
3619+
else
3620+
fMode |= S_IFREG;
3621+
if (!(attr & FILE_ATTRIBUTE_READONLY))
3622+
fMode |= S_IWRITE;
3623+
return fMode;
3624+
}

compat/mingw.h

+5
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

+1-13
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

+14-1
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)