-
-
Notifications
You must be signed in to change notification settings - Fork 48
/
shared_directory.go
184 lines (154 loc) · 5.1 KB
/
shared_directory.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package vz
/*
#cgo darwin CFLAGS: -mmacosx-version-min=11 -x objective-c -fno-objc-arc
#cgo darwin LDFLAGS: -lobjc -framework Foundation -framework Virtualization
# include "virtualization_11.h"
# include "virtualization_12.h"
# include "virtualization_13.h"
*/
import "C"
import (
"os"
"github.com/Code-Hex/vz/v3/internal/objc"
)
// DirectorySharingDeviceConfiguration for a directory sharing device configuration.
type DirectorySharingDeviceConfiguration interface {
objc.NSObject
directorySharingDeviceConfiguration()
}
type baseDirectorySharingDeviceConfiguration struct{}
func (*baseDirectorySharingDeviceConfiguration) directorySharingDeviceConfiguration() {}
var _ DirectorySharingDeviceConfiguration = (*VirtioFileSystemDeviceConfiguration)(nil)
// VirtioFileSystemDeviceConfiguration is a configuration of a Virtio file system device.
//
// see: https://developer.apple.com/documentation/virtualization/vzvirtiofilesystemdeviceconfiguration?language=objc
type VirtioFileSystemDeviceConfiguration struct {
*pointer
*baseDirectorySharingDeviceConfiguration
}
// NewVirtioFileSystemDeviceConfiguration create a new VirtioFileSystemDeviceConfiguration.
//
// This is only supported on macOS 12 and newer, error will
// be returned on older versions.
func NewVirtioFileSystemDeviceConfiguration(tag string) (*VirtioFileSystemDeviceConfiguration, error) {
if err := macOSAvailable(12); err != nil {
return nil, err
}
tagChar := charWithGoString(tag)
defer tagChar.Free()
nserrPtr := newNSErrorAsNil()
fsdConfig := &VirtioFileSystemDeviceConfiguration{
pointer: objc.NewPointer(
C.newVZVirtioFileSystemDeviceConfiguration(tagChar.CString(), &nserrPtr),
),
}
if err := newNSError(nserrPtr); err != nil {
return nil, err
}
objc.SetFinalizer(fsdConfig, func(self *VirtioFileSystemDeviceConfiguration) {
objc.Release(self)
})
return fsdConfig, nil
}
// SetDirectoryShare sets the directory share associated with this configuration.
func (c *VirtioFileSystemDeviceConfiguration) SetDirectoryShare(share DirectoryShare) {
C.setVZVirtioFileSystemDeviceConfigurationShare(objc.Ptr(c), objc.Ptr(share))
}
// SharedDirectory is a shared directory.
type SharedDirectory struct {
*pointer
}
// NewSharedDirectory creates a new shared directory.
//
// This is only supported on macOS 12 and newer, error will
// be returned on older versions.
func NewSharedDirectory(dirPath string, readOnly bool) (*SharedDirectory, error) {
if err := macOSAvailable(12); err != nil {
return nil, err
}
if _, err := os.Stat(dirPath); err != nil {
return nil, err
}
dirPathChar := charWithGoString(dirPath)
defer dirPathChar.Free()
sd := &SharedDirectory{
pointer: objc.NewPointer(
C.newVZSharedDirectory(dirPathChar.CString(), C.bool(readOnly)),
),
}
objc.SetFinalizer(sd, func(self *SharedDirectory) {
objc.Release(self)
})
return sd, nil
}
// DirectoryShare is the base interface for a directory share.
type DirectoryShare interface {
objc.NSObject
directoryShare()
}
type baseDirectoryShare struct{}
func (*baseDirectoryShare) directoryShare() {}
var _ DirectoryShare = (*SingleDirectoryShare)(nil)
// SingleDirectoryShare defines the directory share for a single directory.
type SingleDirectoryShare struct {
*pointer
*baseDirectoryShare
}
// NewSingleDirectoryShare creates a new single directory share.
//
// This is only supported on macOS 12 and newer, error will
// be returned on older versions.
func NewSingleDirectoryShare(share *SharedDirectory) (*SingleDirectoryShare, error) {
if err := macOSAvailable(12); err != nil {
return nil, err
}
config := &SingleDirectoryShare{
pointer: objc.NewPointer(
C.newVZSingleDirectoryShare(objc.Ptr(share)),
),
}
objc.SetFinalizer(config, func(self *SingleDirectoryShare) {
objc.Release(self)
})
return config, nil
}
// MultipleDirectoryShare defines the directory share for multiple directories.
type MultipleDirectoryShare struct {
*pointer
*baseDirectoryShare
}
var _ DirectoryShare = (*MultipleDirectoryShare)(nil)
// NewMultipleDirectoryShare creates a new multiple directories share.
//
// This is only supported on macOS 12 and newer, error will
// be returned on older versions.
func NewMultipleDirectoryShare(shares map[string]*SharedDirectory) (*MultipleDirectoryShare, error) {
if err := macOSAvailable(12); err != nil {
return nil, err
}
directories := make(map[string]objc.NSObject, len(shares))
for k, v := range shares {
directories[k] = v
}
dict := objc.ConvertToNSMutableDictionary(directories)
config := &MultipleDirectoryShare{
pointer: objc.NewPointer(
C.newVZMultipleDirectoryShare(objc.Ptr(dict)),
),
}
objc.SetFinalizer(config, func(self *MultipleDirectoryShare) {
objc.Release(self)
})
return config, nil
}
// MacOSGuestAutomountTag returns the macOS automount tag.
//
// A device configured with this tag will be automatically mounted in a macOS guest.
// This is only supported on macOS 13 and newer, error will be returned on older versions.
func MacOSGuestAutomountTag() (string, error) {
if err := macOSAvailable(13); err != nil {
return "", err
}
cstring := (*char)(C.getMacOSGuestAutomountTag())
return cstring.String(), nil
}