Skip to content

Commit c4b8f02

Browse files
swiggerGit for Windows Build Agent
authored andcommitted
mingw: optionally enable wsl compability file mode bits
The Windows Subsystem for Linux (WSL) version 2 allows to use `chmod` on NTFS volumes provided that they are mounted with metadata enabled (see https://devblogs.microsoft.com/commandline/chmod-chown-wsl-improvements/ for details), for example: $ chmod 0755 /mnt/d/test/a.sh In order to facilitate better collaboration between the Windows version of Git and the WSL version of Git, we can make the Windows version of Git also support reading and writing NTFS file modes in a manner compatible with WSL. Since this slightly slows down operations where lots of files are created (such as an initial checkout), this feature is only enabled when `core.WSLCompat` is set to true. Note that you also have to set `core.fileMode=true` in repositories that have been initialized without enabling WSL compatibility. There are several ways to enable metadata loading for NTFS volumes in WSL, one of which is to modify `/etc/wsl.conf` by adding: ``` [automount] enabled = true options = "metadata,umask=027,fmask=117" ``` And reboot WSL. It can also be enabled temporarily by this incantation: $ sudo umount /mnt/c && sudo mount -t drvfs C: /mnt/c -o metadata,uid=1000,gid=1000,umask=22,fmask=111 It's important to note that this modification is compatible with, but does not depend on WSL. The helper functions in this commit can operate independently and functions normally on devices where WSL is not installed or properly configured. Signed-off-by: xungeng li <xungeng@gmail.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
1 parent 4b71da0 commit c4b8f02

8 files changed

Lines changed: 193 additions & 2 deletions

File tree

Documentation/config/core.adoc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -801,3 +801,9 @@ core.maxTreeDepth::
801801
to allow Git to abort cleanly, and should not generally need to
802802
be adjusted. When Git is compiled with MSVC, the default is 512.
803803
Otherwise, the default is 2048.
804+
805+
core.WSLCompat::
806+
Tells Git whether to enable wsl compatibility mode.
807+
The default value is false. When set to true, Git will set the mode
808+
bits of the file in the way of wsl, so that the executable flag of
809+
files can be set or read correctly.

compat/mingw.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "win32.h"
1919
#include "win32/fscache.h"
2020
#include "win32/lazyload.h"
21+
#include "win32/wsl.h"
2122
#include "wrapper.h"
2223
#include "write-or-die.h"
2324
#include <aclapi.h>
@@ -986,6 +987,11 @@ int mingw_open (const char *filename, int oflags, ...)
986987
if (fd < 0 && create && GetLastError() == ERROR_ACCESS_DENIED &&
987988
INIT_PROC_ADDR(RtlGetLastNtStatus) && RtlGetLastNtStatus() == STATUS_DELETE_PENDING)
988989
errno = EEXIST;
990+
else if ((oflags & O_CREAT) && fd >= 0 && are_wsl_compatible_mode_bits_enabled()) {
991+
_mode_t wsl_mode = S_IFREG | (mode&0777);
992+
set_wsl_mode_bits_by_handle((HANDLE)_get_osfhandle(fd), wsl_mode);
993+
}
994+
989995
if (fd < 0 && (oflags & O_ACCMODE) != O_RDONLY && errno == EACCES) {
990996
DWORD attrs = GetFileAttributesW(wfilename);
991997
if (attrs != INVALID_FILE_ATTRIBUTES && (attrs & FILE_ATTRIBUTE_DIRECTORY))
@@ -1373,6 +1379,11 @@ int mingw_lstat(const char *file_name, struct stat *buf)
13731379
filetime_to_timespec(&(fdata.ftLastAccessTime), &(buf->st_atim));
13741380
filetime_to_timespec(&(fdata.ftLastWriteTime), &(buf->st_mtim));
13751381
filetime_to_timespec(&(fdata.ftCreationTime), &(buf->st_ctim));
1382+
if (S_ISREG(buf->st_mode) &&
1383+
are_wsl_compatible_mode_bits_enabled()) {
1384+
copy_wsl_mode_bits_from_disk(wfilename, -1,
1385+
&buf->st_mode);
1386+
}
13761387
return 0;
13771388
}
13781389

@@ -1424,6 +1435,8 @@ static int get_file_info_by_handle(HANDLE hnd, struct stat *buf)
14241435
filetime_to_timespec(&(fdata.ftLastAccessTime), &(buf->st_atim));
14251436
filetime_to_timespec(&(fdata.ftLastWriteTime), &(buf->st_mtim));
14261437
filetime_to_timespec(&(fdata.ftCreationTime), &(buf->st_ctim));
1438+
if (are_wsl_compatible_mode_bits_enabled())
1439+
get_wsl_mode_bits_by_handle(hnd, &buf->st_mode);
14271440
return 0;
14281441
}
14291442

compat/win32/fscache.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "config.h"
99
#include "../../mem-pool.h"
1010
#include "ntifs.h"
11+
#include "wsl.h"
1112

