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.

Ask the guard before Remove Peel gathers its folder

Țugui Dragoș764aad26f008
diff --git a/GUIDE.md b/GUIDE.md
index 5a20d48f..e9a9b686 100644
--- a/GUIDE.md
+++ b/GUIDE.md
@@ -393,8 +393,8 @@ In Settings > General, choose Remove Peel. Peel then:
helper when only an administrator can, and has the helper move its own record of what it moved;
- removes its helper and its login item;
- moves itself, the files that are certainly its own, and its folder in Application Support to the Trash, all but
- the Terminal settings zsh and ssh read. That folder holds History, your exclusions, and the settings Peel saved
- when you reset an app;
+ the Terminal settings zsh and ssh read and anything in it you excluded in Settings. That folder holds History, your
+ exclusions, and the settings Peel saved when you reset an app;
- ends its Finder extension, takes its icon out of the Dock, clears its settings, and quits.
Remove Peel takes no step while Peel itself can't move, such as from a folder only an administrator can change, and
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/SelfRemoval.swift b/Packages/PeelCore/Sources/PeelCore/Removal/SelfRemoval.swift
index 878dd2ec..ca3f5b34 100644
--- a/Packages/PeelCore/Sources/PeelCore/Removal/SelfRemoval.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/SelfRemoval.swift
@@ -166,17 +166,8 @@ public enum SelfRemoval {
FileManager.default.fileExists(atPath: folder.path(percentEncoded: false))
else { return result }
let terminal = ShellFile.url(in: folder).deletingLastPathComponent()
- let moved: TrashResult
- if terminal.isMissing {
- moved = await service.trash([folder])
- } else {
- do {
- let rest = try gathered(in: folder, leaving: terminal)
- moved = await service.trashOwnFiles(rest)
- } catch {
- moved = TrashResult(failures: [TrashFailure(url: folder, reason: .failed(error.localizedDescription))])
- }
- }
+ let moved = terminal.isMissing
+ ? await service.trash([folder]) : await movingTogether(folder, leaving: terminal, using: service)
result.trashed += moved.trashed
result.failures += moved.failures
return result
@@ -227,19 +218,58 @@ public enum SelfRemoval {
return (await service.trash(links, usingHelperFor: Set(links)), await moveLedger())
}
- /// Gathers everything in `folder` but `kept` into a folder named Peel inside it, so the Trash shows Peel's files
- /// as one item. An entry that cannot be gathered is answered on its own.
- private static func gathered(in folder: URL, leaving kept: URL) throws -> [URL] {
+ /// Moves everything in `folder` but `kept` to the Trash gathered in a folder named Peel, so the Trash shows Peel's
+ /// files as one item. The guard and the open files are asked about each entry where it is, before anything is
+ /// gathered, and what was gathered goes back to its name when the gathered folder stays.
+ private static func movingTogether(
+ _ folder: URL, leaving kept: URL, using service: TrashService
+ ) async -> TrashResult {
let manager = FileManager.default
- let entries = try manager.contentsOfDirectory(at: folder, includingPropertiesForKeys: nil)
- .filter { $0.lastPathComponent != kept.lastPathComponent }
+ let entries: [URL]
+ do {
+ entries = try manager.contentsOfDirectory(at: folder, includingPropertiesForKeys: nil)
+ .filter { $0.lastPathComponent != kept.lastPathComponent }
+ } catch {
+ return TrashResult(failures: [TrashFailure(url: folder, reason: .failed(error.localizedDescription))])
+ }
+ let openFiles = OpenFiles()
+ var stayed: [TrashFailure] = []
+ var going: [URL] = []
+ for entry in entries {
+ if let reason = service.refusal(of: entry) ?? openFiles.refusal(of: entry, lettingItsProgramsRun: false) {
+ stayed.append(TrashFailure(url: entry, reason: reason))
+ } else {
+ going.append(entry)
+ }
+ }
let together = folder.appending(path: "Peel", directoryHint: .isDirectory)
- guard (try? manager.createDirectory(at: together, withIntermediateDirectories: false)) != nil else {
- return entries
+ guard !going.isEmpty, (try? manager.createDirectory(at: together, withIntermediateDirectories: false)) != nil
+ else {
+ var result = going.isEmpty ? TrashResult() : await service.trashOwnFiles(going)
+ result.failures += stayed
+ return result
}
- let left = entries.filter { entry in
- (try? manager.moveItem(at: entry, to: together.appending(path: entry.lastPathComponent))) == nil
+ var gathered: [URL] = []
+ var left: [URL] = []
+ for entry in going {
+ let inside = together.appending(path: entry.lastPathComponent)
+ if (try? manager.moveItem(at: entry, to: inside)) != nil {
+ gathered.append(entry)
+ } else {
+ left.append(entry)
+ }
}
- return [together] + left
+ var result = await service.trashOwnFiles([together] + left)
+ if let refused = result.failures.first(where: { $0.url == together }) {
+ result.failures.removeAll { $0.url == together }
+ for entry in gathered {
+ let inside = together.appending(path: entry.lastPathComponent)
+ let back = (try? manager.moveItem(at: inside, to: entry)) != nil
+ result.failures.append(TrashFailure(url: back ? entry : inside, reason: refused.reason))
+ }
+ rmdir(together.path(percentEncoded: false))
+ }
+ result.failures += stayed
+ return result
}
}
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/SelfRemovalTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/SelfRemovalTests.swift
index 8720d3d2..3ae63935 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/SelfRemovalTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/SelfRemovalTests.swift
@@ -443,6 +443,54 @@ struct SelfRemovalTests {
#expect(Set(try FileManager.default.contentsOfDirectory(atPath: trashed)) == [".hidden", "exclusions.json", "removals.json"])
}
+ @Test func leavesWhatTheGuardKeepsInPeelsFolderWhereItIs() async throws {
+ let directory = try TemporaryDirectory()
+ let app = try directory.directory("home/Applications/Peel.app")
+ let folder = try directory.directory("home/Library/Application Support/Peel")
+ try directory.file("home/Library/Application Support/Peel/Terminal/zshrc")
+ try directory.file("home/Library/Application Support/Peel/exclusions.json")
+ let copies = try directory.directory("home/Library/Application Support/Peel/Preference Backups")
+ try directory.file("home/Library/Application Support/Peel/Preference Backups/org.example.app.plist")
+ let service = try service(in: directory, exclusions: Exclusions(paths: [copies]))
+
+ let result = await SelfRemoval.move([app], app: app, folder: folder, using: service) { _ in }
+
+ #expect(!copies.appending(path: "org.example.app.plist").isMissing)
+ #expect(result.failures.map(\.url.lastPathComponent) == ["Preference Backups"])
+ #expect(result.failures.first?.reason == .guarded(.excluded))
+ #expect(Set(try FileManager.default.contentsOfDirectory(atPath: folder.path(percentEncoded: false)))
+ == ["Terminal", "Preference Backups"])
+ }
+
+ @Test func putsBackWhatItGatheredWhenTheGatheredFolderStays() async throws {
+ let directory = try TemporaryDirectory()
+ let app = try directory.directory("home/Applications/Peel.app")
+ let folder = try directory.directory("home/Library/Application Support/Peel")
+ try directory.file("home/Library/Application Support/Peel/Terminal/zshrc")
+ try directory.file("home/Library/Application Support/Peel/exclusions.json")
+ let gathered = folder.appending(path: "Peel", directoryHint: .isDirectory)
+ let trash = try directory.directory("Trash")
+ let environment = SearchEnvironment(
+ homeDirectory: directory.url.appending(path: "home", directoryHint: .isDirectory),
+ rootDirectory: directory.url.appending(path: "root", directoryHint: .isDirectory)
+ )
+ let service = TrashService(environment: environment) { url in
+ guard url.standardizedFileURL != gathered.standardizedFileURL else {
+ throw CocoaError(.fileWriteNoPermission)
+ }
+ let destination = trash.appending(path: UUID().uuidString)
+ try FileManager.default.moveItem(at: url, to: destination)
+ return destination
+ }
+
+ let result = await SelfRemoval.move([app], app: app, folder: folder, using: service) { _ in }
+
+ #expect(!folder.appending(path: "exclusions.json").isMissing)
+ #expect(Set(try FileManager.default.contentsOfDirectory(atPath: folder.path(percentEncoded: false)))
+ == ["Terminal", "exclusions.json"])
+ #expect(result.failures.map(\.url.lastPathComponent) == ["exclusions.json"])
+ }
+
@Test func movesEachFileOnItsOwnWhenTheFolderCannotBeGathered() async throws {
let directory = try TemporaryDirectory()
let app = try directory.directory("home/Applications/Peel.app")