From e96bdeb5230f42e90faca607dff6ba45574f7ded Mon Sep 17 00:00:00 2001 From: Alsey Coleman Miller Date: Mon, 30 Nov 2026 21:35:28 -0500 Subject: [PATCH 1/7] List snapshot catalog folders in HFS catalog order --- Sources/GraphiteFiles/SnapshotCatalog.swift | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/Sources/GraphiteFiles/SnapshotCatalog.swift b/Sources/GraphiteFiles/SnapshotCatalog.swift index de72860..55a86bd 100644 --- a/Sources/GraphiteFiles/SnapshotCatalog.swift +++ b/Sources/GraphiteFiles/SnapshotCatalog.swift @@ -6,7 +6,8 @@ // IDs are as HFS numbers them: the root's parent is 1, the root 2, and the // other folders and files share catalog node IDs from 16, given depth-first // in the order the host lists them. Names compare as HFS compares them: case -// does not matter, diacritics do. +// does not matter, diacritics do. A folder lists its items as an HFS catalog +// does, in RelString's order of their names, whatever order the host gave. import GraphiteCore @@ -105,6 +106,14 @@ public struct SnapshotCatalog: Sendable { try add(contents, to: DirectoryID(id), nodes: &nodes, next: &next) } } + // HFS keeps a folder's items in its catalog's key order: names by + // RelString, case-insensitive and diacritic-sensitive. Names equal by + // that comparison were rejected above, so the order is total. + let named = nodes + nodes[folder.rawValue]!.children.sort { first, second in + TextUtilities.relString(named[first]!.name, named[second]!.name, + caseSensitive: false, diacriticSensitive: true) < 0 + } } private static func isValid(_ name: PascalString, length: Int) -> Bool { @@ -169,16 +178,14 @@ public struct SnapshotCatalog: Sendable { return FileSpec(volume: volume, parent: node.parent, name: node.name) } - /// The items in a folder, as the host listed them. + /// The items in a folder, in HFS catalog order. public func contents(ofFolder id: DirectoryID) throws(MacError) -> [FileSpec] { guard isFolder(id), let node = nodes[id.rawValue] else { throw .directoryNotFound } 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.) + /// The item at a position in a folder, from 1, in HFS catalog order: + /// dirNFErr if the folder is not there, fnfErr past its last item. 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 } From aae8bd070cac7bed6879fde3e1a5085537e43378 Mon Sep 17 00:00:00 2001 From: Alsey Coleman Miller Date: Mon, 30 Nov 2026 21:35:28 -0500 Subject: [PATCH 2/7] Walk POSIX folders in HFS catalog order for indexed PBGetCatInfo --- Sources/GraphiteFiles/CatalogInformation.swift | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/Sources/GraphiteFiles/CatalogInformation.swift b/Sources/GraphiteFiles/CatalogInformation.swift index 972aaec..6ec637d 100644 --- a/Sources/GraphiteFiles/CatalogInformation.swift +++ b/Sources/GraphiteFiles/CatalogInformation.swift @@ -21,7 +21,7 @@ public extension FileSystem { /// 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, + /// position (from 1, in HFS catalog order) 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) { @@ -48,9 +48,16 @@ public extension FileSystem { 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) + // As an HFS catalog lists a folder: by the items' Mac names, + // in RelString's order. + let items = try contents(ofFolder: folder).map { name in + let path = Paths.appending(name, to: folder) + return (path: path, name: spec(forPath: path).name) + }.sorted { + TextUtilities.relString($0.name, $1.name, caseSensitive: false, diacriticSensitive: true) < 0 + } + guard Int(index) <= items.count else { throw .fileNotFound } + path = items[Int(index) - 1].path } } return (spec(forPath: path).name, try catalogInfo(at: path)) From 86bf010e67cea25a030af8e5a13a34ddbdeb7796 Mon Sep 17 00:00:00 2001 From: Alsey Coleman Miller Date: Mon, 30 Nov 2026 21:35:28 -0500 Subject: [PATCH 3/7] Add Mac OS 9 HFS catalog order probe --- ReferenceTests/Retro68/CatalogOrderCapture.c | 126 +++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 ReferenceTests/Retro68/CatalogOrderCapture.c diff --git a/ReferenceTests/Retro68/CatalogOrderCapture.c b/ReferenceTests/Retro68/CatalogOrderCapture.c new file mode 100644 index 0000000..200744c --- /dev/null +++ b/ReferenceTests/Retro68/CatalogOrderCapture.c @@ -0,0 +1,126 @@ +/* + * Captures the order an HFS volume lists a folder's items in: the catalog's + * key order, which Graphite takes to be RelString's (case-insensitive, + * diacritic-sensitive). It makes two folders next to itself, lists them with + * indexed PBGetCatInfo, deletes them, and writes "CatalogOrder.bin". It + * refuses to run if that file or either folder is already there. + * + * Folder "GCOBytes" gets a file for every one-byte name but NUL and the + * colon; HFS refuses names equal to one already there. Folder "GCONames" + * gets the names in `names` below. + * + * Format GCO1: four ASCII magic bytes, then for each folder a big-endian + * 16-bit count and that many Pascal strings, in the order PBGetCatInfo + * listed them. + */ +#include + +static const unsigned char header[] = { 'G', 'C', 'O', '1' }; +static short out; + +static const char *const names[] = { + "Read Me", "readme", "ReadMe2", "Zeta", "alpha", "Alpha Beta", "\216clair", "eclair", + "Eclair2", "File10", "File2", "File 1", "a b", "a\312c", "\247 Notes", "~Tilde", + "100", "20", "Z", "z\201", "\200z", "\330", "\377end", +}; + +static void pstr(Str255 to, const char *from) +{ + int n = 0; + while (from[n] && n < 255) { to[n + 1] = (unsigned char)from[n]; ++n; } + to[0] = (unsigned char)n; +} + +static void put(const void *bytes, long count) +{ + FSWrite(out, &count, bytes); +} + +/* Lists a folder with indexed PBGetCatInfo, writing its count and names. */ +static OSErr list(short volume, long folder) +{ + static Str255 names[300]; + CInfoPBRec pb; + unsigned short count = 0; + unsigned char countBytes[2]; + short index; + OSErr error = noErr; + for (index = 1; index <= 300; ++index) { + pb.hFileInfo.ioCompletion = 0; + pb.hFileInfo.ioNamePtr = names[count]; + pb.hFileInfo.ioVRefNum = volume; + pb.hFileInfo.ioDirID = folder; + pb.hFileInfo.ioFDirIndex = index; + error = PBGetCatInfoSync(&pb); + if (error == fnfErr) { error = noErr; break; } + if (error != noErr) return error; + ++count; + } + countBytes[0] = count >> 8; + countBytes[1] = count & 0xFF; + put(countBytes, 2); + for (index = 0; index < count; ++index) put(names[index], names[index][0] + 1); + return error; +} + +/* Deletes a folder's items, then the folder. */ +static void removeFolder(short volume, long folder, FSSpec *spec) +{ + Str255 name; + CInfoPBRec pb; + FSSpec item; + for (;;) { + pb.hFileInfo.ioCompletion = 0; + pb.hFileInfo.ioNamePtr = name; + pb.hFileInfo.ioVRefNum = volume; + pb.hFileInfo.ioDirID = folder; + pb.hFileInfo.ioFDirIndex = 1; + if (PBGetCatInfoSync(&pb) != noErr) break; + if (FSMakeFSSpec(volume, folder, name, &item) != noErr || FSpDelete(&item) != noErr) break; + } + FSpDelete(spec); +} + +int main(void) +{ + Str255 name; + FSSpec spec, bytesSpec, namesSpec; + long bytesFolder, namesFolder; + short volume; + int b, i; + OSErr error; + + pstr(name, "CatalogOrder.bin"); + if (Create(name, 0, 'Grph', 'DATA') != noErr) return 1; + if (FSOpen(name, 0, &out) != noErr) return 2; + if (FSMakeFSSpec(0, 0, (ConstStr255Param)"\0", &spec) != noErr) return 3; + volume = spec.vRefNum; + + pstr(name, "GCOBytes"); + if (FSMakeFSSpec(0, 0, name, &bytesSpec) != fnfErr) return 4; + if (FSpDirCreate(&bytesSpec, smSystemScript, &bytesFolder) != noErr) return 5; + pstr(name, "GCONames"); + if (FSMakeFSSpec(0, 0, name, &namesSpec) != fnfErr) return 6; + if (FSpDirCreate(&namesSpec, smSystemScript, &namesFolder) != noErr) return 7; + + for (b = 1; b < 256; ++b) { + if (b == ':') continue; + name[0] = 1; + name[1] = (unsigned char)b; + if (FSMakeFSSpec(volume, bytesFolder, name, &spec) == fnfErr) + FSpCreate(&spec, 'Grph', 'DATA', smSystemScript); + } + for (i = 0; i < (int)(sizeof(names) / sizeof(names[0])); ++i) { + pstr(name, names[i]); + if (FSMakeFSSpec(volume, namesFolder, name, &spec) == fnfErr) + FSpCreate(&spec, 'Grph', 'DATA', smSystemScript); + } + + put(header, sizeof(header)); + error = list(volume, bytesFolder); + if (error == noErr) error = list(volume, namesFolder); + removeFolder(volume, bytesFolder, &bytesSpec); + removeFolder(volume, namesFolder, &namesSpec); + FSClose(out); + return error == noErr ? 0 : 8; +} From 16497a8d520693007ba402c8807b4bb726931e3b Mon Sep 17 00:00:00 2001 From: Alsey Coleman Miller Date: Mon, 30 Nov 2026 21:35:28 -0500 Subject: [PATCH 4/7] Build the catalog order probe with Retro68 --- ReferenceTests/Retro68/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/ReferenceTests/Retro68/CMakeLists.txt b/ReferenceTests/Retro68/CMakeLists.txt index f6ad228..34cc36f 100644 --- a/ReferenceTests/Retro68/CMakeLists.txt +++ b/ReferenceTests/Retro68/CMakeLists.txt @@ -4,3 +4,4 @@ project(GraphiteReferenceTests C CXX) add_application(EqualStringCapture EqualStringCapture.c) add_application(FileManagerCapture FileManagerCapture.c) add_application(RelStringCapture RelStringCapture.c) +add_application(CatalogOrderCapture CatalogOrderCapture.c) From 6ee33211faa4f41f14e9760681b7d1e6805d502b Mon Sep 17 00:00:00 2001 From: Alsey Coleman Miller Date: Mon, 30 Nov 2026 21:35:28 -0500 Subject: [PATCH 5/7] Add Mac OS 9.2.2 HFS catalog order capture --- .../Retro68/CatalogOrder-macos-9.2.2-ppc.bin | Bin 0 -> 568 bytes 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 ReferenceTests/Retro68/CatalogOrder-macos-9.2.2-ppc.bin diff --git a/ReferenceTests/Retro68/CatalogOrder-macos-9.2.2-ppc.bin b/ReferenceTests/Retro68/CatalogOrder-macos-9.2.2-ppc.bin new file mode 100644 index 0000000000000000000000000000000000000000..d90e83b1d35485d697ab408e12d1b573f76853f6 GIT binary patch literal 568 zcmW;ISCGvI0EKbCEq8U%TW}LSQSMzWnM7H=WOagQm;KM!nNXTOT8R>pP=-Z?PqoT4uQw zR$6V1pRBdcdYf#v#a2K2#WvgRu+uJk?X%ynesjPiyJ*66!g!Cr4LPNmW|}l)NF>Dr)LD=Xf~pe#4Jlr>KMe* u!l^-MXeI{+*_xVJuL`#vJUked^-kP~F$p~~cSEB(r)Q)E3GaeIY3~8Mv6m14 literal 0 HcmV?d00001 From 94ea8a6aaf1cb778aa80d4abc06ed3837411cc1d Mon Sep 17 00:00:00 2001 From: Alsey Coleman Miller Date: Mon, 30 Nov 2026 21:35:28 -0500 Subject: [PATCH 6/7] Test catalog folder order against the HFS capture --- .../CatalogOrderTests.swift | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 Tests/GraphiteFilesTests/CatalogOrderTests.swift diff --git a/Tests/GraphiteFilesTests/CatalogOrderTests.swift b/Tests/GraphiteFilesTests/CatalogOrderTests.swift new file mode 100644 index 0000000..4ddd90f --- /dev/null +++ b/Tests/GraphiteFilesTests/CatalogOrderTests.swift @@ -0,0 +1,59 @@ +import Testing +import Foundation +import GraphiteCore +import GraphiteFiles + +/// Folders list their items as an HFS catalog does, checked against the order +/// Mac OS 9.2.2's HFS listed two folders in (ReferenceTests/Retro68, +/// CatalogOrderCapture.c). +@Suite struct CatalogOrderTests { + + /// The capture's two folders: every one-byte name HFS accepted, and a list + /// of longer names, each in the order PBGetCatInfo listed them. + private func capturedFolders() throws -> [[PascalString]] { + let root = URL(fileURLWithPath: #filePath).deletingLastPathComponent() + .deletingLastPathComponent().deletingLastPathComponent() + let bytes = [UInt8](try Data(contentsOf: root.appendingPathComponent( + "ReferenceTests/Retro68/CatalogOrder-macos-9.2.2-ppc.bin"))) + #expect(Array(bytes.prefix(4)) == Array("GCO1".utf8)) + var position = 4 + var folders: [[PascalString]] = [] + for _ in 0 ..< 2 { + let count = Int(bytes[position]) << 8 | Int(bytes[position + 1]) + position += 2 + var names: [PascalString] = [] + for _ in 0 ..< count { + let length = Int(bytes[position]) + names.append(PascalString(bytes: Array(bytes[position + 1 ... position + length]))) + position += 1 + length + } + folders.append(names) + } + #expect(position == bytes.count) + return folders + } + + @Test func catalogFoldersListAsHFSDoes() throws { + for listed in try capturedFolders() { + // Given in another order, the catalog lists them as HFS did. + let catalog = try SnapshotCatalog(volume: -101, name: "Disk", + contents: listed.reversed().map { .file(.init(name: $0)) }) + #expect(try catalog.contents(ofFolder: .root).map(\.name) == listed) + #expect(try catalog.item(at: 1, inFolder: .root).name == listed[0]) + } + #expect(try capturedFolders().map(\.count) == [214, 23]) + } + + @Test func indexedCatInfoWalksPOSIXFoldersInHFSOrder() throws { + let folder = TemporaryFolder() + let files = FileSystem(defaultFolder: folder.path) + for name in ["zeta", "File2", "Alpha", "File10", "beta"] { try files.createFile(at: folder.path + "/" + name) } + var names: [PascalString] = [] + for index in 1 ... 5 { + var param = CatalogInfoParam() + param.directoryIndex = Int16(index) + names.append(try #require(files.getCatalogInfo(param).name)) + } + #expect(names == ["Alpha", "beta", "File10", "File2", "zeta"]) + } +} From 6aed575a115bda8abe86f3972937e17faf996978 Mon Sep 17 00:00:00 2001 From: Alsey Coleman Miller Date: Mon, 30 Nov 2026 21:35:28 -0500 Subject: [PATCH 7/7] Document the HFS catalog order capture --- ReferenceTests/Retro68/README.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/ReferenceTests/Retro68/README.md b/ReferenceTests/Retro68/README.md index 44b8d31..45ad9d5 100644 --- a/ReferenceTests/Retro68/README.md +++ b/ReferenceTests/Retro68/README.md @@ -124,3 +124,22 @@ source). SHA-256: `TextUtilitiesTests/relStringMatchesMacOS9()` checks Graphite against the capture, or against `GRAPHITE_RELSTRING_CAPTURE` when it is set. + +## Catalog order + +`CatalogOrderCapture.c` makes two folders on an HFS volume, lists them with +indexed `PBGetCatInfo`, and deletes them. One holds a file for every one-byte +name but NUL and the colon, the other a list of longer names (format `GCO1`, +described in the source). Its output on Mac OS 9.2.2, on an HFS volume made by +`MakeIDEDisk.py`, is `CatalogOrder-macos-9.2.2-ppc.bin`. SHA-256: +`d566856ad71bb759a560709c3f9215d36696c7ca9f70d036cec94afb9f329626` + +- HFS listed both folders in RelString's case-insensitive, + diacritic-sensitive order, with no exceptions: its catalog comparison + agrees with RelString on every name tried. +- Of the 254 one-byte names, HFS kept 214: one per class of names EqualString + finds equal with case ignored and diacritics significant. +- Numbers are not compared as numbers ("File10" before "File2"). + +`CatalogOrderTests` checks that `SnapshotCatalog` and indexed `PBGetCatInfo` +list the same names in the same order.