Uninstall Mac apps with every file they leave behind, and see why each belongs. Free up space, fine-tune macOS, and set up Terminal.
Swift cache-cleanercleanerclideveloper-toolsdisk-cleanupdisk-spaceduplicate-finderhomebrewliquid-glassmac-cleanermacosmacos-appmacos-golden-gatemacos-tahoemacos-tweaksswiftswiftuiterminal-themesuninstalluninstaller
Say which permission keeps peel uninstall from moving an app
Țugui Dragoș06737ffccbcd
diff --git a/Packages/PeelCore/Sources/PeelCommandLine/Commands/AppCommands.swift b/Packages/PeelCore/Sources/PeelCommandLine/Commands/AppCommands.swiftindex 34cea61c..62884b8e 100644--- a/Packages/PeelCore/Sources/PeelCommandLine/Commands/AppCommands.swift+++ b/Packages/PeelCore/Sources/PeelCommandLine/Commands/AppCommands.swift@@ -372,6 +372,19 @@ struct UninstallCommand: AsyncParsableCommand {}}+ /// Why the app itself can't move: macOS keeps another developer's app from a terminal without App Management,+ /// which the person can allow, while an app in a folder they can't write to needs an administrator.+ static func whyItCannotMove(+ _ app: InstalledApp,+ isProtectedByPrivacy: Bool+ ) -> CommandFailure {+ let name = Output.plain(app.name)+ guard isProtectedByPrivacy else {+ return CommandFailure("Moving \(name) to the Trash needs administrator access. Remove it with the Peel app.")+ }+ return CommandFailure("macOS lets your terminal app move \(name) only with App Management. Allow it in System Settings > Privacy & Security > App Management, then try again, or remove it with the Peel app.")+ }+/// Throws for Peel itself. Peel removes itself from its own Settings, after unregistering its login item/// and privileged helper. If `peel` moved the app, the helper would stay registered with launchd and/// point into the Trash.@@ -438,7 +451,7 @@ struct UninstallCommand: AsyncParsableCommand {)}guard !uninstallation.appRequiresPrivileges else {- throw CommandFailure("Moving \(Output.plain(target.name)) to the Trash needs administrator access. Remove it with the Peel app.")+ throw Self.whyItCannotMove(target, isProtectedByPrivacy: FileAccess.isProtectedByPrivacy(target.url))}if let note = UnreadableHistory.note() {diff --git a/Packages/PeelCore/Sources/PeelCore/Home/Access.swift b/Packages/PeelCore/Sources/PeelCore/Home/Access.swiftindex c3dafe6f..c959a07a 100644--- a/Packages/PeelCore/Sources/PeelCore/Home/Access.swift+++ b/Packages/PeelCore/Sources/PeelCore/Home/Access.swift@@ -145,13 +145,7 @@ public enum AppManagement {/// Whether a refused move of `url` points to App Management: the user can write to the bundle's folder, and/// nothing locks the bundle. The removal alert uses the same test.public static func isRefusedForWantOfPermission(_ url: URL) -> Bool {- !FileAccess.requiresPrivilegesToRemove(url) && !isLocked(url)- }-- /// Whether `url` is locked (`chflags uchg` or `schg`), which blocks a move whatever the permissions say.- private static func isLocked(_ url: URL) -> Bool {- var info = stat()- guard lstat(url.path(percentEncoded: false), &info) == 0 else { return false }- return info.st_flags & (UInt32(UF_IMMUTABLE) | UInt32(SF_IMMUTABLE)) != 0+ let canBeMoved = !FileAccess.requiresPrivilegesToRemove(url) || FileAccess.isProtectedByPrivacy(url)+ return canBeMoved && !FileAccess.isLocked(url)}}diff --git a/Packages/PeelCore/Sources/PeelCore/Support/FileAccess.swift b/Packages/PeelCore/Sources/PeelCore/Support/FileAccess.swiftindex cf3e8458..8c83e1e2 100644--- a/Packages/PeelCore/Sources/PeelCore/Support/FileAccess.swift+++ b/Packages/PeelCore/Sources/PeelCore/Support/FileAccess.swift@@ -1,3 +1,4 @@+import Darwinpublic import Foundationpublic enum FileAccess {@@ -6,6 +7,26 @@ public enum FileAccess {ParentAccess(url.deletingLastPathComponent()).requiresPrivileges(toRemove: url)}+ /// True when macOS's privacy protection, rather than the permissions or a lock, keeps this process from+ /// changing `url`: `access(2)` then fails with `EPERM`, as it does for another developer's app while the process+ /// has no App Management. A lock fails with `EPERM` too, and the permissions with `EACCES`.+ public static func isProtectedByPrivacy(_ url: URL) -> Bool {+ let path = url.path(percentEncoded: false)+ let error = access(path, W_OK) == 0 ? nil : errno+ return isProtectedByPrivacy(accessError: error, isLocked: isLocked(url))+ }++ static func isProtectedByPrivacy(accessError: Int32?, isLocked: Bool) -> Bool {+ accessError == EPERM && !isLocked+ }++ /// Whether `url` is locked (`chflags uchg` or `schg`), which blocks a move whatever the permissions say.+ static func isLocked(_ url: URL) -> Bool {+ var info = stat()+ guard lstat(url.path(percentEncoded: false), &info) == 0 else { return false }+ return info.st_flags & (UInt32(UF_IMMUTABLE) | UInt32(SF_IMMUTABLE)) != 0+ }+/// True when `url` itself is locked as Finder's Get Info locks it (`UF_IMMUTABLE`, `chflags(2)`): the file/// may not be changed until its owner takes the lock off.static func isLockedInFinder(_ url: URL) -> Bool {diff --git a/Packages/PeelCore/Tests/PeelCoreTests/CommandLineTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/CommandLineTests.swiftindex cd2c164a..d0c47ea4 100644--- a/Packages/PeelCore/Tests/PeelCoreTests/CommandLineTests.swift+++ b/Packages/PeelCore/Tests/PeelCoreTests/CommandLineTests.swift@@ -33,6 +33,18 @@ struct CommandLineTests {)}+ /// An app the terminal can't move for want of App Management needs no administrator, so the command names the+ /// permission to give instead.+ @Test func saysWhichPermissionKeepsAnAppInPlace() {+ let app = InstalledApp(+ url: URL(filePath: "/Applications/Example.app"), bundleIdentifier: "org.example.app", name: "Example"+ )+ let privacy = UninstallCommand.whyItCannotMove(app, isProtectedByPrivacy: true).description+ #expect(privacy.contains("App Management") && !privacy.contains("administrator"))+ let permissions = UninstallCommand.whyItCannotMove(app, isProtectedByPrivacy: false).description+ #expect(permissions.contains("administrator"))+ }+@Test func parsesByteSizes() {#expect(ByteSize(argument: "2048")?.bytes == 2_048)#expect(ByteSize(argument: "500KB")?.bytes == 500_000)diff --git a/Packages/PeelCore/Tests/PeelCoreTests/FileAccessTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/FileAccessTests.swiftindex dc877a20..1c2af050 100644--- a/Packages/PeelCore/Tests/PeelCoreTests/FileAccessTests.swift+++ b/Packages/PeelCore/Tests/PeelCoreTests/FileAccessTests.swift@@ -18,6 +18,23 @@ struct FileAccessTests {#expect(FileAccess.requiresPrivilegesToRemove(file))}+ /// macOS answers `EPERM` both for an item its privacy protection keeps from this process, as it keeps another+ /// developer's app from a process without App Management, and for one that is locked. Only the first is a+ /// permission the person can give.+ @Test func tellsAPrivacyRefusalFromALock() throws {+ #expect(FileAccess.isProtectedByPrivacy(accessError: EPERM, isLocked: false))+ #expect(!FileAccess.isProtectedByPrivacy(accessError: EPERM, isLocked: true))+ #expect(!FileAccess.isProtectedByPrivacy(accessError: EACCES, isLocked: false))+ #expect(!FileAccess.isProtectedByPrivacy(accessError: nil, isLocked: false))++ let directory = try TemporaryDirectory()+ let locked = try directory.directory("Locked.app")+ #expect(chflags(locked.path(percentEncoded: false), UInt32(UF_IMMUTABLE)) == 0)+ defer { chflags(locked.path(percentEncoded: false), 0) }+ #expect(!FileAccess.isProtectedByPrivacy(locked))+ #expect(!FileAccess.isProtectedByPrivacy(try directory.directory("Plain.app")))+ }+@Test func whatMacOSOwnsNeedsAnAdministrator() {#expect(FileAccess.requiresPrivilegesToRemove(URL(filePath: "/System/Applications/Calculator.app")))}