Run a virtual iPhone on an Apple Silicon Mac.
Swift
cli: Add byte-counting tar pipeline + progress-bar primitives
zqxwce673146ba0a3a
diff --git a/sources/VPhoneCore/VPhoneProcessRunner.swift b/sources/VPhoneCore/VPhoneProcessRunner.swiftindex 82711973..800351dd 100644--- a/sources/VPhoneCore/VPhoneProcessRunner.swift+++ b/sources/VPhoneCore/VPhoneProcessRunner.swift@@ -79,6 +79,97 @@ public enum VPhoneProcessRunner {stderr: String(decoding: errBox.take(), as: UTF8.self))}+ /// Stream an archive through a `/usr/bin/tar` consumer that reads on stdin,+ /// invoking `onBytes` with the running byte total so a caller can drive a+ /// progress bar off the *uncompressed* (export) or *compressed* (import)+ /// stream. The byte source is exactly one of:+ /// - `producerArgs`: a `tar` producing an uncompressed archive to a pipe+ /// (export → count uncompressed input; the consumer compresses via `@-`).+ /// - `sourceFile`: the archive file read directly (import → count the file+ /// as it is fed into `tar -x`).+ /// Returns the stderr of whichever stage exited nonzero (consumer first), or+ /// `nil` on success. SIGPIPE is ignored for the duration so a consumer that+ /// dies early surfaces as its exit status rather than killing this process.+ public static func runCountingTarPipe(+ producerArgs: [String]?,+ sourceFile: URL?,+ consumerArgs: [String],+ onBytes: ((Int64) -> Void)? = nil+ ) throws -> String? {+ let tar = URL(fileURLWithPath: "/usr/bin/tar")+ let prevPIPE = signal(SIGPIPE, SIG_IGN)+ defer { signal(SIGPIPE, prevPIPE) }++ let group = DispatchGroup()++ let consumer = Process()+ consumer.executableURL = tar+ consumer.arguments = consumerArgs+ let cIn = Pipe()+ consumer.standardInput = cIn+ consumer.standardOutput = FileHandle.nullDevice+ let cErr = Pipe()+ consumer.standardError = cErr+ let cErrBox = DataBox()+ group.enter()+ cErr.fileHandleForReading.readabilityHandler = { handle in+ let chunk = handle.availableData+ if chunk.isEmpty { handle.readabilityHandler = nil; group.leave() }+ else { cErrBox.append(chunk) }+ }++ var producer: Process?+ let source: FileHandle+ let pErrBox = DataBox()+ if let producerArgs {+ let p = Process()+ p.executableURL = tar+ p.arguments = producerArgs+ let pOut = Pipe()+ p.standardOutput = pOut+ let pErr = Pipe()+ p.standardError = pErr+ group.enter()+ pErr.fileHandleForReading.readabilityHandler = { handle in+ let chunk = handle.availableData+ if chunk.isEmpty { handle.readabilityHandler = nil; group.leave() }+ else { pErrBox.append(chunk) }+ }+ producer = p+ source = pOut.fileHandleForReading+ } else if let sourceFile {+ source = try FileHandle(forReadingFrom: sourceFile)+ } else {+ preconditionFailure("runCountingTarPipe: producerArgs or sourceFile required")+ }++ try consumer.run()+ try producer?.run()++ let sink = cIn.fileHandleForWriting+ var total: Int64 = 0+ while true {+ guard let chunk = try? source.read(upToCount: 1 << 20), !chunk.isEmpty else { break }+ do { try sink.write(contentsOf: chunk) } catch { break } // consumer died; status below+ total += Int64(chunk.count)+ onBytes?(total)+ }+ try? sink.close()+ try? source.close()++ producer?.waitUntilExit()+ consumer.waitUntilExit()+ group.wait()++ if consumer.terminationStatus != 0 {+ return String(decoding: cErrBox.take(), as: UTF8.self)+ }+ if let producer, producer.terminationStatus != 0 {+ return String(decoding: pErrBox.take(), as: UTF8.self)+ }+ return nil+ }+/// Run `executable args`, inheriting the parent's stdout/stderr so output/// streams live to the terminal (for long-running tools: downloads, restore,/// CFW install). Returns the child's exit status; throws only on spawn failure.diff --git a/sources/vphone-cli/VPhoneProgressBar.swift b/sources/vphone-cli/VPhoneProgressBar.swiftnew file mode 100644index 00000000..5c2bbdc3--- /dev/null+++ b/sources/vphone-cli/VPhoneProgressBar.swift@@ -0,0 +1,64 @@+import Foundation++// MARK: - VPhoneProgressBar++/// A single-line, redrawing byte progress bar for long transfers. Renders to+/// stderr only when it is a TTY, so piped/`--json`/GUI-subprocess invocations+/// (stdout consumed elsewhere) stay clean and the bar simply no-ops.+final class VPhoneProgressBar {+ private let label: String+ private let enabled: Bool+ private let start = Date()+ private let width = 28+ private var lastRender = Date.distantPast+ private var total: Int64 = 0++ init(label: String) {+ self.label = label+ self.enabled = isatty(FileHandle.standardError.fileDescriptor) != 0+ }++ func update(done: Int64, total: Int64) {+ guard enabled else { return }+ self.total = total+ let now = Date()+ if done < total, now.timeIntervalSince(lastRender) < 0.066 { return } // ~15 fps+ lastRender = now+ render(done: done, now: now)+ }++ func finish() {+ guard enabled else { return }+ render(done: total, now: Date())+ FileHandle.standardError.write(Data("\n".utf8))+ }++ private func render(done: Int64, now: Date) {+ let frac = total > 0 ? min(1.0, Double(done) / Double(total)) : 0+ let filled = Int(frac * Double(width))+ let bar = String(repeating: "█", count: filled) + String(repeating: "░", count: width - filled)+ let elapsed = now.timeIntervalSince(start)+ let rate = elapsed > 0 ? Double(done) / elapsed : 0++ var line = "\r\(label) [\(bar)] \(Int(frac * 100))% \(Self.bytes(done))"+ if total > 0 { line += "/\(Self.bytes(total))" }+ if rate > 0 { line += " \(Self.bytes(Int64(rate)))/s" }+ if total > 0, rate > 0, done < total { line += " eta \(Self.clock(Double(total - done) / rate))" }+ line += "\u{1B}[K" // clear to end of line+ FileHandle.standardError.write(Data(line.utf8))+ }++ static func bytes(_ n: Int64) -> String {+ let units = ["B", "KB", "MB", "GB", "TB"]+ var value = Double(n), i = 0+ while value >= 1024, i < units.count - 1 { value /= 1024; i += 1 }+ return i == 0 ? "\(n) B" : String(format: "%.1f %@", value, units[i])+ }++ static func clock(_ seconds: Double) -> String {+ let s = Int(seconds.rounded())+ return s >= 3600+ ? String(format: "%d:%02d:%02d", s / 3600, (s % 3600) / 60, s % 60)+ : String(format: "%02d:%02d", s / 60, s % 60)+ }+}