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.

Read the open files again once a second while a removal goes on

Țugui Dragoșa899617812e8
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/OpenFiles.swift b/Packages/PeelCore/Sources/PeelCore/Removal/OpenFiles.swift
index 7a072d6f..faf5983d 100644
--- a/Packages/PeelCore/Sources/PeelCore/Removal/OpenFiles.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/OpenFiles.swift
@@ -38,6 +38,23 @@ struct OpenFiles {
self.programs = programs
}
+ /// How long a reading is trusted: a removal of many items takes seconds, and a program can start meanwhile.
+ static let lifetime: Duration = .seconds(1)
+
+ /// A reading of the open files that is made again once it is older than `lifetime`.
+ struct Fresh {
+ private var reading = OpenFiles()
+ private var read = ContinuousClock.now
+
+ mutating func refusal(of url: URL, lettingItsProgramsRun: Bool) -> TrashFailure.Reason? {
+ if ContinuousClock.now - read > OpenFiles.lifetime {
+ reading = OpenFiles()
+ read = .now
+ }
+ return reading.refusal(of: url, lettingItsProgramsRun: lettingItsProgramsRun)
+ }
+ }
+
/// The programs that hold `url`. An app is code, which loses nothing when it moves, so a program that only reads
/// inside it, as Safari reads an app's Safari extension, holds nothing: what runs from it or writes in it does.
func holders(of url: URL, lettingItsProgramsRun: Bool = false) -> [String] {
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift b/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift
index c38b3d9b..849bed91 100644
--- a/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift
@@ -420,7 +420,7 @@ public struct TrashService: Sendable {
// The guard reads the disk, so it is asked once for each item.
let refusals = urls.reduce(into: [URL: GuardRefusal]()) { $0[$1] = removalGuard.refusal(of: $1) }
let allowed = urls.filter { refusals[$0] == nil }
- let openFiles = OpenFiles()
+ var openFiles = OpenFiles.Fresh()
// Read before the move, while the files are still there to say which jobs they declare.
let jobs = LaunchdCleanup.jobs(for: allowed, environment: environment)
@@ -617,7 +617,7 @@ public struct TrashService: Sendable {
// find it gone and report a failure.
var paths = Folders()
permitted.forEach { paths.add(PathPattern.comparablePath(of: $0)) }
- let openFiles = OpenFiles()
+ var openFiles = OpenFiles.Fresh()
var allowed: [URL] = []
for url in permitted {
guard !paths.hold(PathPattern.comparablePath(of: url)) else { continue }
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/TrashServiceTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/TrashServiceTests.swift
index 06af5a3f..243c5281 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/TrashServiceTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/TrashServiceTests.swift
@@ -833,6 +833,31 @@ struct TrashServiceTests {
#expect(caches.appending(path: "Vendor.real/x.pdf").isThere)
}
+ @Test func readsTheOpenFilesAgainWhileAMoveGoesOn() async throws {
+ let directory = try TemporaryDirectory()
+ let service = try service(in: directory)
+ let first = try directory.file("home/Documents/first.txt")
+ let second = try directory.file("home/Documents/second.txt", bytes: 1_000_000)
+ let third = try directory.file("home/Documents/third.txt")
+ let reader = Mutex<Process?>(nil)
+ defer { reader.withLock { $0?.terminate() } }
+
+ let result = await service.trash([first, second, third]) { url in
+ guard url == first else { return nil }
+ let cat = Process()
+ cat.executableURL = URL(filePath: "/bin/cat")
+ cat.arguments = [second.path(percentEncoded: false)]
+ cat.standardOutput = Pipe()
+ try? cat.run()
+ reader.withLock { $0 = cat }
+ Thread.sleep(forTimeInterval: 1.05)
+ return nil
+ }
+
+ #expect(result.trashed.map(\.originalURL) == [first, third])
+ #expect(result.failures.map(\.reason) == [.heldOpen(by: ["cat"])])
+ }
+
@Test func makesNoFolderForAPutBackTheGuardRefusesOnceTheFoldersAreHeld() throws {
let directory = try TemporaryDirectory()
let caches = try directory.directory("home/Library/Caches")
diff --git a/SAFETY.md b/SAFETY.md
index 71ab0ce8..562c8bfc 100644
--- a/SAFETY.md
+++ b/SAFETY.md
@@ -150,9 +150,10 @@ written down the moment it moves, and what its helper moves as soon as the helpe
short by a crash or Force Quit still reaches History, as an interrupted removal, the next time Peel or `peel` opens
it.
-Right before anything moves, Peel looks at the files other programs of yours hold open and at the program each
-process runs, and leaves where it is any file or folder with one of them inside, naming the program and how to free
-the item: quit it, or stop it in Background Items when it has no window, since launchd starts such a program again.
+Right before anything moves, and again once a second while a removal of many items goes on, Peel looks at the files
+other programs of yours hold open and at the program each process runs, and leaves where it is any file or folder
+with one of them inside, naming the program and how to free the item: quit it, or stop it in Background Items when it
+has no window, since launchd starts such a program again.
Moving a database or a cache from under an agent, a daemon, or a server that is using it would break it. A program
of macOS's own, such as a service keeping its cache open, is named as macOS: nobody quits it, so the item stays
where it is. An app is code, which loses nothing when it moves, so a program that only reads inside it, as Safari