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
1 change: 1 addition & 0 deletions ReferenceTests/Retro68/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Binary file not shown.
126 changes: 126 additions & 0 deletions ReferenceTests/Retro68/CatalogOrderCapture.c
Original file line number Diff line number Diff line change
@@ -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 <Files.h>

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;
}
19 changes: 19 additions & 0 deletions ReferenceTests/Retro68/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
15 changes: 11 additions & 4 deletions Sources/GraphiteFiles/CatalogInformation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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))
Expand Down
19 changes: 13 additions & 6 deletions Sources/GraphiteFiles/SnapshotCatalog.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 }
Expand Down
59 changes: 59 additions & 0 deletions Tests/GraphiteFilesTests/CatalogOrderTests.swift
Original file line number Diff line number Diff line change
@@ -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"])
}
}
Loading