Skip to content

Run a virtual iPhone on an Apple Silicon Mac.

Swift

Keep an import running when its progress reader goes away

Lakr6f90a2f9049f
diff --git a/VPhoneExecutable/VPhoneCommand/VPhoneCommand/Support/VPhoneProgressBar.swift b/VPhoneExecutable/VPhoneCommand/VPhoneCommand/Support/VPhoneProgressBar.swift
index c0112ff5..8c234612 100644
--- a/VPhoneExecutable/VPhoneCommand/VPhoneCommand/Support/VPhoneProgressBar.swift
+++ b/VPhoneExecutable/VPhoneCommand/VPhoneCommand/Support/VPhoneProgressBar.swift
@@ -60,7 +60,7 @@ final class VPhoneProgressBar: @unchecked Sendable {
return
case .bar:
render(done: total, now: Date())
- FileHandle.standardError.write(Data("\n".utf8))
+ write("\n")
case .lines:
render(done: total, now: Date())
}
@@ -68,7 +68,7 @@ final class VPhoneProgressBar: @unchecked Sendable {
private func render(done: Int64, now: Date) {
if mode == .lines {
- FileHandle.standardError.write(Data("progress \(done) \(total)\n".utf8))
+ write("progress \(done) \(total)\n")
return
}
let frac = total > 0 ? min(1.0, Double(done) / Double(total)) : 0
@@ -88,7 +88,14 @@ final class VPhoneProgressBar: @unchecked Sendable {
line += " eta \(Self.clock(Double(total - done) / rate))"
}
line += "\u{1B}[K" // clear to end of line
- FileHandle.standardError.write(Data(line.utf8))
+ write(line)
+ }
+
+ /// Progress is best effort. A reader that went away, such as a quit
+ /// vphone-launchpad, leaves stderr a broken pipe; `write(_:)` raises an
+ /// Objective-C exception for that and aborts the transfer under way.
+ private func write(_ text: String) {
+ try? FileHandle.standardError.write(contentsOf: Data(text.utf8))
}
static func bytes(_ n: Int64) -> String {
diff --git a/VPhoneExecutable/VPhoneCommand/VPhoneCommand/VirtualMachine/VPhoneVirtualMachineTransferCommand.swift b/VPhoneExecutable/VPhoneCommand/VPhoneCommand/VirtualMachine/VPhoneVirtualMachineTransferCommand.swift
index db9f9f36..144c67da 100644
--- a/VPhoneExecutable/VPhoneCommand/VPhoneCommand/VirtualMachine/VPhoneVirtualMachineTransferCommand.swift
+++ b/VPhoneExecutable/VPhoneCommand/VPhoneCommand/VirtualMachine/VPhoneVirtualMachineTransferCommand.swift
@@ -90,11 +90,16 @@ struct VPhoneVirtualMachineImportCommand: ParsableCommand {
func run() throws {
let bar = VPhoneProgressBar(label: "importing")
+ // SIGINT (Ctrl-C, or vphone-launchpad stopping the import) and SIGTERM
+ // stop the extraction between blocks, so the staging folder is removed
+ // rather than left in the library at the size it had reached.
+ let stop = VPhoneImportInterruption()
let bundle = try VPhoneBundleTransfer.importArchive(
from: URL(fileURLWithPath: input),
name: name,
in: lib.library,
progress: { done, total in bar.update(done: done, total: total) },
+ isCancelled: { stop.isRequested },
)
try VPhoneHostFilePermissions.makeAccessible(at: bundle.url)
try VPhoneHostFilePermissions.makeDirectoryAccessible(at: lib.library.root)
@@ -103,3 +108,31 @@ struct VPhoneVirtualMachineImportCommand: ParsableCommand {
print("imported → \(bundle.name)")
}
}
+
+// MARK: - Interruption
+
+/// Turns SIGINT and SIGTERM into a flag the import polls. The signals are
+/// ignored for the rest of the process: once the extraction has returned, the
+/// remaining steps are a rename and permission changes, quicker to finish
+/// than to undo.
+private final class VPhoneImportInterruption: @unchecked Sendable {
+ private let lock = NSLock()
+ private var requested = false
+ private var sources: [DispatchSourceSignal] = []
+
+ init() {
+ for signalNumber in [SIGINT, SIGTERM] {
+ signal(signalNumber, SIG_IGN)
+ let source = DispatchSource.makeSignalSource(signal: signalNumber, queue: .global())
+ source.setEventHandler { [self] in
+ lock.withLock { requested = true }
+ }
+ source.resume()
+ sources.append(source)
+ }
+ }
+
+ var isRequested: Bool {
+ lock.withLock { requested }
+ }
+}
diff --git a/VPhoneKit/VPhoneArchiveKit/Transfer/VPhoneBundleTransfer.swift b/VPhoneKit/VPhoneArchiveKit/Transfer/VPhoneBundleTransfer.swift
index 0a3daf09..7817d3a9 100644
--- a/VPhoneKit/VPhoneArchiveKit/Transfer/VPhoneBundleTransfer.swift
+++ b/VPhoneKit/VPhoneArchiveKit/Transfer/VPhoneBundleTransfer.swift
@@ -192,6 +192,7 @@ public enum VPhoneBundleTransfer {
name: String?,
in library: VPhoneLibrary,
progress: ((Int64, Int64) -> Void)? = nil,
+ isCancelled: (() -> Bool)? = nil,
) throws -> VPhoneBundle {
let fm = FileManager.default
// Fail fast when the destination name is already known (explicit rename).
@@ -222,6 +223,7 @@ public enum VPhoneBundleTransfer {
into: staging,
options: .intoHostDirectory,
bytesRead: progress.map { report in { done in report(done, total) } },
+ isCancelled: isCancelled,
)
// A tar reader stops at the end-of-archive marker, which in a
// compressed file can sit short of the last byte, so the read position