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.

Name an app's iCloud folder as macOS shows it

Țugui Dragoșfac02696e7be
diff --git a/Packages/PeelCore/Sources/PeelCore/Space/CloudStorage.swift b/Packages/PeelCore/Sources/PeelCore/Space/CloudStorage.swift
index 12ae51b7..2de40a85 100644
--- a/Packages/PeelCore/Sources/PeelCore/Space/CloudStorage.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Space/CloudStorage.swift
@@ -238,12 +238,15 @@ public enum CloudStorage {
// The budget is spent, or the page that asked was left or stopped.
func stopped() -> Bool { ContinuousClock.now >= deadline || isGivenUp() }
+ var container = ""
for case let url as URL in enumerator {
guard !stopped() else {
collector.cutShort()
return
}
scan?.add(1)
+ // Each folder directly in Mobile Documents is one container, named once for everything inside it.
+ if enumerator.level == 1 { container = containerName(of: url, under: root) }
if isDataless(url) {
enumerator.skipDescendants()
continue
@@ -275,7 +278,7 @@ public enum CloudStorage {
let file = CloudFile(
url: url,
name: url.lastPathComponent,
- container: containerName(of: url, under: root),
+ container: container,
size: measured.size,
modified: measured.modified,
isDesktopPicture: pictures.contains(PathPattern.comparablePath(of: url))
@@ -348,8 +351,9 @@ public enum CloudStorage {
return (contents.size, contents.taken, modified)
}
- /// 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".
+ /// The readable name of the iCloud folder that holds `url`: "iCloud Drive" for `com~apple~CloudDocs`, and for an
+ /// app's folder the name macOS shows, which the app chooses, else the last part of the folder's identifier, so
+ /// `iCloud~com~acme~App` reads "App".
static func containerName(of url: URL, under root: URL) -> String {
let rootNames = PathComponents.of(PathPattern.comparablePath(of: root))
let names = PathComponents.of(PathPattern.comparablePath(of: url))
@@ -363,6 +367,11 @@ public enum CloudStorage {
)
return shown == folder ? "iCloud Drive" : shown
}
+ let container = root.appending(path: folder, directoryHint: .isDirectory)
+ if let shown = try? container.resourceValues(forKeys: [.ubiquitousItemContainerDisplayNameKey])
+ .ubiquitousItemContainerDisplayName, !shown.isEmpty {
+ return shown
+ }
var name = folder.replacingOccurrences(of: "~", with: ".")
if let range = name.range(of: "iCloud.") { name = String(name[range.upperBound...]) }
// Only the last part is kept. The rest, such as the team prefix in "F3LWYJ7GM7.com.apple.garageband10",
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/CloudStorageTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/CloudStorageTests.swift
index 94526bbd..199223eb 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/CloudStorageTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/CloudStorageTests.swift
@@ -103,6 +103,19 @@ struct CloudStorageTests {
#expect(CloudStorage.containerName(of: URL(filePath: "/somewhere/else"), under: root) == "")
}
+ /// An app's iCloud folder is named as macOS shows it, which the app chooses, on this Mac's own folders.
+ @Test func namesAnAppsFolderAsMacOSShowsIt() {
+ let root = URL.homeDirectory.appending(path: "Library/Mobile Documents", directoryHint: .isDirectory)
+ let containers = (try? FileManager.default.contentsOfDirectory(at: root, includingPropertiesForKeys: nil)) ?? []
+ for container in containers where container.lastPathComponent != "com~apple~CloudDocs" {
+ guard let shown = try? container.resourceValues(forKeys: [.ubiquitousItemContainerDisplayNameKey])
+ .ubiquitousItemContainerDisplayName, !shown.isEmpty
+ else { continue }
+ let file = container.appending(path: "Documents/Report.pages")
+ #expect(CloudStorage.containerName(of: file, under: root) == shown, "\(container.lastPathComponent)")
+ }
+ }
+
/// Scans the real iCloud Drive of the Mac running the tests, which must work and change nothing.
@Test func readsThisMacWithoutFreeingAnything() async {
let scan = await CloudStorage.downloaded()