-
Notifications
You must be signed in to change notification settings - Fork 2
/
main_windows.go
72 lines (60 loc) · 1.16 KB
/
main_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
68
69
70
71
72
// +build windows
package junction
import (
"fmt"
"os"
"path/filepath"
"golang.org/x/sys/windows"
"github.com/Microsoft/go-winio"
)
func create(target, mountPt string) error {
_target, err := filepath.Abs(target)
if err != nil {
return fmt.Errorf("%s: %s", target, err)
}
_mountPt, err := windows.UTF16PtrFromString(mountPt)
if err != nil {
return fmt.Errorf("%s: %s", mountPt, err)
}
err = os.Mkdir(mountPt, 0777)
if err != nil {
return fmt.Errorf("%s: %s", mountPt, err)
}
ok := false
defer func() {
if !ok {
os.Remove(mountPt)
}
}()
handle, err := windows.CreateFile(_mountPt,
windows.GENERIC_WRITE,
0,
nil,
windows.OPEN_EXISTING,
windows.FILE_FLAG_BACKUP_SEMANTICS,
0)
if err != nil {
return fmt.Errorf("%s: %s", mountPt, err)
}
defer windows.CloseHandle(handle)
rp := winio.ReparsePoint{
Target: _target,
IsMountPoint: true,
}
data := winio.EncodeReparsePoint(&rp)
var size uint32
err = windows.DeviceIoControl(
handle,
FSCTL_SET_REPARSE_POINT,
&data[0],
uint32(len(data)),
nil,
0,
&size,
nil)
if err != nil {
return fmt.Errorf("windows.DeviceIoControl: %s", err)
}
ok = true
return nil
}