Repository navigation
Expand file tree
/
Copy pathmanage_windows.go
More file actions
69 lines (64 loc) · 1.87 KB
/
Copy pathmanage_windows.go
File metadata and controls
69 lines (64 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package sessions
import (
"os"
"path/filepath"
"strings"
"syscall"
"golang.org/x/sys/windows"
)
const crossDeviceErr = windows.ERROR_NOT_SAME_DEVICE
func copySymlink(target, to string, fi os.FileInfo) error {
linkError := func(err error) error {
return &os.LinkError{Op: "symlink", Old: target, New: to, Err: err}
}
name, err := windowsSymlinkPath(to)
if err != nil {
return linkError(err)
}
n, err := windows.UTF16PtrFromString(name)
if err != nil {
return linkError(err)
}
oldname := filepath.FromSlash(target)
if filepath.IsAbs(oldname) && len(oldname) >= 248 {
oldname, err = windowsSymlinkPath(oldname)
if err != nil {
return linkError(err)
}
}
o, err := windows.UTF16PtrFromString(oldname)
if err != nil {
return linkError(err)
}
// Lstat keeps the link's directory attribute even when its target is
// missing. os.Symlink would guess from the target at the new location.
var flags uint32
if fi.Sys().(*syscall.Win32FileAttributeData).FileAttributes&windows.FILE_ATTRIBUTE_DIRECTORY != 0 {
flags |= windows.SYMBOLIC_LINK_FLAG_DIRECTORY
}
const allowUnprivilegedCreate = 0x2 // supported with Windows Developer Mode
err = windows.CreateSymbolicLink(n, o, flags|allowUnprivilegedCreate)
if err != nil {
// As os.Symlink does, retry without the flag for older Windows builds.
err = windows.CreateSymbolicLink(n, o, flags)
}
if err != nil {
return linkError(err)
}
return nil
}
// Native link creation needs an absolute extended path to keep the long-path
// support os.Symlink provides. Relative targets must remain relative.
func windowsSymlinkPath(path string) (string, error) {
path, err := filepath.Abs(path)
if err != nil {
return "", err
}
if strings.HasPrefix(path, `\\?\`) || strings.HasPrefix(path, `\\.\`) {
return path, nil
}
if strings.HasPrefix(path, `\\`) {
return `\\?\UNC\` + path[2:], nil
}
return `\\?\` + path, nil
}