Skip to content

Run a virtual iPhone on an Apple Silicon Mac.

Swift

Check again after a clone's copy that the source is still stopped

Jacky6ed495430310
diff --git a/VPhoneKit/VPhoneCoreKit/Bundle/VPhoneBundleOperations.swift b/VPhoneKit/VPhoneCoreKit/Bundle/VPhoneBundleOperations.swift
index 0dfcc4e2..6d37fca5 100644
--- a/VPhoneKit/VPhoneCoreKit/Bundle/VPhoneBundleOperations.swift
+++ b/VPhoneKit/VPhoneCoreKit/Bundle/VPhoneBundleOperations.swift
@@ -185,6 +185,18 @@ public enum VPhoneBundleOperations {
to newName: String,
in library: VPhoneLibrary,
newIdentity: Bool = false,
+ ) throws -> VPhoneBundle {
+ try clone(bundleNamed: name, to: newName, in: library, newIdentity: newIdentity, afterCopy: {})
+ }
+
+ /// `afterCopy` runs between the copy and the second running check, so
+ /// tests can start the source while it is being copied.
+ static func clone(
+ bundleNamed name: String,
+ to newName: String,
+ in library: VPhoneLibrary,
+ newIdentity: Bool,
+ afterCopy: () throws -> Void,
) throws -> VPhoneBundle {
try requireValidName(newName)
let source = try library.bundle(named: name)
@@ -194,6 +206,10 @@ public enum VPhoneBundleOperations {
if fm.fileExists(atPath: dst.path) {
throw VPhoneLibraryError.alreadyExists(name: newName)
}
+ // Checked before the copy, so a running source is refused without
+ // copying anything, and again after it: the source may have started
+ // while it was copied (off APFS that takes minutes), and the copy may
+ // then hold state files from different moments.
try VPhoneBundleActivity.requireStopped(source)
// Roll back a half-made copy, so a retry with the same name is not
@@ -213,6 +229,8 @@ public enum VPhoneBundleOperations {
try fm.copyItem(at: src.appendingPathComponent(entry), to: dst.appendingPathComponent(entry))
}
}
+ try afterCopy()
+ try VPhoneBundleActivity.requireStopped(source)
var clone = try VPhoneBundle.load(at: dst)
if newIdentity {
clone = try resetIdentity(of: clone, clonedFrom: name)
diff --git a/VPhoneKit/VPhoneCoreKitTests/Bundle/BundleOperationsTests.swift b/VPhoneKit/VPhoneCoreKitTests/Bundle/BundleOperationsTests.swift
index f3236452..24e4a513 100644
--- a/VPhoneKit/VPhoneCoreKitTests/Bundle/BundleOperationsTests.swift
+++ b/VPhoneKit/VPhoneCoreKitTests/Bundle/BundleOperationsTests.swift
@@ -517,6 +517,26 @@ struct BundleOperationsTests {
}
}
+ @Test func `clone refuses a source that starts while it is copied and leaves no copy`() throws {
+ let root = try makeRoot()
+ defer { try? FileManager.default.removeItem(at: root) }
+ let lib = VPhoneLibrary(root: root)
+ let src = try makeBootedSource(named: "src", in: lib)
+
+ for name in ["Disk.img", "SEPStorage", "nvram.bin"] {
+ var fd: Int32 = -1
+ defer { if fd >= 0 { close(fd) } }
+ #expect(throws: VPhoneBundleActivityError.running(name: "src", pids: [getpid()])) {
+ try VPhoneBundleOperations.clone(bundleNamed: "src", to: "dst", in: lib, newIdentity: false) {
+ // The source starts after the first check passed.
+ fd = open(src.url.appendingPathComponent(name).path, O_RDONLY)
+ }
+ }
+ #expect(fd >= 0)
+ #expect(!FileManager.default.fileExists(atPath: lib.url(forName: "dst").path))
+ }
+ }
+
@Test func `lists the network settings a new identity clears`() {
typealias Network = VPhoneVirtualMachineManifest.NetworkConfig
#expect(VPhoneBundleOperations.networkSettingsClearedByNewIdentity(.default, sourceName: "src").isEmpty)