This repository has been archived by the owner on Oct 19, 2024. It is now read-only.
forked from Code-Hex/vz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shared_directory_arm64.go
234 lines (199 loc) · 7.49 KB
/
shared_directory_arm64.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
//go:build darwin && arm64
// +build darwin,arm64
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_13_arm64.h"
# include "virtualization_14_arm64.h"
*/
import "C"
import (
"fmt"
"os"
"runtime/cgo"
"unsafe"
"github.com/lima-vm/vz/v4/internal/objc"
)
// LinuxRosettaAvailability represents an availability of Rosetta support for Linux binaries.
//
//go:generate go run ./cmd/addtags -tags=darwin,arm64 -file linuxrosettaavailability_string_arm64.go stringer -type=LinuxRosettaAvailability -output=linuxrosettaavailability_string_arm64.go
type LinuxRosettaAvailability int
const (
// LinuxRosettaAvailabilityNotSupported Rosetta support for Linux binaries is not available on the host system.
LinuxRosettaAvailabilityNotSupported LinuxRosettaAvailability = iota
// LinuxRosettaAvailabilityNotInstalled Rosetta support for Linux binaries is not installed on the host system.
LinuxRosettaAvailabilityNotInstalled
// LinuxRosettaAvailabilityInstalled Rosetta support for Linux is installed on the host system.
LinuxRosettaAvailabilityInstalled
)
//export linuxInstallRosettaWithCompletionHandler
func linuxInstallRosettaWithCompletionHandler(cgoHandleUintptr C.uintptr_t, errPtr unsafe.Pointer) {
cgoHandle := cgo.Handle(cgoHandleUintptr)
handler := cgoHandle.Value().(func(error))
if err := newNSError(errPtr); err != nil {
handler(err)
} else {
handler(nil)
}
}
// LinuxRosettaDirectoryShare directory share to enable Rosetta support for Linux binaries.
// see: https://developer.apple.com/documentation/virtualization/vzlinuxrosettadirectoryshare?language=objc
type LinuxRosettaDirectoryShare struct {
*pointer
*baseDirectoryShare
}
var _ DirectoryShare = (*LinuxRosettaDirectoryShare)(nil)
// NewLinuxRosettaDirectoryShare creates a new Rosetta directory share if Rosetta support
// for Linux binaries is installed.
//
// This is only supported on macOS 13 and newer, error will
// be returned on older versions.
func NewLinuxRosettaDirectoryShare() (*LinuxRosettaDirectoryShare, error) {
if err := macOSAvailable(13); err != nil {
return nil, err
}
nserrPtr := newNSErrorAsNil()
ds := &LinuxRosettaDirectoryShare{
pointer: objc.NewPointer(
C.newVZLinuxRosettaDirectoryShare(&nserrPtr),
),
}
if err := newNSError(nserrPtr); err != nil {
return nil, err
}
objc.SetFinalizer(ds, func(self *LinuxRosettaDirectoryShare) {
objc.Release(self)
})
return ds, nil
}
// SetOptions enables translation caching and configure the socket communication type for Rosetta.
//
// This is only supported on macOS 14 and newer. Older versions do nothing.
func (ds *LinuxRosettaDirectoryShare) SetOptions(options LinuxRosettaCachingOptions) {
if err := macOSAvailable(14); err != nil {
return
}
C.setOptionsVZLinuxRosettaDirectoryShare(objc.Ptr(ds), objc.Ptr(options))
}
// LinuxRosettaDirectoryShareInstallRosetta download and install Rosetta support
// for Linux binaries if necessary.
//
// This is only supported on macOS 13 and newer, error will
// be returned on older versions.
func LinuxRosettaDirectoryShareInstallRosetta() error {
if err := macOSAvailable(13); err != nil {
return err
}
errCh := make(chan error, 1)
cgoHandle := cgo.NewHandle(func(err error) {
errCh <- err
})
C.linuxInstallRosetta(C.uintptr_t(cgoHandle))
return <-errCh
}
// LinuxRosettaDirectoryShareAvailability checks the availability of Rosetta support
// for the directory share.
//
// This is only supported on macOS 13 and newer, LinuxRosettaAvailabilityNotSupported will
// be returned on older versions.
func LinuxRosettaDirectoryShareAvailability() LinuxRosettaAvailability {
if err := macOSAvailable(13); err != nil {
return LinuxRosettaAvailabilityNotSupported
}
return LinuxRosettaAvailability(C.availabilityVZLinuxRosettaDirectoryShare())
}
// LinuxRosettaCachingOptions for a directory sharing device configuration.
type LinuxRosettaCachingOptions interface {
objc.NSObject
linuxRosettaCachingOptions()
}
type baseLinuxRosettaCachingOptions struct{}
func (*baseLinuxRosettaCachingOptions) linuxRosettaCachingOptions() {}
// LinuxRosettaUnixSocketCachingOptions is an struct that represents caching options
// for a UNIX domain socket.
//
// This struct configures Rosetta to communicate with the Rosetta daemon using a UNIX domain socket.
type LinuxRosettaUnixSocketCachingOptions struct {
*pointer
*baseLinuxRosettaCachingOptions
}
var _ LinuxRosettaCachingOptions = (*LinuxRosettaUnixSocketCachingOptions)(nil)
// NewLinuxRosettaUnixSocketCachingOptions creates a new Rosetta caching options object for
// a UNIX domain socket with the path you specify.
//
// The path of the Unix Domain Socket to be used to communicate with the Rosetta translation daemon.
//
// This is only supported on macOS 14 and newer, error will
// be returned on older versions.
func NewLinuxRosettaUnixSocketCachingOptions(path string) (*LinuxRosettaUnixSocketCachingOptions, error) {
if err := macOSAvailable(14); err != nil {
return nil, err
}
maxPathLen := maximumPathLengthLinuxRosettaUnixSocketCachingOptions()
if maxPathLen < len(path) {
return nil, fmt.Errorf("path length exceeds maximum allowed length of %d", maxPathLen)
}
if _, err := os.Stat(path); err != nil {
return nil, fmt.Errorf("invalid path: %w", err)
}
cs := charWithGoString(path)
defer cs.Free()
nserrPtr := newNSErrorAsNil()
usco := &LinuxRosettaUnixSocketCachingOptions{
pointer: objc.NewPointer(
C.newVZLinuxRosettaUnixSocketCachingOptionsWithPath(cs.CString(), &nserrPtr),
),
}
if err := newNSError(nserrPtr); err != nil {
return nil, err
}
objc.SetFinalizer(usco, func(self *LinuxRosettaUnixSocketCachingOptions) {
objc.Release(self)
})
return usco, nil
}
func maximumPathLengthLinuxRosettaUnixSocketCachingOptions() int {
return int(uint32(C.maximumPathLengthVZLinuxRosettaUnixSocketCachingOptions()))
}
// LinuxRosettaAbstractSocketCachingOptions is caching options for an abstract socket.
//
// Use this object to configure Rosetta to communicate with the Rosetta daemon using an abstract socket.
type LinuxRosettaAbstractSocketCachingOptions struct {
*pointer
*baseLinuxRosettaCachingOptions
}
var _ LinuxRosettaCachingOptions = (*LinuxRosettaAbstractSocketCachingOptions)(nil)
// NewLinuxRosettaAbstractSocketCachingOptions creates a new LinuxRosettaAbstractSocketCachingOptions.
//
// The name of the Abstract Socket to be used to communicate with the Rosetta translation daemon.
//
// This is only supported on macOS 14 and newer, error will
// be returned on older versions.
func NewLinuxRosettaAbstractSocketCachingOptions(name string) (*LinuxRosettaAbstractSocketCachingOptions, error) {
if err := macOSAvailable(14); err != nil {
return nil, err
}
maxNameLen := maximumNameLengthVZLinuxRosettaAbstractSocketCachingOptions()
if maxNameLen < len(name) {
return nil, fmt.Errorf("name length exceeds maximum allowed length of %d", maxNameLen)
}
cs := charWithGoString(name)
defer cs.Free()
nserrPtr := newNSErrorAsNil()
asco := &LinuxRosettaAbstractSocketCachingOptions{
pointer: objc.NewPointer(
C.newVZLinuxRosettaAbstractSocketCachingOptionsWithName(cs.CString(), &nserrPtr),
),
}
if err := newNSError(nserrPtr); err != nil {
return nil, err
}
objc.SetFinalizer(asco, func(self *LinuxRosettaAbstractSocketCachingOptions) {
objc.Release(self)
})
return asco, nil
}
func maximumNameLengthVZLinuxRosettaAbstractSocketCachingOptions() int {
return int(uint32(C.maximumNameLengthVZLinuxRosettaAbstractSocketCachingOptions()))
}