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
18 changes: 18 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: CI

on:
push:
branches: [main]
pull_request:

jobs:
test:
runs-on: macos-15
steps:
- uses: actions/checkout@v4
- name: Swift version
run: swift --version
- name: Build
run: swift build
- name: Test
run: swift test
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ The cleaner does not follow symbolic links and does not delete files merely beca

## Development

The routing, disc-set planning, and metadata safety behavior are covered by XCTest tests in `Tests/BrickDropTests`.
The routing, disc-set planning, copy semantics (skip/overwrite), and metadata safety (including symlinks) are covered by XCTest tests in `Tests/BrickDropTests`.

```sh
swift build
Expand Down
38 changes: 34 additions & 4 deletions Sources/BrickDrop/DotCleanRunner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Foundation

enum DotCleanError: LocalizedError {
case failed(exitCode: Int32, message: String)
case timedOut(seconds: TimeInterval)

var errorDescription: String? {
switch self {
Expand All @@ -10,15 +11,23 @@ enum DotCleanError: LocalizedError {
return detail.isEmpty
? "dot_clean failed with exit code \(exitCode)."
: "dot_clean failed: \(detail)"
case .timedOut(let seconds):
return "dot_clean did not finish within \(Int(seconds)) seconds and was stopped."
}
}
}

private final class OutputBox: @unchecked Sendable {
var data = Data()
}

struct DotCleanRunner: Sendable {
let executableURL: URL
let timeout: TimeInterval

init(executableURL: URL = URL(fileURLWithPath: "/usr/sbin/dot_clean")) {
init(executableURL: URL = URL(fileURLWithPath: "/usr/sbin/dot_clean"), timeout: TimeInterval = 120) {
self.executableURL = executableURL
self.timeout = timeout
}

@discardableResult
Expand All @@ -30,10 +39,31 @@ struct DotCleanRunner: Sendable {
process.standardOutput = output
process.standardError = output

let finished = DispatchSemaphore(value: 0)
process.terminationHandler = { _ in finished.signal() }
try process.run()
process.waitUntilExit()
let data = output.fileHandleForReading.readDataToEndOfFile()
let message = String(data: data, encoding: .utf8) ?? ""

// Drain the pipe concurrently so a chatty child can't block on a full buffer.
let box = OutputBox()
let drained = DispatchGroup()
drained.enter()
DispatchQueue.global().async {
box.data = output.fileHandleForReading.readDataToEndOfFile()
drained.leave()
}

if finished.wait(timeout: .now() + timeout) == .timedOut {
process.terminate()
if finished.wait(timeout: .now() + 2) == .timedOut {
kill(process.processIdentifier, SIGKILL)
_ = finished.wait(timeout: .now() + 2)
}
_ = drained.wait(timeout: .now() + 2)
throw DotCleanError.timedOut(seconds: timeout)
}
drained.wait()

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Bound output draining after the parent exits

When the executable exits after spawning a subprocess that inherits stdout or stderr, finished is signaled before the pipe reaches EOF, so this unconditional wait can block indefinitely and bypass the configured timeout. For example, a script containing sleep 30 &; exit 0 makes clean wait about 30 seconds even with a sub-second timeout, leaving the eject UI stuck; apply a deadline to the drain phase as well and close or otherwise release the pipe on expiry.

Useful? React with 👍 / 👎.


let message = String(data: box.data, encoding: .utf8) ?? ""
guard process.terminationStatus == 0 else {
throw DotCleanError.failed(exitCode: process.terminationStatus, message: message)
}
Expand Down
51 changes: 32 additions & 19 deletions Sources/BrickDrop/RoutingEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ struct RoutingEngine: Sendable {
private let aliases: [ROMSystem: [String]] = [
.fc: ["fc", "nes", "famicom"],
.sfc: ["sfc", "snes", "superfamicom", "super nintendo"],
.gb: ["gameboy", "game boy", " dmg "],
.gb: ["gameboy", "game boy", "dmg"],
.gbc: ["gbc", "gameboycolor", "game boy color"],
.gba: ["gba", "gameboyadvance", "game boy advance"],
.n64: ["n64", "nintendo64", "nintendo 64"],
Expand All @@ -32,7 +32,7 @@ struct RoutingEngine: Sendable {
.ms: ["mastersystem", "master system"],
.pce: ["pcengine", "pc engine", "turbografx", "tg16"],
.ps: ["ps1", "psx", "playstation", "psone"],
.dc: ["dreamcast", " dc "],
.dc: ["dreamcast", "dc"],
.psp: ["psp"],
.arcade: ["arcade", "mame", "fbneo", "fba"],
.neogeo: ["neogeo", "neo geo"]
Expand All @@ -54,9 +54,12 @@ struct RoutingEngine: Sendable {
let stem = fileURL.deletingPathExtension().lastPathComponent.lowercased()
let hasCue = siblingNames.contains { name in
let sibling = URL(fileURLWithPath: name)
return sibling.pathExtension.lowercased() == "cue" &&
sibling.deletingPathExtension().lastPathComponent.lowercased() == stem
} || siblingNames.contains { URL(fileURLWithPath: $0).pathExtension.lowercased() == "cue" }
guard sibling.pathExtension.lowercased() == "cue" else { return false }
let cueStem = sibling.deletingPathExtension().lastPathComponent.lowercased()
// Multi-track/disc dumps name their files "Game (Track 1).bin" or "Game (Disc 1).bin"
// next to a single "Game.cue".
return stem == cueStem || stem.hasPrefix(cueStem + " (track") || stem.hasPrefix(cueStem + " (disc")
}
if hasCue { return .resolved(.ps, reason: "BIN is part of a CUE disc set") }
if let heuristic = systemFromContext(context) {
return .resolved(heuristic, reason: "Detected \(heuristic.displayName) from its name or source folder")
Expand Down Expand Up @@ -127,25 +130,35 @@ struct RoutingEngine: Sendable {
.compactMap { $0 }.joined(separator: " "))
}

/// Lowercases and turns every non-alphanumeric run into a single space, padded on both ends,
/// so aliases can be matched as whole words with a plain substring search.
private func normalized(_ value: String) -> String {
" " + value.lowercased()
.replacingOccurrences(of: "_", with: " ")
.replacingOccurrences(of: "-", with: " ")
.replacingOccurrences(of: "/", with: " ") + " "
var result = " "
var lastWasSpace = true
for scalar in value.lowercased().unicodeScalars {
if CharacterSet.alphanumerics.contains(scalar) {
result.unicodeScalars.append(scalar)
lastWasSpace = false
} else if !lastWasSpace {
result.append(" ")
lastWasSpace = true
}
}
return lastWasSpace ? result : result + " "
}

private func systemFromContext(_ context: String, allowed: Set<ROMSystem>? = nil) -> ROMSystem? {
// More specific aliases win before shorter terms such as GB.
let ordered = ROMSystem.allCases.sorted {
let left = aliases[$0, default: []].map(\.count).max() ?? 0
let right = aliases[$1, default: []].map(\.count).max() ?? 0
return left > right
}
for system in ordered where allowed?.contains(system) ?? true {
for alias in aliases[system, default: []] where context.contains(alias) {
return system
}
// Longest alias first so "game boy color" beats "game boy"; whole-word matching keeps
// short aliases like "nes" or "dc" from firing inside longer words.
for entry in orderedAliases where allowed?.contains(entry.system) ?? true {
if context.contains(" \(entry.alias) ") { return entry.system }
}
return nil
}

private var orderedAliases: [(alias: String, system: ROMSystem)] {
ROMSystem.allCases
.flatMap { system in aliases[system, default: []].map { (alias: $0, system: system) } }
.sorted { $0.alias.count > $1.alias.count }
}
}
18 changes: 18 additions & 0 deletions Tests/BrickDropTests/DotCleanRunnerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,22 @@ final class DotCleanRunnerTests: XCTestCase {
XCTAssertNotEqual(exitCode, 0)
}
}

func testTimesOutHungProcess() {
let started = Date()

// A script that ignores its arguments and never finishes on its own.
let script = FileManager.default.temporaryDirectory.appending(path: "hang-\(UUID().uuidString).sh")
defer { try? FileManager.default.removeItem(at: script) }
XCTAssertNoThrow(try "#!/bin/sh\nexec sleep 30\n".write(to: script, atomically: true, encoding: .utf8))
XCTAssertNoThrow(try FileManager.default.setAttributes([.posixPermissions: 0o755], ofItemAtPath: script.path))

let hanging = DotCleanRunner(executableURL: script, timeout: 0.5)
XCTAssertThrowsError(try hanging.clean(volumeURL: URL(fileURLWithPath: "/Volumes/Card"))) { error in
guard case DotCleanError.timedOut = error else {
return XCTFail("Unexpected error: \(error)")
}
}
XCTAssertLessThan(Date().timeIntervalSince(started), 10)
}
}
93 changes: 93 additions & 0 deletions Tests/BrickDropTests/ImportExecutorTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import XCTest
@testable import BrickDrop

final class ImportExecutorTests: XCTestCase {
private var directory: URL!

override func setUpWithError() throws {
directory = FileManager.default.temporaryDirectory.appending(path: "BrickDropExecutor-\(UUID().uuidString)")
try FileManager.default.createDirectory(at: directory.appending(path: "SD"), withIntermediateDirectories: true)
}

override func tearDownWithError() throws {
if let directory { try? FileManager.default.removeItem(at: directory) }
}

private func makeItem(contents: UInt8, status: ImportStatus = .ready, withDestination: Bool = true) throws -> ImportItem {
let source = directory.appending(path: "Tetris-\(UUID().uuidString).gb")
try Data([contents]).write(to: source)
return ImportItem(
sourceURL: source,
system: .gb,
candidateSystems: [.gb],
destinationURL: withDestination ? directory.appending(path: "SD/Roms/GB/Tetris.gb") : nil,
status: status,
detail: ""
)
}

func testCopiesIntoNewDestinationDirectories() throws {
let item = try makeItem(contents: 1)
let report = ImportExecutor().execute(items: [item], sdRoot: directory.appending(path: "SD"), overwriteExisting: false)

XCTAssertEqual(report.copied, [item.id])
XCTAssertEqual(try Data(contentsOf: item.destinationURL!), Data([1]))
}

func testSkipsExistingFileWhenNotOverwriting() throws {
let existing = try makeItem(contents: 1)
_ = ImportExecutor().execute(items: [existing], sdRoot: directory.appending(path: "SD"), overwriteExisting: false)
let replacement = try makeItem(contents: 2)

let report = ImportExecutor().execute(items: [replacement], sdRoot: directory.appending(path: "SD"), overwriteExisting: false)

XCTAssertEqual(report.skipped, [replacement.id])
XCTAssertTrue(report.copied.isEmpty)
XCTAssertEqual(try Data(contentsOf: replacement.destinationURL!), Data([1]))
}

func testReplacesExistingFileWhenOverwriting() throws {
let existing = try makeItem(contents: 1)
_ = ImportExecutor().execute(items: [existing], sdRoot: directory.appending(path: "SD"), overwriteExisting: false)
let replacement = try makeItem(contents: 2)

let report = ImportExecutor().execute(items: [replacement], sdRoot: directory.appending(path: "SD"), overwriteExisting: true)

XCTAssertEqual(report.copied, [replacement.id])
XCTAssertEqual(try Data(contentsOf: replacement.destinationURL!), Data([2]))
}

func testIgnoresItemsThatAreNotReady() throws {
let item = try makeItem(contents: 1, status: .needsChoice)
let report = ImportExecutor().execute(items: [item], sdRoot: directory.appending(path: "SD"), overwriteExisting: true)

XCTAssertTrue(report.copied.isEmpty)
XCTAssertFalse(FileManager.default.fileExists(atPath: item.destinationURL!.path))
}

func testReportsFailureForMissingDestinationOrSource() throws {
let noDestination = try makeItem(contents: 1, withDestination: false)
var missingSource = try makeItem(contents: 1)
try FileManager.default.removeItem(at: missingSource.sourceURL)
missingSource.destinationURL = directory.appending(path: "SD/Roms/GB/Other.gb")

let report = ImportExecutor().execute(
items: [noDestination, missingSource], sdRoot: directory.appending(path: "SD"), overwriteExisting: false
)

XCTAssertNotNil(report.failures[noDestination.id])
XCTAssertNotNil(report.failures[missingSource.id])
XCTAssertTrue(report.copied.isEmpty)
}

func testCleansMetadataAfterCopy() throws {
let sd = directory.appending(path: "SD")
try Data([9]).write(to: sd.appending(path: ".DS_Store"))
let item = try makeItem(contents: 1)

let report = ImportExecutor().execute(items: [item], sdRoot: sd, overwriteExisting: false)

XCTAssertEqual(report.cleanup.removedCount, 1)
XCTAssertFalse(FileManager.default.fileExists(atPath: sd.appending(path: ".DS_Store").path))
}
}
33 changes: 33 additions & 0 deletions Tests/BrickDropTests/ImportPlannerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,36 @@ final class ImportPlannerTests: XCTestCase {
XCTAssertTrue(items.allSatisfy { $0.destinationURL?.path.contains("Roms/PS/Game/") == true })
}
}

extension ImportPlannerTests {
func testM3UIncludesExistingReferencedDiscsAndSkipsMissingOnes() throws {
let source = temporaryDirectory.appending(path: "Source")
let sd = temporaryDirectory.appending(path: "SD")
try FileManager.default.createDirectory(at: source, withIntermediateDirectories: true)
try FileManager.default.createDirectory(at: sd, withIntermediateDirectories: true)
let playlist = source.appending(path: "Final Fantasy VII PS1.m3u")
try "# comment\nDisc 1.chd\n\nDisc 2.chd\nMissing.chd\n".write(to: playlist, atomically: true, encoding: .utf8)
try Data([1]).write(to: source.appending(path: "Disc 1.chd"))
try Data([2]).write(to: source.appending(path: "Disc 2.chd"))

let items = ImportPlanner().plan(urls: [playlist], sdRoot: sd)

XCTAssertEqual(items.count, 3)
XCTAssertTrue(items.allSatisfy { $0.system == .ps })
XCTAssertTrue(items.contains { $0.destinationURL?.path.hasSuffix("Roms/PS/Final Fantasy VII PS1/Disc 2.chd") == true })
}

func testCueIgnoresMissingAndUnquotedReferences() throws {
let source = temporaryDirectory.appending(path: "Source")
let sd = temporaryDirectory.appending(path: "SD")
try FileManager.default.createDirectory(at: source, withIntermediateDirectories: true)
try FileManager.default.createDirectory(at: sd, withIntermediateDirectories: true)
let cue = source.appending(path: "Game.cue")
try "FILE Track01.bin BINARY\nFILE \"Missing.bin\" BINARY\n".write(to: cue, atomically: true, encoding: .utf8)
try Data([1]).write(to: source.appending(path: "Track01.bin"))

let items = ImportPlanner().plan(urls: [cue], sdRoot: sd)

XCTAssertEqual(items.map { $0.sourceURL.lastPathComponent }.sorted(), ["Game.cue", "Track01.bin"])
}
}
44 changes: 44 additions & 0 deletions Tests/BrickDropTests/MetadataCleanerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,48 @@ final class MetadataCleanerTests: XCTestCase {
XCTAssertFalse(FileManager.default.fileExists(atPath: appleDouble.path))
XCTAssertFalse(FileManager.default.fileExists(atPath: dsStore.path))
}

func testCleanerNeverFollowsOrRemovesSymlinks() throws {
let base = FileManager.default.temporaryDirectory.appending(path: "BrickDropSymlink-\(UUID().uuidString)")
defer { try? FileManager.default.removeItem(at: base) }
let root = base.appending(path: "SD")
let outside = base.appending(path: "Outside")
try FileManager.default.createDirectory(at: root, withIntermediateDirectories: true)
try FileManager.default.createDirectory(at: outside.appending(path: ".Trashes"), withIntermediateDirectories: true)
let outsideMetadata = outside.appending(path: "._precious")
let outsideDSStore = outside.appending(path: ".DS_Store")
try Data([1]).write(to: outsideMetadata)
try Data([2]).write(to: outsideDSStore)

// A symlinked directory pointing outside the card, one named like metadata, and a file symlink.
let linkedDirectory = root.appending(path: "Linked")
let linkedTrashes = root.appending(path: ".Trashes")
let linkedAppleDouble = root.appending(path: "._link")
try FileManager.default.createSymbolicLink(at: linkedDirectory, withDestinationURL: outside)
try FileManager.default.createSymbolicLink(at: linkedTrashes, withDestinationURL: outside.appending(path: ".Trashes"))
try FileManager.default.createSymbolicLink(at: linkedAppleDouble, withDestinationURL: outsideMetadata)

let report = MetadataCleaner().clean(root: root)

XCTAssertEqual(report.removedCount, 0)
XCTAssertTrue(FileManager.default.fileExists(atPath: outsideMetadata.path))
XCTAssertTrue(FileManager.default.fileExists(atPath: outsideDSStore.path))
XCTAssertTrue(FileManager.default.fileExists(atPath: outside.appending(path: ".Trashes").path))
for link in [linkedDirectory, linkedTrashes, linkedAppleDouble] {
XCTAssertNotNil(try? FileManager.default.destinationOfSymbolicLink(atPath: link.path), link.lastPathComponent)
}
}

func testCleanerRemovesMetadataDirectoriesRecursively() throws {
let root = FileManager.default.temporaryDirectory.appending(path: "BrickDropDirs-\(UUID().uuidString)")
defer { try? FileManager.default.removeItem(at: root) }
let trashes = root.appending(path: ".Trashes/501")
try FileManager.default.createDirectory(at: trashes, withIntermediateDirectories: true)
try Data([1]).write(to: trashes.appending(path: "old.gb"))

let report = MetadataCleaner().clean(root: root)

XCTAssertEqual(report.removedCount, 1)
XCTAssertFalse(FileManager.default.fileExists(atPath: root.appending(path: ".Trashes").path))
}
}
Loading
Loading