From 525c65d2b3f585c7a33631fb9abc3833df40b82a Mon Sep 17 00:00:00 2001 From: Alsey Coleman Miller Date: Mon, 30 Nov 2026 20:14:45 -0500 Subject: [PATCH 1/4] Add CInfoPBRec as packed guest bytes with typed fields --- Sources/GraphiteFiles/CatalogInfoParam.swift | 120 +++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 Sources/GraphiteFiles/CatalogInfoParam.swift diff --git a/Sources/GraphiteFiles/CatalogInfoParam.swift b/Sources/GraphiteFiles/CatalogInfoParam.swift new file mode 100644 index 0000000..1b4b5f5 --- /dev/null +++ b/Sources/GraphiteFiles/CatalogInfoParam.swift @@ -0,0 +1,120 @@ +// The File Manager's CInfoPBRec, as a program passes it to PBGetCatInfo: its +// 108 guest bytes, with HFileInfo's and DirInfo's fields over them. Offsets +// are those of Retro68's Multiversal interfaces. The two variants share the +// record from ioFlAttrib on, and several 32-bit fields (ioFlLgLen at 54, +// ioFlPyLen at 58, ioFlRLgLen at 64) are only two-byte aligned, so the record +// stays bytes and each field is read and written a byte at a time. + +import GraphiteCore + +public struct CatalogInfoParam: Hashable, Sendable { + + public static let byteCount = 108 + + /// The record's big-endian guest bytes. + public private(set) var bytes: [UInt8] + + /// An empty record. + public init() { + bytes = [UInt8](repeating: 0, count: Self.byteCount) + } + + /// A record from guest bytes: paramErr if there are fewer than 108. + /// Trailing bytes are ignored. + public init(bytes: some Collection) throws(MacError) { + let data = Array(bytes.prefix(Self.byteCount)) + guard data.count == Self.byteCount else { throw .parameter } + self.bytes = data + } + + // MARK: Fields + + private func u16(_ at: Int) -> UInt16 { UInt16(bytes[at]) << 8 | UInt16(bytes[at + 1]) } + private func u32(_ at: Int) -> UInt32 { UInt32(u16(at)) << 16 | UInt32(u16(at + 2)) } + private mutating func set16(_ at: Int, _ value: UInt16) { + bytes[at] = UInt8(truncatingIfNeeded: value >> 8) + bytes[at + 1] = UInt8(truncatingIfNeeded: value) + } + private mutating func set32(_ at: Int, _ value: UInt32) { + set16(at, UInt16(truncatingIfNeeded: value >> 16)) + set16(at + 2, UInt16(truncatingIfNeeded: value)) + } + + /// ioCompletion, a guest address. + public var completion: UInt32 { get { u32(12) } set { set32(12, newValue) } } + /// ioResult. + public var result: Int16 { get { Int16(bitPattern: u16(16)) } set { set16(16, UInt16(bitPattern: newValue)) } } + /// ioNamePtr, a guest address. + public var namePointer: UInt32 { get { u32(18) } set { set32(18, newValue) } } + /// ioVRefNum. + public var volume: Int16 { get { Int16(bitPattern: u16(22)) } set { set16(22, UInt16(bitPattern: newValue)) } } + /// ioFRefNum. + public var forkReference: Int16 { get { Int16(bitPattern: u16(24)) } set { set16(24, UInt16(bitPattern: newValue)) } } + /// ioFDirIndex: an item's position in its folder (from 1), 0 to use + /// ioNamePtr, or negative for the folder ioDirID itself. + public var directoryIndex: Int16 { get { Int16(bitPattern: u16(28)) } set { set16(28, UInt16(bitPattern: newValue)) } } + /// ioFlAttrib. + public var attributes: UInt8 { get { bytes[30] } set { bytes[30] = newValue } } + /// ioDirID / ioDrDirID: on input the folder; on output a folder's own ID + /// or a file's ID. + public var directoryID: UInt32 { get { u32(48) } set { set32(48, newValue) } } + /// FInfo or DInfo (bytes 32-47) followed by FXInfo or DXInfo (84-99). + public var finderInfo: FinderInfo { + get { FinderInfo(bytes: bytes[32 ..< 48] + bytes[84 ..< 100]) } + set { + bytes.replaceSubrange(32 ..< 48, with: newValue.bytes[0 ..< 16]) + bytes.replaceSubrange(84 ..< 100, with: newValue.bytes[16 ..< 32]) + } + } + /// ioFlCrDat / ioDrCrDat. + public var created: UInt32 { get { u32(72) } set { set32(72, newValue) } } + /// ioFlMdDat / ioDrMdDat. + public var modified: UInt32 { get { u32(76) } set { set32(76, newValue) } } + /// ioFlBkDat / ioDrBkDat. + public var backedUp: UInt32 { get { u32(80) } set { set32(80, newValue) } } + /// ioFlParID / ioDrParID. + public var parentID: UInt32 { get { u32(100) } set { set32(100, newValue) } } + + // HFileInfo's + + /// ioFlLgLen. + public var dataLogicalLength: Int32 { get { Int32(bitPattern: u32(54)) } set { set32(54, UInt32(bitPattern: newValue)) } } + /// ioFlPyLen. + public var dataPhysicalLength: Int32 { get { Int32(bitPattern: u32(58)) } set { set32(58, UInt32(bitPattern: newValue)) } } + /// ioFlRLgLen. + public var resourceLogicalLength: Int32 { get { Int32(bitPattern: u32(64)) } set { set32(64, UInt32(bitPattern: newValue)) } } + /// ioFlRPyLen. + public var resourcePhysicalLength: Int32 { get { Int32(bitPattern: u32(68)) } set { set32(68, UInt32(bitPattern: newValue)) } } + /// ioFlClpSiz. + public var clumpSize: Int32 { get { Int32(bitPattern: u32(104)) } set { set32(104, UInt32(bitPattern: newValue)) } } + + // DirInfo's + + /// ioDrNmFls: the items in a folder. + public var folderItemCount: UInt16 { get { u16(52) } set { set16(52, newValue) } } + + /// Zeros a range of the record, such as a variant's fields the other + /// variant does not have. + mutating func zero(_ range: Range) { + bytes.replaceSubrange(range, with: repeatElement(0, count: range.count)) + } + + // MARK: Results + + /// ioFlAttrib's bits. + public enum Attribute { + /// The file is locked. + public static let locked: UInt8 = 0x01 + /// The item is a folder. + public static let directory: UInt8 = 0x10 + } + + /// The fields PBGetCatInfo writes back, as (offset, bytes) pairs: ioResult, + /// then ioFRefNum through ioFlClpSiz except the program's ioFDirIndex. A + /// failed call writes only ioResult. + public var results: [(offset: Int, bytes: [UInt8])] { + let result = (16, Array(bytes[16 ..< 18])) + guard self.result == 0 else { return [result] } + return [result, (24, Array(bytes[24 ..< 28])), (30, Array(bytes[30 ..< Self.byteCount]))] + } +} From 0616749fea06cc347dd1b554646afd65f2d491cb Mon Sep 17 00:00:00 2001 From: Alsey Coleman Miller Date: Mon, 30 Nov 2026 20:14:45 -0500 Subject: [PATCH 2/4] Report snapshot catalog items by position and their catalog information --- Sources/GraphiteFiles/SnapshotCatalog.swift | 38 +++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/Sources/GraphiteFiles/SnapshotCatalog.swift b/Sources/GraphiteFiles/SnapshotCatalog.swift index ec44bcc..90a2a53 100644 --- a/Sources/GraphiteFiles/SnapshotCatalog.swift +++ b/Sources/GraphiteFiles/SnapshotCatalog.swift @@ -148,6 +148,44 @@ public struct SnapshotCatalog: Sendable { return node.children.map { FileSpec(volume: volume, parent: id, name: nodes[$0]!.name) } } + /// The item at a position in a folder, from 1, in the order the host + /// listed them: dirNFErr if the folder is not there, fnfErr past its + /// last item. (HFS lists a folder in its catalog's name order; this + /// catalog does not sort.) + public func item(at index: Int, inFolder id: DirectoryID) throws(MacError) -> FileSpec { + let items = try contents(ofFolder: id) + guard index >= 1, index <= items.count else { throw .fileNotFound } + return items[index - 1] + } + + /// What PBGetCatInfo reports of an item. The catalog has no dates, and + /// its files are not locked one by one: the volume is (see + /// `FileSystem.volumeInformation`). + public func catalogInfo(_ spec: FileSpec) throws(MacError) -> CatalogInfo { + guard spec.volume == volume else { throw .noSuchVolume } + guard let item = item(spec) else { + guard spec.parent == .rootParent || isFolder(spec.parent) else { throw .directoryNotFound } + throw .fileNotFound + } + let never = MacDate(seconds: 0) + func physical(_ count: Int) -> Int64 { Int64((count + 511) / 512 * 512) } + switch item { + case .folder(let id): + let node = nodes[id.rawValue]! + return CatalogInfo(isDirectory: true, parentID: node.parent, nodeID: id.rawValue, + created: never, contentModified: never, attributeModified: never, accessed: never, + valence: UInt32(node.children.count)) + case .file(let id): + let node = nodes[id]!, file = node.file! + return CatalogInfo(isDirectory: false, parentID: node.parent, nodeID: id, + created: never, contentModified: never, attributeModified: never, accessed: never, + dataLogicalSize: Int64(file.dataFork.count), dataPhysicalSize: physical(file.dataFork.count), + resourceLogicalSize: Int64(file.resourceFork.count), + resourcePhysicalSize: physical(file.resourceFork.count), + finderInfo: file.finderInfo) + } + } + private func isFolder(_ id: DirectoryID) -> Bool { guard let node = nodes[id.rawValue] else { return false } return node.file == nil From cbe35cfaf22150c8e1af464cc5f4b22bc40658a7 Mon Sep 17 00:00:00 2001 From: Alsey Coleman Miller Date: Mon, 30 Nov 2026 20:14:45 -0500 Subject: [PATCH 3/4] Add Graphite-owned PBGetCatInfo results on every volume --- .../GraphiteFiles/CatalogInformation.swift | 99 +++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 Sources/GraphiteFiles/CatalogInformation.swift diff --git a/Sources/GraphiteFiles/CatalogInformation.swift b/Sources/GraphiteFiles/CatalogInformation.swift new file mode 100644 index 0000000..972aaec --- /dev/null +++ b/Sources/GraphiteFiles/CatalogInformation.swift @@ -0,0 +1,99 @@ +// PBGetCatInfo on any mounted volume, with Graphite deciding which item a +// call names and what it reports. The host decodes the guest's CInfoPBRec, +// reads the name at ioNamePtr when ioFDirIndex is 0, writes back the record's +// `results`, and copies the outcome's `name` to ioNamePtr when that is not +// nil and ioNamePtr is not 0. + +import GraphiteCore + +public extension FileSystem { + + /// A finished PBGetCatInfo call. + struct CatalogInfoOutcome: Hashable, Sendable { + public var param: CatalogInfoParam + /// The item's name, which the call returns when ioFDirIndex is not 0. + public var name: PascalString? + /// The guest completion routine an asynchronous call names. + public var completion: UInt32? + + public var result: MacError { MacError(Int32(param.result)) } + } + + /// The item a volume, folder, name and index name, on whichever volume: + /// the folder itself for a negative index, the folder's item at a + /// position (from 1) for a positive one, and the item the name gives, + /// as FSMakeFSSpec resolves it, for 0. fnfErr if there is none. + func catalogInfo(volume: Int16, directory: Int32, name: PascalString, + index: Int16) throws(MacError) -> (name: PascalString, info: CatalogInfo) { + if let (catalog, folder) = routedCatalog(volume: volume, directory: directory, name: index == 0 ? name : "") { + let spec: FileSpec + if index < 0 { + spec = try catalog.spec(ofFolder: folder) + } else if index > 0 { + spec = try catalog.item(at: Int(index), inFolder: folder) + } else { + let made = try catalog.makeSpec(volume: catalog.volume, directory: folder, name: name) + guard made.exists else { throw .fileNotFound } + spec = made.spec + } + return (spec.name, try catalog.catalogInfo(spec)) + } + let path: String + if index == 0 { + let made = try makeSpec(volume: volume, directory: directory, name: name) + guard made.exists, let found = self.path(of: made.spec) else { throw .fileNotFound } + path = found + } else { + guard let folder = folder(volume: volume, directory: directory) else { throw .directoryNotFound } + if index < 0 { + path = folder + } else { + let names = try contents(ofFolder: folder) + guard Int(index) <= names.count else { throw .fileNotFound } + path = Paths.appending(names[Int(index) - 1], to: folder) + } + } + return (spec(forPath: path).name, try catalogInfo(at: path)) + } + + /// PBGetCatInfo. `name` is the string at ioNamePtr, which the host reads + /// only when ioFDirIndex is 0; nil or empty names the folder ioDirID. + func getCatalogInfo(_ param: CatalogInfoParam, name: PascalString? = nil, + asynchronous: Bool = false) -> CatalogInfoOutcome { + var result = param + let completion = asynchronous && param.completion != 0 ? param.completion : nil + let found: (name: PascalString, info: CatalogInfo) + do throws(MacError) { + found = try catalogInfo(volume: param.volume, directory: Int32(bitPattern: param.directoryID), + name: name ?? "", index: param.directoryIndex) + } catch { + result.result = Int16(truncatingIfNeeded: error.code) + return CatalogInfoOutcome(param: result, name: nil, completion: completion) + } + let info = found.info + result.result = 0 + result.forkReference = 0 + result.zero(26 ..< 28) + result.attributes = (info.isDirectory ? CatalogInfoParam.Attribute.directory : 0) + | (info.isLocked ? CatalogInfoParam.Attribute.locked : 0) + result.zero(31 ..< 32) + result.finderInfo = info.finderInfo + result.directoryID = info.nodeID + result.zero(52 ..< 72) + if info.isDirectory { + result.folderItemCount = UInt16(clamping: info.valence) + } else { + result.dataLogicalLength = Int32(clamping: info.dataLogicalSize) + result.dataPhysicalLength = Int32(clamping: info.dataPhysicalSize) + result.resourceLogicalLength = Int32(clamping: info.resourceLogicalSize) + result.resourcePhysicalLength = Int32(clamping: info.resourcePhysicalSize) + } + result.created = info.created.classicSeconds + result.modified = info.contentModified.classicSeconds + result.backedUp = 0 + result.parentID = info.parentID.rawValue + result.zero(104 ..< CatalogInfoParam.byteCount) + return CatalogInfoOutcome(param: result, name: param.directoryIndex != 0 ? found.name : nil, + completion: completion) + } +} From 8c18ba1a6433f8dc6d69c12e99f306dba145a6cb Mon Sep 17 00:00:00 2001 From: Alsey Coleman Miller Date: Mon, 30 Nov 2026 20:14:45 -0500 Subject: [PATCH 4/4] Test the CInfoPBRec layout and PBGetCatInfo --- .../CatalogInformationTests.swift | 147 ++++++++++++++++++ 1 file changed, 147 insertions(+) create mode 100644 Tests/GraphiteFilesTests/CatalogInformationTests.swift diff --git a/Tests/GraphiteFilesTests/CatalogInformationTests.swift b/Tests/GraphiteFilesTests/CatalogInformationTests.swift new file mode 100644 index 0000000..a423692 --- /dev/null +++ b/Tests/GraphiteFilesTests/CatalogInformationTests.swift @@ -0,0 +1,147 @@ +import Testing +import GraphiteCore +import GraphiteFiles + +@Suite struct CatalogInformationTests { + + // MARK: The guest record + + @Test func fieldsSitAtMultiversalOffsets() throws { + var param = CatalogInfoParam() + param.result = -43 + param.volume = -101 + param.directoryIndex = 3 + param.attributes = 0x10 + param.directoryID = 0x0102_0304 + param.dataLogicalLength = 0x1112_1314 + param.dataPhysicalLength = 0x2122_2324 + param.resourceLogicalLength = 0x3132_3334 + param.resourcePhysicalLength = 0x4142_4344 + param.parentID = 0x5152_5354 + param.finderInfo = FinderInfo(bytes: Array(1 ... 32)) + let bytes = param.bytes + #expect(bytes.count == 108) + #expect(Array(bytes[16 ..< 18]) == [0xFF, 0xD5]) + #expect(Array(bytes[28 ..< 31]) == [0x00, 0x03, 0x10]) + #expect(Array(bytes[48 ..< 52]) == [0x01, 0x02, 0x03, 0x04]) + // The two-byte-aligned lengths. + #expect(Array(bytes[54 ..< 58]) == [0x11, 0x12, 0x13, 0x14]) + #expect(Array(bytes[58 ..< 62]) == [0x21, 0x22, 0x23, 0x24]) + #expect(Array(bytes[64 ..< 68]) == [0x31, 0x32, 0x33, 0x34]) + #expect(Array(bytes[68 ..< 72]) == [0x41, 0x42, 0x43, 0x44]) + #expect(Array(bytes[100 ..< 104]) == [0x51, 0x52, 0x53, 0x54]) + // FInfo at 32, FXInfo at 84. + #expect(Array(bytes[32 ..< 48]) == Array(1 ... 16) && Array(bytes[84 ..< 100]) == Array(17 ... 32)) + // DirInfo's ioDrNmFls shares bytes with HFileInfo's ioFlStBlk. + param.folderItemCount = 0xABCD + #expect(Array(param.bytes[52 ..< 54]) == [0xAB, 0xCD]) + #expect(try CatalogInfoParam(bytes: ([0xEE] + bytes).dropFirst()) == CatalogInfoParam(bytes: bytes)) + #expect(throws: MacError.parameter) { try CatalogInfoParam(bytes: bytes.prefix(107)) } + } + + @Test func resultsSkipTheProgramsIndex() throws { + var param = CatalogInfoParam() + param.result = -43 + #expect(param.results.map(\.offset) == [16]) + param.result = 0 + #expect(param.results.map(\.offset) == [16, 24, 30]) + #expect(param.results.map(\.bytes.count) == [2, 4, 78]) + } + + // MARK: PBGetCatInfo + + /// App Disk (-101) + /// BBEdit (16) 'APPL'/'R*ch', data 3 bytes, resource 600 bytes + /// Support (17) + /// Read Me (18) data 1 byte + private func mounted() throws -> (FileSystem, TemporaryFolder) { + let folder = TemporaryFolder() + let files = FileSystem(defaultFolder: folder.path) + try files.mount(SnapshotCatalog(volume: -101, name: "App Disk", contents: [ + .file(.init(name: "BBEdit", dataFork: [1, 2, 3], resourceFork: [UInt8](repeating: 9, count: 600), + finderInfo: FinderInfo(type: "APPL", creator: "R*ch"))), + .folder("Support", [.file(.init(name: "Read Me", dataFork: [7]))]), + ])) + return (files, folder) + } + + private func call(_ files: FileSystem, volume: Int16 = -101, directory: UInt32 = 2, + index: Int16 = 0, name: PascalString? = nil) -> FileSystem.CatalogInfoOutcome { + var param = CatalogInfoParam() + param.volume = volume + param.directoryID = directory + param.directoryIndex = index + return files.getCatalogInfo(param, name: name) + } + + @Test func describesACatalogFileByName() throws { + let (files, folder) = try mounted() + defer { withExtendedLifetime(folder) {} } + let outcome = call(files, name: "bbedit") + #expect(outcome.result == .success && outcome.name == nil) + let param = outcome.param + #expect(param.attributes == 0) + #expect(param.directoryID == 16 && param.parentID == 2) + #expect(param.finderInfo.type == "APPL" && param.finderInfo.creator == "R*ch") + #expect(param.dataLogicalLength == 3 && param.dataPhysicalLength == 512) + #expect(param.resourceLogicalLength == 600 && param.resourcePhysicalLength == 1024) + #expect(param.directoryIndex == 0) + } + + @Test func describesFoldersAndIndexedItems() throws { + let (files, folder) = try mounted() + defer { withExtendedLifetime(folder) {} } + // A negative index: the folder ioDirID itself, with its name. + let support = call(files, directory: 17, index: -1) + #expect(support.result == .success && support.name == "Support") + #expect(support.param.attributes == CatalogInfoParam.Attribute.directory) + #expect(support.param.directoryID == 17 && support.param.parentID == 2 && support.param.folderItemCount == 1) + let root = call(files, index: -1) + #expect(root.name == "App Disk" && root.param.parentID == 1 && root.param.folderItemCount == 2) + // Positive indexes walk the folder, returning names; past the end is fnfErr. + #expect(call(files, index: 1).name == "BBEdit") + #expect(call(files, index: 2).name == "Support") + #expect(call(files, index: 3).result == .fileNotFound) + #expect(call(files, directory: 17, index: 1).param.directoryID == 18) + // A partial pathname, and an empty name for the folder itself. + #expect(call(files, name: ":Support:Read Me").param.dataLogicalLength == 1) + #expect(call(files, directory: 17, name: "").param.directoryID == 17) + } + + @Test func reportsMissingItems() throws { + let (files, folder) = try mounted() + defer { withExtendedLifetime(folder) {} } + #expect(call(files, name: "Nothing").result == .fileNotFound) + #expect(call(files, directory: 99, index: 1).result == .directoryNotFound) + #expect(call(files, name: ":Nowhere:x").result == .directoryNotFound) + let failed = call(files, name: "Nothing") + #expect(failed.name == nil && failed.param.results.map(\.offset) == [16]) + } + + @Test func describesPOSIXItemsToo() throws { + let (files, folder) = try mounted() + defer { withExtendedLifetime(folder) {} } + try files.createFile(at: folder.path + "/Notes", finderInfo: FinderInfo(type: "TEXT", creator: "ttxt")) + let byName = call(files, volume: 0, directory: 0, name: "Notes") + #expect(byName.result == .success) + #expect(byName.param.finderInfo.type == "TEXT" && byName.param.attributes & CatalogInfoParam.Attribute.directory == 0) + let indexed = call(files, volume: 0, directory: 0, index: 1) + #expect(indexed.result == .success && indexed.name == "Notes") + #expect(call(files, volume: 0, directory: 0, index: 2).result == .fileNotFound) + let defaultFolder = call(files, volume: 0, directory: 0, index: -1) + #expect(defaultFolder.param.attributes == CatalogInfoParam.Attribute.directory) + #expect(defaultFolder.param.folderItemCount == 1) + } + + @Test func asynchronousCallsNameTheirCompletion() throws { + let (files, folder) = try mounted() + defer { withExtendedLifetime(folder) {} } + var param = CatalogInfoParam() + param.volume = -101 + param.directoryID = 2 + param.directoryIndex = 1 + param.completion = 0x0800_3000 + #expect(files.getCatalogInfo(param, asynchronous: true).completion == 0x0800_3000) + #expect(files.getCatalogInfo(param).completion == nil) + } +}