Skip to content

armgit / peel

Public Imported from GitHub
0

Uninstall Mac apps with every file they leave behind, and see why each belongs. Free up space, fine-tune macOS, and set up Terminal.

The helper takes as many items a request as it can hold open

Țugui Dragoș9585048a15c5
diff --git a/Localization/Peel/Localizable.xcstrings b/Localization/Peel/Localizable.xcstrings
index 8a5c2724..040c9335 100644
--- a/Localization/Peel/Localizable.xcstrings
+++ b/Localization/Peel/Localizable.xcstrings
@@ -94381,6 +94381,112 @@
}
}
},
+ "Peel’s helper had too many folders open at once. Try again." : {
+ "localizations" : {
+ "cs" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Pomocník Peelu měl otevřeno příliš mnoho složek najednou. Zkuste to znovu."
+ }
+ },
+ "de" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Das Hilfsprogramm von Peel hatte zu viele Ordner gleichzeitig geöffnet. Versuche es erneut."
+ }
+ },
+ "es" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "El asistente de Peel tenía demasiadas carpetas abiertas a la vez. Vuelve a intentarlo."
+ }
+ },
+ "fr" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "L’utilitaire de Peel avait trop de dossiers ouverts à la fois. Réessayez."
+ }
+ },
+ "it" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "L’assistente di Peel aveva troppe cartelle aperte contemporaneamente. Riprova."
+ }
+ },
+ "ja" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Peelのヘルパーが同時に開いているフォルダが多すぎました。もう一度試してください。"
+ }
+ },
+ "ko" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Peel 보조 프로그램이 한 번에 너무 많은 폴더를 열어 두었습니다. 다시 시도하십시오."
+ }
+ },
+ "nl" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Het hulpprogramma van Peel had te veel mappen tegelijk open. Probeer het opnieuw."
+ }
+ },
+ "pl" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Pomocnik Peel miał naraz otwartych zbyt wiele folderów. Spróbuj ponownie."
+ }
+ },
+ "pt" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "O auxiliar do Peel estava com pastas demais abertas ao mesmo tempo. Tente novamente."
+ }
+ },
+ "ro" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Instrumentul de ajutor Peel avea prea multe dosare deschise în același timp. Încercați din nou."
+ }
+ },
+ "ru" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "У помощника Peel было открыто слишком много папок одновременно. Попробуйте еще раз."
+ }
+ },
+ "sv" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Peels hjälpprogram hade för många mappar öppna samtidigt. Försök igen."
+ }
+ },
+ "tr" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Peel’in yardımcısı aynı anda çok fazla klasör açmıştı. Yeniden deneyin."
+ }
+ },
+ "uk" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "У помічника Peel було відкрито забагато папок одночасно. Спробуйте ще раз."
+ }
+ },
+ "zh-Hans" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Peel的帮助程序同时打开的文件夹过多。请再试一次。"
+ }
+ },
+ "zh-Hant" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Peel的輔助程式同時打開的檔案夾過多。請再試一次。"
+ }
+ }
+ }
+ },
"Peel’s helper is out of date. Reinstall it in Settings." : {
"localizations" : {
"cs" : {
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/PrivilegedHelper.swift b/Packages/PeelCore/Sources/PeelCore/Removal/PrivilegedHelper.swift
index 99a617a5..097a9395 100644
--- a/Packages/PeelCore/Sources/PeelCore/Removal/PrivilegedHelper.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/PrivilegedHelper.swift
@@ -121,15 +121,14 @@ public enum PrivilegedHelper {
/// How long the helper is waited for before the request counts as unanswered.
private static let longestWait: TimeInterval = 120
- /// The most items one request carries, which limits how many items an answer that never comes can affect.
- private static let itemsInARequest = 100
-
@concurrent
public static func moveToTrash(_ urls: [URL]) async -> TrashResult {
var result = TrashResult()
let trash = URL.homeDirectory.appending(path: ".Trash", directoryHint: .isDirectory)
- for start in stride(from: 0, to: urls.count, by: itemsInARequest) {
- let batch = Array(urls[start..<min(start + itemsInARequest, urls.count)])
+ // The helper's own limit also bounds how many items an answer that never comes can affect.
+ let limit = HelperRequest.maximumItems
+ for start in stride(from: 0, to: urls.count, by: limit) {
+ let batch = Array(urls[start..<min(start + limit, urls.count)])
// Read before the move, so that if the answer is lost, moved items can be found in the Trash by identity.
let links = Dictionary(batch.map { ($0, FileIdentity.Link.of($0)) }, uniquingKeysWith: { first, _ in first })
let paths = batch.map { $0.path(percentEncoded: false) }
@@ -237,8 +236,8 @@ public enum PrivilegedHelper {
}
}
// A helper that is running but never answers keeps the connection valid, so only this timer ends
- // the wait. `longestWait` outlasts any request: at most `itemsInARequest` renames, or one `launchctl`
- // run that the helper stops after 30 seconds.
+ // the wait. `longestWait` outlasts any request: at most `HelperRequest.maximumItems` renames, or one
+ // `launchctl` run that the helper stops after 30 seconds.
DispatchQueue.global(qos: .utility).asyncAfter(deadline: .now() + longestWait) { finish(fallback) }
guard let helper = connection.remoteObjectProxyWithErrorHandler({ _ in finish(fallback) }) as? any PeelHelperProtocol else {
finish(fallback)
diff --git a/Packages/PeelCore/Sources/PeelCore/Support/FixedSentence.swift b/Packages/PeelCore/Sources/PeelCore/Support/FixedSentence.swift
index 2a991cd4..6d892845 100644
--- a/Packages/PeelCore/Sources/PeelCore/Support/FixedSentence.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Support/FixedSentence.swift
@@ -6,7 +6,8 @@ internal import PeelPrivileged
/// the app recognizes it here to show it translated. Anything else (a tool's own output, an error macOS
/// wrote) is not one of these.
public enum FixedSentence: CaseIterable, Sendable {
- case notAFullPath, dotInPath, controlCharacter, folderNotFound, outsideHelpersFolders, onlyAppsThere
+ case notAFullPath, dotInPath, controlCharacter, folderNotFound, tooManyOpenFolders, outsideHelpersFolders
+ case onlyAppsThere
case notThereAnymore, alreadyThere, protectedByMacOS, irreplaceable, codeFolder, onlyLinksThere, leadsSomewhere
case helperOutOfDate, accountNotAllowed, tooManyItems, noTrash, pathTooLong, cannotKeepRecord, cannotReadRecord
case notInTrash, notMovedByHelper, invalidRequest, configurationMissing
@@ -25,6 +26,7 @@ public enum FixedSentence: CaseIterable, Sendable {
case .dotInPath: PrivilegedPathPolicy.Rejection.relativeComponent.explanation
case .controlCharacter: PrivilegedPathPolicy.Rejection.controlCharacter.explanation
case .folderNotFound: PrivilegedPathPolicy.Rejection.unresolvableParent.explanation
+ case .tooManyOpenFolders: PrivilegedPathPolicy.Rejection.tooManyOpenFolders.explanation
case .outsideHelpersFolders: PrivilegedPathPolicy.Rejection.outsideAllowedLocations.explanation
case .onlyAppsThere: PrivilegedPathPolicy.Rejection.notAnApplication.explanation
case .notThereAnymore: PrivilegedPathPolicy.Rejection.missing.explanation
diff --git a/Packages/PeelCore/Sources/PeelPrivileged/DirectoryHandle.swift b/Packages/PeelCore/Sources/PeelPrivileged/DirectoryHandle.swift
index 0cc45bf4..48a48adf 100644
--- a/Packages/PeelCore/Sources/PeelPrivileged/DirectoryHandle.swift
+++ b/Packages/PeelCore/Sources/PeelPrivileged/DirectoryHandle.swift
@@ -23,11 +23,7 @@ public final class DirectoryHandle: Sendable {
/// Opens a path that `realpath` has already resolved, so it holds no links of its own. A component
/// that has become a link since then fails here rather than redirecting the operation.
- static func at(canonical path: String) -> DirectoryHandle? {
- try? open(canonical: path).get()
- }
-
- private static func open(canonical path: String) -> Result<DirectoryHandle, POSIXError> {
+ static func at(canonical path: String) -> Result<DirectoryHandle, POSIXError> {
var descriptor = Darwin.open("/", O_SEARCH | O_CLOEXEC)
guard descriptor >= 0 else { return .failure(.last) }
for component in PathComponents.of(path) {
@@ -44,7 +40,7 @@ public final class DirectoryHandle: Sendable {
/// without following any link.
public static func at(_ path: String) -> Result<DirectoryHandle, POSIXError> {
guard let real = PrivilegedPathPolicy.realPath(path) else { return .failure(.last) }
- return open(canonical: real)
+ return at(canonical: real)
}
/// What the kernel calls this folder now, wherever it has been moved to since it was opened.
diff --git a/Packages/PeelCore/Sources/PeelPrivileged/HelperRequest.swift b/Packages/PeelCore/Sources/PeelPrivileged/HelperRequest.swift
index d58377a3..312626b1 100644
--- a/Packages/PeelCore/Sources/PeelPrivileged/HelperRequest.swift
+++ b/Packages/PeelCore/Sources/PeelPrivileged/HelperRequest.swift
@@ -3,9 +3,11 @@ public import Darwin
/// The checks every request to the helper passes before any work, in the order they are made: what costs nothing
/// comes first, and the account behind the connection is asked last, once the request is known to be sound.
public enum HelperRequest {
- /// Limits on one request, so a client cannot hand the helper an unbounded amount of work. `PATH_MAX`
- /// counts the terminating NUL, so the longest real path is 1,023 bytes.
- public static let maximumItems = 10_000
+ /// Limits on one request, so a client cannot hand the helper an unbounded amount of work. Each item the helper
+ /// accepts keeps its folder open until the move ends, and launchd gives a daemon 256 descriptors
+ /// (`launchctl limit maxfiles`), so the app sends its items in requests of this size. `PATH_MAX` counts the
+ /// terminating NUL, so the longest real path is 1,023 bytes.
+ public static let maximumItems = 100
public static let maximumPathLength = 1_023
public struct Caller: Sendable {
diff --git a/Packages/PeelCore/Sources/PeelPrivileged/PrivilegedPathPolicy.swift b/Packages/PeelCore/Sources/PeelPrivileged/PrivilegedPathPolicy.swift
index 44fb01e5..0a7ef438 100644
--- a/Packages/PeelCore/Sources/PeelPrivileged/PrivilegedPathPolicy.swift
+++ b/Packages/PeelCore/Sources/PeelPrivileged/PrivilegedPathPolicy.swift
@@ -7,6 +7,8 @@ public struct PrivilegedPathPolicy: Sendable {
case relativeComponent
case controlCharacter
case unresolvableParent
+ /// The folder it is in is there, but the helper had no descriptor left to open it with.
+ case tooManyOpenFolders
case outsideAllowedLocations
case notAnApplication
case missing
@@ -24,6 +26,7 @@ public struct PrivilegedPathPolicy: Sendable {
case .relativeComponent: "The path has a . or .. in it."
case .controlCharacter: "The path holds a character no real name has."
case .unresolvableParent: "The folder it’s in couldn’t be found."
+ case .tooManyOpenFolders: "Peel’s helper had too many folders open at once. Try again."
case .outsideAllowedLocations: "It is outside the folders Peel’s helper may touch."
case .notAnApplication: "Peel removes only apps from that folder."
case .missing: "It isn’t there anymore."
@@ -35,6 +38,11 @@ public struct PrivilegedPathPolicy: Sendable {
case .leadsSomewhere: "It still leads to something on this Mac."
}
}
+
+ /// Why a folder would not open: it is gone, or the helper had no descriptor left to open it with.
+ init(openingFolderFailedWith error: POSIXError) {
+ self = [.EMFILE, .ENFILE].contains(error.code) ? .tooManyOpenFolders : .unresolvableParent
+ }
}
public static let systemLocations = ["/Applications"]
@@ -156,7 +164,11 @@ public struct PrivilegedPathPolicy: Sendable {
case .failure(let rejection):
return .failure(rejection)
case .success(let item):
- guard let parent = DirectoryHandle.at(canonical: item.parent) else { return .failure(.unresolvableParent) }
+ let parent: DirectoryHandle
+ switch DirectoryHandle.at(canonical: item.parent) {
+ case .success(let opened): parent = opened
+ case .failure(let error): return .failure(Rejection(openingFolderFailedWith: error))
+ }
var info = stat()
guard item.name.withCString({ fstatat(parent.descriptor, $0, &info, AT_SYMLINK_NOFOLLOW) }) == 0 else {
return .failure(.missing)
@@ -188,7 +200,11 @@ public struct PrivilegedPathPolicy: Sendable {
if !restorable.contains(where: { PathComponents.isPath(item.path, inside: $0) }) {
guard trashed.owner == trustedOwner, let mode = trashed.mode, mode & 0o022 == 0 else { return .failure(.loadsCode) }
}
- guard let parent = DirectoryHandle.at(canonical: item.parent) else { return .failure(.unresolvableParent) }
+ let parent: DirectoryHandle
+ switch DirectoryHandle.at(canonical: item.parent) {
+ case .success(let opened): parent = opened
+ case .failure(let error): return .failure(Rejection(openingFolderFailedWith: error))
+ }
var info = stat()
guard item.name.withCString({ fstatat(parent.descriptor, $0, &info, AT_SYMLINK_NOFOLLOW) }) != 0 else {
return .failure(.alreadyExists)
@@ -202,7 +218,7 @@ public struct PrivilegedPathPolicy: Sendable {
public func openTrash(ownedBy user: uid_t) -> DirectoryHandle? {
guard
let home = Self.realPath(homeDirectory),
- let trash = DirectoryHandle.at(canonical: home)?.child(".Trash"),
+ let trash = (try? DirectoryHandle.at(canonical: home).get())?.child(".Trash"),
trash.ownerIdentifier() == user
else { return nil }
return trash
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/PrivilegedPathPolicyTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/PrivilegedPathPolicyTests.swift
index e20d5c5b..687106e3 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/PrivilegedPathPolicyTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/PrivilegedPathPolicyTests.swift
@@ -1,8 +1,16 @@
import Foundation
-import PeelPrivileged
+@testable import PeelPrivileged
import Testing
struct PrivilegedPathPolicyTests {
+ /// A folder the helper could not open because it had no descriptor left is still there, so it is never
+ /// reported as a folder that could not be found.
+ @Test func aFolderOpenedWithNoDescriptorLeftIsNotCalledMissing() {
+ #expect(PrivilegedPathPolicy.Rejection(openingFolderFailedWith: POSIXError(.EMFILE)) == .tooManyOpenFolders)
+ #expect(PrivilegedPathPolicy.Rejection(openingFolderFailedWith: POSIXError(.ENFILE)) == .tooManyOpenFolders)
+ #expect(PrivilegedPathPolicy.Rejection(openingFolderFailedWith: POSIXError(.ENOENT)) == .unresolvableParent)
+ }
+
/// A policy over a fabricated root and home folder. The helper trusts only root (`trustedOwner` 0), and a
/// test cannot make a file root's, so a test may pass its own user ID instead.
private func policy(in directory: borrowing TemporaryDirectory, trustedOwner: uid_t = 0) throws -> PrivilegedPathPolicy {
diff --git a/Peel/Views/FixedSentenceText.swift b/Peel/Views/FixedSentenceText.swift
index 624a5be5..aa33e070 100644
--- a/Peel/Views/FixedSentenceText.swift
+++ b/Peel/Views/FixedSentenceText.swift
@@ -9,6 +9,7 @@ extension FixedSentence {
case .dotInPath: "The path has a . or .. in it."
case .controlCharacter: "The path holds a character no real name has."
case .folderNotFound: "The folder it’s in couldn’t be found."
+ case .tooManyOpenFolders: "Peel’s helper had too many folders open at once. Try again."
case .outsideHelpersFolders: "It is outside the folders Peel’s helper may touch."
case .onlyAppsThere: "Peel removes only apps from that folder."
case .notThereAnymore: "It isn’t there anymore."