Skip to content

Commit e4a2ade

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 0a04e07 commit e4a2ade

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
@@ -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());
@@ -3126,7 +3129,7 @@ int symlink(const char *target, const char *link)
31263129
wtarget[len] = '\\';
31273130

31283131
/* create file symlink */
3129-
if (!CreateSymbolicLinkW(wlink, wtarget, 0)) {
3132+
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags)) {
31303133
errno = err_win_to_posix(GetLastError());
31313134
return -1;
31323135
}
@@ -4062,6 +4065,24 @@ static void maybe_redirect_std_handles(void)
40624065
GENERIC_WRITE, FILE_FLAG_NO_BUFFERING);
40634066
}
40644067

4068+
static void adjust_symlink_flags(void)
4069+
{
4070+
/*
4071+
* Starting with Windows 10 Build 14972, symbolic links can be created
4072+
* using CreateSymbolicLink() without elevation by passing the flag
4073+
* SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE (0x02) as last
4074+
* parameter, provided the Developer Mode has been enabled. Some
4075+
* earlier Windows versions complain about this flag with an
4076+
* ERROR_INVALID_PARAMETER, hence we have to test the build number
4077+
* specifically.
4078+
*/
4079+
if (GetVersion() >= 14972 << 16) {
4080+
symlink_file_flags |= 2;
4081+
symlink_directory_flags |= 2;
4082+
}
4083+
4084+
}
4085+
40654086
#ifdef _MSC_VER
40664087
#ifdef _DEBUG
40674088
#include <crtdbg.h>
@@ -4097,6 +4118,7 @@ int wmain(int argc, const wchar_t **wargv)
40974118
#endif
40984119

40994120
maybe_redirect_std_handles();
4121+
adjust_symlink_flags();
41004122
fsync_object_files = 1;
41014123

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

0 commit comments

Comments
 (0)