Skip to content

Run a virtual iPhone on an Apple Silicon Mac.

Swift

Read and write UTF-8 archive names in the C locale

Lakr7f746eedfe79
diff --git a/VPhoneKit/VPhoneArchiveKit/Archive/VPhoneArchiveExtractor.swift b/VPhoneKit/VPhoneArchiveKit/Archive/VPhoneArchiveExtractor.swift
index deffa85b..25c59dd7 100644
--- a/VPhoneKit/VPhoneArchiveKit/Archive/VPhoneArchiveExtractor.swift
+++ b/VPhoneKit/VPhoneArchiveKit/Archive/VPhoneArchiveExtractor.swift
@@ -35,6 +35,26 @@ public enum VPhoneArchiveExtractor {
progress: ((Progress) -> Void)? = nil,
bytesRead: ((Int64) -> Void)? = nil,
isCancelled: (() -> Bool)? = nil,
+ ) throws -> Int {
+ try withArchiveLocale {
+ try unpack(
+ archive,
+ into: destination,
+ options: options,
+ progress: progress,
+ bytesRead: bytesRead,
+ isCancelled: isCancelled,
+ )
+ }
+ }
+
+ private static func unpack(
+ _ archive: URL,
+ into destination: URL,
+ options: VPhoneArchiveExtractOptions,
+ progress: ((Progress) -> Void)?,
+ bytesRead: ((Int64) -> Void)?,
+ isCancelled: (() -> Bool)?,
) throws -> Int {
let reader = archive_read_new()
archive_read_support_format_all(reader)
@@ -86,7 +106,16 @@ public enum VPhoneArchiveExtractor {
)
}
- let memberPath = archive_entry_pathname(entry).map { String(cString: $0) } ?? ""
+ // A name libarchive could not convert comes back NULL. Treated as
+ // "", it would resolve to the destination itself and pass the
+ // containment check below.
+ guard let rawPath = archive_entry_pathname(entry) else {
+ throw VPhoneArchiveError.readFailed(
+ path: archive.path,
+ reason: archiveErrorString(reader),
+ )
+ }
+ let memberPath = String(cString: rawPath)
let target = resolvedDestination.appendingPathComponent(memberPath)
// libarchive's SECURE_* flags already refuse absolute paths and
diff --git a/VPhoneKit/VPhoneArchiveKit/Archive/VPhoneArchiveLocale.swift b/VPhoneKit/VPhoneArchiveKit/Archive/VPhoneArchiveLocale.swift
new file mode 100644
index 00000000..0a27a7e0
--- /dev/null
+++ b/VPhoneKit/VPhoneArchiveKit/Archive/VPhoneArchiveLocale.swift
@@ -0,0 +1,33 @@
+import Foundation
+
+/// Runs one synchronous libarchive session with a UTF-8 LC_CTYPE on the
+/// calling thread.
+///
+/// libarchive converts member names and link targets between the archive's
+/// charset and the thread's LC_CTYPE codeset. Nothing in vphone calls
+/// setlocale(3), so every process — vphone-cli from a terminal, as a child of
+/// Launchpad, or under its root helper — runs in "C", whose codeset is
+/// US-ASCII. There a zip entry with the UTF-8 flag or a pax path with
+/// non-ASCII characters comes back with a NULL pathname and ARCHIVE_WARN, and
+/// the pax writer refuses the header outright.
+///
+/// uselocale(3) scopes the change to this thread and to `body`; setlocale(3)
+/// would change it for every thread in the process. The other categories are
+/// copied from the thread's current locale, so only LC_CTYPE changes.
+func withArchiveLocale<T>(_ body: () throws -> T) rethrows -> T {
+ guard let base = duplocale(uselocale(nil)) else {
+ return try body()
+ }
+ guard let locale = newlocale(LC_CTYPE_MASK, "UTF-8", base) else {
+ // On failure newlocale leaves `base` to the caller. Without a UTF-8
+ // locale there is nothing better to do than the old behaviour.
+ freelocale(base)
+ return try body()
+ }
+ let previous = uselocale(locale)
+ defer {
+ uselocale(previous)
+ freelocale(locale)
+ }
+ return try body()
+}
diff --git a/VPhoneKit/VPhoneArchiveKit/Archive/VPhoneArchiveReader.swift b/VPhoneKit/VPhoneArchiveKit/Archive/VPhoneArchiveReader.swift
index 8dfd0012..7192d48e 100644
--- a/VPhoneKit/VPhoneArchiveKit/Archive/VPhoneArchiveReader.swift
+++ b/VPhoneKit/VPhoneArchiveKit/Archive/VPhoneArchiveReader.swift
@@ -92,19 +92,21 @@ public enum VPhoneArchiveReader {
_ archive: URL,
_ body: (OpaquePointer?) throws -> T,
) throws -> T {
- let reader = archive_read_new()
- archive_read_support_format_all(reader)
- archive_read_support_format_raw(reader)
- archive_read_support_filter_all(reader)
- defer { archive_read_free(reader) }
+ try withArchiveLocale {
+ let reader = archive_read_new()
+ archive_read_support_format_all(reader)
+ archive_read_support_format_raw(reader)
+ archive_read_support_filter_all(reader)
+ defer { archive_read_free(reader) }
- guard archive_read_open_filename(reader, archive.path, blockSize) == ARCHIVE_OK else {
- throw VPhoneArchiveError.cannotOpen(
- path: archive.path,
- reason: archiveErrorString(reader),
- )
+ guard archive_read_open_filename(reader, archive.path, blockSize) == ARCHIVE_OK else {
+ throw VPhoneArchiveError.cannotOpen(
+ path: archive.path,
+ reason: archiveErrorString(reader),
+ )
+ }
+ return try body(reader)
}
- return try body(reader)
}
private static func nextHeader(
diff --git a/VPhoneKit/VPhoneArchiveKit/Archive/VPhoneArchiveWriter.swift b/VPhoneKit/VPhoneArchiveKit/Archive/VPhoneArchiveWriter.swift
index 967fea38..b0f56772 100644
--- a/VPhoneKit/VPhoneArchiveKit/Archive/VPhoneArchiveWriter.swift
+++ b/VPhoneKit/VPhoneArchiveKit/Archive/VPhoneArchiveWriter.swift
@@ -38,6 +38,32 @@ public enum VPhoneArchiveWriter {
progress: ((Progress) -> Void)? = nil,
bytesPacked: ((Int64) -> Void)? = nil,
isCancelled: (() -> Bool)? = nil,
+ ) throws -> Int {
+ try withArchiveLocale {
+ try pack(
+ archive: archive,
+ from: root,
+ topLevel: topLevel,
+ format: format,
+ compression: compression,
+ excluding: patterns,
+ progress: progress,
+ bytesPacked: bytesPacked,
+ isCancelled: isCancelled,
+ )
+ }
+ }
+
+ private static func pack(
+ archive: URL,
+ from root: URL,
+ topLevel: String?,
+ format: VPhoneArchiveFormat,
+ compression: VPhoneArchiveCompression,
+ excluding patterns: [String],
+ progress: ((Progress) -> Void)?,
+ bytesPacked: ((Int64) -> Void)?,
+ isCancelled: (() -> Bool)?,
) throws -> Int {
let writer = archive_write_new()
defer { archive_write_free(writer) }
diff --git a/VPhoneKit/VPhoneArchiveKitTests/RoundTripTests.swift b/VPhoneKit/VPhoneArchiveKitTests/RoundTripTests.swift
index 598f39f4..03525622 100644
--- a/VPhoneKit/VPhoneArchiveKitTests/RoundTripTests.swift
+++ b/VPhoneKit/VPhoneArchiveKitTests/RoundTripTests.swift
@@ -1,3 +1,4 @@
+import ArchiveKit
import Foundation
import Testing
@testable import VPhoneArchiveKit
@@ -342,6 +343,121 @@ struct RoundTripTests {
))
}
+ // MARK: - Non-ASCII names
+
+ /// libarchive converts member names through the calling thread's LC_CTYPE.
+ /// A process that never calls setlocale(3) — every launchd job, the
+ /// Launchpad helper, and vphone-cli itself — runs in "C", where UTF-8 names
+ /// cannot be converted: the reader returned a NULL pathname and the pax
+ /// writer refused the header. Pinned to "C" here so the test does not
+ /// depend on how the test runner set up its locale.
+ private func inLocale<T>(_ name: String, _ body: () throws -> T) throws -> T {
+ let locale = try #require(newlocale(LC_CTYPE_MASK, name, nil))
+ let previous = uselocale(locale)
+ defer {
+ uselocale(previous)
+ freelocale(locale)
+ }
+ return try body()
+ }
+
+ private static let unicodeName = "café-中文.txt"
+
+ private static func makeUnicodeTree() throws -> URL {
+ let root = scratch("unicode")
+ try FileManager.default.createDirectory(
+ at: root.appendingPathComponent("目录"),
+ withIntermediateDirectories: true,
+ )
+ try Data("bonjour\n".utf8).write(to: root.appendingPathComponent("目录/\(unicodeName)"))
+ try FileManager.default.createSymbolicLink(
+ atPath: root.appendingPathComponent("lien-é").path,
+ withDestinationPath: "目录/\(unicodeName)",
+ )
+ return root
+ }
+
+ @Test(arguments: [VPhoneArchiveFormat.gnutar, .pax])
+ func `non-ASCII names round trip in the C locale`(format: VPhoneArchiveFormat) throws {
+ let source = try Self.makeUnicodeTree()
+ let archive = Self.scratch("unicode").appendingPathExtension("tar")
+ let destination = Self.scratch("unicode-out")
+ try FileManager.default.createDirectory(at: destination, withIntermediateDirectories: true)
+ defer {
+ for url in [source, archive, destination] {
+ try? FileManager.default.removeItem(at: url)
+ }
+ }
+
+ let member = "目录/\(Self.unicodeName)"
+ let (entries, data) = try inLocale("C") {
+ try VPhoneArchiveWriter.create(archive: archive, from: source, format: format)
+ try VPhoneArchiveExtractor.extract(archive, into: destination, options: .intoHostDirectory)
+ return try (
+ VPhoneArchiveReader.entries(of: archive),
+ VPhoneArchiveReader.readMember(member, from: archive),
+ )
+ }
+
+ #expect(Set(entries.map(\.path)) == ["目录/", member, "lien-é"])
+ #expect(entries.first { $0.path == "lien-é" }?.linkTarget == member)
+ #expect(String(decoding: data, as: UTF8.self) == "bonjour\n")
+ #expect(try String(contentsOf: destination.appendingPathComponent(member), encoding: .utf8) == "bonjour\n")
+ #expect(try FileManager.default.destinationOfSymbolicLink(
+ atPath: destination.appendingPathComponent("lien-é").path,
+ ) == member)
+ }
+
+ /// A zip entry with the UTF-8 flag (0x800) is the case libarchive converts
+ /// rather than passing the bytes through. Neither ditto nor /usr/bin/zip
+ /// sets that flag, so the fixture is written here, in a UTF-8 locale.
+ @Test
+ func `a UTF-8 flagged zip is listed and unpacked in the C locale`() throws {
+ let zip = Self.scratch("unicode").appendingPathExtension("zip")
+ let destination = Self.scratch("unicode-zip-out")
+ try FileManager.default.createDirectory(at: destination, withIntermediateDirectories: true)
+ defer {
+ for url in [zip, destination] {
+ try? FileManager.default.removeItem(at: url)
+ }
+ }
+
+ let member = "目录/\(Self.unicodeName)"
+ try inLocale("UTF-8") {
+ let writer = archive_write_new()
+ defer { archive_write_free(writer) }
+ archive_write_set_format_zip(writer)
+ try #require(archive_write_set_options(writer, "zip:hdrcharset=UTF-8") == ARCHIVE_OK)
+ try #require(archive_write_open_filename(writer, zip.path) == ARCHIVE_OK)
+ let entry = archive_entry_new()
+ defer { archive_entry_free(entry) }
+ archive_entry_set_pathname(entry, member)
+ archive_entry_set_filetype(entry, UInt32(S_IFREG))
+ archive_entry_set_perm(entry, 0o644)
+ archive_entry_set_size(entry, 8)
+ try #require(archive_write_header(writer, entry) == ARCHIVE_OK)
+ #expect(archive_write_data(writer, "bonjour\n", 8) == 8)
+ try #require(archive_write_close(writer) == ARCHIVE_OK)
+ }
+ // General purpose flags are at +6 of the first local header.
+ let header = try [UInt8](Data(contentsOf: zip).prefix(8))
+ try #require(header[0 ..< 4] == [0x50, 0x4B, 0x03, 0x04])
+ try #require(header[7] & 0x08 != 0)
+
+ let (entries, data) = try inLocale("C") {
+ try VPhoneArchiveExtractor.extract(zip, into: destination, options: .intoHostDirectory)
+ return try (
+ VPhoneArchiveReader.entries(of: zip),
+ VPhoneArchiveReader.readMember(member, from: zip),
+ )
+ }
+
+ #expect(entries.map(\.path).contains(member))
+ #expect(!entries.contains { $0.path.isEmpty })
+ #expect(String(decoding: data, as: UTF8.self) == "bonjour\n")
+ #expect(try String(contentsOf: destination.appendingPathComponent(member), encoding: .utf8) == "bonjour\n")
+ }
+
/// Overwrite the name in a ustar/gnutar header in place. The name field is
/// 100 bytes at offset 0 of the header block, and the header checksum has
/// to be recomputed or libarchive rejects the entry before we see it.