Files
VirtualBuddy/VirtualInstallation/Source/Helpers.swift
Guilherme Rambo 71c570e31a Implement custom virtual machine restore mechanism (#688)
* Move ECID getter into VZMacMachineIdentifier

* Implement a custom macOS restore backend to work around macOS 27 issues on macOS 26 hosts

* Add virtual installation requirement set and enforce during VM restore

* Change requirement for macOS 27 Virtual Installation to min host macOS 26

* Add CI script that sets CURRENT_PROJECT_VERSION to the Xcode Cloud build number
2026-06-12 10:02:57 -03:00

89 lines
2.7 KiB
Swift

import Foundation
extension URL {
var safePath: String { absoluteURL.path(percentEncoded: false) }
var exists: Bool { FileManager.default.fileExists(atPath: path) }
var isExistingDirectory: Bool {
var isDir = ObjCBool(false)
guard FileManager.default.fileExists(atPath: path, isDirectory: &isDir) else { return false }
return isDir.boolValue
}
func ensureExistingDirectory(createIfNeeded: Bool = false) throws -> URL {
if !exists {
guard createIfNeeded else {
throw CocoaError(.fileNoSuchFile)
}
try FileManager.default.createDirectory(at: self, withIntermediateDirectories: true)
}
try requireExistingDirectory()
return self
}
func requireExistingDirectory() throws {
guard exists else {
throw CocoaError(.fileNoSuchFile)
}
guard isExistingDirectory else {
throw CocoaError(.fileReadInvalidFileName)
}
}
static var viApplicationSupportURL: URL {
do {
let url: URL
url = try FileManager.default.url(for: .applicationSupportDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
.appending(path: Bundle.main.bundleURL.deletingPathExtension().lastPathComponent, directoryHint: .isDirectory)
return try url.ensureExistingDirectory(createIfNeeded: true)
} catch {
assertionFailure("Failed to create application support directory: \(error)")
return URL(filePath: NSTemporaryDirectory())
}
}
}
extension PropertyListEncoder {
static let xpc: PropertyListEncoder = {
let e = PropertyListEncoder()
e.outputFormat = .binary
return e
}()
}
extension PropertyListDecoder {
static let xpc = PropertyListDecoder()
}
private final class _VILookupClass { }
extension Bundle {
static let virtualInstallation = Bundle(for: _VILookupClass.self)
}
extension ProcessInfo {
#if DEBUG
/// When `VI_TEST_MODE` is set to `1` in the environment, installation uses the test backend instead of attempting to restore a real device.
static let virtualInstallationTestModeEnabled: Bool = processInfo.environment["VI_TEST_MODE"] == "1"
#else
/// Test mode always disabled in release builds.
static let virtualInstallationTestModeEnabled = false
#endif
}
extension AMRestorableDeviceState: CustomStringConvertible {
public var description: String {
switch self {
case .unknown: return "Unknown"
case .DFU: return "DFU"
case .recovery: return "Recovery"
case .restoreOS: return "RestoreOS"
case .bootedOS: return "BootedOS"
@unknown default: return "Unknown(\(rawValue))"
}
}
}