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.

Space leaves out what you exclude

Țugui Dragoș1f3538622ec9
diff --git a/Packages/PeelCore/Sources/PeelCore/Space/SpaceReport.swift b/Packages/PeelCore/Sources/PeelCore/Space/SpaceReport.swift
index 41d68d24..818621f3 100644
--- a/Packages/PeelCore/Sources/PeelCore/Space/SpaceReport.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Space/SpaceReport.swift
@@ -267,14 +267,22 @@ public enum SpaceInventory {
),
]
- /// The total size of `urls`, or nil when any of them could not be measured. A folder a file provider owns
- /// can stall a directory read for minutes, and a very big folder can run out of time as well. An area with
- /// an unknown size is still listed, since it may be the biggest one on the disk.
- private static func size(of urls: [URL], measure: @escaping FileSize.Measure) async -> Int64? {
+ /// The total size of `urls` less what is excluded inside them, or nil when any of it could not be measured. A
+ /// folder a file provider owns can stall a directory read for minutes, and a very big folder can run out of
+ /// time as well. An area with an unknown size is still listed, since it may be the biggest one on the disk.
+ private static func size(
+ of urls: [URL],
+ leaving exclusions: Exclusions,
+ measure: @escaping FileSize.Measure
+ ) async -> Int64? {
var total: Int64 = 0
for url in urls {
- guard let measured = await measure(url) else { return nil }
- total += measured
+ guard var measured = await measure(url) else { return nil }
+ for place in exclusions.places(inside: url) where !place.isMissing {
+ guard let excluded = await measure(place) else { return nil }
+ measured -= excluded
+ }
+ total += max(0, measured)
}
return total
}
@@ -324,9 +332,21 @@ public enum SpaceInventory {
/// Anything smaller than this is noise in a report about space.
public static let minimumSize: Int64 = 50 * 1_000_000
+ /// What is excluded is left out: an area that is excluded is not listed, and what is excluded inside one is
+ /// not counted in its size.
@concurrent
- public static func scan(home: URL = .homeDirectory, minimumSize: Int64 = SpaceInventory.minimumSize) async -> SpaceReport {
- await scan(home: home, root: URL(filePath: "/", directoryHint: .isDirectory), minimumSize: minimumSize, measure: FileSize.measure)
+ public static func scan(
+ home: URL = .homeDirectory,
+ minimumSize: Int64 = SpaceInventory.minimumSize,
+ exclusions: Exclusions = .none
+ ) async -> SpaceReport {
+ await scan(
+ home: home,
+ root: URL(filePath: "/", directoryHint: .isDirectory),
+ minimumSize: minimumSize,
+ exclusions: exclusions,
+ measure: FileSize.measure
+ )
}
/// `root` is where the paths outside the home folder start, so a test can point it at a folder of its own.
@@ -335,6 +355,7 @@ public enum SpaceInventory {
home: URL,
root: URL = URL(filePath: "/", directoryHint: .isDirectory),
minimumSize: Int64,
+ exclusions: Exclusions = .none,
measure: @escaping FileSize.Measure
) async -> SpaceReport {
let containers = appContainers(in: home)
@@ -348,7 +369,8 @@ public enum SpaceInventory {
+ (definition.groupContainerFolder.map { folder in
groups.map { $0.appending(path: folder, directoryHint: .isDirectory) }.filter(\.isRealFolder)
} ?? [])
- return urls.isEmpty ? nil : (definition, urls)
+ let kept = urls.filter { !exclusions.excludes($0) }
+ return kept.isEmpty ? nil : (definition, kept)
}
// Four areas are measured at a time. One after another, the wait would be the sum of them all, and a
@@ -359,8 +381,9 @@ public enum SpaceInventory {
var pending = wanted.makeIterator()
func addNext() -> Bool {
guard !Task.isCancelled, let (definition, urls) = pending.next() else { return false }
- let measured = definition.leavesMacOSsOwn ? offered(in: urls, of: definition, root: root) : urls
- group.addTask { (definition, urls, await size(of: measured, measure: measure)) }
+ let measured = definition.leavesMacOSsOwn
+ ? offered(in: urls, of: definition, root: root).filter { !exclusions.excludes($0) } : urls
+ group.addTask { (definition, urls, await size(of: measured, leaving: exclusions, measure: measure)) }
return true
}
for _ in 0..<4 where addNext() {}
diff --git a/Packages/PeelCore/Sources/PeelCore/Support/Exclusions.swift b/Packages/PeelCore/Sources/PeelCore/Support/Exclusions.swift
index 04102219..d349deca 100644
--- a/Packages/PeelCore/Sources/PeelCore/Support/Exclusions.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Support/Exclusions.swift
@@ -121,6 +121,17 @@ public struct Exclusions: Sendable, Codable, Hashable {
}
}
+ /// The excluded places inside `url`, leaving out one inside another, so what they hold is counted once.
+ public func places(inside url: URL) -> [URL] {
+ let asked = Self.spellings(of: url).map(PathComponents.of)
+ let inside = paths.filter { path in
+ Self.spellings(of: path).map(PathComponents.of).contains { names in
+ asked.contains { names.count > $0.count && names.starts(with: $0) }
+ }
+ }
+ return inside.filter { place in !inside.contains { $0 != place && Exclusions(paths: [$0]).excludes(place) } }
+ }
+
/// Every way `url` can be written (see `PathPattern.spellings(of:)`). An exclusion has to match all of
/// them, or an item under `/var`, `/tmp` or `/etc` would slip past it when named through `/private`.
private static func spellings(of url: URL) -> Set<String> {
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/ExclusionsTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/ExclusionsTests.swift
index e8b4a119..bf78d2b0 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/ExclusionsTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/ExclusionsTests.swift
@@ -3,6 +3,22 @@ import Foundation
import Testing
struct ExclusionsTests {
+ @Test func findsTheExcludedPlacesInsideAFolderOnce() {
+ let caches = URL(filePath: "/Users/x/Library/Caches", directoryHint: .isDirectory)
+ let exclusions = Exclusions(paths: [
+ caches.appending(path: "org.example.one"),
+ caches.appending(path: "org.example.one/inner"),
+ caches.appending(path: "org.example.two"),
+ URL(filePath: "/Users/x/Documents"),
+ ])
+
+ let places = Set(exclusions.places(inside: caches).map(\.lastPathComponent))
+ #expect(places == ["org.example.one", "org.example.two"])
+ #expect(exclusions.places(inside: URL(filePath: "/Users/x/Library/Logs")).isEmpty)
+ let two = caches.appending(path: "org.example.two")
+ #expect(exclusions.places(inside: two).isEmpty, "a place is not inside itself")
+ }
+
/// A slash and a combining mark after it are one `Character`, so a name that begins with the mark is not
/// "inside" its folder to a comparison made on characters. Compared name by name, what sits in an excluded
/// folder is excluded whatever its name begins with, and an excluded item inside a folder is still held.
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/SpaceInventoryTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/SpaceInventoryTests.swift
index 0adf3259..5a197a7b 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/SpaceInventoryTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/SpaceInventoryTests.swift
@@ -69,6 +69,41 @@ struct SpaceInventoryTests {
#expect(!report.needsFullDiskAccess, "ordinary permissions were read as a refusal Full Disk Access would lift")
}
+ @Test func leavesOutWhatIsExcludedAndWhatItHolds() async throws {
+ let directory = try TemporaryDirectory()
+ try directory.file("Parallels/Example.pvm/disk.hdd", bytes: 400_000)
+ try directory.file("Library/Caches/org.example.kept/blob", bytes: 300_000)
+ try directory.file("Library/Caches/org.example.excluded/blob", bytes: 500_000)
+ let exclusions = Exclusions(paths: [
+ directory.url.appending(path: "Parallels"),
+ directory.url.appending(path: "Library/Caches/org.example.excluded"),
+ ])
+
+ let report = await SpaceInventory.scan(
+ home: directory.url, root: directory.url, minimumSize: 1, exclusions: exclusions, measure: FileSize.measure
+ )
+
+ #expect(!report.items.contains { $0.id == "parallels" })
+ let caches = try #require(report.items.first { $0.id == "caches" })
+ let kept = await FileSize.reclaimableSize(of: directory.url.appending(path: "Library/Caches/org.example.kept"))
+ #expect(caches.size == kept)
+ }
+
+ @Test func anExcludedPlaceThatCannotBeMeasuredLeavesTheAreaUnknown() async throws {
+ let directory = try TemporaryDirectory()
+ try directory.file("Library/Caches/org.example.kept/blob", bytes: 300_000)
+ try directory.file("Library/Caches/org.example.excluded/blob", bytes: 500_000)
+ let excluded = directory.url.appending(path: "Library/Caches/org.example.excluded")
+
+ let report = await SpaceInventory.scan(
+ home: directory.url, root: directory.url, minimumSize: 1, exclusions: Exclusions(paths: [excluded])
+ ) { url in
+ url.lastPathComponent == excluded.lastPathComponent ? nil : await FileSize.measure(url)
+ }
+
+ #expect(report.items.first { $0.id == "caches" }?.size == nil)
+ }
+
@Test func leavesWhatOthersOwnToThem() async throws {
let directory = try TemporaryDirectory()
try directory.file(".colima/machine/disk.img", bytes: 4096)
diff --git a/Peel/Model/SpaceLibrary.swift b/Peel/Model/SpaceLibrary.swift
index f395f48c..b6676efa 100644
--- a/Peel/Model/SpaceLibrary.swift
+++ b/Peel/Model/SpaceLibrary.swift
@@ -52,7 +52,8 @@ final class SpaceLibrary: RowSelection {
/// Scans again, and makes again the plans of the areas in `replanning` and of every area that changed since
/// its plan was made, so what is carried from them follows what is on disk.
private func refresh(replanning: Set<SpaceItem.ID>) async {
- guard let result = await scanRun.run({ await SpaceInventory.scan() }) else { return }
+ let exclusions = ExclusionsStore.shared.exclusions
+ guard let result = await scanRun.run({ await SpaceInventory.scan(exclusions: exclusions) }) else { return }
report = result
for id in plans.keys.filter({ id in !result.items.contains { $0.id == id } }) {
selectedURLs.subtract(plans[id]?.removable ?? [])