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(install): tell root-owned Steam apart from another account (#391)
Raystorm4bfeafeabd8e
diff --git a/app/Sources/BridgeplayApp/Model/SteamInstaller.swift b/app/Sources/BridgeplayApp/Model/SteamInstaller.swiftindex 6b03ffa..919279d 100644--- a/app/Sources/BridgeplayApp/Model/SteamInstaller.swift+++ b/app/Sources/BridgeplayApp/Model/SteamInstaller.swift@@ -321,15 +321,22 @@ enum SteamInstaller {do {try assertBundleIsWritable(app)- } catch let refusal as WriteRefused where refusal.remedy == .otherAccount {- throw StepFailure(- step: step,- detail: deployed ? Self.mismatchAcrossAccounts : Self.unpatchedAcrossAccounts- )+ } catch let refusal as WriteRefused {+ guard let detail = refusalDetail(refusal.remedy, deployed: deployed) else { throw refusal }+ throw StepFailure(step: step, detail: detail)}return true}+ // What to say when Steam.app can't be written for a reason this account can't fix.+ static func refusalDetail(_ remedy: Remedy, deployed: Bool) -> String? {+ switch remedy {+ case .otherAccount: deployed ? mismatchAcrossAccounts : unpatchedAcrossAccounts+ case .rootOwned: remedy.advice+ case .appManagement, .ownership: nil+ }+ }+private static let mismatchAcrossAccounts =String(localized: "Steam has a different copy of Bridgeplay, installed by another account on this Mac. Update Bridgeplay from that account, then try again.")diff --git a/app/Sources/BridgeplayApp/Resources/Localization/Localizable.xcstrings b/app/Sources/BridgeplayApp/Resources/Localization/Localizable.xcstringsindex 91fa17b..fae8b43 100644--- a/app/Sources/BridgeplayApp/Resources/Localization/Localizable.xcstrings+++ b/app/Sources/BridgeplayApp/Resources/Localization/Localizable.xcstrings@@ -81,6 +81,22 @@}}},+ "%@ belongs to the system (root), not to your account, so Bridgeplay cannot change it. An administrator has to make your account its owner, for example by running this in Terminal: %@" : {+ "localizations" : {+ "it" : {+ "stringUnit" : {+ "state" : "translated",+ "value" : "%@ appartiene al sistema (root), non al tuo account, quindi Bridgeplay non può modificarlo. Un amministratore deve rendere il tuo account proprietario, per esempio eseguendo questo comando nel Terminale: %@"+ }+ },+ "zh-Hans" : {+ "stringUnit" : {+ "state" : "translated",+ "value" : "%@ 属于系统(root),而不是你的账户,因此 Bridgeplay 无法修改它。需要由管理员将其所有者改为你的账户,例如在“终端”中运行:%@"+ }+ }+ }+ },"%@ carries no entitlements to extend." : {"localizations" : {"it" : {@@ -9669,6 +9685,22 @@}}},+ "This folder belongs to the system (root), not to your account, so Bridgeplay cannot change it. An administrator has to make your account its owner." : {+ "localizations" : {+ "it" : {+ "stringUnit" : {+ "state" : "translated",+ "value" : "Questa cartella appartiene al sistema (root), non al tuo account, quindi Bridgeplay non può modificarla. Un amministratore deve rendere il tuo account proprietario."+ }+ },+ "zh-Hans" : {+ "stringUnit" : {+ "state" : "translated",+ "value" : "此文件夹属于系统(root),而不是你的账户,因此 Bridgeplay 无法修改它。需要由管理员将其所有者改为你的账户。"+ }+ }+ }+ },"This tool is intended to repair a prefix after switching between Rosetta/FEX CrossOver." : {"localizations" : {"it" : {diff --git a/app/Sources/BridgeplayApp/Support/RemoveEverythingCommand.swift b/app/Sources/BridgeplayApp/Support/RemoveEverythingCommand.swiftindex b6d8ed0..9075a4b 100644--- a/app/Sources/BridgeplayApp/Support/RemoveEverythingCommand.swift+++ b/app/Sources/BridgeplayApp/Support/RemoveEverythingCommand.swift@@ -41,6 +41,10 @@ enum RemoveEverythingCommand {"Allow your terminal app in System Settings > Privacy & Security > App Management, then run it again."case .otherAccount:"Steam was installed by another account on this Mac. Run this again while logged in to that account."+ case .rootOwned(let bundle?):+ "\(bundle) belongs to root. Make your account its owner with: \(Remedy.chownCommand(bundle))"+ case .rootOwned(nil):+ "The folder belongs to root. An administrator has to make your account its owner."case .ownership:"Check permissions, make sure your user owns the folder."}diff --git a/app/Sources/BridgeplayApp/Support/WriteRefused.swift b/app/Sources/BridgeplayApp/Support/WriteRefused.swiftindex 63c70bb..ce8a8dc 100644--- a/app/Sources/BridgeplayApp/Support/WriteRefused.swift+++ b/app/Sources/BridgeplayApp/Support/WriteRefused.swift@@ -30,6 +30,9 @@ extension WriteRefused {enum Remedy: Hashable {case appManagementcase otherAccount+ // Root owns it (MDM, `sudo cp`). Only a bundle gets a chown command: a root folder+ // outside one may be /Applications itself.+ case rootOwned(bundle: String?)case ownershipvar advice: String {@@ -40,10 +43,20 @@ enum Remedy: Hashable {String(localized: "Bridgeplay needs the App Management permission. Turn on Bridgeplay in System Settings › Privacy & Security › App Management. After a Bridgeplay update the switch can look on but no longer work: turn it off and on again.")case .otherAccount:String(localized: "Steam was installed by another account on this Mac. Only that account can modify it. Log in to macOS with that account to reinstall Bridgeplay.")+ case .rootOwned(let bundle?):+ String(localized: "\(bundle) belongs to the system (root), not to your account, so Bridgeplay cannot change it. An administrator has to make your account its owner, for example by running this in Terminal: \(Self.chownCommand(bundle))")+ case .rootOwned(nil):+ String(localized: "This folder belongs to the system (root), not to your account, so Bridgeplay cannot change it. An administrator has to make your account its owner.")case .ownership: String(localized: "Check permissions, make sure your user owns the folder.")}}+ // Names this account rather than $(whoami): the administrator may run it from their own.+ static func chownCommand(_ path: String, user: String = NSUserName()) -> String {+ func quoted(_ word: String) -> String { "'" + word.replacingOccurrences(of: "'", with: "'\\''") + "'" }+ return "sudo chown -R \(quoted(user)) \(quoted(path))"+ }+static let settingsButton = String(localized: "Open System Settings")private static let appManagementPane =@@ -52,7 +65,7 @@ enum Remedy: Hashable {var settingsPane: URL? {switch self {case .appManagement: URL(string: Self.appManagementPane)- case .otherAccount, .ownership: nil+ case .otherAccount, .rootOwned, .ownership: nil}}@@ -77,23 +90,32 @@ enum Remedy: Hashable {return nil}- init(forPath path: String) {- if let owner = Self.owner(of: path), owner != getuid() {- self = .otherAccount- return- }-+ static func enclosingBundle(of path: String) -> String? {let files = FileManager.defaultvar directory = URL(filePath: path)while directory.pathComponents.count > 1 {let plist = directory.appending(path: "Contents/Info.plist")if files.fileExists(atPath: plist.path(percentEncoded: false)) {- self = .appManagement- return+ var bundle = directory.path(percentEncoded: false)+ if bundle.count > 1, bundle.hasSuffix("/") { bundle.removeLast() }+ return bundle}directory = directory.deletingLastPathComponent()}- self = .ownership+ return nil+ }++ init(+ forPath path: String,+ owner: (String) -> uid_t? = Remedy.owner(of:),+ user: uid_t = getuid()+ ) {+ let bundle = Self.enclosingBundle(of: path)+ switch owner(path) {+ case 0? where user != 0: self = .rootOwned(bundle: bundle)+ case let other? where other != user: self = .otherAccount+ default: self = bundle == nil ? .ownership : .appManagement+ }}}diff --git a/app/Tests/BridgeplayAppTests/RemedyOwnerTests.swift b/app/Tests/BridgeplayAppTests/RemedyOwnerTests.swiftnew file mode 100644index 0000000..12d4ace--- /dev/null+++ b/app/Tests/BridgeplayAppTests/RemedyOwnerTests.swift@@ -0,0 +1,123 @@+import Foundation+import Testing++@testable import BridgeplayApp++// #391: a Steam.app root owns (MDM, `sudo cp`) was blamed on another account on this Mac.+@Suite("Who owns a refused path")+struct RemedyOwnerTests {++ private static let user: uid_t = 501++ private func steamBundle() throws -> (root: URL, app: URL, inside: String) {+ let root = URL.temporaryDirectory.appending(path: "np-owner-\(UUID().uuidString)")+ let app = root.appending(path: "Steam.app")+ let inside = app.appending(path: "Contents/MacOS")+ try FileManager.default.createDirectory(at: inside, withIntermediateDirectories: true)+ try Data("{}".utf8).write(to: app.appending(path: "Contents/Info.plist"))+ return (root, app, inside.path(percentEncoded: false))+ }++ @Test("A bundle root owns is root's, with the bundle to hand over")+ func rootOwnedBundle() throws {+ let steam = try steamBundle()+ defer { try? FileManager.default.removeItem(at: steam.root) }++ let remedy = Remedy(forPath: steam.inside, owner: { _ in 0 }, user: Self.user)++ #expect(remedy == .rootOwned(bundle: steam.app.path(percentEncoded: false)))+ #expect(remedy.settingsPane == nil)+ }++ @Test("A root folder outside a bundle gets no chown command")+ func rootOwnedFolder() {+ let remedy = Remedy(forPath: "/nonexistent/folder", owner: { _ in 0 }, user: Self.user)++ #expect(remedy == .rootOwned(bundle: nil))+ #expect(!remedy.advice.contains("chown"))+ }++ @Test("A bundle another user owns is still another account's")+ func otherUser() throws {+ let steam = try steamBundle()+ defer { try? FileManager.default.removeItem(at: steam.root) }++ #expect(Remedy(forPath: steam.inside, owner: { _ in 502 }, user: Self.user) == .otherAccount)+ }++ @Test("Root running the app owns what root owns")+ func rootIsItsOwnAccount() throws {+ let steam = try steamBundle()+ defer { try? FileManager.default.removeItem(at: steam.root) }++ #expect(Remedy(forPath: steam.inside, owner: { _ in 0 }, user: 0) == .appManagement)+ }++ @Test("An own folder inside a bundle asks for App Management")+ func ownBundle() throws {+ let steam = try steamBundle()+ defer { try? FileManager.default.removeItem(at: steam.root) }++ #expect(Remedy(forPath: steam.inside) == .appManagement)+ #expect(Remedy(forPath: steam.inside, owner: { _ in Self.user }, user: Self.user) == .appManagement)+ }++ @Test("An own folder outside a bundle asks to check ownership")+ func ownFolder() throws {+ let root = URL.temporaryDirectory.appending(path: "np-owner-\(UUID().uuidString)")+ try FileManager.default.createDirectory(at: root, withIntermediateDirectories: true)+ defer { try? FileManager.default.removeItem(at: root) }++ #expect(Remedy(forPath: root.path(percentEncoded: false)) == .ownership)+ }++ @Test("The advice names the bundle and a command that hands it over")+ func adviceCarriesTheCommand() {+ let advice = Remedy.rootOwned(bundle: "/Applications/Steam.app").advice++ #expect(advice.contains("/Applications/Steam.app belongs to the system (root)"))+ #expect(advice.contains("sudo chown -R '\(NSUserName())' '/Applications/Steam.app'"))+ #expect(!advice.contains("another account"))+ }++ @Test("The command quotes paths with spaces and apostrophes")+ func commandQuotesThePath() {+ #expect(+ Remedy.chownCommand("/Volumes/Ray's Drive/Steam.app", user: "ray")+ == #"sudo chown -R 'ray' '/Volumes/Ray'\''s Drive/Steam.app'"#+ )+ }++ @Test("Installing into a root-owned Steam says root owns it, not another account")+ func installerNamesRoot() throws {+ let root = Remedy.rootOwned(bundle: "/Applications/Steam.app")++ for deployed in [false, true] {+ let detail = try #require(SteamInstaller.refusalDetail(root, deployed: deployed))+ #expect(detail.contains("(root)"))+ #expect(detail.contains("sudo chown -R"))+ #expect(!detail.contains("another account"))+ #expect(!detail.contains("account that owns Steam"))+ }+ }++ @Test("Installing into another account's Steam keeps its own messages")+ func installerNamesOtherAccount() throws {+ let unpatched = try #require(SteamInstaller.refusalDetail(.otherAccount, deployed: false))+ let mismatch = try #require(SteamInstaller.refusalDetail(.otherAccount, deployed: true))++ #expect(unpatched.contains("Install from the account that owns Steam"))+ #expect(mismatch.contains("installed by another account"))+ #expect(SteamInstaller.refusalDetail(.appManagement, deployed: false) == nil)+ #expect(SteamInstaller.refusalDetail(.ownership, deployed: true) == nil)+ }++ @Test("The command line says root owns it")+ func commandLineNamesRoot() {+ let refusal = WriteRefused(path: "/usr/bin")+ let message = RemoveEverythingCommand.failureMessage(refusal)++ #expect(message.contains("belongs to root"))+ #expect(!message.contains("another account"))+ }+}diff --git a/app/Tests/BridgeplayAppTests/SystemStatusTests.swift b/app/Tests/BridgeplayAppTests/SystemStatusTests.swiftindex 912c80e..37c1e6a 100644--- a/app/Tests/BridgeplayAppTests/SystemStatusTests.swift+++ b/app/Tests/BridgeplayAppTests/SystemStatusTests.swift@@ -290,9 +290,9 @@ struct FailureReportTests {)}- @Test("A path owned by another account is not blamed on App Management")- func otherAccountBeatsAppManagement() throws {- #expect(Remedy(forPath: "/usr/bin") == .otherAccount)+ @Test("A path root owns is not blamed on App Management")+ func rootOwnedBeatsAppManagement() throws {+ #expect(Remedy(forPath: "/usr/bin") == .rootOwned(bundle: nil))#expect(Remedy(forPath: "/usr/bin").settingsPane == nil)}