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
fix(installer): another app's updater no longer stops the install
raymondchins5eee81d69ec3
diff --git a/app/Sources/BridgeplayInstallerCore/InstallerLog.swift b/app/Sources/BridgeplayInstallerCore/InstallerLog.swiftnew file mode 100644index 0000000..e9b4e15--- /dev/null+++ b/app/Sources/BridgeplayInstallerCore/InstallerLog.swift@@ -0,0 +1,58 @@+import Darwin+import Foundation++/// What the installer did, for bug reports. Home paths are written as `~`.+public enum InstallerLog {+ public static let url = FileManager.default.homeDirectoryForCurrentUser+ .appending(path: "Library/Logs/Bridgeplay/installer.log")+ private static let lock = NSLock()+ private static let limit = 512 * 1024++ public static func write(_ message: String) {+ lock.lock()+ defer { lock.unlock() }+ let manager = FileManager.default+ try? manager.createDirectory(at: url.deletingLastPathComponent(), withIntermediateDirectories: true)+ if let size = (try? manager.attributesOfItem(atPath: url.path))?[.size] as? Int, size > limit {+ let previous = url.appendingPathExtension("1")+ try? manager.removeItem(at: previous)+ try? manager.moveItem(at: url, to: previous)+ }+ let stamp = ISO8601DateFormatter().string(from: Date())+ guard let data = "\(stamp) \(redact(message))\n".data(using: .utf8) else { return }+ if let handle = try? FileHandle(forWritingTo: url) {+ defer { try? handle.close() }+ _ = try? handle.seekToEnd()+ try? handle.write(contentsOf: data)+ } else {+ try? data.write(to: url)+ }+ }++ /// The last lines, capped so they fit in a GitHub issue link.+ public static func tail(lines count: Int = 40, maxCharacters: Int = 4000) -> String {+ lock.lock()+ defer { lock.unlock() }+ guard let text = try? String(contentsOf: url, encoding: .utf8) else { return "" }+ let tail = text.split(separator: "\n", omittingEmptySubsequences: false).suffix(count).joined(separator: "\n")+ return tail.count > maxCharacters ? String(tail.suffix(maxCharacters)) : tail+ }++ public static var system: String {+ let os = ProcessInfo.processInfo.operatingSystemVersion+ let version = "macOS \(os.majorVersion).\(os.minorVersion).\(os.patchVersion)"+ return [sysctl("hw.model"), sysctl("machdep.cpu.brand_string"), version].compactMap { $0 }.joined(separator: ", ")+ }++ static func redact(_ text: String) -> String {+ text.replacingOccurrences(of: NSHomeDirectory(), with: "~")+ }++ private static func sysctl(_ name: String) -> String? {+ var size = 0+ guard sysctlbyname(name, nil, &size, nil, 0) == 0, size > 0 else { return nil }+ var value = [CChar](repeating: 0, count: size)+ guard sysctlbyname(name, &value, &size, nil, 0) == 0 else { return nil }+ return String(cString: value)+ }+}diff --git a/app/Sources/BridgeplayInstallerCore/InstallerService.swift b/app/Sources/BridgeplayInstallerCore/InstallerService.swiftindex f9b5caf..dce69cb 100644--- a/app/Sources/BridgeplayInstallerCore/InstallerService.swift+++ b/app/Sources/BridgeplayInstallerCore/InstallerService.swift@@ -10,6 +10,13 @@ public enum InstallationState: Sendable, Equatable {if case .upToDate = self { return true }return false}++ public var installedVersion: String? {+ switch self {+ case .notInstalled: nil+ case .updateAvailable(_, let version, _), .upToDate(_, let version): version+ }+ }}/// Detects and safely replaces the Bridgeplay app bundle. Game setup stays in the app.@@ -300,16 +307,28 @@ public struct InstallerService: Sendable {}private static func requireAppIsClosed() throws {- for name in ["BridgeplayApp", "Autoupdate"] {- let process = Process()- process.executableURL = URL(fileURLWithPath: "/usr/bin/pgrep")- process.arguments = ["-x", name]- process.standardOutput = FileHandle.nullDevice- process.standardError = FileHandle.nullDevice- do { try process.run() }- catch { throw InstallerError.appIsRunning }- process.waitUntilExit()- if process.terminationStatus == 0 { throw InstallerError.appIsRunning }+ let count = proc_listallpids(nil, 0)+ guard count > 0 else { throw InstallerError.appIsRunning }+ var pids = [pid_t](repeating: 0, count: Int(count) + 64)+ let filled = proc_listallpids(&pids, Int32(pids.count * MemoryLayout<pid_t>.size))+ guard filled > 0 else { throw InstallerError.appIsRunning }+ var path = [CChar](repeating: 0, count: Int(MAXPATHLEN) * 4)+ for pid in pids.prefix(Int(filled)) where pid > 0 {+ guard proc_pidpath(pid, &path, UInt32(path.count)) > 0 else { continue }+ let executable = String(cString: path)+ if blocksInstall(executable) {+ InstallerLog.write("Blocked by running process \(pid): \(executable)")+ throw InstallerError.appIsRunning+ }+ }+ }++ // Sparkle names every app's updater Autoupdate, so only Bridgeplay's own copy counts.+ static func blocksInstall(_ executable: String) -> Bool {+ switch (executable as NSString).lastPathComponent {+ case "BridgeplayApp": true+ case "Autoupdate": executable.contains("/\(applicationName)/") || executable.contains("/\(bundleIdentifier)/")+ default: false}}diff --git a/app/Tests/BridgeplayInstallerCoreTests/InstallerServiceTests.swift b/app/Tests/BridgeplayInstallerCoreTests/InstallerServiceTests.swiftindex c531fd7..fab4e02 100644--- a/app/Tests/BridgeplayInstallerCoreTests/InstallerServiceTests.swift+++ b/app/Tests/BridgeplayInstallerCoreTests/InstallerServiceTests.swift@@ -506,4 +506,16 @@ struct InstallerServiceTests {#expect(try Data(contentsOf: destination.appending(path: "Contents/Info.plist")) == oldInfo)#expect(!FileManager.default.fileExists(atPath: parent.appending(path: ".Bridgeplay.app.previous").path))}++ @Test func onlyBridgeplaysOwnUpdaterBlocksInstall() {+ #expect(InstallerService.blocksInstall("/Applications/Bridgeplay.app/Contents/MacOS/BridgeplayApp"))+ #expect(InstallerService.blocksInstall("/Applications/Bridgeplay.app/Contents/Frameworks/Sparkle.framework/Versions/B/Autoupdate"))+ #expect(InstallerService.blocksInstall("/Users/a/Library/Caches/com.bridgeplay.Bridgeplay/org.sparkle-project.Sparkle/Launcher/X/Autoupdate"))+ #expect(!InstallerService.blocksInstall("/Applications/OpenUsage.app/Contents/Frameworks/Sparkle.framework/Versions/B/Autoupdate"))+ #expect(!InstallerService.blocksInstall("/Applications/Bridgeplay.app/Contents/MacOS/Helper"))+ }++ @Test func logRedactsHomeDirectory() {+ #expect(InstallerLog.redact(NSHomeDirectory() + "/Downloads/x") == "~/Downloads/x")+ }}