From 550fbf9e9be9a4983f2c11271de6f5f93a3490ea Mon Sep 17 00:00:00 2001 From: Alsey Coleman Miller Date: Mon, 30 Nov 2026 20:09:04 -0500 Subject: [PATCH 1/4] Add HVolumeParam and its packed guest codec --- Sources/GraphiteFiles/VolumeParam.swift | 163 ++++++++++++++++++++++++ 1 file changed, 163 insertions(+) create mode 100644 Sources/GraphiteFiles/VolumeParam.swift diff --git a/Sources/GraphiteFiles/VolumeParam.swift b/Sources/GraphiteFiles/VolumeParam.swift new file mode 100644 index 0000000..dcf73a7 --- /dev/null +++ b/Sources/GraphiteFiles/VolumeParam.swift @@ -0,0 +1,163 @@ +// The HVolumeParam variant of the File Manager's HParamBlockRec, as a program +// passes it to PBHGetVInfo, and its 122-byte guest representation. Offsets +// are those of Retro68's Multiversal interfaces; several 32-bit fields +// (ioVNxtCNID, ioVBkUp, ioVWrCnt, ioVFilCnt, ioVDirCnt) are only two-byte +// aligned, so every field is assembled from bytes. + +import GraphiteCore + +/// An HVolumeParam's fields, by their Inside Macintosh names. Guest addresses +/// stay opaque 32-bit values. +public struct HVolumeParam: Hashable, Sendable { + + public var queueLink: UInt32 = 0 + public var queueType: Int16 = 0 + public var trap: UInt16 = 0 + public var commandAddress: UInt32 = 0 + /// ioCompletion, a guest address. + public var completion: UInt32 = 0 + public var result: Int16 = 0 + /// ioNamePtr, a guest address: the volume's name is returned there. + public var namePointer: UInt32 = 0 + public var volume: Int16 = 0 + /// filler2, the four bytes after ioVRefNum. + public var filler: UInt32 = 0 + /// ioVolIndex: a volume's position (from 1), 0 to use ioVRefNum, or + /// negative to use ioNamePtr and then ioVRefNum. + public var volumeIndex: Int16 = 0 + /// ioVCrDate, seconds since 1904. + public var creationDate: UInt32 = 0 + /// ioVLsMod. + public var modificationDate: UInt32 = 0 + /// ioVAtrb. + public var attributes: UInt16 = 0 + /// ioVNmFls: files in the root folder. + public var rootFileCount: UInt16 = 0 + /// ioVBitMap. + public var bitmapBlock: UInt16 = 0 + /// ioVAllocPtr. + public var allocationPointer: UInt16 = 0 + /// ioVNmAlBlks. + public var allocationBlocks: UInt16 = 0 + /// ioVAlBlkSiz. + public var allocationBlockSize: UInt32 = 0 + /// ioVClpSiz. + public var clumpSize: UInt32 = 0 + /// ioAlBlSt. + public var firstAllocationBlock: UInt16 = 0 + /// ioVNxtCNID. + public var nextCatalogID: UInt32 = 0 + /// ioVFrBlk. + public var freeBlocks: UInt16 = 0 + /// ioVSigWord: 'BD' for HFS. + public var signature: UInt16 = 0 + /// ioVDrvInfo: the drive number. + public var driveNumber: Int16 = 0 + /// ioVDRefNum: the driver's reference number. + public var driverReference: Int16 = 0 + /// ioVFSID: 0 for HFS. + public var fileSystemID: Int16 = 0 + /// ioVBkUp. + public var backupDate: UInt32 = 0 + /// ioVSeqNum. + public var sequenceNumber: UInt16 = 0 + /// ioVWrCnt. + public var writeCount: UInt32 = 0 + /// ioVFilCnt: files on the volume. + public var fileCount: UInt32 = 0 + /// ioVDirCnt: folders on the volume. + public var folderCount: UInt32 = 0 + /// ioVFndrInfo: eight longs for the Finder. + public var finderInfo: [UInt8] = [UInt8](repeating: 0, count: 32) + + public init() {} +} + +public enum HVolumeParamGuestCodec { + + public static let byteCount = 122 + + public enum Offset { + public static let result = 16 + public static let volume = 22 + public static let volumeIndex = 28 + /// ioVCrDate, where the volume's description begins. + public static let creationDate = 30 + } + + /// Rejects truncated records and ignores trailing bytes. + public static func decode(_ bytes: some Collection) throws(MacError) -> HVolumeParam { + let data = Array(bytes.prefix(byteCount)) + guard data.count == byteCount else { throw .parameter } + func u16(_ at: Int) -> UInt16 { UInt16(data[at]) << 8 | UInt16(data[at + 1]) } + func u32(_ at: Int) -> UInt32 { UInt32(u16(at)) << 16 | UInt32(u16(at + 2)) } + var param = HVolumeParam() + param.queueLink = u32(0) + param.queueType = Int16(bitPattern: u16(4)) + param.trap = u16(6) + param.commandAddress = u32(8) + param.completion = u32(12) + param.result = Int16(bitPattern: u16(16)) + param.namePointer = u32(18) + param.volume = Int16(bitPattern: u16(22)) + param.filler = u32(24) + param.volumeIndex = Int16(bitPattern: u16(28)) + param.creationDate = u32(30) + param.modificationDate = u32(34) + param.attributes = u16(38) + param.rootFileCount = u16(40) + param.bitmapBlock = u16(42) + param.allocationPointer = u16(44) + param.allocationBlocks = u16(46) + param.allocationBlockSize = u32(48) + param.clumpSize = u32(52) + param.firstAllocationBlock = u16(56) + param.nextCatalogID = u32(58) + param.freeBlocks = u16(62) + param.signature = u16(64) + param.driveNumber = Int16(bitPattern: u16(66)) + param.driverReference = Int16(bitPattern: u16(68)) + param.fileSystemID = Int16(bitPattern: u16(70)) + param.backupDate = u32(72) + param.sequenceNumber = u16(76) + param.writeCount = u32(78) + param.fileCount = u32(82) + param.folderCount = u32(86) + param.finderInfo = Array(data[90 ..< 122]) + return param + } + + /// Encodes a complete record: paramErr if the Finder info is not 32 bytes. + public static func encode(_ param: HVolumeParam) throws(MacError) -> [UInt8] { + guard param.finderInfo.count == 32 else { throw .parameter } + var data: [UInt8] = [] + data.reserveCapacity(byteCount) + func u16(_ value: UInt16) { data += [UInt8(truncatingIfNeeded: value >> 8), UInt8(truncatingIfNeeded: value)] } + func u32(_ value: UInt32) { u16(UInt16(truncatingIfNeeded: value >> 16)); u16(UInt16(truncatingIfNeeded: value)) } + func s16(_ value: Int16) { u16(UInt16(bitPattern: value)) } + u32(param.queueLink); s16(param.queueType); u16(param.trap); u32(param.commandAddress) + u32(param.completion); s16(param.result); u32(param.namePointer); s16(param.volume) + u32(param.filler); s16(param.volumeIndex); u32(param.creationDate); u32(param.modificationDate) + u16(param.attributes); u16(param.rootFileCount); u16(param.bitmapBlock); u16(param.allocationPointer) + u16(param.allocationBlocks); u32(param.allocationBlockSize); u32(param.clumpSize) + u16(param.firstAllocationBlock); u32(param.nextCatalogID); u16(param.freeBlocks); u16(param.signature) + s16(param.driveNumber); s16(param.driverReference); s16(param.fileSystemID); u32(param.backupDate) + u16(param.sequenceNumber); u32(param.writeCount); u32(param.fileCount); u32(param.folderCount) + data += param.finderInfo + return data + } + + /// The fields PBHGetVInfo writes back, as (offset, bytes) pairs: ioResult, + /// ioVRefNum and the volume's description from ioVCrDate to the end. A + /// failed call writes only ioResult. + public static func results(_ param: HVolumeParam) throws(MacError) -> [(offset: Int, bytes: [UInt8])] { + let record = try encode(param) + let result = (Offset.result, Array(record[Offset.result ..< Offset.result + 2])) + guard param.result == 0 else { return [result] } + return [ + result, + (Offset.volume, Array(record[Offset.volume ..< Offset.volume + 2])), + (Offset.creationDate, Array(record[Offset.creationDate ..< byteCount])), + ] + } +} From b3e44a0d5a2ac749c202e5d4744d973877e92dd9 Mon Sep 17 00:00:00 2001 From: Alsey Coleman Miller Date: Mon, 30 Nov 2026 20:09:04 -0500 Subject: [PATCH 2/4] Report snapshot catalog statistics for volume information --- Sources/GraphiteFiles/SnapshotCatalog.swift | 27 +++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/Sources/GraphiteFiles/SnapshotCatalog.swift b/Sources/GraphiteFiles/SnapshotCatalog.swift index ec44bcc..4ef6d10 100644 --- a/Sources/GraphiteFiles/SnapshotCatalog.swift +++ b/Sources/GraphiteFiles/SnapshotCatalog.swift @@ -116,6 +116,33 @@ public struct SnapshotCatalog: Sendable { TextUtilities.equalString(first, second, caseSensitive: false, diacriticSensitive: true) } + // MARK: Statistics + + /// What PBHGetVInfo reports of the catalog. + public struct Statistics: Hashable, Sendable { + /// Files directly in the root folder. + public var rootFileCount: Int + /// Files on the volume. + public var fileCount: Int + /// Folders on the volume, not counting the root. + public var folderCount: Int + /// Both forks of every file, in bytes. + public var byteCount: Int + /// The catalog node ID the next item would have. + public var nextCatalogID: UInt32 + } + + public var statistics: Statistics { + let files = nodes.values.filter { $0.file != nil } + return Statistics( + rootFileCount: nodes[DirectoryID.root.rawValue]!.children.filter { nodes[$0]!.file != nil }.count, + fileCount: files.count, + folderCount: nodes.count - files.count - 1, + byteCount: files.reduce(0) { $0 + $1.file!.dataFork.count + $1.file!.resourceFork.count }, + nextCatalogID: max(Self.firstNodeID, (nodes.keys.max() ?? 0) &+ 1) + ) + } + // MARK: Items /// The item a spec names, if it is in the catalog. From e0f12557643c0eecf60c9adf4ef48aa784223124 Mon Sep 17 00:00:00 2001 From: Alsey Coleman Miller Date: Mon, 30 Nov 2026 20:09:04 -0500 Subject: [PATCH 3/4] Add Graphite-owned PBHGetVInfo results for every mounted volume --- Sources/GraphiteFiles/VolumeInformation.swift | 119 ++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 Sources/GraphiteFiles/VolumeInformation.swift diff --git a/Sources/GraphiteFiles/VolumeInformation.swift b/Sources/GraphiteFiles/VolumeInformation.swift new file mode 100644 index 0000000..23e4537 --- /dev/null +++ b/Sources/GraphiteFiles/VolumeInformation.swift @@ -0,0 +1,119 @@ +// PBHGetVInfo: a mounted volume's description, with Graphite deciding which +// volume a call names and what it reports. The host decodes the guest's +// HVolumeParam, reads the name at ioNamePtr when ioVolIndex is negative, +// writes back the fields `HVolumeParamGuestCodec.results` gives, and copies +// the outcome's `name` to ioNamePtr when that is not nil. +// +// Policies: +// - Drive numbers are volumes' positions (from 1); driver reference numbers +// are 0, as no volume has a driver. +// - A catalog volume is read-only and reports itself locked by hardware (bit +// 7 of ioVAtrb), as a CD-ROM is; it has no free space and no dates. +// - The POSIX volume's space comes from `Volume.space`, in 16-bit block +// counts; it does not count its files and folders, which are 0. +// - A volume number that is no mounted volume's is the POSIX volume, as for +// other calls; an index or a name that names none is nsvErr. + +import GraphiteCore + +public extension FileSystem { + + /// A finished PBHGetVInfo call. + struct VolumeInformationOutcome: Hashable, Sendable { + public var param: HVolumeParam + /// The volume's name, for the host to write as a Str27 at ioNamePtr + /// if that is not 0. Nil when the call failed. + public var name: PascalString? + /// The guest completion routine an asynchronous call names. + public var completion: UInt32? + + public var result: MacError { MacError(Int32(param.result)) } + } + + /// HFS's signature word, 'BD'. + static var hfsSignature: UInt16 { 0x4244 } + + /// ioVAtrb's bit for a volume locked by its hardware. + static var hardwareLockedAttribute: UInt16 { 0x0080 } + + /// PBHGetVInfo. `name` is the string at ioNamePtr, which the host reads + /// only when ioVolIndex is negative. + func volumeInformation(_ param: HVolumeParam, name: PascalString? = nil, + asynchronous: Bool = false) -> VolumeInformationOutcome { + var result = param + let completion = asynchronous && param.completion != 0 ? param.completion : nil + guard let reference = volumeReference(index: param.volumeIndex, name: name, volume: param.volume) else { + result.result = Int16(truncatingIfNeeded: MacError.noSuchVolume.code) + return VolumeInformationOutcome(param: result, name: nil, completion: completion) + } + let position = mountedVolumes.firstIndex(of: reference)! + 1 + result.result = 0 + result.volume = reference + result.signature = Self.hfsSignature + result.fileSystemID = 0 + result.driveNumber = Int16(position) + result.driverReference = 0 + result.firstAllocationBlock = 0 + result.bitmapBlock = 0 + result.allocationPointer = 0 + result.sequenceNumber = 0 + result.writeCount = 0 + result.backupDate = 0 + result.finderInfo = [UInt8](repeating: 0, count: 32) + let volumeName: PascalString + if let catalog = catalog(volume: reference) { + let statistics = catalog.statistics + let (blockSize, blocks, _) = Volume.Space(total: UInt64(statistics.byteCount), free: 0).classic + volumeName = catalog.volumeName + result.creationDate = 0 + result.modificationDate = 0 + result.attributes = Self.hardwareLockedAttribute + result.rootFileCount = UInt16(clamping: statistics.rootFileCount) + result.allocationBlocks = max(blocks, 1) + result.allocationBlockSize = blockSize + result.clumpSize = blockSize + result.freeBlocks = 0 + result.nextCatalogID = statistics.nextCatalogID + result.fileCount = UInt32(clamping: statistics.fileCount) + result.folderCount = UInt32(clamping: statistics.folderCount) + } else { + let space = volume.space ?? Volume.Space(total: 0, free: 0) + let (blockSize, blocks, free) = space.classic + volumeName = PascalString(volume.name) ?? "Macintosh HD" + result.creationDate = 0 + result.modificationDate = 0 + result.attributes = 0 + result.rootFileCount = 0 + result.allocationBlocks = blocks + result.allocationBlockSize = blockSize + result.clumpSize = blockSize + result.freeBlocks = free + result.nextCatalogID = 0 + result.fileCount = 0 + result.folderCount = 0 + } + return VolumeInformationOutcome(param: result, name: volumeName, completion: completion) + } + + /// The volume a call names: by position for a positive index, by name + /// (the part before a colon) then number for a negative one, and by + /// number for 0, where 0 is the default volume. + internal func volumeReference(index: Int16, name: PascalString?, volume reference: Int16) -> Int16? { + let volumes = mountedVolumes + if index > 0 { + return Int(index) <= volumes.count ? volumes[Int(index) - 1] : nil + } + if index < 0, let name, !name.isEmpty { + let bytes = name.bytes + let volumeName = PascalString(bytes: Array(bytes[..<(bytes.firstIndex(of: UInt8(ascii: ":")) ?? bytes.endIndex)])) + if !volumeName.isEmpty { + let posixName = PascalString(volume.name) ?? "Macintosh HD" + if SnapshotCatalog.sameName(volumeName, posixName) { return volume.reference } + return volumes.compactMap { catalog(volume: $0) } + .first { SnapshotCatalog.sameName($0.volumeName, volumeName) }?.volume + } + } + if reference == 0 { return defaultVolume } + return volumes.contains(reference) ? reference : volume.reference + } +} From 699931e29190f36795193a96ea204351f6e5b18a Mon Sep 17 00:00:00 2001 From: Alsey Coleman Miller Date: Mon, 30 Nov 2026 20:09:04 -0500 Subject: [PATCH 4/4] Test the HVolumeParam layout and PBHGetVInfo --- .../VolumeInformationTests.swift | 129 ++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 Tests/GraphiteFilesTests/VolumeInformationTests.swift diff --git a/Tests/GraphiteFilesTests/VolumeInformationTests.swift b/Tests/GraphiteFilesTests/VolumeInformationTests.swift new file mode 100644 index 0000000..e90fb41 --- /dev/null +++ b/Tests/GraphiteFilesTests/VolumeInformationTests.swift @@ -0,0 +1,129 @@ +import Testing +import GraphiteCore +import GraphiteFiles + +@Suite struct VolumeInformationTests { + + // MARK: The guest record + + @Test func encodesAtMultiversalOffsets() throws { + var param = HVolumeParam() + param.result = -35 + param.volume = -101 + param.filler = 0xAABB_CCDD + param.volumeIndex = 2 + param.allocationBlockSize = 0x0102_0304 + param.nextCatalogID = 0x1112_1314 + param.backupDate = 0x2122_2324 + param.writeCount = 0x3132_3334 + param.fileCount = 0x4142_4344 + param.folderCount = 0x5152_5354 + param.finderInfo = Array(0 ..< 32) + let bytes = try HVolumeParamGuestCodec.encode(param) + #expect(bytes.count == 122) + #expect(Array(bytes[16 ..< 18]) == [0xFF, 0xDD]) + #expect(Array(bytes[22 ..< 30]) == [0xFF, 0x9B, 0xAA, 0xBB, 0xCC, 0xDD, 0x00, 0x02]) + #expect(Array(bytes[48 ..< 52]) == [0x01, 0x02, 0x03, 0x04]) + // The two-byte-aligned longs. + #expect(Array(bytes[58 ..< 62]) == [0x11, 0x12, 0x13, 0x14]) + #expect(Array(bytes[72 ..< 76]) == [0x21, 0x22, 0x23, 0x24]) + #expect(Array(bytes[78 ..< 82]) == [0x31, 0x32, 0x33, 0x34]) + #expect(Array(bytes[82 ..< 86]) == [0x41, 0x42, 0x43, 0x44]) + #expect(Array(bytes[86 ..< 90]) == [0x51, 0x52, 0x53, 0x54]) + #expect(Array(bytes[90 ..< 122]) == Array(0 ..< 32)) + #expect(try HVolumeParamGuestCodec.decode(([0xEE] + bytes).dropFirst()) == param) + #expect(throws: MacError.parameter) { try HVolumeParamGuestCodec.decode(bytes.prefix(121)) } + param.finderInfo = [1] + #expect(throws: MacError.parameter) { try HVolumeParamGuestCodec.encode(param) } + } + + @Test func failedCallsWriteOnlyTheResult() throws { + var param = HVolumeParam() + param.result = -35 + #expect(try HVolumeParamGuestCodec.results(param).map(\.offset) == [16]) + param.result = 0 + let fields = try HVolumeParamGuestCodec.results(param) + #expect(fields.map(\.offset) == [16, 22, 30]) + #expect(fields[2].bytes.count == 92) + } + + // MARK: PBHGetVInfo + + /// App Disk (-101): BBEdit (3 + 1 bytes) and Read Me in Support (1 byte). + private func mounted() throws -> (FileSystem, TemporaryFolder) { + let folder = TemporaryFolder() + let files = FileSystem(defaultFolder: folder.path, volume: Volume(name: "Macintosh HD", spacePath: folder.path)) + try files.mount(SnapshotCatalog(volume: -101, name: "App Disk", contents: [ + .file(.init(name: "BBEdit", dataFork: [1, 2, 3], resourceFork: [4])), + .folder("Support", [.file(.init(name: "Read Me", dataFork: [7]))]), + ])) + return (files, folder) + } + + private func info(_ files: FileSystem, index: Int16 = 0, volume: Int16 = 0, + name: PascalString? = nil) -> FileSystem.VolumeInformationOutcome { + var param = HVolumeParam() + param.volumeIndex = index + param.volume = volume + return files.volumeInformation(param, name: name) + } + + @Test func describesACatalogVolume() throws { + let (files, folder) = try mounted() + defer { withExtendedLifetime(folder) {} } + let outcome = info(files, volume: -101) + #expect(outcome.result == .success && outcome.name == "App Disk") + let param = outcome.param + #expect(param.volume == -101 && param.driveNumber == 2 && param.driverReference == 0) + #expect(param.signature == 0x4244 && param.fileSystemID == 0) + #expect(param.attributes == FileSystem.hardwareLockedAttribute) + #expect(param.rootFileCount == 1 && param.fileCount == 2 && param.folderCount == 1) + #expect(param.allocationBlockSize == 512 && param.allocationBlocks == 1 && param.freeBlocks == 0) + #expect(param.nextCatalogID == 19) + } + + @Test func describesThePOSIXVolume() throws { + let (files, folder) = try mounted() + defer { withExtendedLifetime(folder) {} } + let outcome = info(files, volume: -100) + #expect(outcome.result == .success && outcome.name == "Macintosh HD") + #expect(outcome.param.volume == -100 && outcome.param.driveNumber == 1) + #expect(outcome.param.attributes == 0 && outcome.param.signature == 0x4244) + #expect(outcome.param.allocationBlocks > 0 && outcome.param.allocationBlockSize >= 512) + } + + @Test func choosesTheVolumeByIndexNameOrNumber() throws { + let (files, folder) = try mounted() + defer { withExtendedLifetime(folder) {} } + // By position, from 1, in mount order. + #expect(info(files, index: 1).param.volume == -100) + #expect(info(files, index: 2).param.volume == -101) + #expect(info(files, index: 3).result == .noSuchVolume) + #expect(info(files, index: 3).name == nil) + // By name, in any case, with or without the rest of a path; the + // number is used when there is no name. + #expect(info(files, index: -1, name: "app disk:").param.volume == -101) + #expect(info(files, index: -1, name: "App Disk:Support:Read Me").param.volume == -101) + #expect(info(files, index: -1, name: "MACINTOSH HD").param.volume == -100) + #expect(info(files, index: -1, name: "Nowhere:").result == .noSuchVolume) + #expect(info(files, index: -1, volume: -101).param.volume == -101) + #expect(info(files, index: -1, volume: -101, name: "").param.volume == -101) + // Volume 0 is the default volume, which may be a catalog. + #expect(info(files).param.volume == -100) + try files.setDefaultDirectory(volume: -101, directory: .root) + #expect(info(files).param.volume == -101) + // A number that is no mounted volume's is the POSIX volume's. + #expect(info(files, volume: 3).param.volume == -100) + } + + @Test func asynchronousCallsNameTheirCompletion() throws { + let (files, folder) = try mounted() + defer { withExtendedLifetime(folder) {} } + var param = HVolumeParam() + param.completion = 0x0800_2000 + param.volumeIndex = 9 + let outcome = files.volumeInformation(param, asynchronous: true) + #expect(outcome.result == .noSuchVolume && outcome.completion == 0x0800_2000) + #expect(files.volumeInformation(param).completion == nil) + } +}