Skip to content

Run a virtual iPhone on an Apple Silicon Mac.

Swift

Publish the patched restore tree as clones, not copies

Maximilian Paßf8ddb5f171f5
diff --git a/VPhoneKit/VPhoneCoreKit/Process/VPhoneConfinedDirectory.swift b/VPhoneKit/VPhoneCoreKit/Process/VPhoneConfinedDirectory.swift
index bcf65be4..93a56b8f 100644
--- a/VPhoneKit/VPhoneCoreKit/Process/VPhoneConfinedDirectory.swift
+++ b/VPhoneKit/VPhoneCoreKit/Process/VPhoneConfinedDirectory.swift
@@ -396,6 +396,11 @@ public final class VPhoneConfinedDirectory: Sendable {
guard fstat(input, &metadata) == 0, metadata.st_mode & S_IFMT == S_IFREG else {
throw VPhoneConfinedDirectoryError.notRegularFile(source.path)
}
+ // A multi-gigabyte restore image published back from the firmware
+ // workspace is a clone on the same APFS volume, so it takes no room.
+ if try installClone(relative, of: input, mode: mode, owner: owner) {
+ return
+ }
try install(relative, mode: mode, owner: owner) { file in
try Self.copyContents(from: input, to: file, path: relative)
}
@@ -651,6 +656,47 @@ public final class VPhoneConfinedDirectory: Sendable {
installed = true
}
+ /// `install` with an APFS clone of the open file `input` in place of a
+ /// copy. Returns false, leaving nothing behind, where the volume cannot
+ /// clone it (another volume, or a file system without clones).
+ private func installClone(
+ _ relative: String,
+ of input: Int32,
+ mode: mode_t,
+ owner: (uid: uid_t, gid: gid_t)?,
+ ) throws -> Bool {
+ let (directory, leaf) = try parent(of: relative)
+ let temporary = ".\(leaf).tmp-\(UUID().uuidString)"
+ guard fclonefileat(input, directory.descriptor, temporary, UInt32(CLONE_NOOWNERCOPY)) == 0 else {
+ switch errno {
+ case EXDEV, ENOTSUP, ENOTTY:
+ return false
+ default:
+ throw VPhoneConfinedDirectoryError.from(errno, relative)
+ }
+ }
+ var installed = false
+ defer {
+ if !installed {
+ unlinkat(directory.descriptor, temporary, 0)
+ }
+ }
+ let file = openat(directory.descriptor, temporary, O_WRONLY | O_NOFOLLOW | O_CLOEXEC)
+ guard file >= 0 else {
+ throw VPhoneConfinedDirectoryError.from(errno, relative)
+ }
+ defer { close(file) }
+ try Self.applyAttributes(to: file, owner: owner, mode: mode, path: relative)
+ guard fsync(file) == 0 else {
+ throw VPhoneConfinedDirectoryError.system(relative, errno)
+ }
+ guard renameat(directory.descriptor, temporary, directory.descriptor, leaf) == 0 else {
+ throw VPhoneConfinedDirectoryError.from(errno, relative)
+ }
+ installed = true
+ return true
+ }
+
/// Owner first: chown clears set-ID bits, so the mode has to come after.
static func applyAttributes(to file: Int32, owner: (uid: uid_t, gid: gid_t)?, mode: mode_t, path: String) throws {
if let owner, fchown(file, owner.uid, owner.gid) != 0 {
diff --git a/VPhoneKit/VPhoneCoreKitTests/Process/ConfinedDirectoryTests.swift b/VPhoneKit/VPhoneCoreKitTests/Process/ConfinedDirectoryTests.swift
index 48fd9d58..db629f5c 100644
--- a/VPhoneKit/VPhoneCoreKitTests/Process/ConfinedDirectoryTests.swift
+++ b/VPhoneKit/VPhoneCoreKitTests/Process/ConfinedDirectoryTests.swift
@@ -121,6 +121,20 @@ struct ConfinedDirectoryTests {
#expect(try FileManager.default.contentsOfDirectory(atPath: tree.root.path).sorted() == ["leaf", "link"])
}
+ @Test func `a replaced file is its own copy even when it is a clone`() throws {
+ let tree = try Tree()
+ defer { tree.remove() }
+ let root = try VPhoneConfinedDirectory(root: tree.root.path)
+ let source = try tree.source("first")
+ try root.replaceFile("leaf", fromFileAt: source, mode: 0o640)
+ try Data("second".utf8).write(to: source)
+
+ let leaf = tree.root.appendingPathComponent("leaf")
+ #expect(try Data(contentsOf: leaf) == Data("first".utf8))
+ #expect(mode(leaf) & 0o7777 == 0o640)
+ #expect(try FileManager.default.contentsOfDirectory(atPath: tree.root.path).sorted() == ["leaf", "link"])
+ }
+
@Test func `reading a leaf symlink is refused`() throws {
let tree = try Tree()
defer { tree.remove() }