Skip to content

Free fork of NotProton: Windows Steam games in Mac Steam, on CodeWeavers' Wine and Apple's Game Porting Toolkit

Keep steam.cfg intact when it isn't UTF-8 or can't be read

xmb965063450338b
diff --git a/app/Sources/NotProtonApp/Model/UpdateBlock.swift b/app/Sources/NotProtonApp/Model/UpdateBlock.swift
index fe412b5..de98af6 100644
--- a/app/Sources/NotProtonApp/Model/UpdateBlock.swift
+++ b/app/Sources/NotProtonApp/Model/UpdateBlock.swift
@@ -14,60 +14,80 @@ enum UpdateBlock {
}
static func isPresent(at urls: [URL] = paths) -> Bool {
- guard let required = urls.first else { return false }
- guard let text = try? String(contentsOf: required, encoding: .utf8) else { return false }
+ guard let required = urls.first, let text = try? read(required) else { return false }
return setting(in: text) == "enable"
}
+ // Latin-1 maps every byte, so a file that is not UTF-8 is rewritten unchanged.
+ private static func read(_ url: URL) throws -> String? {
+ guard FileManager.default.fileExists(atPath: url.path(percentEncoded: false)) else { return nil }
+ do {
+ let bytes = try Data(contentsOf: url)
+ return String(String.UnicodeScalarView(bytes.map { Unicode.Scalar($0) }))
+ } catch let error as NSError where error.code == NSFileReadNoPermissionError {
+ throw WriteRefused(path: url.path(percentEncoded: false))
+ }
+ }
+
+ private static func encoded(_ text: String) -> Data {
+ Data(text.unicodeScalars.map { UInt8($0.value) })
+ }
+
+ // Split on the scalar: "\r\n" is one Character, so a CRLF file would read as one line.
+ private static func lines(of text: String) -> [String] {
+ text.unicodeScalars.split(separator: "\n", omittingEmptySubsequences: false)
+ .map { String(String.UnicodeScalarView($0)) }
+ }
+
private static func setting(in text: String) -> String? {
- text.split(separator: "\n", omittingEmptySubsequences: false).compactMap(value(of:)).last
+ lines(of: text).compactMap(value(of:)).last
}
- private static func value(of line: Substring) -> String? {
+ private static func value(of line: String) -> String? {
let parts = line.split(separator: "=", maxSplits: 1, omittingEmptySubsequences: false)
guard parts.count == 2, parts[0].trimmingCharacters(in: .whitespaces) == key else { return nil }
- return parts[1].trimmingCharacters(in: .whitespaces)
+ return parts[1].trimmingCharacters(in: .whitespacesAndNewlines)
}
private static func blocked(_ text: String) -> String {
+ let rows = lines(of: text)
+ func line(endingLike row: String?) -> String {
+ contents.trimmingCharacters(in: .newlines) + (row?.unicodeScalars.last == "\r" ? "\r" : "")
+ }
var seen = false
var out: [String] = []
- for line in text.split(separator: "\n", omittingEmptySubsequences: false) {
- guard value(of: line) != nil else {
- out.append(String(line))
+ for existing in rows {
+ guard value(of: existing) != nil else {
+ out.append(existing)
continue
}
if !seen {
- out.append(contents.trimmingCharacters(in: .newlines))
+ out.append(line(endingLike: existing))
seen = true
}
}
guard !seen else { return out.joined(separator: "\n") }
if out.last?.isEmpty == true { out.removeLast() }
- out.append(contents.trimmingCharacters(in: .newlines))
+ out.append(line(endingLike: rows.first))
out.append("")
return out.joined(separator: "\n")
}
private static func unblocked(_ text: String) -> String {
- let out = text.split(separator: "\n", omittingEmptySubsequences: false)
- .filter { value(of: $0) == nil }
- return out.joined(separator: "\n")
+ lines(of: text).filter { value(of: $0) == nil }.joined(separator: "\n")
}
@discardableResult
static func write(to urls: [URL] = paths) throws -> [String] {
guard let required = urls.first else { return [] }
- func existing(at url: URL) -> String {
- (try? String(contentsOf: url, encoding: .utf8)) ?? ""
- }
-
do {
try FileManager.default.createDirectory(
at: required.deletingLastPathComponent(), withIntermediateDirectories: true
)
- try Data(blocked(existing(at: required)).utf8).write(to: required)
+ try encoded(blocked(read(required) ?? "")).write(to: required)
+ } catch let refusal as WriteRefused {
+ throw refusal
} catch let error as NSError where error.code == NSFileWriteNoPermissionError {
throw WriteRefused(path: required.path(percentEncoded: false))
} catch {
@@ -82,7 +102,8 @@ enum UpdateBlock {
for url in urls.dropFirst() {
let parent = url.deletingLastPathComponent()
guard FileManager.default.fileExists(atPath: parent.path(percentEncoded: false)) else { continue }
- guard (try? Data(blocked(existing(at: url)).utf8).write(to: url)) != nil else { continue }
+ guard let text = try? read(url) ?? "",
+ (try? encoded(blocked(text)).write(to: url)) != nil else { continue }
written.append(url.path(percentEncoded: false))
}
return written
@@ -94,15 +115,14 @@ enum UpdateBlock {
var removed: [String] = []
for url in urls {
let path = url.path(percentEncoded: false)
- guard files.fileExists(atPath: path) else { continue }
- let text = try? String(contentsOf: url, encoding: .utf8)
- let kept = text.map(unblocked) ?? ""
- guard text == nil || kept != text else { continue }
+ guard let text = try read(url) else { continue }
+ let kept = unblocked(text)
+ guard kept != text else { continue }
try WriteRefused.catching(path) {
if kept.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
try files.removeItem(at: url)
} else {
- try Data(kept.utf8).write(to: url)
+ try encoded(kept).write(to: url)
}
}
removed.append(path)
diff --git a/app/Tests/NotProtonAppTests/UpdateBlockTests.swift b/app/Tests/NotProtonAppTests/UpdateBlockTests.swift
index 3d030e4..8f2169b 100644
--- a/app/Tests/NotProtonAppTests/UpdateBlockTests.swift
+++ b/app/Tests/NotProtonAppTests/UpdateBlockTests.swift
@@ -171,6 +171,106 @@ struct UpdateBlockTests {
#expect(try String(contentsOf: cfg, encoding: .utf8) == "Other=2\n")
}
+ // Read as UTF-8, this file used to come back empty, so removing the block deleted it.
+ @Test("Removing the block keeps the bytes of a file that is not UTF-8")
+ func removalKeepsAFileThatIsNotUTF8() throws {
+ let work = try scratchDirectory("cfg")
+ defer { try? FileManager.default.removeItem(at: work) }
+ let cfg = work.appending(path: "steam.cfg")
+ let other = Data("Name=caf".utf8) + Data([0xE9, 0x0A])
+ try (Data("BootStrapperInhibitUpdateOnLaunch=enable\n".utf8) + other).write(to: cfg)
+
+ #expect(try UpdateBlock.remove(from: [cfg]) == [cfg.path(percentEncoded: false)])
+ #expect(try Data(contentsOf: cfg) == other)
+ }
+
+ @Test("Writing the block keeps the bytes of a file that is not UTF-8")
+ func writingKeepsAFileThatIsNotUTF8() throws {
+ let work = try scratchDirectory("cfg")
+ defer { try? FileManager.default.removeItem(at: work) }
+ let cfg = work.appending(path: "steam.cfg")
+ let other = Data("Name=caf".utf8) + Data([0xE9, 0x0A])
+ try other.write(to: cfg)
+
+ try UpdateBlock.write(to: [cfg])
+
+ let block = Data("BootStrapperInhibitUpdateOnLaunch=enable\n".utf8)
+ #expect(try Data(contentsOf: cfg) == other + block)
+ #expect(UpdateBlock.isPresent(at: [cfg]))
+ }
+
+ // "\r\n" is one Character, so this file used to read as one line and removal deleted it.
+ @Test("Removing the block from a CRLF file keeps the other lines")
+ func removalKeepsTheRestOfACRLFFile() throws {
+ let work = try scratchDirectory("cfg")
+ defer { try? FileManager.default.removeItem(at: work) }
+ let cfg = work.appending(path: "steam.cfg")
+ try Data("BootStrapperInhibitUpdateOnLaunch=enable\r\nOther=2\r\n".utf8).write(to: cfg)
+
+ #expect(UpdateBlock.isPresent(at: [cfg]))
+ #expect(try UpdateBlock.remove(from: [cfg]) == [cfg.path(percentEncoded: false)])
+ #expect(try String(contentsOf: cfg, encoding: .utf8) == "Other=2\r\n")
+ }
+
+ @Test("Writing the block into a CRLF file keeps its line endings")
+ func writingKeepsCRLFLineEndings() throws {
+ let work = try scratchDirectory("cfg")
+ defer { try? FileManager.default.removeItem(at: work) }
+ let cfg = work.appending(path: "steam.cfg")
+ try Data("Other=2\r\n".utf8).write(to: cfg)
+
+ try UpdateBlock.write(to: [cfg])
+
+ let text = try String(contentsOf: cfg, encoding: .utf8)
+ #expect(text == "Other=2\r\nBootStrapperInhibitUpdateOnLaunch=enable\r\n")
+ #expect(UpdateBlock.isPresent(at: [cfg]))
+ }
+
+ @Test("A required copy that cannot be read is refused and left as it is")
+ func anUnreadableRequiredCopyIsRefused() throws {
+ let work = try scratchDirectory("cfg")
+ defer { try? FileManager.default.removeItem(at: work) }
+ let cfg = work.appending(path: "steam.cfg")
+ let text = "BootStrapperInhibitUpdateOnLaunch=enable\nOther=2\n"
+ try unreadable(cfg, holding: text)
+ defer { try? readable(cfg) }
+
+ #expect(throws: WriteRefused.self) { try UpdateBlock.remove(from: [cfg]) }
+ #expect(throws: WriteRefused.self) { try UpdateBlock.write(to: [cfg]) }
+
+ try readable(cfg)
+ #expect(try String(contentsOf: cfg, encoding: .utf8) == text)
+ }
+
+ @Test("A best-effort copy that cannot be read is skipped and left as it is")
+ func anUnreadableBestEffortCopyIsSkipped() throws {
+ let work = try scratchDirectory("cfg")
+ defer { try? FileManager.default.removeItem(at: work) }
+ let required = work.appending(path: "steam.cfg")
+ let inner = work.appending(path: "inner.cfg")
+ let text = "Other=2\n"
+ try unreadable(inner, holding: text)
+ defer { try? readable(inner) }
+
+ #expect(try UpdateBlock.write(to: [required, inner]) == [required.path(percentEncoded: false)])
+
+ try readable(inner)
+ #expect(try String(contentsOf: inner, encoding: .utf8) == text)
+ }
+
+ private func unreadable(_ url: URL, holding text: String) throws {
+ try Data(text.utf8).write(to: url)
+ try FileManager.default.setAttributes(
+ [.posixPermissions: 0o000], ofItemAtPath: url.path(percentEncoded: false)
+ )
+ }
+
+ private func readable(_ url: URL) throws {
+ try FileManager.default.setAttributes(
+ [.posixPermissions: 0o644], ofItemAtPath: url.path(percentEncoded: false)
+ )
+ }
+
@Test("The shipped paths are the two the client actually reads")
func shippedPathsAreTheClientPaths() {
let paths = UpdateBlock.paths.map { $0.path(percentEncoded: false) }