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.

peel finishes writing History when its terminal closes

Țugui Dragoșc213e664f310
diff --git a/Packages/PeelCore/Sources/PeelCommandLine/Commands/AppCommands.swift b/Packages/PeelCore/Sources/PeelCommandLine/Commands/AppCommands.swift
index 3d00f24e..c9d80d25 100644
--- a/Packages/PeelCore/Sources/PeelCommandLine/Commands/AppCommands.swift
+++ b/Packages/PeelCore/Sources/PeelCommandLine/Commands/AppCommands.swift
@@ -420,18 +420,14 @@ struct UninstallCommand: AsyncParsableCommand {
// Checked again: the scan and the question take time, and the app may have been opened meanwhile.
try await Self.refuseWhileRunning(target, among: apps)
- // Ctrl-C and SIGTERM are ignored until History is written: an interrupt after the first move would
- // leave items in the Trash that History knows nothing about.
- signal(SIGINT, SIG_IGN)
- signal(SIGTERM, SIG_IGN)
- // The privacy reset comes before the move, because `tccutil` only finds an app that is still in place.
- let reset = resetPrivacy ? await PrivacyReset.reset(bundleIdentifier: target.bundleIdentifier) : nil
+ let (reset, result, recorded) = await Uninterrupted.run {
+ // The privacy reset comes before the move, because `tccutil` only finds an app that is still in place.
+ let reset = resetPrivacy ? await PrivacyReset.reset(bundleIdentifier: target.bundleIdentifier) : nil
+ let result = await plan.move(using: service)
+ let sizes = [URL: Int64](measured: plan.items.map { ($0.url, $0.size) })
+ return (reset, result, await Removals.record(result, from: target.name, sizes: sizes, tool: "applications"))
+ }
let privacy = reset.map { Self.privacyOutcome($0, app: target) }
- let result = await plan.move(using: service)
- let sizes = [URL: Int64](measured: plan.items.map { ($0.url, $0.size) })
- let recorded = await Removals.record(result, from: target.name, sizes: sizes, tool: "applications")
- signal(SIGINT, SIG_DFL)
- signal(SIGTERM, SIG_DFL)
if output.json {
let report = Self.report(app: target, plan: plan, result: result, privacy: reset, unreadable: unreadable)
diff --git a/Packages/PeelCore/Sources/PeelCommandLine/Commands/HistoryCommands.swift b/Packages/PeelCore/Sources/PeelCommandLine/Commands/HistoryCommands.swift
index a8c03d90..9d7955ed 100644
--- a/Packages/PeelCore/Sources/PeelCommandLine/Commands/HistoryCommands.swift
+++ b/Packages/PeelCore/Sources/PeelCommandLine/Commands/HistoryCommands.swift
@@ -250,23 +250,19 @@ struct RestoreCommand: AsyncParsableCommand {
try Output.confirm("Put \(Output.count(going.count, "item", "items")) back?")
}
- // Ctrl-C and SIGTERM are ignored until History is written: an interrupt after the first item goes back
- // would leave History listing items as removed when they are already back in place.
- signal(SIGINT, SIG_IGN)
- signal(SIGTERM, SIG_IGN)
- var restored: Set<UUID> = []
- var failures: [(RemovalRecord, RestoreFailure)] = []
- for record in going {
- // `peel` never uses the helper: items that need administrator access are left for the Peel app.
- if let failure = await service.restore(record.trashedItem, canUseHelper: false) {
- failures.append((record, failure))
- } else {
- restored.insert(record.id)
+ let (restored, failures, forgotten) = await Uninterrupted.run {
+ var restored: Set<UUID> = []
+ var failures: [(RemovalRecord, RestoreFailure)] = []
+ for record in going {
+ // `peel` never uses the helper: items that need administrator access are left for the Peel app.
+ if let failure = await service.restore(record.trashedItem, canUseHelper: false) {
+ failures.append((record, failure))
+ } else {
+ restored.insert(record.id)
+ }
}
+ return (restored, failures, await log.remove(restored).records != nil)
}
- let forgotten = await log.remove(restored).records != nil
- signal(SIGINT, SIG_DFL)
- signal(SIGTERM, SIG_DFL)
Output.line("Put \(Output.count(restored.count, "item", "items")) back.")
if !forgotten {
diff --git a/Packages/PeelCore/Sources/PeelCommandLine/Support/Cleanup.swift b/Packages/PeelCore/Sources/PeelCommandLine/Support/Cleanup.swift
index eb0f9286..b1bc652e 100644
--- a/Packages/PeelCore/Sources/PeelCommandLine/Support/Cleanup.swift
+++ b/Packages/PeelCore/Sources/PeelCommandLine/Support/Cleanup.swift
@@ -91,23 +91,20 @@ struct Cleanup {
try Output.confirm(question)
}
- // Ctrl-C and SIGTERM are ignored until History is written: an interrupt after the first move would
- // leave items in the Trash that History knows nothing about.
- signal(SIGINT, SIG_IGN)
- signal(SIGTERM, SIG_IGN)
- let result = await move(service, moving.map(\.url))
- let sizes = [URL: Int64](measured: items.map { ($0.url, $0.size) })
- let recorded = await Removals.record(
- TrashResult(trashed: result.trashed, failures: result.failures + refused),
- from: source,
- key: sourceKey,
- sizes: sizes,
- tool: tool,
- in: log,
- refusals: refusals
- )
- signal(SIGINT, SIG_DFL)
- signal(SIGTERM, SIG_DFL)
+ let (result, recorded) = await Uninterrupted.run {
+ let result = await move(service, moving.map(\.url))
+ let sizes = [URL: Int64](measured: items.map { ($0.url, $0.size) })
+ let recorded = await Removals.record(
+ TrashResult(trashed: result.trashed, failures: result.failures + refused),
+ from: source,
+ key: sourceKey,
+ sizes: sizes,
+ tool: tool,
+ in: log,
+ refusals: refusals
+ )
+ return (result, recorded)
+ }
Output.line("Moved \(Output.count(result.trashed.count, "item", "items")) to the Trash.")
if !recorded {
diff --git a/Packages/PeelCore/Sources/PeelCommandLine/Support/Uninterrupted.swift b/Packages/PeelCore/Sources/PeelCommandLine/Support/Uninterrupted.swift
new file mode 100644
index 00000000..53789d72
--- /dev/null
+++ b/Packages/PeelCore/Sources/PeelCommandLine/Support/Uninterrupted.swift
@@ -0,0 +1,35 @@
+import Darwin
+import Synchronization
+
+/// Keeps `peel` running from a move, or a put back, until History records it: stopped in between, History would miss
+/// what moved, or keep listing what went back.
+enum Uninterrupted {
+ /// What would stop `peel` there: Ctrl-C, a kill, a terminal closing or an SSH connection dropping, Ctrl-\, and
+ /// output piped into a command that has quit.
+ static let signals = [SIGINT, SIGTERM, SIGHUP, SIGQUIT, SIGPIPE]
+
+ /// How many runs are under way, and the handlers the signals had before the first of them.
+ private static let held = Mutex<(runs: Int, handlers: [sig_t?])>((0, []))
+
+ /// Runs `work` with `signals` ignored. Ignored rather than blocked, since a signal blocked on one thread still
+ /// stops the process through another. Only the last run to end gives each signal its handler back.
+ static func run<T>(_ work: () async throws -> T) async rethrows -> T {
+ held.withLock { held in
+ if held.runs == 0 {
+ held.handlers = signals.map { signal($0, SIG_IGN) }
+ }
+ held.runs += 1
+ }
+ defer {
+ held.withLock { held in
+ held.runs -= 1
+ if held.runs == 0 {
+ for (number, handler) in zip(signals, held.handlers) {
+ signal(number, handler)
+ }
+ }
+ }
+ }
+ return try await work()
+ }
+}
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/UninterruptedTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/UninterruptedTests.swift
new file mode 100644
index 00000000..127579c9
--- /dev/null
+++ b/Packages/PeelCore/Tests/PeelCoreTests/UninterruptedTests.swift
@@ -0,0 +1,41 @@
+import Darwin
+@testable import PeelCommandLine
+import Testing
+
+/// `peel` keeps running from a move, or a put back, until History records it.
+struct UninterruptedTests {
+ /// Whether `number` is ignored now, read without changing it.
+ private static func isIgnored(_ number: Int32) -> Bool {
+ var action = sigaction()
+ guard sigaction(number, nil, &action) == 0 else { return false }
+ return unsafeBitCast(action.__sigaction_u.__sa_handler, to: Int.self) == unsafeBitCast(SIG_IGN, to: Int.self)
+ }
+
+ /// Everything that would stop `peel` between a move and History is ignored meanwhile: Ctrl-C and a kill, and also
+ /// a terminal closing or an SSH connection dropping, Ctrl-\, and output piped into a command that has quit.
+ @Test func whatWouldStopPeelIsIgnoredUntilHistoryIsWritten() async {
+ #expect(Set(Uninterrupted.signals) == [SIGINT, SIGTERM, SIGHUP, SIGQUIT, SIGPIPE])
+ let ignored = await Uninterrupted.run { Uninterrupted.signals.filter(Self.isIgnored) }
+ #expect(ignored == Uninterrupted.signals)
+ }
+
+ /// A run that ends while another is still under way, as when tests run at once, leaves the other held: only the
+ /// last one to end gives the signals their handlers back.
+ @Test func aRunEndingFirstLeavesTheOtherHeld() async {
+ let (entered, enter) = AsyncStream.makeStream(of: Void.self)
+ let (released, release) = AsyncStream.makeStream(of: Void.self)
+ let first = Task {
+ await Uninterrupted.run {
+ enter.yield()
+ for await _ in released { break }
+ }
+ }
+ for await _ in entered { break }
+
+ await Uninterrupted.run {
+ release.yield()
+ await first.value
+ #expect(Uninterrupted.signals.allSatisfy(Self.isIgnored), "the run still under way lost its hold")
+ }
+ }
+}
diff --git a/SAFETY.md b/SAFETY.md
index 10885c33..5271bb4f 100644
--- a/SAFETY.md
+++ b/SAFETY.md
@@ -108,9 +108,10 @@ Peel never deletes anything of yours. Everything it removes goes to the Trash, a
History can put it back; History keeps the most recent 20,000 items, and what it forgets is still in the Trash.
That includes an installer receipt you ask Peel to forget: the two files that make it up go to the Trash
through the helper, where `pkgutil --forget` would have discarded them for good. Quitting Peel waits for a
-removal until History has it, and each item is written down the moment it moves, so one cut short by a crash
-or Force Quit still reaches History, as an interrupted removal, the next time Peel or `peel` opens it. The
-space is freed when *you* empty the Trash: Peel never does that for you.
+removal until History has it, `peel` carries on through Ctrl-C or a closed terminal until it has written History,
+and each item is written down the moment it moves, so one cut short by a crash or Force Quit still reaches
+History, as an interrupted removal, the next time Peel or `peel` opens it. The space is freed when *you* empty
+the Trash: Peel never does that for you.
The one file Peel deletes outright is its own list of refusals, and only when `peel history --refused --clear` is
asked to forget it.