Free fork of NotProton: Windows Steam games in Mac Steam, on CodeWeavers' Wine and Apple's Game Porting Toolkit
Swift apple-silicond3dmetaldxmtdxvkgame-porting-toolkitgptkmac-gamingmacosmetalfxrosetta-2steamsteam-playwinewinecx
Update bridge files by content instead of size and timestamps
Sharad Mayank Sinha7ca0a3bf956e
diff --git a/app/Sources/NotProtonApp/Model/BridgePayload.swift b/app/Sources/NotProtonApp/Model/BridgePayload.swiftindex b8c25e0..5f0a5d3 100644--- a/app/Sources/NotProtonApp/Model/BridgePayload.swift+++ b/app/Sources/NotProtonApp/Model/BridgePayload.swift@@ -84,35 +84,25 @@ enum BridgePayload {located: Located,bridge: URL = SupportPaths.bridge) throws -> Outcome {- let files = FileManager.defaultvar staged: [String] = []var unchanged: [String] = []for entry in located.sources {- let sourceSize = try fileSize(entry.source)+ let sourceHash = try Digest.sha256(of: entry.source)for bridgePath in entry.bridgePaths {let destination = bridge.appending(path: bridgePath)- if let existing = try? fileSize(destination), existing == sourceSize {+ if Digest.sha256IfPresent(destination) == sourceHash {unchanged.append(bridgePath)continue}- try files.createDirectory(- at: destination.deletingLastPathComponent(), withIntermediateDirectories: true- )- try? files.removeItem(at: destination)- try files.copyItem(at: entry.source, to: destination)+ try WriteRefused.catching(destination) {+ try atomicReplace(destination, from: entry.source, step: step)+ }staged.append(bridgePath)}}return Outcome(staged: staged, unchanged: unchanged)}-- private static func fileSize(_ url: URL) throws -> Int {- let attrs = try FileManager.default.attributesOfItem(- atPath: url.path(percentEncoded: false)- )- return (attrs[.size] as? Int) ?? 0- }}diff --git a/app/Sources/NotProtonApp/Support/AtomicReplace.swift b/app/Sources/NotProtonApp/Support/AtomicReplace.swiftindex fd53625..8cbace4 100644--- a/app/Sources/NotProtonApp/Support/AtomicReplace.swift+++ b/app/Sources/NotProtonApp/Support/AtomicReplace.swift@@ -2,6 +2,30 @@import Foundation+func atomicReplace(_ destination: URL, from source: URL, step: String) throws {+ let fm = FileManager.default+ let directory = destination.deletingLastPathComponent()+ try fm.createDirectory(at: directory, withIntermediateDirectories: true)++ let staging = directory.appending(path: ".\(destination.lastPathComponent).\(UUID().uuidString)")+ defer { try? fm.removeItem(at: staging) }+ try fm.copyItem(at: source, to: staging)++ if rename(+ staging.path(percentEncoded: false), destination.path(percentEncoded: false)+ ) != 0 {+ let code = errno+ if code == EACCES || code == EPERM {+ throw WriteRefused(path: destination.path(percentEncoded: false))+ }+ let reason = String(cString: strerror(code))+ throw StepFailure(+ step: step,+ detail: "Replacing \(destination.path(percentEncoded: false)) failed. \(reason)"+ )+ }+}+func atomicReplace(_ destination: URL, with data: Data, step: String) throws {let fm = FileManager.defaultlet directory = destination.deletingLastPathComponent()diff --git a/app/Tests/NotProtonAppTests/BridgePayloadTests.swift b/app/Tests/NotProtonAppTests/BridgePayloadTests.swiftindex c8904dd..220a631 100644--- a/app/Tests/NotProtonAppTests/BridgePayloadTests.swift+++ b/app/Tests/NotProtonAppTests/BridgePayloadTests.swift@@ -1,3 +1,4 @@+import Darwinimport Foundationimport Testing@@ -60,8 +61,8 @@ struct BridgePayloadTests {}}- @Test("Files at the correct size are left alone on a second run")- func skipsMatchingSize() throws {+ @Test("Matching contents are left untouched on a second run")+ func skipsMatchingContents() throws {let work = try scratchDirectory("bridge")defer { try? FileManager.default.removeItem(at: work) }@@ -70,12 +71,81 @@ struct BridgePayloadTests {let bridge = work.appending(path: "staged")let first = try BridgePayload.stage(located: located, bridge: bridge)+ let allPaths = BridgePayload.entries.flatMap(\.bridgePaths)+ let untouchedDate = Date(timeIntervalSince1970: 1_000_000)+ let before = try allPaths.map { path in+ let destination = bridge.appending(path: path).path(percentEncoded: false)+ try FileManager.default.setAttributes([.modificationDate: untouchedDate], ofItemAtPath: destination)+ return try FileManager.default.attributesOfItem(atPath: destination)+ }let second = try BridgePayload.stage(located: located, bridge: bridge)#expect(!first.staged.isEmpty)- #expect(second.staged.isEmpty, "files were re-staged despite matching size")- let allPaths = BridgePayload.entries.flatMap(\.bridgePaths)+ #expect(second.staged.isEmpty, "files were re-staged despite matching contents")#expect(second.unchanged.count == allPaths.count)+ for (path, original) in zip(allPaths, before) {+ let after = try FileManager.default.attributesOfItem(+ atPath: bridge.appending(path: path).path(percentEncoded: false)+ )+ #expect(after[.systemFileNumber] as? NSNumber == original[.systemFileNumber] as? NSNumber)+ #expect(after[.modificationDate] as? Date == original[.modificationDate] as? Date)+ }+ }++ @Test("Changed contents replace every copy even when byte counts match")+ func replacesSameSizeContents() throws {+ let work = try scratchDirectory("bridge")+ defer { try? FileManager.default.removeItem(at: work) }++ let root = try fakeResources(in: work)+ let located = try BridgePayload.locate(root: root)+ let bridge = work.appending(path: "staged")+ _ = try BridgePayload.stage(located: located, bridge: bridge)++ let entry = try #require(located.sources.first { $0.bridgePaths.count > 1 })+ let previous = try Data(contentsOf: entry.source)+ let changed = Data(repeating: 0xff, count: previous.count)+ try changed.write(to: entry.source)+ let result = try BridgePayload.stage(located: located, bridge: bridge)++ #expect(result.staged == entry.bridgePaths)+ #expect(Set(result.unchanged).isDisjoint(with: entry.bridgePaths))+ for path in entry.bridgePaths {+ #expect(try Data(contentsOf: bridge.appending(path: path)) == changed)+ }+ }++ @Test("An unavailable source fails without changing the previous destination", arguments: [false, true])+ func unavailableSourcePreservesDestination(missing: Bool) throws {+ let work = try scratchDirectory("bridge")+ defer { try? FileManager.default.removeItem(at: work) }+ let source = work.appending(path: "source")+ let destination = work.appending(path: "staged/steam.exe")+ try Data("old".utf8).write(to: source)+ let located = BridgePayload.Located(sources: [(source: source, bridgePaths: ["steam.exe"])])+ _ = try BridgePayload.stage(located: located, bridge: destination.deletingLastPathComponent())++ try Data("new".utf8).write(to: source)+ defer {+ try? FileManager.default.setAttributes(+ [.posixPermissions: 0o644], ofItemAtPath: source.path(percentEncoded: false)+ )+ }+ if missing {+ try FileManager.default.removeItem(at: source)+ } else {+ try FileManager.default.setAttributes(+ [.posixPermissions: 0], ofItemAtPath: source.path(percentEncoded: false)+ )+ }++ #expect(throws: (any Error).self) {+ try BridgePayload.stage(located: located, bridge: destination.deletingLastPathComponent())+ }+ #expect(try Data(contentsOf: destination) == Data("old".utf8))+ #expect(try FileManager.default.contentsOfDirectory(+ atPath: destination.deletingLastPathComponent().path(percentEncoded: false)+ ) == ["steam.exe"])}@Test("Flat and arch copies are byte-identical")@@ -99,6 +169,88 @@ struct BridgePayloadTests {}}+ @Test("Replacement preserves source permissions and extended attributes")+ func replacementPreservesMetadata() throws {+ let work = try scratchDirectory("bridge")+ defer { try? FileManager.default.removeItem(at: work) }+ let source = work.appending(path: "source")+ let bridge = work.appending(path: "staged")+ let destination = bridge.appending(path: "steam.exe")+ try FileManager.default.createDirectory(at: bridge, withIntermediateDirectories: true)+ try Data("old".utf8).write(to: destination)+ try Data("new".utf8).write(to: source)+ try FileManager.default.setAttributes(+ [.posixPermissions: 0o751], ofItemAtPath: source.path(percentEncoded: false)+ )+ let attribute = Data("metadata".utf8)+ let setResult = attribute.withUnsafeBytes {+ setxattr(source.path(percentEncoded: false), "com.notproton.test", $0.baseAddress, $0.count, 0, 0)+ }+ #expect(setResult == 0)++ let located = BridgePayload.Located(sources: [(source: source, bridgePaths: ["steam.exe"])])+ let result = try BridgePayload.stage(located: located, bridge: bridge)++ #expect(result.staged == ["steam.exe"])+ #expect(try Data(contentsOf: destination) == Data("new".utf8))+ let attributes = try FileManager.default.attributesOfItem(atPath: destination.path(percentEncoded: false))+ #expect(attributes[.posixPermissions] as? Int == 0o751)+ var copiedAttribute = Data(count: attribute.count)+ let readCount = copiedAttribute.withUnsafeMutableBytes {+ getxattr(destination.path(percentEncoded: false), "com.notproton.test", $0.baseAddress, $0.count, 0, 0)+ }+ #expect(readCount == attribute.count)+ #expect(copiedAttribute == attribute)+ #expect(try FileManager.default.contentsOfDirectory(atPath: bridge.path(percentEncoded: false)) == ["steam.exe"])+ }++ @Test("An atomic copy failure preserves the destination and removes staging files", arguments: [false, true])+ func atomicCopyFailurePreservesDestination(missing: Bool) throws {+ let work = try scratchDirectory("bridge")+ defer { try? FileManager.default.removeItem(at: work) }+ let source = work.appending(path: "source")+ let bridge = work.appending(path: "staged")+ let destination = bridge.appending(path: "steam.exe")+ try FileManager.default.createDirectory(at: bridge, withIntermediateDirectories: true)+ try Data("old".utf8).write(to: destination)+ defer {+ try? FileManager.default.setAttributes(+ [.posixPermissions: 0o644], ofItemAtPath: source.path(percentEncoded: false)+ )+ }+ if !missing {+ try Data("new".utf8).write(to: source)+ try FileManager.default.setAttributes(+ [.posixPermissions: 0], ofItemAtPath: source.path(percentEncoded: false)+ )+ }++ #expect(throws: (any Error).self) {+ try atomicReplace(destination, from: source, step: BridgePayload.step)+ }+ #expect(try Data(contentsOf: destination) == Data("old".utf8))+ #expect(try FileManager.default.contentsOfDirectory(atPath: bridge.path(percentEncoded: false)) == ["steam.exe"])+ }++ @Test("An atomic rename failure preserves the destination and removes staging files")+ func atomicRenameFailurePreservesDestination() throws {+ let work = try scratchDirectory("bridge")+ defer { try? FileManager.default.removeItem(at: work) }+ let source = work.appending(path: "source")+ let bridge = work.appending(path: "staged")+ let destination = bridge.appending(path: "steam.exe")+ try FileManager.default.createDirectory(at: destination, withIntermediateDirectories: true)+ let previous = destination.appending(path: "previous")+ try Data("old".utf8).write(to: previous)+ try Data("new".utf8).write(to: source)++ #expect(throws: StepFailure.self) {+ try atomicReplace(destination, from: source, step: BridgePayload.step)+ }+ #expect(try Data(contentsOf: previous) == Data("old".utf8))+ #expect(try FileManager.default.contentsOfDirectory(atPath: bridge.path(percentEncoded: false)) == ["steam.exe"])+ }+@Test("The entry manifest covers every built path in payload.manifest")func coversPayloadManifest() throws {let manifest = try PayloadManifest.bundled()diff --git a/dylib/feats/compat_run.sh b/dylib/feats/compat_run.shindex f7ad98a..fbc7959 100755--- a/dylib/feats/compat_run.sh+++ b/dylib/feats/compat_run.sh@@ -820,11 +820,14 @@ bridge_files="$bridge_files lsteamclient.dll steam.exe"if [ -d "$bridge_src" ] && [ -n "$WINEPREFIX" ]; thenstage_step="bridge staging"mkdir -p "$prefix_steam"- # shellcheck disable=SC2086 # the list is ours and has no spaces- src_stamp=$(cd "$bridge_src" && stat -f '%z %m' $bridge_files 2>/dev/null) || true- # shellcheck disable=SC2086- dst_stamp=$(cd "$prefix_steam" && stat -f '%z %m' $bridge_files 2>/dev/null) || true- if [ -n "$src_stamp" ] && [ "$src_stamp" = "$dst_stamp" ]; then+ bridge_matches=1+ for f in $bridge_files; do+ if ! cmp -s "$bridge_src/$f" "$prefix_steam/$f"; then+ bridge_matches=0+ break+ fi+ done+ if [ "$bridge_matches" -eq 1 ]; thenecho "=== bridge already staged ===" >> "$log" 2>&1 || trueelsefor f in $bridge_files; do@@ -833,6 +836,7 @@ if [ -d "$bridge_src" ] && [ -n "$WINEPREFIX" ]; thenecho "=== bridge missing $f ===" >> "$log" 2>&1 || truecontinuefi+ cmp -s "$src" "$prefix_steam/$f" && continuecp -fp "$src" "$prefix_steam/$f" || \echo "=== failed to stage $f ===" >> "$log" 2>&1donediff --git a/dylib/tests/bridgecheck.sh b/dylib/tests/bridgecheck.shindex cd5f788..ccc497a 100644--- a/dylib/tests/bridgecheck.sh+++ b/dylib/tests/bridgecheck.sh@@ -6,7 +6,7 @@ SRC="${1:-$(dirname "$0")/../feats/compat_run.sh}"block=$(sed -n '/^bridge_files="steamclient64/,/^ verify_runner$/p' "$SRC" | sed '$d')case "$block" in- *'stat -f'*) ;;+ *'bridge_matches='*) ;;*) echo "FAIL: the bridge staging block not found in $SRC"; exit 1 ;;esacbody="$block@@ -52,6 +52,15 @@ stageis "the launch goes on" reached "$(cat "$work/out")"logged "the copy is recognised" "=== bridge already staged ==="+echo "== changed bytes with the same size and timestamp"+cp -p "$work/bridge/steam.exe" "$work/stamp"+printf 'edited steam.exe\n' > "$work/bridge/steam.exe"+touch -r "$work/stamp" "$work/bridge/steam.exe"+is "the metadata still matches" "$(stat -f '%z %m' "$steam/steam.exe")" "$(stat -f '%z %m' "$work/bridge/steam.exe")"+stage+unlogged "matching metadata does not hide changed contents" "=== bridge already staged ==="+is "the prefix receives the changed bytes" 6 "$(staged)"+echo "== a DLL the bridge no longer ships is pruned"printf 'old\n' > "$steam/old.dll"stage