UTMDownloadIPSWTask.swift 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. //
  2. // Copyright © 2022 osy. 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. import Virtualization
  18. /// Downloads an IPSW from the web and adds it to the VM.
  19. @available(iOS, unavailable, message: "Apple Virtualization not available on iOS")
  20. @available(macOS 12, *)
  21. class UTMDownloadIPSWTask: UTMDownloadTask {
  22. let config: UTMAppleConfiguration
  23. private var cacheUrl: URL {
  24. fileManager.urls(for: .cachesDirectory, in: .userDomainMask).first!
  25. }
  26. @MainActor init(for config: UTMAppleConfiguration) {
  27. self.config = config
  28. super.init(for: config.system.boot.macRecoveryIpswURL!, named: config.information.name)
  29. }
  30. override func processCompletedDownload(at location: URL, response: URLResponse?) async throws -> any UTMVirtualMachine {
  31. if !fileManager.fileExists(atPath: cacheUrl.path) {
  32. try fileManager.createDirectory(at: cacheUrl, withIntermediateDirectories: false)
  33. }
  34. let cacheIpsw = cacheUrl.appendingPathComponent(url.lastPathComponent)
  35. if fileManager.fileExists(atPath: cacheIpsw.path) {
  36. try fileManager.removeItem(at: cacheIpsw)
  37. }
  38. try fileManager.moveItem(at: location, to: cacheIpsw)
  39. await MainActor.run {
  40. config.system.boot.macRecoveryIpswURL = cacheIpsw
  41. }
  42. return try UTMAppleVirtualMachine(newForConfiguration: config, destinationUrl: UTMData.defaultStorageUrl)
  43. }
  44. }