Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

os: enable symlink creation on Windows 10 #24307

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/internal/syscall/windows/symlink_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package windows

import "syscall"

const (
ERROR_INVALID_PARAMETER syscall.Errno = 87

// symlink support for CreateSymbolicLink() starting with Windows 10 (1703, v10.0.14972)
SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE = 0x2
)
11 changes: 10 additions & 1 deletion src/os/file_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,11 +373,20 @@ func Symlink(oldname, newname string) error {
return &LinkError{"symlink", oldname, newname, err}
}

var flags uint32
var flags uint32 = windows.SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE
if isdir {
flags |= syscall.SYMBOLIC_LINK_FLAG_DIRECTORY
}
err = syscall.CreateSymbolicLink(n, o, flags)

if err != nil {
// the unprivileged create flag is unsupported
// below Windows 10 (1703, v10.0.14972). retry without it.
flags &^= windows.SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE

err = syscall.CreateSymbolicLink(n, o, flags)
}

if err != nil {
return &LinkError{"symlink", oldname, newname, err}
}
Expand Down
44 changes: 44 additions & 0 deletions src/os/os_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1011,3 +1011,47 @@ func TestWindowsDevNullFile(t *testing.T) {
t.Errorf(`"NUL" and "nul" are not the same file`)
}
}

// TestSymlinkCreation verifies that creating a symbolic link
// works on Windows when developer mode is active.
// This is supported starting Windows 10 (1703, v10.0.14972).
func TestSymlinkCreation(t *testing.T) {
if !isWindowsDeveloperModeActive() {
t.Skip("Windows developer mode is not active")
}

temp, err := ioutil.TempDir("", "TestSymlinkCreation")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(temp)

dummyFile := filepath.Join(temp, "file")
err = ioutil.WriteFile(dummyFile, []byte(""), 0644)
if err != nil {
t.Fatal(err)
}

linkFile := filepath.Join(temp, "link")
err = os.Symlink(dummyFile, linkFile)
if err != nil {
t.Fatal(err)
}
}

// isWindowsDeveloperModeActive checks whether or not the developer mode is active on Windows 10.
// Returns false for prior Windows versions.
// see https://docs.microsoft.com/en-us/windows/uwp/get-started/enable-your-device-for-development
func isWindowsDeveloperModeActive() bool {
key, err := registry.OpenKey(registry.LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\AppModelUnlock", registry.READ)
if err != nil {
return false
}

val, _, err := key.GetIntegerValue("AllowDevelopmentWithoutDevLicense")
if err != nil {
return false
}

return val != 0
}