Skip to content

Commit 96891c3

Browse files
committed
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 f7ab0fb commit 96891c3

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

compat/mingw.c

+24-2
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,8 @@ static const wchar_t *make_relative_to(const wchar_t *path,
368368
return out;
369369
}
370370

371+
static DWORD symlink_file_flags = 0, symlink_directory_flags = 1;
372+
371373
enum phantom_symlink_result {
372374
PHANTOM_SYMLINK_RETRY,
373375
PHANTOM_SYMLINK_DONE,
@@ -418,7 +420,8 @@ process_phantom_symlink(const wchar_t *wtarget, const wchar_t *wlink)
418420
return PHANTOM_SYMLINK_DONE;
419421

420422
/* otherwise recreate the symlink with directory flag */
421-
if (DeleteFileW(wlink) && CreateSymbolicLinkW(wlink, wtarget, 1))
423+
if (DeleteFileW(wlink) &&
424+
CreateSymbolicLinkW(wlink, wtarget, symlink_directory_flags))
422425
return PHANTOM_SYMLINK_DIRECTORY;
423426

424427
errno = err_win_to_posix(GetLastError());
@@ -3050,7 +3053,7 @@ int symlink(const char *target, const char *link)
30503053
wtarget[len] = '\\';
30513054

30523055
/* create file symlink */
3053-
if (!CreateSymbolicLinkW(wlink, wtarget, 0)) {
3056+
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags)) {
30543057
errno = err_win_to_posix(GetLastError());
30553058
return -1;
30563059
}
@@ -3986,6 +3989,24 @@ static void maybe_redirect_std_handles(void)
39863989
GENERIC_WRITE, FILE_FLAG_NO_BUFFERING);
39873990
}
39883991

3992+
static void adjust_symlink_flags(void)
3993+
{
3994+
/*
3995+
* Starting with Windows 10 Build 14972, symbolic links can be created
3996+
* using CreateSymbolicLink() without elevation by passing the flag
3997+
* SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE (0x02) as last
3998+
* parameter, provided the Developer Mode has been enabled. Some
3999+
* earlier Windows versions complain about this flag with an
4000+
* ERROR_INVALID_PARAMETER, hence we have to test the build number
4001+
* specifically.
4002+
*/
4003+
if (GetVersion() >= 14972 << 16) {
4004+
symlink_file_flags |= 2;
4005+
symlink_directory_flags |= 2;
4006+
}
4007+
4008+
}
4009+
39894010
#ifdef _MSC_VER
39904011
#ifdef _DEBUG
39914012
#include <crtdbg.h>
@@ -4021,6 +4042,7 @@ int wmain(int argc, const wchar_t **wargv)
40214042
#endif
40224043

40234044
maybe_redirect_std_handles();
4045+
adjust_symlink_flags();
40244046
fsync_object_files = 1;
40254047

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

0 commit comments

Comments
 (0)