-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection.go
67 lines (59 loc) · 1.77 KB
/
connection.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
package winnetwork
import (
"github.com/gentlemanautomaton/winnetwork/netresource"
"github.com/gentlemanautomaton/winnetwork/wnetapi"
)
// GetConnection returns the remote resource for the local drive letter.
//
// Connections are unique to each user.
func GetConnection(local string) (remote string, err error) {
local, err = netresource.MakeDriveLetter(local)
if err != nil {
return "", err
}
return wnetapi.GetConnection(local + ":")
}
// AddConnection attempts to map the given local name to a remote resource.
// If persistent is true it creates a persistent connection that survives
// logon/logoff cycles.
//
// Connections are unique to each user.
func AddConnection(local, remote string, persistent bool) (err error) {
local, err = netresource.MakeDriveLetter(local)
if err != nil {
return err
}
local += ":"
flags := netresource.Option(netresource.ConnectRedirect)
if persistent {
flags |= netresource.ConnectUpdateProfile
} else {
flags |= netresource.ConnectTemporary
}
return wnetapi.AddConnection2("", "", flags, netresource.Data{
Type: netresource.TypeDisk,
LocalName: local,
RemoteName: remote,
})
}
// RemoveConnection attempts to unmap the given local resource name that
// has previously been mapped to a remote resource.
//
// If persistent is true, it permanently removes the connection from the user
// profile.
//
// If force is true, it will forcefully close open files if necessary.
//
// Connections are unique to each user.
func RemoveConnection(name string, persistent, force bool) (err error) {
name, err = netresource.MakeDriveLetter(name)
if err != nil {
return err
}
name += ":"
var flags netresource.Option
if persistent {
flags |= netresource.ConnectUpdateProfile
}
return wnetapi.CancelConnection2(name, flags, force)
}