|
| 1 | +// |
| 2 | +// Copyright © 2025 naveenrajm7. All rights reserved. |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | +// |
| 16 | + |
| 17 | +import Foundation |
| 18 | + |
| 19 | +@objc extension UTMScriptingVirtualMachineImpl { |
| 20 | + @objc var registry: [URL] { |
| 21 | + let wrapper = UTMScriptingRegistryEntryImpl(vm.registryEntry) |
| 22 | + return wrapper.serializeRegistry() |
| 23 | + } |
| 24 | + |
| 25 | + @objc func updateRegistry(_ command: NSScriptCommand) { |
| 26 | + let newRegistry = command.evaluatedArguments?["newRegistry"] as? [URL] |
| 27 | + withScriptCommand(command) { [self] in |
| 28 | + guard let newRegistry = newRegistry else { |
| 29 | + throw ScriptingError.invalidParameter |
| 30 | + } |
| 31 | + let wrapper = UTMScriptingRegistryEntryImpl(vm.registryEntry) |
| 32 | + try await wrapper.updateRegistry(from: newRegistry, qemuProcess) |
| 33 | + } |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +@MainActor |
| 38 | +class UTMScriptingRegistryEntryImpl { |
| 39 | + private(set) var registry: UTMRegistryEntry |
| 40 | + |
| 41 | + init(_ registry: UTMRegistryEntry) { |
| 42 | + self.registry = registry |
| 43 | + } |
| 44 | + |
| 45 | + func serializeRegistry() -> [URL] { |
| 46 | + return registry.sharedDirectories.compactMap { $0.url } |
| 47 | + } |
| 48 | + |
| 49 | + func updateRegistry(from fileUrls: [URL], _ system: UTMQemuSystem?) async throws { |
| 50 | + // Clear all shared directories, we add all directories here |
| 51 | + registry.removeAllSharedDirectories() |
| 52 | + |
| 53 | + // Add urls to the registry |
| 54 | + for url in fileUrls { |
| 55 | + // Start scoped access |
| 56 | + let isScopedAccess = url.startAccessingSecurityScopedResource() |
| 57 | + defer { |
| 58 | + if isScopedAccess { |
| 59 | + url.stopAccessingSecurityScopedResource() |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + // Get bookmark from UTM process |
| 64 | + let standardBookmark = try url.bookmarkData() |
| 65 | + let system = system ?? UTMProcess() |
| 66 | + let (success, bookmark, path) = await system.accessData(withBookmark: standardBookmark, securityScoped: false) |
| 67 | + guard let bookmark = bookmark, let _ = path, success else { |
| 68 | + throw UTMQemuVirtualMachineError.accessDriveImageFailed |
| 69 | + } |
| 70 | + |
| 71 | + // Store bookmark in registry |
| 72 | + let file = UTMRegistryEntry.File(dummyFromPath: url.path, remoteBookmark: bookmark) |
| 73 | + registry.appendSharedDirectory(file) |
| 74 | + } |
| 75 | + |
| 76 | + } |
| 77 | +} |
0 commit comments