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.

Count what a snapshot keeps as freed, on every page

Țugui Dragoș77a6b9b7d199
diff --git a/AGENTS.md b/AGENTS.md
index ce9f80ad..2816f9fe 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -253,10 +253,12 @@ If you are unsure whether something belongs in the helper, it doesn't.
`PathPattern.locatedWithoutOpening` and never opens them.
- **`lstat` on a path that ends in a slash follows a link** to where it leads. A folder walk hands back folders
with that slash, so paths are compared and checked through `PathPattern.comparablePath`, which drops it.
-- **What removing a file frees is its private size, never its length**: the bytes "not trapped inside a clone or
- snapshot" (`getattrlist(2)`, `ATTR_CMNEXT_PRIVATESIZE`). Once iCloud has uploaded a file, the copy it keeps of it
- shares the file's blocks, so removing that download frees nothing. `ReclaimableSpace.of` measures every size Peel
- offers to free, and iCloud Drive says how many downloads it leaves out for that (`CloudScan.sharedDownloads`).
+- **What removing a file frees is never its length**: a file that may share blocks with a clone
+ (`EF_MAY_SHARE_BLOCKS`) frees its private size, the bytes "not trapped inside a clone or snapshot"
+ (`getattrlist(2)`, `ATTR_CMNEXT_PRIVATESIZE`), and any other file every block it takes, since what a snapshot
+ keeps comes back once macOS deletes the snapshot. Once iCloud has uploaded a file, the copy it keeps of it shares
+ the file's blocks, so removing that download frees nothing. `ReclaimableSpace.of` measures every size Peel offers to
+ free, and iCloud Drive says how many downloads it leaves out for that (`CloudScan.sharedDownloads`).
- **Settings are forgotten with `defaults delete`, never by deleting their file.** cfprefsd keeps a domain in
memory and writes it back, and `defaults delete` of a domain it still holds writes an empty file where the moved
one was. `PreferenceCleanup` handles both.
diff --git a/Packages/PeelCore/Sources/PeelCore/Duplicates/Duplicates.swift b/Packages/PeelCore/Sources/PeelCore/Duplicates/Duplicates.swift
index c0ed788d..9a760228 100644
--- a/Packages/PeelCore/Sources/PeelCore/Duplicates/Duplicates.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Duplicates/Duplicates.swift
@@ -27,8 +27,8 @@ public enum DuplicateScanProgress: Sendable, Hashable {
public struct DuplicateFile: Sendable, Hashable, Identifiable {
public let url: URL
public let size: Int64
- /// The bytes removing it would free: less than `size` for a hard link, an APFS clone, a file kept by a
- /// snapshot, or a compressed or sparse file.
+ /// The bytes removing it would free: less than `size` for a hard link, an APFS clone, or a compressed or sparse
+ /// file.
public let reclaimableSize: Int64
/// The space it takes on disk, less than `size` when it is compressed or sparse. Compared with
/// `reclaimableSize`, it shows whether the file really shares blocks with something else.
diff --git a/Packages/PeelCore/Sources/PeelCore/Duplicates/ReclaimableSpace.swift b/Packages/PeelCore/Sources/PeelCore/Duplicates/ReclaimableSpace.swift
index c7e9454e..d66a9a29 100644
--- a/Packages/PeelCore/Sources/PeelCore/Duplicates/ReclaimableSpace.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Duplicates/ReclaimableSpace.swift
@@ -16,8 +16,8 @@ enum ReclaimableSpace {
info.st_flags & UInt32(UF_COMPRESSED) != 0 ? info.st_size : min(info.st_size, Int64(info.st_blocks) * 512)
}
- /// The bytes removing the file would free: none while it has other hard links, and otherwise only the blocks
- /// it does not share with APFS clones or snapshots.
+ /// The bytes removing the file would free: none while it has other hard links, and otherwise every block it
+ /// does not share with an APFS clone.
static func of(_ url: URL) -> Int64 {
let path = url.path(percentEncoded: false)
var info = stat()
@@ -25,13 +25,20 @@ enum ReclaimableSpace {
// exFAT and FAT answer the private size with zero, though they share no block.
guard canShareBlocks(path) else { return Int64(info.st_blocks) * 512 }
let blocks = forkAttributes(of: path)
- // The private size counts only the data fork, while a compressed file keeps its bytes in its resource
- // fork or an extended attribute, which the private size leaves out. If the file was never cloned,
- // removing it frees every block; otherwise it counts as freeing nothing, since a clone may hold them all.
- if info.st_flags & UInt32(UF_COMPRESSED) != 0 {
- return blocks?.mayBeShared == false ? Int64(info.st_blocks) * 512 : 0
- }
- return blocks?.privateSize ?? Int64(info.st_blocks) * 512
+ return freed(
+ allocated: Int64(info.st_blocks) * 512, privateSize: blocks?.privateSize, mayBeShared: blocks?.mayBeShared,
+ isCompressed: info.st_flags & UInt32(UF_COMPRESSED) != 0
+ )
+ }
+
+ /// What removing a file with one name frees, from what `getattrlist(2)` says of it. A file that was never
+ /// cloned frees every block it takes, those a snapshot keeps included, since they come back once macOS deletes
+ /// the snapshot.
+ static func freed(allocated: Int64, privateSize: Int64?, mayBeShared: Bool?, isCompressed: Bool) -> Int64 {
+ guard mayBeShared == true else { return allocated }
+ // The private size counts only the data fork, while a compressed file keeps its bytes in its resource fork or
+ // an extended attribute, which the private size leaves out: a clone may hold them all.
+ return isCompressed ? 0 : privateSize ?? allocated
}
/// Whether the disk `path` is on can clone a file or keep a snapshot, the only ways its files share blocks. True
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/DuplicateFinderTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/DuplicateFinderTests.swift
index 6a3c87a4..874f6465 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/DuplicateFinderTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/DuplicateFinderTests.swift
@@ -708,6 +708,22 @@ struct DuplicateFinderTests {
await #expect(throws: CancellationError.self) { try await task.value }
}
+ /// A file that may share blocks with a clone frees only its private size. Any other frees every block it takes,
+ /// since what a snapshot keeps of it comes back once macOS deletes the snapshot. A snapshot can't be made in a
+ /// test, so the rule is asked with what `getattrlist(2)` would say of such a file.
+ @Test func aFileThatSharesNoBlockFreesEveryBlockItTakes() {
+ let freed = { (privateSize: Int64?, mayBeShared: Bool?, isCompressed: Bool) in
+ ReclaimableSpace.freed(
+ allocated: 4_096_000, privateSize: privateSize, mayBeShared: mayBeShared, isCompressed: isCompressed
+ )
+ }
+ #expect(freed(0, false, false) == 4_096_000, "kept by a snapshot")
+ #expect(freed(nil, nil, false) == 4_096_000)
+ #expect(freed(1_000, true, false) == 1_000, "cloned")
+ #expect(freed(0, true, true) == 0, "compressed and cloned")
+ #expect(freed(0, false, true) == 4_096_000, "compressed")
+ }
+
@Test func reportsSpaceThatClonesAndHardLinksDoNotFree() throws {
let directory = try TemporaryDirectory()
let original = try directory.file("original.bin", contents: randomData(count: 1_048_576))