From e7d93f7f42b88499f76fce1d184127a4e73ccae9 Mon Sep 17 00:00:00 2001 From: Andrea Mazzoleni Date: Sat, 13 May 2017 13:23:41 +0200 Subject: [PATCH] Better backward compatibility in symlink creation The new flag may result in ERROR_INVALID_PARAMETER in old Windows. --- cmdline/mingw.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/cmdline/mingw.c b/cmdline/mingw.c index 6e701e50..e094cd40 100644 --- a/cmdline/mingw.c +++ b/cmdline/mingw.c @@ -1559,15 +1559,25 @@ int windows_symlink(const char* existing, const char* file) { wchar_t conv_buf_file[CONV_MAX]; wchar_t conv_buf_existing[CONV_MAX]; - DWORD flags = SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE; /* We must convert to the extended-length \\?\ format if the path is too long */ /* otherwise the link creation fails. */ /* But we don't want to always convert it, to avoid to recreate */ /* user symlinks different than they were before */ - if (!CreateSymbolicLinkW(convert(conv_buf_file, file), convert_if_required(conv_buf_existing, existing), flags)) { - windows_errno(GetLastError()); - return -1; + if (!CreateSymbolicLinkW(convert(conv_buf_file, file), convert_if_required(conv_buf_existing, existing), + SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE) + ) { + DWORD error = GetLastError(); + if (GetLastError() != ERROR_INVALID_PARAMETER) { + windows_errno(error); + return -1; + } + + /* retry without the new flag SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE */ + if (!CreateSymbolicLinkW(convert(conv_buf_file, file), convert_if_required(conv_buf_existing, existing), 0)) { + windows_errno(GetLastError()); + return -1; + } } return 0;