Run Windows games on Apple Silicon — free, open source, with an open compatibility database that tells you what actually works.
Swift apple-silicond3dmetaldxmtdxvkfexgame-porting-toolkitgamingmac-gamingmacosmetalmoltenvksteamswiftuivkd3d-protonwhisky-alternativewindows-gameswinewine-wrapper
Bottles: the deep-tree walk must survive the app's file limit, not the shell's (#38)
Gauthier Piarrette9c258c4ad1dc
diff --git a/Sources/HighballKit/BottleStore.swift b/Sources/HighballKit/BottleStore.swiftindex eb0a930..e343c65 100644--- a/Sources/HighballKit/BottleStore.swift+++ b/Sources/HighballKit/BottleStore.swift@@ -8,6 +8,10 @@ public struct BottleStore: Sendable {guard FileManager.default.fileExists(atPath: paths.bottles.path) else { return [] }return try FileManager.default.contentsOfDirectory(at: paths.bottles, includingPropertiesForKeys: nil).compactMap { url -> Bottle? in+ // Real directories only. Listing used to reconcile the name of a symlinked entry+ // by writing bottle.json through the link, mutating a file outside bottles/.+ var st = stat()+ guard lstat(url.path, &st) == 0, (st.st_mode & S_IFMT) == S_IFDIR else { return nil }guard var b = try? Bottle.load(url) else { return nil }// The folder is the bottle's identity (get/delete resolve by folder). A copied// folder keeps the old internal name, which crashed the app and confused lookups@@ -22,7 +26,9 @@ public struct BottleStore: Sendable {}public func get(_ name: String) throws -> Bottle {- try Bottle.load(paths.bottle(name))+ // Every subcommand resolves through here, so the name guard belongs here too rather than+ // only on the destructive calls.+ try Bottle.load(try Self.bottleURL(name, in: paths))}/// Returns a human-readable problem with a bottle name, or nil if it is usable.@@ -198,9 +204,18 @@ public struct BottleStore: Sendable {// over trees removeItem cannot touch (0555 and 0000 directories, uchg flags, deny-delete// ACLs). A poisoned root node is the one case it can't, so repair that node first.var st = stat()- if lstat(url.path, &st) == 0 {+ if lstat(url.path, &st) == 0, (st.st_mode & S_IFMT) == S_IFDIR {+ // Only a real directory is repaired, and only through lchflags/fchmodat, which do not+ // follow links: chmod(2) here would have rewritten the mode of whatever a symlinked+ // bottle entry pointed at, outside bottles/ entirely.if st.st_flags != 0 { _ = lchflags(url.path, 0) }- if (st.st_mode & 0o700) != 0o700 { _ = chmod(url.path, st.st_mode | 0o700) }+ if (st.st_mode & 0o700) != 0o700 {+ let parent = open(paths.bottles.path, O_RDONLY | O_DIRECTORY | O_CLOEXEC)+ if parent >= 0 {+ _ = fchmodat(parent, url.lastPathComponent, st.st_mode | 0o700, AT_SYMLINK_NOFOLLOW)+ close(parent)+ }+ }}let target = paths.trash.appending(path: "\(Self.trashName(name))-\(UUID().uuidString)",directoryHint: .isDirectory)diff --git a/Sources/HighballKit/Purge.swift b/Sources/HighballKit/Purge.swiftindex 5cbd064..991804e 100644--- a/Sources/HighballKit/Purge.swift+++ b/Sources/HighballKit/Purge.swift@@ -60,6 +60,12 @@ public enum Purge {// Healthy trees, which is nearly all of them, take the cheap path.if (try? FileManager.default.removeItem(at: url)) != nil { return [] }+ // The walk below holds one descriptor per directory level, so its ceiling is the file+ // limit rather than the stack. That matters because the shipping app is not the shell:+ // a GUI-launched .app gets a soft RLIMIT_NOFILE of 256, so a prefix a few hundred levels+ // deep stranded itself in .trash and blamed "Directory not empty".+ raiseFileLimit()+var failures: [PurgeFailure] = []let parentPath = url.deletingLastPathComponent().pathlet name = url.lastPathComponent@@ -70,6 +76,17 @@ public enum Purge {remove(in: parent, named: name, at: url.path, &failures)close(parent)+ // Last resort for whatever the walk could not finish — a depth past even the raised+ // file limit, most realistically. rm walks with fts, which recycles descriptors and does+ // not follow symlinks, so it is safe to point at a tree already isolated inside .trash.+ if !failures.isEmpty {+ var probe = stat()+ if lstat(url.path, &probe) == 0 {+ _ = run("/bin/rm", ["-rf", url.path])+ if lstat(url.path, &probe) != 0 { return [] }+ }+ }+// Never report an all-clear we have not verified. A silently stranded prefix is how the// original bug hid: the caller believed the delete had finished.var st = stat()@@ -215,6 +232,20 @@ public enum Purge {return names}+ /// Lifts the soft file limit toward the hard one. A GUI-launched app starts at 256, which is+ /// far below what a deep prefix needs, and the limit is per-process so raising it here is+ /// enough for the walk.+ private static func raiseFileLimit() {+ var lim = rlimit()+ guard getrlimit(RLIMIT_NOFILE, &lim) == 0 else { return }+ // macOS refuses an unlimited value here, so aim at a large concrete one; when the hard+ // limit is effectively unlimited this simply lands on `want`.+ let target = min(rlim_t(65536), lim.rlim_max)+ guard lim.rlim_cur < target else { return }+ lim.rlim_cur = target+ _ = setrlimit(RLIMIT_NOFILE, &lim)+ }+private static func run(_ tool: String, _ args: [String]) -> Int32 {let p = Process()p.executableURL = URL(fileURLWithPath: tool)diff --git a/Tests/HighballKitTests/BottleDeleteTests.swift b/Tests/HighballKitTests/BottleDeleteTests.swiftindex 920a4d7..6ed9025 100644--- a/Tests/HighballKitTests/BottleDeleteTests.swift+++ b/Tests/HighballKitTests/BottleDeleteTests.swift@@ -213,6 +213,36 @@ final class BottleDeleteTests: XCTestCase {try assertFullyGone("play", try store.delete("play"))}+ /// The walk holds one descriptor per level, so its ceiling is RLIMIT_NOFILE, not the stack —+ /// and the shipping app is not the shell. A GUI-launched .app starts with a soft limit of 256+ /// while a login shell has over a million, so a deep prefix purged fine in testing and+ /// stranded itself permanently for a real user, blaming "Directory not empty".+ func testDeleteHandlesADeepTreeUnderTheAppsFileLimit() throws {+ var original = rlimit()+ XCTAssertEqual(getrlimit(RLIMIT_NOFILE, &original), 0)+ defer { var restore = original; _ = setrlimit(RLIMIT_NOFILE, &restore) }+ var tight = original+ tight.rlim_cur = 256+ try XCTSkipUnless(setrlimit(RLIMIT_NOFILE, &tight) == 0, "cannot lower RLIMIT_NOFILE here")++ let url = try makeBottle("play")+ let fm = FileManager.default+ let start = fm.currentDirectoryPath+ defer { fm.changeCurrentDirectoryPath(start) }+ let deep = url.appending(path: "drive_c/deep")+ try fm.createDirectory(at: deep, withIntermediateDirectories: true)+ XCTAssertTrue(fm.changeCurrentDirectoryPath(deep.path))+ let segment = String(repeating: "d", count: 20)+ for _ in 0..<500 {+ guard (try? fm.createDirectory(atPath: segment, withIntermediateDirectories: false)) != nil,+ fm.changeCurrentDirectoryPath(segment) else { break }+ }+ fm.createFile(atPath: "leaf.txt", contents: Data("x".utf8))+ fm.changeCurrentDirectoryPath(start)++ try assertFullyGone("play", try store.delete("play"))+ }+func testPurgeRefusesShallowPaths() {for dangerous in ["/", "/Users", "/Users/someone", "/Volumes"] {let failures = Purge.tree(at: URL(fileURLWithPath: dangerous))