Skip to content

Commit 475fcb2

Browse files
dschoGit for Windows Build Agent
authored andcommitted
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 #1184. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent a881199 commit 475fcb2

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

compat/mingw.c

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,8 @@ int mingw_core_config(const char *var, const char *value, void *cb)
301301
return 0;
302302
}
303303

304+
static DWORD symlink_file_flags = 0, symlink_directory_flags = 1;
305+
304306
enum phantom_symlink_result {
305307
PHANTOM_SYMLINK_RETRY,
306308
PHANTOM_SYMLINK_DONE,
@@ -386,7 +388,8 @@ process_phantom_symlink(const wchar_t *wtarget, const wchar_t *wlink)
386388
return PHANTOM_SYMLINK_DONE;
387389

388390
/* otherwise recreate the symlink with directory flag */
389-
if (DeleteFileW(wlink) && CreateSymbolicLinkW(wlink, wtarget, 1))
391+
if (DeleteFileW(wlink) &&
392+
CreateSymbolicLinkW(wlink, wtarget, symlink_directory_flags))
390393
return PHANTOM_SYMLINK_DIRECTORY;
391394

392395
errno = err_win_to_posix(GetLastError());
@@ -2817,7 +2820,7 @@ int symlink(const char *target, const char *link)
28172820
wtarget[len] = '\\';
28182821

28192822
/* create file symlink */
2820-
if (!CreateSymbolicLinkW(wlink, wtarget, 0)) {
2823+
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags)) {
28212824
errno = err_win_to_posix(GetLastError());
28222825
return -1;
28232826
}
@@ -3678,6 +3681,24 @@ static void maybe_redirect_std_handles(void)
36783681
GENERIC_WRITE, FILE_FLAG_NO_BUFFERING);
36793682
}
36803683

3684+
static void adjust_symlink_flags(void)
3685+
{
3686+
/*
3687+
* Starting with Windows 10 Build 14972, symbolic links can be created
3688+
* using CreateSymbolicLink() without elevation by passing the flag
3689+
* SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE (0x02) as last
3690+
* parameter, provided the Developer Mode has been enabled. Some
3691+
* earlier Windows versions complain about this flag with an
3692+
* ERROR_INVALID_PARAMETER, hence we have to test the build number
3693+
* specifically.
3694+
*/
3695+
if (GetVersion() >= 14972 << 16) {
3696+
symlink_file_flags |= 2;
3697+
symlink_directory_flags |= 2;
3698+
}
3699+
3700+
}
3701+
36813702
#ifdef _MSC_VER
36823703
#ifdef _DEBUG
36833704
#include <crtdbg.h>
@@ -3712,6 +3733,7 @@ int wmain(int argc, const wchar_t **wargv)
37123733
#endif
37133734

37143735
maybe_redirect_std_handles();
3736+
adjust_symlink_flags();
37153737
fsync_object_files = 1;
37163738

37173739
/* determine size of argv and environ conversion buffer */

0 commit comments

Comments
 (0)