Uninstall Mac apps with every file they leave behind, and see why each belongs. Free up space, fine-tune macOS, and set up Terminal.
Swift cache-cleanercleanerclideveloper-toolsdisk-cleanupdisk-spaceduplicate-finderhomebrewliquid-glassmac-cleanermacosmacos-appmacos-golden-gatemacos-tahoemacos-tweaksswiftswiftuiterminal-themesuninstalluninstaller
Duplicates weighs a copy on an exFAT or FAT disk by what it frees
Țugui Dragoșada5ab85baee
diff --git a/Packages/PeelCore/Sources/PeelCore/Duplicates/ReclaimableSpace.swift b/Packages/PeelCore/Sources/PeelCore/Duplicates/ReclaimableSpace.swiftindex bf715220..94719a5d 100644--- a/Packages/PeelCore/Sources/PeelCore/Duplicates/ReclaimableSpace.swift+++ b/Packages/PeelCore/Sources/PeelCore/Duplicates/ReclaimableSpace.swift@@ -22,6 +22,8 @@ enum ReclaimableSpace {let path = url.path(percentEncoded: false)var info = stat()guard lstat(path, &info) == 0, info.st_nlink <= 1 else { return 0 }+ // 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,@@ -32,6 +34,26 @@ enum ReclaimableSpace {return blocks?.privateSize ?? Int64(info.st_blocks) * 512}+ /// Whether the disk `path` is on can clone a file or keep a snapshot, the only ways its files share blocks. True+ /// when the disk does not say.+ private static func canShareBlocks(_ path: String) -> Bool {+ var request = attrlist()+ request.bitmapcount = u_short(ATTR_BIT_MAP_COUNT)+ request.volattr = attrgroup_t(ATTR_VOL_INFO) | attrgroup_t(ATTR_VOL_CAPABILITIES)+ // Layout: UInt32 length, then the capabilities, whose interfaces word holds cloning and snapshots.+ var buffer = [UInt8](repeating: 0, count: MemoryLayout<UInt32>.size + MemoryLayout<vol_capabilities_attr_t>.size)+ let status = buffer.withUnsafeMutableBytes { bytes in+ getattrlist(path, &request, bytes.baseAddress, bytes.count, 0)+ }+ guard status == 0 else { return true }+ let capabilities = buffer.withUnsafeBytes { bytes in+ bytes.loadUnaligned(fromByteOffset: MemoryLayout<UInt32>.size, as: vol_capabilities_attr_t.self)+ }+ let sharing = UInt32(VOL_CAP_INT_CLONE) | UInt32(VOL_CAP_INT_SNAPSHOT)+ let valid = capabilities.valid.1 & sharing+ return valid != sharing || capabilities.capabilities.1 & sharing != 0+ }+private static func forkAttributes(of path: String) -> (privateSize: Int64?, mayBeShared: Bool?)? {var request = attrlist()request.bitmapcount = u_short(ATTR_BIT_MAP_COUNT)diff --git a/Packages/PeelCore/Tests/PeelCoreTests/DuplicateFinderTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/DuplicateFinderTests.swiftindex f3167fb7..97e1cc30 100644--- a/Packages/PeelCore/Tests/PeelCoreTests/DuplicateFinderTests.swift+++ b/Packages/PeelCore/Tests/PeelCoreTests/DuplicateFinderTests.swift@@ -414,6 +414,35 @@ struct DuplicateFinderTests {#expect(ReclaimableSpace.of(compressed) == 0)#expect(ReclaimableSpace.of(clone) == 0)}++ /// A USB stick or a card formatted exFAT or FAT can neither clone a file nor keep a snapshot, so its copies share+ /// no block, yet it answers the private size with zero. Removing a copy there frees every block the copy takes.+ @Test(arguments: ["ExFAT", "MS-DOS FAT32"])+ func aCopyOnADiskThatSharesNothingFreesAllItTakes(fileSystem: String) async throws {+ let directory = try TemporaryDirectory()+ let home = try directory.directory("home")+ let volume = try ScratchVolume(fileSystem: fileSystem, mountedAt: home.appending(path: "Stick", directoryHint: .isDirectory))+ let photo = randomData(count: 1_048_576)+ for (path, contents) in [("one/photo.bin", photo), ("two/photo.bin", photo), ("two/other.bin", randomData(count: 4_096))] {+ let url = volume.url.appending(path: path)+ try FileManager.default.createDirectory(at: url.deletingLastPathComponent(), withIntermediateDirectories: true)+ try contents.write(to: url)+ }+ for folder in ["a", "b"] {+ let url = volume.url.appending(path: "\(folder)/scan.bin")+ try FileManager.default.createDirectory(at: url.deletingLastPathComponent(), withIntermediateDirectories: true)+ try photo.prefix(524_288).write(to: url)+ }++ let result = try await DuplicateFinder(homeDirectory: home).scan(DuplicateScanOptions(folders: [volume.url]))++ let photos = try #require(result.groups.first { $0.files.contains { $0.url.lastPathComponent == "photo.bin" } })+ let copy = try #require(photos.files.last)+ #expect(copy.reclaimableSize == copy.allocatedSize && copy.allocatedSize >= 1_048_576)+ #expect(!copy.sharesStorage)+ let folders = try #require(result.folderGroups.first { $0.folders.contains { $0.url.lastPathComponent == "b" } })+ #expect(folders.reclaimableSize >= 524_288)+ }}struct DuplicateRemovalTests {diff --git a/Packages/PeelCore/Tests/PeelCoreTests/ScratchVolume.swift b/Packages/PeelCore/Tests/PeelCoreTests/ScratchVolume.swiftnew file mode 100644index 00000000..af6131ea--- /dev/null+++ b/Packages/PeelCore/Tests/PeelCoreTests/ScratchVolume.swift@@ -0,0 +1,28 @@+import Foundation++/// A small disk of its own for a test, formatted with a file system the startup disk does not use, made with `hdiutil`+/// and mounted at a folder the test chooses, out of Finder's sight. It is detached when the test lets go of it.+final class ScratchVolume {+ let url: URL+ private let image: TemporaryDirectory++ /// `fileSystem` is a name `hdiutil create -fs` takes, such as `ExFAT` or `MS-DOS FAT32`.+ init(fileSystem: String, mountedAt url: URL) throws {+ image = try TemporaryDirectory()+ self.url = url+ let file = image.url.appending(path: "disk.dmg")+ try FileManager.default.createDirectory(at: url, withIntermediateDirectories: true)+ try Self.hdiutil("create", "-quiet", "-size", "64m", "-fs", fileSystem, "-volname", "Scratch", "-type", "UDIF", file.path)+ try Self.hdiutil("attach", "-quiet", "-nobrowse", "-noautoopen", "-noverify", "-mountpoint", url.path, file.path)+ }++ deinit {+ try? Self.hdiutil("detach", "-quiet", "-force", url.path)+ }++ private static func hdiutil(_ arguments: String...) throws {+ let process = try Process.run(URL(filePath: "/usr/bin/hdiutil"), arguments: arguments)+ process.waitUntilExit()+ guard process.terminationStatus == 0 else { throw CocoaError(.fileWriteUnknown, userInfo: [NSLocalizedDescriptionKey: arguments.joined(separator: " ")]) }+ }+}