Skip to content

Commit 7e52043

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 3cc8cdd commit 7e52043

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
@@ -361,6 +361,8 @@ static const wchar_t *make_relative_to(const wchar_t *path,
361361
return out;
362362
}
363363

364+
static DWORD symlink_file_flags = 0, symlink_directory_flags = 1;
365+
364366
enum phantom_symlink_result {
365367
PHANTOM_SYMLINK_RETRY,
366368
PHANTOM_SYMLINK_DONE,
@@ -411,7 +413,8 @@ process_phantom_symlink(const wchar_t *wtarget, const wchar_t *wlink)
411413
return PHANTOM_SYMLINK_DONE;
412414

413415
/* otherwise recreate the symlink with directory flag */
414-
if (DeleteFileW(wlink) && CreateSymbolicLinkW(wlink, wtarget, 1))
416+
if (DeleteFileW(wlink) &&
417+
CreateSymbolicLinkW(wlink, wtarget, symlink_directory_flags))
415418
return PHANTOM_SYMLINK_DIRECTORY;
416419

417420
errno = err_win_to_posix(GetLastError());
@@ -3112,7 +3115,7 @@ int symlink(const char *target, const char *link)
31123115
wtarget[len] = '\\';
31133116

31143117
/* create file symlink */
3115-
if (!CreateSymbolicLinkW(wlink, wtarget, 0)) {
3118+
if (!CreateSymbolicLinkW(wlink, wtarget, symlink_file_flags)) {
31163119
errno = err_win_to_posix(GetLastError());
31173120
return -1;
31183121
}
@@ -4076,6 +4079,24 @@ static void maybe_redirect_std_handles(void)
40764079
GENERIC_WRITE, FILE_FLAG_NO_BUFFERING);
40774080
}
40784081

4082+
static void adjust_symlink_flags(void)
4083+
{
4084+
/*
4085+
* Starting with Windows 10 Build 14972, symbolic links can be created
4086+
* using CreateSymbolicLink() without elevation by passing the flag
4087+
* SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE (0x02) as last
4088+
* parameter, provided the Developer Mode has been enabled. Some
4089+
* earlier Windows versions complain about this flag with an
4090+
* ERROR_INVALID_PARAMETER, hence we have to test the build number
4091+
* specifically.
4092+
*/
4093+
if (GetVersion() >= 14972 << 16) {
4094+
symlink_file_flags |= 2;
4095+
symlink_directory_flags |= 2;
4096+
}
4097+
4098+
}
4099+
40794100
#ifdef _MSC_VER
40804101
#ifdef _DEBUG
40814102
#include <crtdbg.h>
@@ -4111,6 +4132,7 @@ int wmain(int argc, const wchar_t **wargv)
41114132
#endif
41124133

41134134
maybe_redirect_std_handles();
4135+
adjust_symlink_flags();
41144136
fsync_object_files = 1;
41154137

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

0 commit comments

Comments
 (0)