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.

iCloud Drive offers a document saved as a package

Țugui Dragoș14cc497cf23d
diff --git a/Packages/PeelCore/Sources/PeelCore/Space/CloudStorage.swift b/Packages/PeelCore/Sources/PeelCore/Space/CloudStorage.swift
index 67e52494..f56a851e 100644
--- a/Packages/PeelCore/Sources/PeelCore/Space/CloudStorage.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Space/CloudStorage.swift
@@ -2,9 +2,9 @@ public import Foundation
import Synchronization
internal import PeelPrivileged
-/// A file in iCloud Drive that also has a local copy. Freeing the local copy deletes nothing: the file stays in
-/// iCloud, Finder still shows it, and it downloads again when it is opened. So freeing does not go through the
-/// Trash, since there is nothing to put back.
+/// A file in iCloud Drive, or a document saved as a package, that also has a local copy. Freeing the local copy
+/// deletes nothing: the file stays in iCloud, Finder still shows it, and it downloads again when it is opened. So
+/// freeing does not go through the Trash, since there is nothing to put back.
public struct CloudFile: Sendable, Hashable, Identifiable {
public let url: URL
public let name: String
@@ -71,7 +71,7 @@ public enum CloudStorage {
/// The resource keys read for each file, by the scan and again by `free(_:)`.
static let keys: Set<URLResourceKey> = [
- .isRegularFileKey, .contentModificationDateKey, .ubiquitousItemDownloadingStatusKey,
+ .isRegularFileKey, .isPackageKey, .contentModificationDateKey, .ubiquitousItemDownloadingStatusKey,
.ubiquitousItemIsUploadedKey, .ubiquitousItemIsUploadingKey, .ubiquitousItemHasUnresolvedConflictsKey,
.ubiquitousItemIsSyncPausedKey, .ubiquitousItemUploadingErrorKey,
]
@@ -215,28 +215,38 @@ public enum CloudStorage {
return
}
+ // The budget is spent, or the page that asked was left or stopped.
+ func stopped() -> Bool { ContinuousClock.now >= deadline || isGivenUp() }
for case let url as URL in enumerator {
- // The budget is spent, or the page that asked was left or stopped.
- guard ContinuousClock.now < deadline, !isGivenUp() else {
+ guard !stopped() else {
collector.cutShort()
return
}
scan?.add(1)
guard
let values = try? url.resourceValues(forKeys: keys),
- values.isRegularFile == true,
+ values.isRegularFile == true || values.isPackage == true,
!exclusions.excludes(url),
isSafe(values)
else { continue }
- let size = ReclaimableSpace.of(url)
- guard size >= minimumSize else { continue }
+ let measured = values.isRegularFile == true
+ ? (size: ReclaimableSpace.of(url), modified: values.contentModificationDate)
+ : package(url, values, exclusions: exclusions, countingFor: scan, unless: stopped)
+ guard let measured else {
+ if stopped() {
+ collector.cutShort()
+ return
+ }
+ continue
+ }
+ guard measured.size >= minimumSize else { continue }
let file = CloudFile(
url: url,
name: url.lastPathComponent,
container: containerName(of: url, under: root),
- size: size,
- modified: values.contentModificationDate
+ size: measured.size,
+ modified: measured.modified
)
collector.add(file)
}
@@ -248,6 +258,13 @@ public enum CloudStorage {
@discardableResult
@concurrent
public static func free(_ files: [CloudFile], exclusions: Exclusions) async -> [CloudRefusal] {
+ await free(files, exclusions: exclusions, isSafe: isSafeToFree(_:))
+ }
+
+ @concurrent
+ static func free(
+ _ files: [CloudFile], exclusions: Exclusions, isSafe: (URLResourceValues) -> Bool
+ ) async -> [CloudRefusal] {
var refused: [CloudRefusal] = []
for file in files {
guard exclusions.isKnown, !exclusions.excludes(file.url) else {
@@ -258,9 +275,11 @@ public enum CloudStorage {
url.removeAllCachedResourceValues()
guard
let values = try? url.resourceValues(forKeys: keys),
- values.isRegularFile == true,
- isSafeToFree(values),
- values.contentModificationDate == file.modified
+ isSafe(values),
+ values.isRegularFile == true
+ ? values.contentModificationDate == file.modified
+ : values.isPackage == true
+ && package(url, values, exclusions: exclusions, unless: { false })?.modified == file.modified
else {
refused.append(CloudRefusal(url: file.url, reason: .changedSinceScan))
continue
@@ -274,6 +293,22 @@ public enum CloudStorage {
return refused
}
+ /// A document saved as a package, which iCloud keeps as one item: what removing its download frees, and the
+ /// newest date of anything inside, since editing a file inside leaves the package's own date as it was. Nil for
+ /// a package with something excluded inside, one that could not be read whole, or a walk that was stopped.
+ static func package(
+ _ url: URL,
+ _ values: URLResourceValues,
+ exclusions: Exclusions,
+ countingFor scan: ScanCount? = nil,
+ unless isStopped: () -> Bool
+ ) -> (size: Int64, modified: Date?)? {
+ guard !exclusions.holds(url), let contents = FileSize.walk(url, countingFor: scan, unless: isStopped),
+ !contents.couldNotBeRead
+ else { return nil }
+ return (contents.size, [values.contentModificationDate, contents.newestChange].compactMap(\.self).max())
+ }
+
/// The readable name of the iCloud folder that holds `url`: "iCloud Drive" for `com~apple~CloudDocs`, and
/// the last part of the identifier for an app's folder, so `iCloud~com~acme~App` reads "App".
static func containerName(of url: URL, under root: URL) -> String {
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/CloudStorageTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/CloudStorageTests.swift
index 31be157d..7d60b7dd 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/CloudStorageTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/CloudStorageTests.swift
@@ -189,4 +189,50 @@ struct CloudStorageTests {
#expect(collector.collected.wasCutShort)
#expect(collector.collected.files.isEmpty)
}
+
+ /// iCloud Drive keeps a document saved as a package as one item, and Finder removes its download whole.
+ @Test func aDocumentSavedAsAPackageIsOneItem() throws {
+ let directory = try TemporaryDirectory()
+ let drive = "Library/Mobile Documents/com~apple~CloudDocs"
+ try directory.file("\(drive)/Notes.rtfd/TXT.rtf", bytes: 2_000_000)
+ try directory.file("\(drive)/Notes.rtfd/Attachments/take.bin", bytes: 1_000_000)
+ try directory.file("\(drive)/Kept.rtfd/TXT.rtf", bytes: 2_000_000)
+ try directory.file("\(drive)/Kept.rtfd/private.bin", bytes: 2_000_000)
+ try directory.file("\(drive)/Folder/inside.bin", bytes: 2_000_000)
+ let package = directory.url.appending(path: "\(drive)/Notes.rtfd", directoryHint: .isDirectory)
+ let excluded = directory.url.appending(path: "\(drive)/Kept.rtfd/private.bin")
+ let collector = CloudStorage.Collector()
+
+ CloudStorage.collect(
+ home: directory.url, minimumSize: 1, exclusions: Exclusions(paths: [excluded]), into: collector,
+ deadline: .now + .seconds(20), countingFor: nil, isSafe: { _ in true }, unless: { false }
+ )
+ let files = Dictionary(uniqueKeysWithValues: collector.collected.files.map { ($0.name, $0) })
+
+ #expect(files.keys.sorted() == ["Notes.rtfd", "inside.bin"])
+ #expect(files["Notes.rtfd"]?.size == FileSize.walk(package, unless: { false })?.size)
+ #expect((files["Notes.rtfd"]?.size ?? 0) >= 3_000_000)
+ }
+
+ /// Editing a file inside a package leaves the package's own date as it was, so the newest date inside is what
+ /// tells that it changed since the scan.
+ @Test func freesNoPackageEditedInsideSinceTheScan() async throws {
+ let directory = try TemporaryDirectory()
+ let drive = "Library/Mobile Documents/com~apple~CloudDocs"
+ let inside = try directory.file("\(drive)/Notes.rtfd/TXT.rtf", bytes: 2_000_000)
+ let collector = CloudStorage.Collector()
+ CloudStorage.collect(
+ home: directory.url, minimumSize: 1, exclusions: .none, into: collector, deadline: .now + .seconds(20),
+ countingFor: nil, isSafe: { _ in true }, unless: { false }
+ )
+ let scanned = try #require(collector.collected.files.first)
+
+ let untouched = await CloudStorage.free([scanned], exclusions: .none, isSafe: { _ in true })
+ let later: [FileAttributeKey: Any] = [.modificationDate: Date.now.addingTimeInterval(60)]
+ try FileManager.default.setAttributes(later, ofItemAtPath: inside.path(percentEncoded: false))
+ let edited = await CloudStorage.free([scanned], exclusions: .none, isSafe: { _ in true })
+
+ #expect(untouched.map(\.reason) != [.changedSinceScan], "only iCloud can remove a download, so a test's fails")
+ #expect(edited.map(\.reason) == [.changedSinceScan])
+ }
}