-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathsubst_windows.go
More file actions
91 lines (83 loc) · 1.76 KB
/
Copy pathsubst_windows.go
File metadata and controls
91 lines (83 loc) · 1.76 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//go:build windows
package util
import (
"os"
"path/filepath"
"syscall"
"unsafe"
"golang.org/x/sys/windows"
)
var (
dll *syscall.DLL
procResolve *syscall.Proc
procFree *syscall.Proc
available bool
)
func init() {
dist := os.Getenv("CODEQL_DIST")
if dist == "" {
return
}
dllPath := filepath.Join(dist, "tools", "win64", "canonicalize.dll")
d, err := syscall.LoadDLL(dllPath)
if err != nil {
return
}
p, err := d.FindProc("resolve_subst")
if err != nil {
d.Release()
return
}
f, err := d.FindProc("resolve_subst_free")
if err != nil {
d.Release()
return
}
dll = d
procResolve = p
procFree = f
available = true
}
// If "path" is an absolute path starting with a "subst"ed drive letter, return an
// equivalent path with the drive letter replaced by its target. Otherwise return
// "path" unchanged.
func ResolvePath(path string) string {
if len(path) < 3 {
return path
}
if path[1] != ':' {
return path
}
if path[2] != '\\' && path[2] != '/' {
return path
}
c := path[0]
if !((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) {
return path
}
resolved, ok := resolveDrive(path[:3])
if !ok {
return path
}
return resolved + path[2:]
}
// Given a drive root like "X:\" (or "X:/"), returns the path that drive is
// "subst"ed to. Returns false if the drive is not "subst"ed or an error occurred.
func resolveDrive(driveRoot string) (string, bool) {
if !available {
return "", false
}
driveBytes, err := windows.ByteSliceFromString(driveRoot)
if err != nil {
return "", false
}
ret, _, _ := procResolve.Call(uintptr(unsafe.Pointer(&driveBytes[0])))
if ret == 0 {
return "", false
}
result := windows.BytePtrToString((*byte)(unsafe.Pointer(ret)))
if procFree != nil {
procFree.Call(ret)
}
return result, true
}