xtool.git
Publicgit clone https://git.maproj.org/xtool.gitxtool.git / Sources / XKit / Installation / DDIMounter.swift
DDIMounter.swift198 lines · 6.3 KiBRaw
| 1 | #if false |
| 2 | |
| 3 | // |
| 4 | // DDIMounter.swift |
| 5 | // XKit |
| 6 | // |
| 7 | // Created by Kabir Oberai on 25/03/21. |
| 8 | // Copyright © 2021 Kabir Oberai. All rights reserved. |
| 9 | // |
| 10 | |
| 11 | import Foundation |
| 12 | import SwiftyMobileDevice |
| 13 | #if os(Linux) |
| 14 | import FoundationNetworking |
| 15 | #endif |
| 16 | |
| 17 | public final class DDIMounter { |
| 18 | |
| 19 | public struct DDILoc { |
| 20 | public let dmg: URL |
| 21 | public let signature: URL |
| 22 | |
| 23 | public init(dmg: URL, signature: URL) { |
| 24 | self.dmg = dmg |
| 25 | self.signature = signature |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | private class RequestDelegate: NSObject, URLSessionTaskDelegate, URLSessionDataDelegate { |
| 30 | enum Verdict { |
| 31 | case streamData(size: Int) |
| 32 | case downloadFull(URL) |
| 33 | case failed(Swift.Error) |
| 34 | } |
| 35 | |
| 36 | private var verdict: Verdict? |
| 37 | private let cache: URL |
| 38 | private let fileStream: OutputStream |
| 39 | private let pipedStream: OutputStream |
| 40 | private let onVerdict: (Verdict) -> Void |
| 41 | init(stream: OutputStream, cache: URL, onVerdict: @escaping (Verdict) -> Void) { |
| 42 | self.pipedStream = stream |
| 43 | self.cache = cache |
| 44 | self.fileStream = OutputStream(url: cache, append: false)! |
| 45 | self.onVerdict = onVerdict |
| 46 | stream.open() |
| 47 | fileStream.open() |
| 48 | } |
| 49 | |
| 50 | func urlSession( |
| 51 | _ session: URLSession, |
| 52 | dataTask: URLSessionDataTask, |
| 53 | didReceive response: URLResponse, |
| 54 | completionHandler: @escaping (URLSession.ResponseDisposition) -> Void |
| 55 | ) { |
| 56 | let len = response.expectedContentLength |
| 57 | completionHandler(.allow) |
| 58 | if len == -1 { |
| 59 | verdict = .downloadFull(cache) |
| 60 | } else { |
| 61 | let verdict: Verdict = .streamData(size: Int(len)) |
| 62 | self.verdict = verdict |
| 63 | onVerdict(verdict) |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) { |
| 68 | data.withUnsafeBytes { bytes in |
| 69 | let buf = bytes.bindMemory(to: UInt8.self) |
| 70 | _ = pipedStream.write(buf.baseAddress!, maxLength: buf.count) |
| 71 | _ = fileStream.write(buf.baseAddress!, maxLength: buf.count) |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Swift.Error?) { |
| 76 | pipedStream.close() |
| 77 | fileStream.close() |
| 78 | if case let .downloadFull(url) = verdict { |
| 79 | onVerdict(error.map(Verdict.failed) ?? .downloadFull(url)) |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | private enum MounterStatus: String, Decodable { |
| 85 | case complete = "Complete" |
| 86 | } |
| 87 | |
| 88 | private struct MountedImage: Decodable { |
| 89 | let status: MounterStatus |
| 90 | let signature: [Data]? |
| 91 | |
| 92 | private enum CodingKeys: String, CodingKey { |
| 93 | case signature = "ImageSignature" |
| 94 | case status = "Status" |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | private struct MountResult: Decodable { |
| 99 | let status: MounterStatus? |
| 100 | let error: String? |
| 101 | |
| 102 | private enum CodingKeys: String, CodingKey { |
| 103 | case status = "Status" |
| 104 | case error = "Error" |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | public struct Error: Swift.Error, LocalizedError { |
| 109 | public let message: String? |
| 110 | public var errorDescription: String? { message } |
| 111 | } |
| 112 | |
| 113 | private let client: MobileImageMounterClient |
| 114 | public init(connection: Connection) throws { |
| 115 | self.client = try connection.startClient() |
| 116 | } |
| 117 | |
| 118 | private func mount(file: InputStream, size: Int, signature: Data) throws { |
| 119 | file.open() |
| 120 | defer { file.close() } |
| 121 | |
| 122 | try client.upload(imageType: "Developer", file: file, size: size, signature: signature) |
| 123 | let result = try client.mount(imageType: "Developer", signature: signature, resultType: MountResult.self) |
| 124 | guard result.status == .complete else { |
| 125 | throw Error(message: result.error) |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | public func mountIfNeeded(local: DDILoc, fetchRemote: () throws -> DDILoc) throws { |
| 130 | #warning("we can't use CFSocketStream on Linux at all") |
| 131 | #if os(Linux) |
| 132 | // maybe just tear all of this down and download first, then mount the ddi. |
| 133 | fatalError("DDIMounter does not yet work on Linux") |
| 134 | #else |
| 135 | let mounted = try client.lookup(imageType: "Developer", resultType: MountedImage.self) |
| 136 | if mounted.signature != nil { return } |
| 137 | |
| 138 | if local.dmg.exists, |
| 139 | local.signature.exists, |
| 140 | let file = InputStream(url: local.dmg), |
| 141 | let fileSize = try? local.dmg.resourceValues(forKeys: [.fileSizeKey]).fileSize, |
| 142 | let signature = try? Data(contentsOf: local.signature) { |
| 143 | return try mount(file: file, size: fileSize, signature: signature) |
| 144 | } |
| 145 | |
| 146 | let remote = try fetchRemote() |
| 147 | |
| 148 | var istream: InputStream! |
| 149 | var ostream: OutputStream! |
| 150 | Stream.getBoundStreams(withBufferSize: 1024, inputStream: &istream, outputStream: &ostream) |
| 151 | |
| 152 | let group = DispatchGroup() |
| 153 | |
| 154 | var mode: RequestDelegate.Verdict! |
| 155 | group.enter() |
| 156 | let delegate = RequestDelegate(stream: ostream, cache: local.dmg) { m in |
| 157 | mode = m |
| 158 | group.leave() |
| 159 | } |
| 160 | |
| 161 | let dmgReq = URLRequest(url: remote.dmg) |
| 162 | let session = URLSession(configuration: .default, delegate: delegate, delegateQueue: nil) |
| 163 | session.dataTask(with: dmgReq).resume() |
| 164 | |
| 165 | let sigReq = URLRequest(url: remote.signature) |
| 166 | var sigRes: Result<Data, Swift.Error>! |
| 167 | group.enter() |
| 168 | URLSession.shared.dataTask(with: sigReq) { data, _, err in |
| 169 | if let data = data { |
| 170 | sigRes = .success(data) |
| 171 | } else { |
| 172 | sigRes = .failure(err ?? Error(message: nil)) |
| 173 | } |
| 174 | group.leave() |
| 175 | try? data?.write(to: local.signature) |
| 176 | }.resume() |
| 177 | |
| 178 | group.wait() |
| 179 | let signature = try sigRes.get() |
| 180 | |
| 181 | switch mode! { |
| 182 | case .downloadFull(let url): |
| 183 | let file = InputStream(url: url)! |
| 184 | let size = try url.resourceValues(forKeys: [.fileSizeKey]).fileSize! |
| 185 | try mount(file: file, size: size, signature: signature) |
| 186 | case .streamData(let size): |
| 187 | try mount(file: istream, size: size, signature: signature) |
| 188 | case .failed(let error): |
| 189 | throw error |
| 190 | } |
| 191 | |
| 192 | _ = delegate |
| 193 | #endif |
| 194 | } |
| 195 | |
| 196 | } |
| 197 | |
| 198 | #endif |