...
1
2
3 package main
4
5
6
7 import (
8 "path/filepath"
9 "strings"
10
11 "github.com/pkg/errors"
12 "golang.org/x/sys/windows"
13 )
14
15
16
17 func setVolumeMountPoint(targetPath string, volumePath string) error {
18 if !strings.HasPrefix(volumePath, "\\\\?\\Volume{") {
19 return errors.Errorf("unable to mount non-volume path %s", volumePath)
20 }
21
22
23 slashedTarget := filepath.Clean(targetPath) + string(filepath.Separator)
24 slashedVolume := volumePath + string(filepath.Separator)
25
26 targetP, err := windows.UTF16PtrFromString(slashedTarget)
27 if err != nil {
28 return errors.Wrapf(err, "unable to utf16-ise %s", slashedTarget)
29 }
30
31 volumeP, err := windows.UTF16PtrFromString(slashedVolume)
32 if err != nil {
33 return errors.Wrapf(err, "unable to utf16-ise %s", slashedVolume)
34 }
35
36 if err := windows.SetVolumeMountPoint(targetP, volumeP); err != nil {
37 return errors.Wrapf(err, "failed calling SetVolumeMount('%s', '%s')", slashedTarget, slashedVolume)
38 }
39
40 return nil
41 }
42
43
44
45 func deleteVolumeMountPoint(targetPath string) error {
46
47 slashedTarget := filepath.Clean(targetPath) + string(filepath.Separator)
48
49 targetP, err := windows.UTF16PtrFromString(slashedTarget)
50 if err != nil {
51 return errors.Wrapf(err, "unable to utf16-ise %s", slashedTarget)
52 }
53
54 if err := windows.DeleteVolumeMountPoint(targetP); err != nil {
55 return errors.Wrapf(err, "failed calling DeleteVolumeMountPoint('%s')", slashedTarget)
56 }
57
58 return nil
59 }
60
View as plain text