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.

A folder Peel could not read to the end is never taken for all it holds

Țugui Dragoșd962bea336f0
diff --git a/Packages/PeelCore/Sources/PeelCore/Support/FileSize.swift b/Packages/PeelCore/Sources/PeelCore/Support/FileSize.swift
index f3192e1b..b71fd242 100644
--- a/Packages/PeelCore/Sources/PeelCore/Support/FileSize.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Support/FileSize.swift
@@ -14,8 +14,9 @@ public struct FolderContents: Sendable, Hashable {
/// The latest modification date of anything inside. A folder's own date does not change when a file in
/// it is rewritten in place.
public let newestChange: Date?
- /// True when the folder itself could not be opened, so nothing inside was seen. `size` is then zero, which
- /// must never be read as empty. From macOS 27, another team's container is refused outright.
+ /// True when the folder, or a folder inside it, could not be opened, so not everything inside was seen. `size`
+ /// then counts only what was, which must never be read as the folder's size. From macOS 27, another team's
+ /// container is refused outright.
public let couldNotBeRead: Bool
init(size: Int64, holdsRepository: Bool, holdsWallet: Bool = false, newestChange: Date? = nil, couldNotBeRead: Bool = false) {
@@ -132,15 +133,16 @@ public enum FileSize {
let fileKeys: Set<URLResourceKey> = [
.isRegularFileKey, .totalFileAllocatedSizeKey, .linkCountKey, .fileIdentifierKey, .contentModificationDateKey,
]
- // Notes whether the folder itself failed to open. The enumerator reports that only to the error
- // handler and then yields nothing, which would read as an empty folder.
- let refused = Refused(path: PathPattern.comparablePath(of: url))
+ // Notes whether the folder, or a folder inside it, failed to open. The enumerator reports that only to the
+ // error handler and goes on without it, which would read as a folder holding less than it does. A folder
+ // that is no longer there holds nothing, which leaves nothing unseen.
+ let refused = Refused()
guard let enumerator = FileManager.default.enumerator(
at: url,
includingPropertiesForKeys: Array(fileKeys),
options: [],
- errorHandler: { failing, _ in
- if PathPattern.comparablePath(of: failing) == refused.path { refused.note() }
+ errorHandler: { _, error in
+ if (error as? CocoaError)?.code != .fileReadNoSuchFile { refused.note() }
return true
}
) else { return FolderContents(size: 0, holdsRepository: false, couldNotBeRead: true) }
@@ -195,16 +197,11 @@ public enum FileSize {
case next, finished, stopped
}
- /// Whether the folder being walked refused to open. Set by the enumerator's error handler on the walking
- /// thread, and read once the walk ends.
+ /// Whether a folder of the walk refused to open. Set by the enumerator's error handler on the walking thread,
+ /// and read once the walk ends.
private final class Refused: Sendable {
- let path: String
private let state = Mutex(false)
- init(path: String) {
- self.path = path
- }
-
var happened: Bool { state.withLock { $0 } }
func note() {
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/FileSizeTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/FileSizeTests.swift
index 67899336..435ea438 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/FileSizeTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/FileSizeTests.swift
@@ -101,6 +101,40 @@ struct FileSizeTests {
#expect(await FileSize.allocatedSize(of: folder) == nil)
}
+ /// A folder inside that macOS will not open leaves the walk short: its size, and whatever wallet or repository
+ /// it holds, were never seen. So the folder around it is not known either.
+ @Test func knowsNoSizeForAFolderItCannotReadToTheEnd() async throws {
+ let directory = try TemporaryDirectory()
+ let folder = try directory.directory("Vendor")
+ try directory.file("Vendor/settings.plist", bytes: 100_000)
+ try directory.file("Vendor/Private/wallet.dat", bytes: 100_000)
+ try directory.setPermissions(0, of: "Vendor/Private")
+ defer { try? directory.setPermissions(0o755, of: "Vendor/Private") }
+
+ #expect(await FileSize.contents(of: folder)?.couldNotBeRead == true)
+ #expect(await FileSize.allocatedSize(of: folder) == nil)
+ }
+
+ /// A folder that goes away while the walk goes on holds nothing any more, which is no reason to doubt the rest:
+ /// an app that makes and removes folders in its cache as it runs would otherwise leave that cache never known.
+ @Test func aFolderThatGoesAwayDuringTheWalkLeavesTheRestKnown() throws {
+ let directory = try TemporaryDirectory()
+ let folder = try directory.directory("Cache")
+ try directory.file("Cache/work-in-progress/part.bin", bytes: 100_000)
+ try directory.directory("elsewhere")
+ var hasMoved = false
+ let contents = FileSize.walk(folder, unless: {
+ if !hasMoved {
+ hasMoved = true
+ try? FileManager.default.moveItem(at: folder.appending(path: "work-in-progress"), to: directory.url.appending(path: "elsewhere/work-in-progress"))
+ }
+ return false
+ })
+
+ #expect(hasMoved)
+ #expect(contents?.couldNotBeRead == false)
+ }
+
/// An item whose own attributes macOS will not give, because the folder around it cannot be searched, is not
/// known either, and never reads as zero. An item that is not there at all holds nothing.
@Test func knowsNoSizeForAnItemItCannotLookAt() async throws {
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/LeftoverScannerTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/LeftoverScannerTests.swift
index e832a82f..6b218ef3 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/LeftoverScannerTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/LeftoverScannerTests.swift
@@ -577,6 +577,23 @@ struct LeftoverScannerTests {
#expect(!found.isMeasured)
}
+ /// A folder inside the leftover that macOS will not open may hold what nothing brings back, as a helper's own
+ /// `Private` folder with a wallet in it, so the leftover is not read to the end and is never selected.
+ @Test func aLeftoverNotReadToTheEndIsNeverSelected() async throws {
+ let directory = try TemporaryDirectory()
+ try directory.file("home/Library/Application Support/net.example.client/settings.plist", bytes: 4_096)
+ try directory.file("home/Library/Application Support/net.example.client/Private/wallet.dat", bytes: 4_096)
+ try directory.setPermissions(0, of: "home/Library/Application Support/net.example.client/Private")
+ defer { try? directory.setPermissions(0o755, of: "home/Library/Application Support/net.example.client/Private") }
+
+ let scan = await LeftoverScanner(environment: environment(in: directory)).scan(tunewell, installedApps: [tunewell])
+ let found = try #require(scan.leftovers.first { $0.url.path(percentEncoded: false).hasSuffix("Application Support/net.example.client") })
+
+ #expect(found.match.heldBack == .couldNotBeRead, "a folder read only in part was taken for all it holds")
+ #expect(!found.match.isRecommended)
+ #expect(!found.isMeasured)
+ }
+
/// An exclusion inside replaces `notMeasured` as the reason, but the folder's size stays unknown, not zero.
@Test func aFolderHoldingAnExclusionKeepsASizeNobodyKnows() async throws {
let directory = try TemporaryDirectory()
diff --git a/SAFETY.md b/SAFETY.md
index ec35a8b4..fe71f363 100644
--- a/SAFETY.md
+++ b/SAFETY.md
@@ -222,8 +222,9 @@ A wallet's keys can be the only way to what they hold, so Peel keeps them in fou
or `.keys`, and the others `FileSize.isWallet` lists. A folder with one inside is shown with that reason and
never selected for you, not even by Select All in Orphaned Files, `peel uninstall`, or `peel orphans --remove`.
You can still select it yourself, because a name can mislead: a Java project keeps a `keystore` too.
-- **A folder Peel could not finish reading is treated the same way.** A coin's data folder, with its blockchain,
- is the one most likely to be too big to read in time, and its wallet may be inside.
+- **A folder Peel could not finish reading is treated the same way**, whether it ran out of time or macOS kept a
+ folder inside it closed. A coin's data folder, with its blockchain, is the one most likely to be too big to read
+ in time, and its wallet may be inside.
What this cannot cover, said plainly. Peel looks for a browser profile at most two levels inside a folder, as it
does for a photo library, so a profile kept deeper in a folder you move yourself is not seen. A wallet kept where no