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.

Say which folders in iCloud Drive could not be read

Țugui Dragoș182773dc5912
diff --git a/Packages/PeelCore/Sources/PeelCore/Space/CloudStorage.swift b/Packages/PeelCore/Sources/PeelCore/Space/CloudStorage.swift
index d9a39c2b..0eff7c80 100644
--- a/Packages/PeelCore/Sources/PeelCore/Space/CloudStorage.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Space/CloudStorage.swift
@@ -42,6 +42,8 @@ public struct CloudScan: Sendable {
public var isNotSetUp = false
/// The folders of the home iCloud Drive keeps that macOS would not let Peel read: Desktop, Documents.
public var unreadableFolders: [String] = []
+ /// The folders inside, and the documents saved as packages, that could not be read, so nothing in them is listed.
+ public var notRead: [URL] = []
public enum CutShort: Sendable, Equatable {
/// The walk ran out of time or was stopped before it read everything.
@@ -179,7 +181,8 @@ public enum CloudStorage {
sharedDownloads: collected.sharedDownloads,
folders: folders(of: collected.files, home: home),
isNotSetUp: !isSetUp(home: home),
- unreadableFolders: collected.unreadableFolders
+ unreadableFolders: collected.unreadableFolders,
+ notRead: collected.notRead
)
}
@@ -206,6 +209,7 @@ public enum CloudStorage {
var couldNotRead = false
var sharedDownloads = 0
var unreadableFolders: [String] = []
+ var notRead: [URL] = []
}
private let state = Mutex(Collected())
@@ -270,6 +274,10 @@ public enum CloudStorage {
state.withLock { $0.unreadableFolders.append(name) }
}
+ func notRead(_ url: URL) {
+ state.withLock { $0.notRead.append(url) }
+ }
+
func leftOutAsShared() {
state.withLock { $0.sharedDownloads += 1 }
}
@@ -300,14 +308,19 @@ public enum CloudStorage {
includingPropertiesForKeys: Array(keys),
options: [.skipsHiddenFiles, .skipsPackageDescendants],
errorHandler: { url, error in
- // Only an error on the root counts: without Full Disk Access nothing under it can be read. A
- // missing root (`NSFileReadNoSuchFileError`) means iCloud Drive was never set up, which is
- // nothing to list rather than a refusal.
+ // What is missing is no refusal: a root that isn't there means iCloud Drive was never set up, and
+ // a folder inside went away during the walk. Without Full Disk Access nothing under the root can
+ // be read.
let isMissing =
(error as NSError).domain == NSCocoaErrorDomain
&& (error as NSError).code == NSFileReadNoSuchFileError
- if PathPattern.comparablePath(of: url) == rootPath, !isMissing {
- if let folder { collector.unreadable(folder) } else { collector.unreadable() }
+ guard !isMissing else { return true }
+ if PathPattern.comparablePath(of: url) != rootPath {
+ collector.notRead(url)
+ } else if let folder {
+ collector.unreadable(folder)
+ } else {
+ collector.unreadable()
}
return true
}
@@ -364,6 +377,7 @@ public enum CloudStorage {
collector.ranOutOfTime()
return
}
+ if !exclusions.holds(url) { collector.notRead(url) }
continue
}
guard measured.size >= minimumSize else {
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/CloudStorageTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/CloudStorageTests.swift
index 8873620a..fb01b4af 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/CloudStorageTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/CloudStorageTests.swift
@@ -527,6 +527,69 @@ struct CloudStorageTests {
#expect(scan.unreadableFolders == ["Documents"])
}
+ @Test(.permissionsHold) func namesAFolderInsideThatCouldNotBeRead() async throws {
+ let directory = try TemporaryDirectory()
+ let drive = "Library/Mobile Documents/com~apple~CloudDocs"
+ try directory.file("\(drive)/Found.png", bytes: 2_000)
+ try directory.file("\(drive)/Locked/Hidden.png", bytes: 2_000)
+ try directory.setPermissions(0, of: "\(drive)/Locked")
+ let locked = directory.url.appending(path: "\(drive)/Locked").path(percentEncoded: false)
+ defer { try? FileManager.default.setAttributes([.posixPermissions: 0o755], ofItemAtPath: locked) }
+
+ let scan = await safeScan(of: directory)
+
+ #expect(scan.files.map(\.name) == ["Found.png"])
+ #expect(scan.notRead.map(\.lastPathComponent) == ["Locked"])
+ #expect(!scan.couldNotRead)
+ #expect(scan.unreadableFolders.isEmpty)
+ }
+
+ @Test(.permissionsHold) func namesADocumentSavedAsAPackageThatCouldNotBeReadWhole() throws {
+ let directory = try TemporaryDirectory()
+ let drive = "Library/Mobile Documents/com~apple~CloudDocs"
+ try directory.file("\(drive)/Notes.rtfd/Attachments/take.bin", bytes: 2_000)
+ try directory.setPermissions(0, of: "\(drive)/Notes.rtfd/Attachments")
+ let attachments = directory.url.appending(path: "\(drive)/Notes.rtfd/Attachments").path(percentEncoded: false)
+ defer { try? FileManager.default.setAttributes([.posixPermissions: 0o755], ofItemAtPath: attachments) }
+ let collector = CloudStorage.Collector()
+
+ CloudStorage.collect(
+ home: directory.url, minimumSize: 1, exclusions: .none, into: collector,
+ countingFor: nil, isSafe: { _ in true }, unless: { false }
+ )
+
+ #expect(collector.collected.files.isEmpty)
+ #expect(collector.collected.notRead.map(\.lastPathComponent) == ["Notes.rtfd"])
+ }
+
+ @Test func aFolderGoneDuringTheWalkWasNotRefused() throws {
+ let directory = try TemporaryDirectory()
+ let drive = "Library/Mobile Documents/com~apple~CloudDocs"
+ try directory.file("\(drive)/First/one.bin", bytes: 2_000)
+ try directory.file("\(drive)/Second/two.bin", bytes: 2_000)
+ let folders = directory.url.appending(path: drive, directoryHint: .isDirectory)
+ let collector = CloudStorage.Collector()
+ var isGone = false
+
+ CloudStorage.collect(
+ home: directory.url, minimumSize: 1, exclusions: .none, into: collector,
+ countingFor: nil, isSafe: { _ in true },
+ isDataless: { url in
+ if !isGone, ["First", "Second"].contains(url.lastPathComponent) {
+ isGone = true
+ let other = url.lastPathComponent == "First" ? "Second" : "First"
+ try? FileManager.default.removeItem(at: folders.appending(path: other))
+ }
+ return false
+ },
+ unless: { false }
+ )
+
+ #expect(isGone)
+ #expect(collector.collected.notRead.isEmpty)
+ #expect(!collector.collected.couldNotRead)
+ }
+
@Test func aWalkThatRunsOutOfTimeCostsTheOtherWalksNothing() async throws {
let directory = try TemporaryDirectory()
try directory.file("Library/Mobile Documents/com~apple~CloudDocs/Stalls/File.png", bytes: 2_000)
@@ -715,6 +778,7 @@ struct CloudStorageTests {
let files = Dictionary(uniqueKeysWithValues: collector.collected.files.map { ($0.name, $0) })
#expect(files.keys.sorted() == ["Notes.rtfd", "inside.bin"])
+ #expect(collector.collected.notRead.isEmpty)
#expect(files["Notes.rtfd"]?.size == FileSize.walk(package, unless: { false })?.size)
#expect((files["Notes.rtfd"]?.size ?? 0) >= 3_000_000)
}
diff --git a/Peel/Model/CloudLibrary.swift b/Peel/Model/CloudLibrary.swift
index 8eba366a..0db245a0 100644
--- a/Peel/Model/CloudLibrary.swift
+++ b/Peel/Model/CloudLibrary.swift
@@ -36,6 +36,7 @@ final class CloudLibrary {
private(set) var isNotSetUp = false
/// The folders of the home iCloud Drive keeps that macOS would not let Peel read.
private(set) var unreadableFolders: [String] = []
+ private(set) var notRead: [URL] = []
var selectedURLs: Set<URL> = [] {
didSet { updateSelectedSize() }
}
@@ -58,11 +59,15 @@ final class CloudLibrary {
self.removeDownloads = removeDownloads
}
+ var hasMoreThanItRead: Bool {
+ cutShort != nil || !unreadableFolders.isEmpty || !notRead.isEmpty
+ }
+
/// What the listed files take on this Mac, only the least it can be when there is more than the list shows, and
/// nil until iCloud Drive has been read.
var total: SizeTotal? {
guard let files, !couldNotRead else { return nil }
- return SizeTotal(known: files.map(\.size).cappedSum, isComplete: cutShort == nil && unreadableFolders.isEmpty)
+ return SizeTotal(known: files.map(\.size).cappedSum, isComplete: !hasMoreThanItRead)
}
/// The files whose folder or path holds `query`, every file for an empty one.
@@ -103,6 +108,7 @@ final class CloudLibrary {
sharedDownloads = found.sharedDownloads
isNotSetUp = found.isNotSetUp
unreadableFolders = found.unreadableFolders
+ notRead = found.notRead
selectedURLs.formIntersection(Set(found.files.map(\.url)))
}
diff --git a/Peel/Views/Cloud/CloudView.swift b/Peel/Views/Cloud/CloudView.swift
index 2e89952f..ee40558f 100644
--- a/Peel/Views/Cloud/CloudView.swift
+++ b/Peel/Views/Cloud/CloudView.swift
@@ -25,6 +25,9 @@ struct CloudView: View {
header
.listRowSeparator(.hidden)
RemovalsHeldBanner(recordsInHistory: false)
+ if !cloud.notRead.isEmpty {
+ UnreadableFoldersNotice(folders: cloud.notRead)
+ }
if !cloud.refusals.isEmpty {
Section {
@@ -136,7 +139,7 @@ struct CloudView: View {
} actions: {
Button("Open System Settings") { home.openFullDiskAccessSettings() }
}
- } else if cloud.files?.isEmpty == true, hasMoreThanItRead {
+ } else if cloud.files?.isEmpty == true, cloud.hasMoreThanItRead {
// The scan stopped early, so an empty list does not mean there is nothing to free.
ContentUnavailableView {
Label("Nothing Found So Far", systemImage: "icloud")
@@ -146,8 +149,15 @@ struct CloudView: View {
Text("There is more in iCloud Drive than Peel can read in the time it gives a scan, and nothing in the part it read is big enough to be worth freeing.")
}
unreadableFoldersNote
+ if !cloud.notRead.isEmpty {
+ Text(verbatim: UnreadableFoldersNotice.detail(
+ naming: cloud.notRead.map(\.abbreviatedPath), needsFullDiskAccess: false
+ ))
+ }
sharedDownloadsNote
}
+ } actions: {
+ UnreadableFoldersActions(folders: cloud.notRead, needsFullDiskAccess: false)
}
} else if cloud.files?.isEmpty == true {
ContentUnavailableView {
@@ -235,10 +245,6 @@ struct CloudView: View {
}
}
- private var hasMoreThanItRead: Bool {
- cloud.cutShort != nil || !cloud.unreadableFolders.isEmpty
- }
-
@ViewBuilder private var unreadableFoldersNote: some View {
if !cloud.unreadableFolders.isEmpty {
Text("macOS didn’t let Peel read \(cloud.unreadableFolders.formatted(.list(type: .and))) in iCloud Drive, so this list isn’t all of it. Full Disk Access in System Settings gives Peel what it needs.")
@@ -278,7 +284,7 @@ struct CloudView: View {
private var couldNotLook: LocalizedStringResource? {
if cloud.couldNotRead { return "iCloud Drive Couldn’t Be Read" }
- if cloud.files?.isEmpty == true, hasMoreThanItRead { return "Nothing Found So Far" }
+ if cloud.files?.isEmpty == true, cloud.hasMoreThanItRead { return "Nothing Found So Far" }
return nil
}
diff --git a/PeelTests/CloudLibraryTests.swift b/PeelTests/CloudLibraryTests.swift
index c9d5ff48..c9261c88 100644
--- a/PeelTests/CloudLibraryTests.swift
+++ b/PeelTests/CloudLibraryTests.swift
@@ -28,6 +28,18 @@ struct CloudLibraryTests {
#expect(cloud.total?.isComplete == false)
}
+ @Test func aFolderInsideThatCouldNotBeReadLeavesTheTotalOnlyTheLeastItCanBe() async {
+ let locked = URL(filePath: "/org.example/Locked")
+ let cloud = CloudLibrary(scan: { _, _, _ in
+ CloudScan(files: [Self.download("Plan.pdf")], notRead: [locked])
+ })
+
+ await cloud.refresh()
+
+ #expect(cloud.notRead == [locked])
+ #expect(cloud.total?.isComplete == false)
+ }
+
@Test func hasNoTotalUntilICloudDriveIsRead() async throws {
let home = FileManager.default.temporaryDirectory.appending(path: "PeelTests-\(UUID().uuidString)")
let drive = home.appending(path: "Library/Mobile Documents", directoryHint: .isDirectory)