Skip to content

Run Windows games on Apple Silicon — free, open source, with an open compatibility database that tells you what actually works.

Bottles: nothing occupying a bottle name can stay invisible (#38)

Gauthier Piarrette30531299b5d5
diff --git a/Sources/HighballKit/BottleStore.swift b/Sources/HighballKit/BottleStore.swift
index 4a6df81..eb0a930 100644
--- a/Sources/HighballKit/BottleStore.swift
+++ b/Sources/HighballKit/BottleStore.swift
@@ -49,8 +49,12 @@ public struct BottleStore: Sendable {
let name = rawName.trimmingCharacters(in: .whitespaces)
if let problem = Self.nameProblem(name) { throw HighballError.invalid(problem) }
try paths.ensure()
- let url = paths.bottle(name)
- guard !FileManager.default.fileExists(atPath: url.path) else { throw HighballError.invalid("bottle '\(name)' already exists") }
+ let url = try Self.bottleURL(name, in: paths)
+ // lstat, not fileExists: fileExists follows symlinks, so a dangling one sailed through
+ // this guard and createDirectory then failed with a raw Foundation error on a name the
+ // user could not see in the list.
+ var existing = stat()
+ guard lstat(url.path, &existing) != 0 else { throw HighballError.invalid("bottle '\(name)' already exists") }
try FileManager.default.createDirectory(at: url, withIntermediateDirectories: true)
var settings = BottleSettings(name: name, engineID: engine.id)
settings.renderer = renderer
@@ -255,15 +259,23 @@ public struct BottleStore: Sendable {
.compactMap { url -> DamagedBottle? in
let name = url.lastPathComponent
if name.hasPrefix(".") { return nil }
+ // Deliberately lstat and no S_IFDIR requirement: anything occupying a name in
+ // bottles/ blocks create() and must therefore be reachable. Requiring a directory
+ // left a stray file or a dangling symlink invisible to both list() and damaged()
+ // while still taking the name — the #38 stranding shape in miniature.
var st = stat()
- guard lstat(url.path, &st) == 0, (st.st_mode & S_IFMT) == S_IFDIR else { return nil }
+ guard lstat(url.path, &st) == 0 else { return nil }
if (try? Bottle.load(url)) != nil { return nil }
- // A prefix with a Windows dir is a real bottle that lost its settings; anything
- // else is the wreckage of a delete that stopped part way.
- let hasPrefix = FileManager.default.fileExists(atPath: url.appending(path: "drive_c/windows").path)
- return DamagedBottle(name: name, url: url,
- reason: hasPrefix ? "its bottle.json is missing or unreadable"
- : "a delete stopped part way and left these files behind")
+ let reason: String
+ if (st.st_mode & S_IFMT) != S_IFDIR {
+ reason = "something that isn't a bottle is using this name"
+ } else if FileManager.default.fileExists(atPath: url.appending(path: "drive_c/windows").path) {
+ // A real prefix that lost its settings, rather than the wreckage of a delete.
+ reason = "its bottle.json is missing or unreadable"
+ } else {
+ reason = "a delete stopped part way and left these files behind"
+ }
+ return DamagedBottle(name: name, url: url, reason: reason)
}
.sorted { $0.name < $1.name }
}
diff --git a/Tests/HighballKitTests/BottleDeleteTests.swift b/Tests/HighballKitTests/BottleDeleteTests.swift
index 92e6b1f..920a4d7 100644
--- a/Tests/HighballKitTests/BottleDeleteTests.swift
+++ b/Tests/HighballKitTests/BottleDeleteTests.swift
@@ -260,14 +260,33 @@ final class BottleDeleteTests: XCTestCase {
XCTAssertThrowsError(try store.delete("nope"))
}
- func testDamagedIgnoresDotFilesLooseFilesAndSymlinks() throws {
+ /// Anything occupying a name in bottles/ blocks create(), so it must be reachable. Only
+ /// dot-files are hidden; a stray file or dangling symlink is reported so it can be removed.
+ func testDamagedSurfacesAnythingOccupyingABottleName() throws {
try "x".write(to: paths.bottles.appending(path: ".DS_Store"), atomically: true, encoding: .utf8)
try "x".write(to: paths.bottles.appending(path: "loose.txt"), atomically: true, encoding: .utf8)
- try FileManager.default.createSymbolicLink(at: paths.bottles.appending(path: "alink"),
- withDestinationURL: home)
+ try FileManager.default.createSymbolicLink(atPath: paths.bottles.appending(path: "Unplugged").path,
+ withDestinationPath: "/nonexistent-target")
try makeBottle("play")
- XCTAssertTrue(try store.damaged().isEmpty, "damaged() must list only real bottle directories")
+
XCTAssertEqual(try store.list().map(\.name), ["play"])
+ XCTAssertEqual(try store.damaged().map(\.name), ["Unplugged", "loose.txt"],
+ "a dot-file stays hidden; anything else taking a name must be visible")
+ // And each must be removable, or surfacing it would be pointless.
+ for name in ["Unplugged", "loose.txt"] { XCTAssertNoThrow(try store.delete(name)) }
+ XCTAssertTrue(try store.damaged().isEmpty)
+ }
+
+ /// A dangling symlink used to slip past create()'s fileExists guard, which follows links, so
+ /// creation failed later with a raw Foundation error on a name nothing would show.
+ func testCreateRefusesANameHeldByADanglingSymlink() throws {
+ try FileManager.default.createSymbolicLink(atPath: paths.bottle("Unplugged").path,
+ withDestinationPath: "/nonexistent-target")
+ XCTAssertEqual(try store.damaged().map(\.name), ["Unplugged"])
+ var st = stat()
+ XCTAssertEqual(lstat(paths.bottle("Unplugged").path, &st), 0, "lstat must see the link")
+ XCTAssertFalse(FileManager.default.fileExists(atPath: paths.bottle("Unplugged").path),
+ "fileExists follows the link, which is why the guard had to change")
}
func testSweepTrashClearsWhatAnEarlierPurgeLeft() throws {