1213
static volatile long initialized;
1314
static DWORD dwTlsIndex;
@@ -220,6 +221,21 @@ static struct fsentry *fseentry_create_entry(struct fscache *cache,
220221
&(fse->u.s.st_mtim));
221222
filetime_to_timespec((FILETIME *)&(fdata->CreationTime),
222223
&(fse->u.s.st_ctim));
224+
if (fdata->EaSize > 0 &&
225+
sizeof(buf) >= (size_t)(list ? list->len+1 : 0) + fse->len+1 &&
226+
are_wsl_compatible_mode_bits_enabled()) {
227+
size_t off = 0;
228+
wchar_t wpath[MAX_LONG_PATH];
229+
if (list && list->len) {
230+
memcpy(buf, list->dirent.d_name, list->len);
231+
buf[list->len] = '/';
232+
off = list->len + 1;
233+
}
234+
memcpy(buf + off, fse->dirent.d_name, fse->len);
235+
buf[off + fse->len] = '\0';
236+
if (xutftowcs_long_path(wpath, buf) >= 0)
237+
copy_wsl_mode_bits_from_disk(wpath, -1, &fse->st_mode);
238+
}
223239

224240
return fse;
225241
}

compat/win32/wsl.c

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
#define USE_THE_REPOSITORY_VARIABLE
2+
#include "../../git-compat-util.h"
3+
#include "../win32.h"
4+
#include "../../repository.h"
5+
#include "config.h"
6+
#include "ntifs.h"
7+
#include "wsl.h"
8+
9+
int are_wsl_compatible_mode_bits_enabled(void)
10+
{
11+
/* default to `false` during initialization */
12+
static const int fallback = 0;
13+
static int enabled = -1;
14+
15+
if (enabled < 0) {
16+
/* avoid infinite recursion */
17+
if (!the_repository)
18+
return fallback;
19+
20+
if (the_repository->config &&
21+
the_repository->config->hash_initialized &&
22+
repo_config_get_bool(the_repository, "core.wslcompat", &enabled) < 0)
23+
enabled = 0;
24+
}
25+
26+
return enabled < 0 ? fallback : enabled;
27+
}
28+
29+
int copy_wsl_mode_bits_from_disk(const wchar_t *wpath, ssize_t wpathlen,
30+
_mode_t *mode)
31+
{
32+
int ret = -1;
33+
HANDLE h;
34+
if (wpathlen >= 0) {
35+
/*
36+
* It's caller's duty to make sure wpathlen is reasonable so
37+
* it does not overflow.
38+
*/
39+
wchar_t *fn2 = (wchar_t*)alloca((wpathlen + 1) * sizeof(wchar_t));
40+
memcpy(fn2, wpath, wpathlen * sizeof(wchar_t));
41+
fn2[wpathlen] = 0;
42+
wpath = fn2;
43+
}
44+
h = CreateFileW(wpath, FILE_READ_EA | SYNCHRONIZE,
45+
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
46+
NULL, OPEN_EXISTING,
47+
FILE_FLAG_BACKUP_SEMANTICS |
48+
FILE_FLAG_OPEN_REPARSE_POINT,
49+
NULL);
50+
if (h != INVALID_HANDLE_VALUE) {
51+
ret = get_wsl_mode_bits_by_handle(h, mode);
52+
CloseHandle(h);
53+
}
54+
return ret;
55+
}
56+
57+
#ifndef LX_FILE_METADATA_HAS_UID
58+
#define LX_FILE_METADATA_HAS_UID 0x1
59+
#define LX_FILE_METADATA_HAS_GID 0x2
60+
#define LX_FILE_METADATA_HAS_MODE 0x4
61+
#define LX_FILE_METADATA_HAS_DEVICE_ID 0x8
62+
#define LX_FILE_CASE_SENSITIVE_DIR 0x10
63+
typedef struct _FILE_STAT_LX_INFORMATION {
64+
LARGE_INTEGER FileId;
65+
LARGE_INTEGER CreationTime;
66+
LARGE_INTEGER LastAccessTime;
67+
LARGE_INTEGER LastWriteTime;
68+
LARGE_INTEGER ChangeTime;
69+
LARGE_INTEGER AllocationSize;
70+
LARGE_INTEGER EndOfFile;
71+
uint32_t FileAttributes;
72+
uint32_t ReparseTag;
73+
uint32_t NumberOfLinks;
74+
ACCESS_MASK EffectiveAccess;
75+
uint32_t LxFlags;
76+
uint32_t LxUid;
77+
uint32_t LxGid;
78+
uint32_t LxMode;
79+
uint32_t LxDeviceIdMajor;
80+
uint32_t LxDeviceIdMinor;
81+
} FILE_STAT_LX_INFORMATION, *PFILE_STAT_LX_INFORMATION;
82+
#endif
83+
84+
/*
85+
* This struct is extended from the original FILE_FULL_EA_INFORMATION of
86+
* Microsoft Windows.
87+
*/
88+
struct wsl_full_ea_info_t {
89+
uint32_t NextEntryOffset;
90+
uint8_t Flags;
91+
uint8_t EaNameLength;
92+
uint16_t EaValueLength;
93+
char EaName[7];
94+
char EaValue[4];
95+
char Padding[1];
96+
};
97+
98+
enum {
99+
FileStatLxInformation = 70,
100+
};
101+
__declspec(dllimport) NTSTATUS WINAPI
102+
NtQueryInformationFile(HANDLE FileHandle,
103+
PIO_STATUS_BLOCK IoStatusBlock,
104+
PVOID FileInformation, ULONG Length,
105+
uint32_t FileInformationClass);
106+
__declspec(dllimport) NTSTATUS WINAPI
107+
NtSetInformationFile(HANDLE FileHandle, PIO_STATUS_BLOCK IoStatusBlock,
108+
PVOID FileInformation, ULONG Length,
109+
uint32_t FileInformationClass);
110+
__declspec(dllimport) NTSTATUS WINAPI
111+
NtSetEaFile(HANDLE FileHandle, PIO_STATUS_BLOCK IoStatusBlock,
112+
PVOID EaBuffer, ULONG EaBufferSize);
113+
114+
int set_wsl_mode_bits_by_handle(HANDLE h, _mode_t mode)
115+
{
116+
uint32_t value = mode;
117+
struct wsl_full_ea_info_t ea_info;
118+
IO_STATUS_BLOCK iob;
119+
/* mode should be valid to make WSL happy */
120+
assert(S_ISREG(mode) || S_ISDIR(mode));
121+
ea_info.NextEntryOffset = 0;
122+
ea_info.Flags = 0;
123+
ea_info.EaNameLength = 6;
124+
ea_info.EaValueLength = sizeof(value); /* 4 */
125+
strlcpy(ea_info.EaName, "$LXMOD", sizeof(ea_info.EaName));
126+
memcpy(ea_info.EaValue, &value, sizeof(value));
127+
ea_info.Padding[0] = 0;
128+
return NtSetEaFile(h, &iob, &ea_info, sizeof(ea_info));
129+
}
130+
131+
int get_wsl_mode_bits_by_handle(HANDLE h, _mode_t *mode)
132+
{
133+
FILE_STAT_LX_INFORMATION fxi;
134+
IO_STATUS_BLOCK iob;
135+
if (NtQueryInformationFile(h, &iob, &fxi, sizeof(fxi),
136+
FileStatLxInformation) == 0) {
137+
if (fxi.LxFlags & LX_FILE_METADATA_HAS_MODE)
138+
*mode = (_mode_t)fxi.LxMode;
139+
return 0;
140+
}
141+
return -1;
142+
}

