Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions Sources/GraphiteFiles/SnapshotCatalog.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
119 changes: 119 additions & 0 deletions Sources/GraphiteFiles/VolumeInformation.swift
Original file line number Diff line number Diff line change
@@ -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
}
}
163 changes: 163 additions & 0 deletions Sources/GraphiteFiles/VolumeParam.swift
Original file line number Diff line number Diff line change
@@ -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<UInt8>) 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])),
]
}
}
Loading
Loading