Skip to content

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

engine install: a directory component never replaces a filled directory (#76)

Gauthier Piarrette02f369beeced
diff --git a/Sources/HighballKit/EngineStore.swift b/Sources/HighballKit/EngineStore.swift
index 970dda9..0d05673 100644
--- a/Sources/HighballKit/EngineStore.swift
+++ b/Sources/HighballKit/EngineStore.swift
@@ -300,6 +300,16 @@ public struct EngineStore: Sendable {
onProgress(written, total)
}
+ /// Non-nil, with the reason, when placing a directory at `dest` would take over a directory
+ /// that already has contents. A single-file target and an empty or absent directory are fine.
+ static func directoryCollision(at dest: URL, sourceIsDirectory: Bool) -> String? {
+ guard sourceIsDirectory else { return nil }
+ var isDir: ObjCBool = false
+ guard FileManager.default.fileExists(atPath: dest.path, isDirectory: &isDir), isDir.boolValue else { return nil }
+ let count = (try? FileManager.default.contentsOfDirectory(atPath: dest.path).count) ?? 0
+ return count == 0 ? nil : "which already holds \(count) items"
+ }
+
func extract(_ archive: URL, component: EngineManifest.Component, name: String, into root: URL) throws {
guard let ex = component.extract else {
// Plain file (e.g. winetricks script): copy into tools/.
@@ -330,6 +340,12 @@ public struct EngineStore: Sendable {
var isDir: ObjCBool = false
_ = FileManager.default.fileExists(atPath: source.path, isDirectory: &isDir)
let dest = root.appending(path: ex.into, directoryHint: isDir.boolValue ? .isDirectory : .notDirectory)
+ if let why = Self.directoryCollision(at: dest, sourceIsDirectory: isDir.boolValue) {
+ // highball#76: a directory component that lands on a directory another component
+ // filled used to replace it wholesale (the first r7 candidate lost its whole Wine
+ // tree this way and still reported "installed"). Refuse and say which.
+ throw HighballError.invalid("component '\(name)' would replace \(ex.into), \(why). A component adds a directory of its own or replaces one file; to add a file to another component's directory, name that file as 'into'.")
+ }
try FileManager.default.createDirectory(at: dest.deletingLastPathComponent(), withIntermediateDirectories: true)
try? FileManager.default.removeItem(at: dest)
try FileManager.default.moveItem(at: source, to: dest)
diff --git a/Tests/HighballKitTests/EngineExtractTests.swift b/Tests/HighballKitTests/EngineExtractTests.swift
new file mode 100644
index 0000000..05edf24
--- /dev/null
+++ b/Tests/HighballKitTests/EngineExtractTests.swift
@@ -0,0 +1,21 @@
+import XCTest
+@testable import HighballKit
+
+/// highball#76: a component whose `into` named the existing `engine` directory replaced the
+/// whole Wine tree (removeItem then moveItem) and the install still said "installed". A
+/// directory component may create its target, never take over one another component filled.
+final class EngineExtractTests: XCTestCase {
+ func testADirectoryComponentMayNotReplaceAFilledDirectory() throws {
+ let root = FileManager.default.temporaryDirectory.appending(path: "engine-\(UUID().uuidString)")
+ defer { try? FileManager.default.removeItem(at: root) }
+ let engine = root.appending(path: "engine")
+ try FileManager.default.createDirectory(at: engine.appending(path: "bin"), withIntermediateDirectories: true)
+ try Data("wine".utf8).write(to: engine.appending(path: "bin/wine"))
+ XCTAssertNotNil(EngineStore.directoryCollision(at: engine, sourceIsDirectory: true), "a filled directory is not replaced")
+ XCTAssertNil(EngineStore.directoryCollision(at: root.appending(path: "renderers/dxmt/wine"), sourceIsDirectory: true), "a new directory is fine")
+ XCTAssertNil(EngineStore.directoryCollision(at: engine.appending(path: "bin/wine"), sourceIsDirectory: false), "a single file may replace a file (the MoltenVK override)")
+ let empty = root.appending(path: "empty")
+ try FileManager.default.createDirectory(at: empty, withIntermediateDirectories: true)
+ XCTAssertNil(EngineStore.directoryCollision(at: empty, sourceIsDirectory: true), "an empty directory is just a placeholder")
+ }
+}