compat/win32/wsl.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#ifndef COMPAT_WIN32_WSL_H
2+
#define COMPAT_WIN32_WSL_H
3+
4+
int are_wsl_compatible_mode_bits_enabled(void);
5+
6+
int copy_wsl_mode_bits_from_disk(const wchar_t *wpath, ssize_t wpathlen,
7+
_mode_t *mode);
8+
9+
int get_wsl_mode_bits_by_handle(HANDLE h, _mode_t *mode);
10+
int set_wsl_mode_bits_by_handle(HANDLE h, _mode_t mode);
11+
12+
#endif

config.mak.uname

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ endif
521521
compat/win32/path-utils.o \
522522
compat/win32/pthread.o compat/win32/syslog.o \
523523
compat/win32/trace2_win32_process_info.o \
524-
compat/win32/dirent.o compat/win32/fscache.o
524+
compat/win32/dirent.o compat/win32/fscache.o compat/win32/wsl.o
525525
COMPAT_CFLAGS = -D__USE_MINGW_ACCESS -DDETECT_MSYS_TTY \
526526
-DENSURE_MSYSTEM_IS_SET="\"$(MSYSTEM)\"" -DMINGW_PREFIX="\"$(patsubst /%,%,$(MINGW_PREFIX))\"" \
527527
-DNOGDI -DHAVE_STRING_H -Icompat -Icompat/regex -Icompat/win32 -DSTRIP_EXTENSION=\".exe\"
@@ -726,7 +726,7 @@ ifeq ($(uname_S),MINGW)
726726
compat/win32/flush.o \
727727
compat/win32/path-utils.o \
728728
compat/win32/pthread.o compat/win32/syslog.o \
729-
compat/win32/dirent.o compat/win32/fscache.o
729+
compat/win32/dirent.o compat/win32/fscache.o compat/win32/wsl.o
730730
BASIC_CFLAGS += -DWIN32
731731
EXTLIBS += -lws2_32
732732
GITLIBS += git.res

contrib/buildsystems/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
300300
compat/win32/syslog.c
301301
compat/win32/trace2_win32_process_info.c
302302
compat/win32/dirent.c
303+
compat/win32/wsl.c
303304
compat/strdup.c
304305
compat/win32/fscache.c)
305306
set(NO_UNIX_SOCKETS 1)

meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,6 +1295,7 @@ elif host_machine.system() == 'windows'
12951295
'compat/win32/path-utils.c',
12961296
'compat/win32/pthread.c',
12971297
'compat/win32/syslog.c',
1298+
'compat/win32/wsl.c',
12981299
'compat/win32mmap.c',
12991300
]
13001301

0 commit comments

Comments
 (0)