-
-
Notifications
You must be signed in to change notification settings - Fork 184
/
helpers_windows.go
67 lines (57 loc) · 1.22 KB
/
helpers_windows.go
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
//go:build windows
package main
import (
"os"
"path/filepath"
"strings"
"syscall"
)
func toFullPath(s string) (string, error) {
p := syscall.StringToUTF16(s)
b := p
n, err := syscall.GetLongPathName(&p[0], &b[0], uint32(len(b)))
if err != nil {
return s, err
}
if n > uint32(len(b)) {
b = make([]uint16, n)
n, err = syscall.GetLongPathName(&p[0], &b[0], uint32(len(b)))
if err != nil {
return s, err
}
}
b = b[:n]
return syscall.UTF16ToString(b), nil
}
func evalSymlinks(path string) (string, error) {
_, err := os.Stat(path)
if err != nil {
return "", err
}
list := filepathSplitAll(path)
evaled := list[0]
for i := 1; i < len(list); i++ {
evaled = filepath.Join(evaled, list[i])
linkSrc, err := os.Readlink(evaled)
if err != nil {
// not symlink
continue
} else {
if filepath.IsAbs(linkSrc) {
evaled = linkSrc
} else {
evaled = filepath.Join(filepath.Dir(evaled), linkSrc)
}
}
}
return evaled, nil
}
func filepathSplitAll(path string) []string {
path = filepath.Clean(path)
path = filepath.ToSlash(path)
vol := filepath.VolumeName(path)
path = path[len(vol):]
list := strings.Split(path, "/")
list[0] = vol + string(filepath.Separator) + list[0]
return list
}