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
120 changes: 120 additions & 0 deletions Sources/GraphiteFiles/CatalogInfoParam.swift
Original file line number Diff line number Diff line change
@@ -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<UInt8>) 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<Int>) {
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]))]
}
}
99 changes: 99 additions & 0 deletions Sources/GraphiteFiles/CatalogInformation.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}
38 changes: 38 additions & 0 deletions Sources/GraphiteFiles/SnapshotCatalog.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading
Loading