Skip to content

Commit 8da296c

Browse files
authored
Merge pull request #147 from Code-Hex/add/validateSaveRestoreSupport
added ValidateSaveRestoreSupport method of config
2 parents f86b595 + d4e78b9 commit 8da296c

File tree

4 files changed

+57
-1
lines changed

4 files changed

+57
-1
lines changed

configuration_arm64.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//go:build darwin && arm64
2+
// +build darwin,arm64
3+
4+
package vz
5+
6+
/*
7+
#cgo darwin CFLAGS: -mmacosx-version-min=11 -x objective-c -fno-objc-arc
8+
#cgo darwin LDFLAGS: -lobjc -framework Foundation -framework Virtualization
9+
# include "virtualization_14_arm64.h"
10+
*/
11+
import "C"
12+
import "github.com/Code-Hex/vz/v3/internal/objc"
13+
14+
// ValidateSaveRestoreSupport Determines whether the framework can save or restore the VM’s current configuration.
15+
//
16+
// Verify that a virtual machine with this configuration is savable.
17+
// Not all configuration options can be safely saved and restored from file.
18+
//
19+
// If this evaluates to false, the caller should expect future calls to `(*VirtualMachine).SaveMachineStateToPath` to fail.
20+
// error If not nil, assigned with an error describing the unsupported configuration option.
21+
func (v *VirtualMachineConfiguration) ValidateSaveRestoreSupport() (bool, error) {
22+
nserrPtr := newNSErrorAsNil()
23+
ret := C.validateSaveRestoreSupportWithError(objc.Ptr(v), &nserrPtr)
24+
err := newNSError(nserrPtr)
25+
if err != nil {
26+
return false, err
27+
}
28+
return (bool)(ret), nil
29+
}

virtualization_14_arm64.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@ uint32_t maximumPathLengthVZLinuxRosettaUnixSocketCachingOptions();
2222
uint32_t maximumNameLengthVZLinuxRosettaAbstractSocketCachingOptions();
2323
void setOptionsVZLinuxRosettaDirectoryShare(void *rosetta, void *cachingOptions);
2424
void *newVZMacKeyboardConfiguration();
25+
bool validateSaveRestoreSupportWithError(void *config, void **error);
2526
#endif

virtualization_14_arm64.m

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,4 +155,24 @@ void setOptionsVZLinuxRosettaDirectoryShare(void *rosetta, void *cachingOptions)
155155
}
156156
#endif
157157
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
158-
}
158+
}
159+
160+
/*!
161+
@abstract Validate the configuration is savable.
162+
@discussion
163+
Verify that a virtual machine with this configuration is savable.
164+
Not all configuration options can be safely saved and restored from file.
165+
If this evaluates to NO, the caller should expect future calls to saveMachineStateToURL:completionHandler: to fail.
166+
@param error If not nil, assigned with an error describing the unsupported configuration option.
167+
@return YES if the configuration is savable.
168+
*/
169+
bool validateSaveRestoreSupportWithError(void *config, void **error)
170+
{
171+
#ifdef INCLUDE_TARGET_OSX_14
172+
if (@available(macOS 14, *)) {
173+
return (bool)[(VZVirtualMachineConfiguration *)config
174+
validateSaveRestoreSupportWithError:(NSError *_Nullable *_Nullable)error];
175+
}
176+
#endif
177+
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
178+
}

virtualization_arm64.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,9 @@ func (m *MacOSInstaller) Done() <-chan struct{} { return m.doneCh }
587587
//
588588
// If this method is successful, the framework writes the file, and the VM state remains unchanged.
589589
//
590+
// Note that If you want to implement proper error handling, please make sure to call the
591+
// `(*VirtualMachineConfiguration).ValidateSaveRestoreSupport` method before calling this method.
592+
//
590593
// If you want to listen status change events, use the "StateChangedNotify" method.
591594
//
592595
// This is only supported on macOS 14 and newer, error will
@@ -615,6 +618,9 @@ func (v *VirtualMachine) SaveMachineStateToPath(saveFilePath string) error {
615618
//
616619
// If this method is successful, the framework restores the VM and places it in the paused state.
617620
//
621+
// Note that If you want to implement proper error handling, please make sure to call the
622+
// `(*VirtualMachineConfiguration).ValidateSaveRestoreSupport` method before calling this method.
623+
//
618624
// If you want to listen status change events, use the "StateChangedNotify" method.
619625
//
620626
// This is only supported on macOS 14 and newer, error will

0 commit comments

Comments
 (0)