2
0

UTMScriptingRegistryEntryImpl.swift 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. import Foundation
  17. @objc extension UTMScriptingVirtualMachineImpl {
  18. @objc var registry: [URL] {
  19. let wrapper = UTMScriptingRegistryEntryImpl(vm.registryEntry)
  20. return wrapper.serializeRegistry()
  21. }
  22. @objc func updateRegistry(_ command: NSScriptCommand) {
  23. let newRegistry = command.evaluatedArguments?["newRegistry"] as? [URL]
  24. withScriptCommand(command) { [self] in
  25. guard let newRegistry = newRegistry else {
  26. throw ScriptingError.invalidParameter
  27. }
  28. let wrapper = UTMScriptingRegistryEntryImpl(vm.registryEntry)
  29. try await wrapper.updateRegistry(from: newRegistry, qemuProcess)
  30. }
  31. }
  32. }
  33. @MainActor
  34. class UTMScriptingRegistryEntryImpl {
  35. private(set) var registry: UTMRegistryEntry
  36. init(_ registry: UTMRegistryEntry) {
  37. self.registry = registry
  38. }
  39. func serializeRegistry() -> [URL] {
  40. return registry.sharedDirectories.compactMap { $0.url }
  41. }
  42. func updateRegistry(from fileUrls: [URL], _ system: UTMQemuSystem?) async throws {
  43. // Clear all shared directories, we add all directories here
  44. registry.removeAllSharedDirectories()
  45. // Add urls to the registry
  46. for url in fileUrls {
  47. // Start scoped access
  48. let isScopedAccess = url.startAccessingSecurityScopedResource()
  49. defer {
  50. if isScopedAccess {
  51. url.stopAccessingSecurityScopedResource()
  52. }
  53. }
  54. // Get bookmark from UTM process
  55. let standardBookmark = try url.bookmarkData()
  56. let system = system ?? UTMProcess()
  57. let (success, bookmark, path) = await system.accessData(withBookmark: standardBookmark, securityScoped: false)
  58. guard let bookmark = bookmark, let _ = path, success else {
  59. throw UTMQemuVirtualMachineError.accessDriveImageFailed
  60. }
  61. // Store bookmark in registry
  62. let file = UTMRegistryEntry.File(dummyFromPath: url.path, remoteBookmark: bookmark)
  63. registry.appendSharedDirectory(file)
  64. }
  65. }
  66. }