From e8f13ee97ec1f09867c30eb598531260b53cb7de Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Tue, 30 May 2017 22:26:43 +0200 Subject: [PATCH 1/2] fixup! Win32: implement basic symlink() functionality (file symlinks only) This patch fixes signature of the CreateSymbolicLinkW() function's declaratoin. The previous declaration claimed that said function returns a BOOL, while it really returns a BOOLEAN. This is not an academic distinction: BOOL is defined as an int (i.e. 32-bit) and BOOLEAN as an unsigned char (i.e. 8-bit). Therefore, the return value 0 (meaning, the least-significant 8 bits are all zero) could be mistaken to indicate a successful creation of the symbolic link (because the remaining 24 bits are undefined, and quite likely non-zero). Signed-off-by: Johannes Schindelin --- compat/mingw.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compat/mingw.c b/compat/mingw.c index 1898a012e7b950..7b930e90e3d815 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -270,7 +270,7 @@ int mingw_core_config(const char *var, const char *value) return 0; } -DECLARE_PROC_ADDR(kernel32.dll, BOOL, CreateSymbolicLinkW, LPCWSTR, LPCWSTR, DWORD); +DECLARE_PROC_ADDR(kernel32.dll, BOOLEAN, CreateSymbolicLinkW, LPCWSTR, LPCWSTR, DWORD); enum phantom_symlink_result { PHANTOM_SYMLINK_RETRY, From dbba5f1376d06ce704b8972c77ece312129fb64a Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Tue, 30 May 2017 21:50:57 +0200 Subject: [PATCH 2/2] mingw: try to create symlinks without elevated permissions With Windows 10 Build 14972 in Developer Mode, a new flag is supported by CreateSymbolicLink() to create symbolic links even when running outside of an elevated session (which was previously required). This new flag is called SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE and has the numeric value 0x02. Previous Windows 10 versions will not understand that flag and return an ERROR_INVALID_PARAMETER, therefore we have to be careful to try passing that flag only when the build number indicates that it is supported. For more information about the new flag, see this blog post: https://blogs.windows.com/buildingapps/2016/12/02/symlinks-windows-10/ This patch is loosely based on the patch submitted by Samuel D. Leslie as https://github.com/git-for-windows/git/pull/1184. Signed-off-by: Johannes Schindelin --- compat/mingw.c | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/compat/mingw.c b/compat/mingw.c index 7b930e90e3d815..538bf95f4405e8 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -270,6 +270,7 @@ int mingw_core_config(const char *var, const char *value) return 0; } +static DWORD symlink_file_flags = 0, symlink_directory_flags = 1; DECLARE_PROC_ADDR(kernel32.dll, BOOLEAN, CreateSymbolicLinkW, LPCWSTR, LPCWSTR, DWORD); enum phantom_symlink_result { @@ -314,7 +315,8 @@ static enum phantom_symlink_result process_phantom_symlink( return PHANTOM_SYMLINK_DONE; /* otherwise recreate the symlink with directory flag */ - if (DeleteFileW(wlink) && CreateSymbolicLinkW(wlink, wtarget, 1)) + if (DeleteFileW(wlink) && + CreateSymbolicLinkW(wlink, wtarget, symlink_directory_flags)) return PHANTOM_SYMLINK_DIRECTORY; errno = err_win_to_posix(GetLastError()); @@ -2656,7 +2658,7 @@ int symlink(const char *target, const char *link) wtarget[len] = '\\'; /* create file symlink */ - if (!CreateSymbolicLinkW(wlink, wtarget, 0)) { + if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags)) { errno = err_win_to_posix(GetLastError()); return -1; } @@ -3178,6 +3180,24 @@ static void maybe_redirect_std_handles(void) GENERIC_WRITE, FILE_FLAG_NO_BUFFERING); } +static void adjust_symlink_flags(void) +{ + /* + * Starting with Windows 10 Build 14972, symbolic links can be created + * using CreateSymbolicLink() without elevation by passing the flag + * SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE (0x02) as last + * parameter, provided the Developer Mode has been enabled. Some + * earlier Windows versions complain about this flag with an + * ERROR_INVALID_PARAMETER, hence we have to test the build number + * specifically. + */ + if (GetVersion() >= 14972 << 16) { + symlink_file_flags |= 2; + symlink_directory_flags |= 2; + } + +} + #if defined(_MSC_VER) #ifdef _DEBUG @@ -3217,6 +3237,7 @@ int msc_startup(int argc, wchar_t **w_argv, wchar_t **w_env) #endif maybe_redirect_std_handles(); + adjust_symlink_flags(); /* determine size of argv conversion buffer */ maxlen = wcslen(_wpgmptr); @@ -3283,6 +3304,7 @@ void mingw_startup(void) _startupinfo si; maybe_redirect_std_handles(); + adjust_symlink_flags(); /* get wide char arguments and environment */ si.newmode = 0;