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 how far Remove Downloads got in iCloud Drive and let it stop

Țugui Dragoș2313353ea8a0
diff --git a/Localization/Peel/Localizable.xcstrings b/Localization/Peel/Localizable.xcstrings
index 6aa4ab25..c07b3ee0 100644
--- a/Localization/Peel/Localizable.xcstrings
+++ b/Localization/Peel/Localizable.xcstrings
@@ -125069,6 +125069,112 @@
}
}
},
+ "Removed %@ of %@" : {
+ "localizations" : {
+ "cs" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Odstraněno %@ z %@"
+ }
+ },
+ "de" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "%@ von %@ entfernt"
+ }
+ },
+ "es" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "%@ de %@ eliminadas"
+ }
+ },
+ "fr" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Retrait : %@ sur %@"
+ }
+ },
+ "it" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Rimossi %@ di %@"
+ }
+ },
+ "ja" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "削除済み: %@ / %@"
+ }
+ },
+ "ko" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "%@/%@ 제거됨"
+ }
+ },
+ "nl" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "%@ van %@ verwijderd"
+ }
+ },
+ "pl" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Usunięto %@ z %@"
+ }
+ },
+ "pt" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "%@ de %@ removidos"
+ }
+ },
+ "ro" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Eliminat %@ din %@"
+ }
+ },
+ "ru" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Удалено %@ из %@"
+ }
+ },
+ "sv" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Tagit bort %@ av %@"
+ }
+ },
+ "tr" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Kaldırıldı: %@ / %@"
+ }
+ },
+ "uk" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Вилучено %@ із %@"
+ }
+ },
+ "zh-Hans" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "已移除%@/%@"
+ }
+ },
+ "zh-Hant" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "已移除%@/%@"
+ }
+ }
+ }
+ },
"Removed with %@’s own uninstaller" : {
"localizations" : {
"cs" : {
@@ -147919,6 +148025,112 @@
}
}
},
+ "Stop removing downloads" : {
+ "localizations" : {
+ "cs" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Zastavit odstraňování stažených položek"
+ }
+ },
+ "de" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Entfernen der Downloads stoppen"
+ }
+ },
+ "es" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Detener la eliminación de descargas"
+ }
+ },
+ "fr" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Arrêter le retrait des téléchargements"
+ }
+ },
+ "it" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Interrompi la rimozione dei download"
+ }
+ },
+ "ja" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "ダウンロードしたものの削除を停止"
+ }
+ },
+ "ko" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "다운로드 제거 중단"
+ }
+ },
+ "nl" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Stop met verwijderen van downloads"
+ }
+ },
+ "pl" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Zatrzymaj usuwanie pobranych rzeczy"
+ }
+ },
+ "pt" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Parar de remover os downloads"
+ }
+ },
+ "ro" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Opriți eliminarea descărcărilor"
+ }
+ },
+ "ru" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Остановить удаление загрузок"
+ }
+ },
+ "sv" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Stoppa borttagningen av hämtningarna"
+ }
+ },
+ "tr" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "İndirilenleri kaldırmayı durdur"
+ }
+ },
+ "uk" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Зупинити вилучення викачувань"
+ }
+ },
+ "zh-Hans" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "停止移除下载"
+ }
+ },
+ "zh-Hant" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "停止移除下載項目"
+ }
+ }
+ }
+ },
"Stop the scan" : {
"localizations" : {
"cs" : {
diff --git a/Packages/PeelCore/Sources/PeelCore/Space/CloudStorage.swift b/Packages/PeelCore/Sources/PeelCore/Space/CloudStorage.swift
index 74e24ffc..0c9d2384 100644
--- a/Packages/PeelCore/Sources/PeelCore/Space/CloudStorage.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Space/CloudStorage.swift
@@ -54,6 +54,12 @@ public struct CloudRefusal: Sendable, Hashable, Identifiable {
public var id: URL { url }
}
+/// What freeing the local copies did.
+public struct CloudFreeing: Sendable {
+ public var freed: [CloudFile] = []
+ public var refused: [CloudRefusal] = []
+}
+
public enum CloudStorage {
/// A file that frees less than this isn't worth a row in a list about space.
public static let minimumSize: Int64 = 1_000_000
@@ -299,23 +305,28 @@ public enum CloudStorage {
}
}
- /// Frees the local copies of `files` and returns the ones that were not freed. Each file is checked again
- /// first, against `exclusions` as they are now as well: the list may have been on screen for hours, and a file
- /// edited since then may exist in its newest form only on the local disk.
+ /// Frees the local copies of `files`, one at a time. Each file is checked again first, against `exclusions` as
+ /// they are now as well: the list may have been on screen for hours, and a file edited since then may exist in
+ /// its newest form only on the local disk.
@discardableResult
@concurrent
- public static func free(_ files: [CloudFile], exclusions: Exclusions) async -> [CloudRefusal] {
+ public static func free(_ files: [CloudFile], exclusions: Exclusions) async -> CloudFreeing {
await free(files, exclusions: exclusions, isSafe: isSafeToFree(_:))
}
@concurrent
static func free(
- _ files: [CloudFile], exclusions: Exclusions, isSafe: (URLResourceValues) -> Bool
- ) async -> [CloudRefusal] {
- var refused: [CloudRefusal] = []
+ _ files: [CloudFile],
+ exclusions: Exclusions,
+ isSafe: (URLResourceValues) -> Bool,
+ evict: (URL) throws -> Void = { try FileManager.default.evictUbiquitousItem(at: $0) }
+ ) async -> CloudFreeing {
+ var done = CloudFreeing()
for file in files {
+ // A stop lets the file iCloud is answering for finish, and nothing after it starts.
+ if Task.isCancelled { break }
guard exclusions.isKnown, !exclusions.excludes(file.url) else {
- refused.append(CloudRefusal(url: file.url, reason: .excluded))
+ done.refused.append(CloudRefusal(url: file.url, reason: .excluded))
continue
}
var url = file.url
@@ -328,16 +339,18 @@ public enum CloudStorage {
: values.isPackage == true
&& package(url, values, exclusions: exclusions, unless: { false })?.modified == file.modified
else {
- refused.append(CloudRefusal(url: file.url, reason: .changedSinceScan))
+ done.refused.append(CloudRefusal(url: file.url, reason: .changedSinceScan))
continue
}
do {
- try FileManager.default.evictUbiquitousItem(at: file.url)
+ try evict(file.url)
+ done.freed.append(file)
+ MoveCount.current?.add(1)
} catch {
- refused.append(CloudRefusal(url: file.url, reason: reason(for: error)))
+ done.refused.append(CloudRefusal(url: file.url, reason: reason(for: error)))
}
}
- return refused
+ return done
}
/// Why freeing a file failed: `NSFileLockingError` when a program holds it, and macOS's own message otherwise.
diff --git a/Packages/PeelCore/Sources/PeelCore/Support/MoveCount.swift b/Packages/PeelCore/Sources/PeelCore/Support/MoveCount.swift
index d6cb9207..ff3e4f3f 100644
--- a/Packages/PeelCore/Sources/PeelCore/Support/MoveCount.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Support/MoveCount.swift
@@ -1,6 +1,7 @@
import Synchronization
-/// How many items a removal has moved so far, so a long one can say how far it got.
+/// How many items a removal has taken so far (moved to the Trash, or downloads removed from iCloud Drive), so a long
+/// one can say how far it got.
public final class MoveCount: Sendable {
private let moved = Atomic<Int>(0)
@@ -12,6 +13,6 @@ public final class MoveCount: Sendable {
moved.wrappingAdd(count, ordering: .relaxed)
}
- /// The count that moves in the current task add to, set by whoever runs the removal.
+ /// The count the removal in the current task adds to, set by whoever runs it.
@TaskLocal public static var current: MoveCount?
}
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/CloudStorageTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/CloudStorageTests.swift
index e62d816c..b7fa37ed 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/CloudStorageTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/CloudStorageTests.swift
@@ -70,7 +70,7 @@ struct CloudStorageTests {
let url = try directory.file("file.bin", bytes: 2_000_000)
let scanned = CloudFile(url: url, name: "file.bin", container: "iCloud Drive", size: 2_000_000, modified: .distantPast)
- let refused = await CloudStorage.free([scanned], exclusions: .none)
+ let refused = await CloudStorage.free([scanned], exclusions: .none).refused
#expect(refused.map(\.url) == [url])
#expect(refused.first?.reason == .changedSinceScan)
@@ -85,9 +85,9 @@ struct CloudStorageTests {
let scanned = CloudFile(url: url, name: "file.bin", container: "iCloud Drive", size: 2_000_000, modified: .distantPast)
let offline = Exclusions(paths: [directory.url.appending(path: "Offline", directoryHint: .isDirectory)])
- #expect(await CloudStorage.free([scanned], exclusions: offline).map(\.reason) == [.excluded])
- #expect(await CloudStorage.free([scanned], exclusions: .notYetRead).map(\.reason) == [.excluded])
- #expect(await CloudStorage.free([scanned], exclusions: .unreadable).map(\.reason) == [.excluded])
+ #expect(await CloudStorage.free([scanned], exclusions: offline).refused.map(\.reason) == [.excluded])
+ #expect(await CloudStorage.free([scanned], exclusions: .notYetRead).refused.map(\.reason) == [.excluded])
+ #expect(await CloudStorage.free([scanned], exclusions: .unreadable).refused.map(\.reason) == [.excluded])
}
@Test func callsEachFolderWhatTheUserCallsIt() {
@@ -405,15 +405,55 @@ struct CloudStorageTests {
)
let scanned = try #require(collector.collected.files.first)
- let untouched = await CloudStorage.free([scanned], exclusions: .none, isSafe: { _ in true })
+ let untouched = await CloudStorage.free([scanned], exclusions: .none, isSafe: { _ in true }).refused
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 })
+ let edited = await CloudStorage.free([scanned], exclusions: .none, isSafe: { _ in true }).refused
#expect(untouched.map(\.reason) != [.changedSinceScan], "only iCloud can remove a download, so a test's fails")
#expect(edited.map(\.reason) == [.changedSinceScan])
}
+ @Test func stopsBetweenFilesOnceItsTaskIsCancelled() async throws {
+ let directory = try TemporaryDirectory()
+ let files = try downloads(3, in: directory)
+
+ let done = await Task {
+ await CloudStorage.free(files, exclusions: .none, isSafe: { _ in true }) { _ in
+ withUnsafeCurrentTask { $0?.cancel() }
+ }
+ }.value
+
+ #expect(done.freed.map(\.url) == [files[0].url])
+ #expect(done.refused.isEmpty)
+ }
+
+ @Test func countsEachFileItFrees() async throws {
+ let directory = try TemporaryDirectory()
+ let files = try downloads(3, in: directory)
+ let counted = MoveCount()
+
+ let done = await MoveCount.$current.withValue(counted) {
+ await CloudStorage.free(files, exclusions: .none, isSafe: { _ in true }) { url in
+ if url == files[2].url { throw CocoaError(.fileLocking) }
+ }
+ }
+
+ #expect(done.freed.map(\.url) == [files[0].url, files[1].url])
+ #expect(done.refused.map(\.reason) == [.inUse])
+ #expect(counted.value == 2)
+ }
+
+ private func downloads(_ count: Int, in directory: borrowing TemporaryDirectory) throws -> [CloudFile] {
+ try (1...count).map { index in
+ let url = try directory.file("file\(index).bin", bytes: 2_000_000)
+ let modified = try url.resourceValues(forKeys: [.contentModificationDateKey]).contentModificationDate
+ return CloudFile(
+ url: url, name: url.lastPathComponent, container: "iCloud Drive", size: 2_000_000, modified: modified
+ )
+ }
+ }
+
/// A file a program holds, as the desktop picture is held, cannot be locked, and the person is told so.
@Test func aFileAProgramHoldsIsRefusedAsInUse() {
let locked = CocoaError(.fileLocking)
diff --git a/Peel/Model/CloudLibrary.swift b/Peel/Model/CloudLibrary.swift
index 78297190..3ffea0dc 100644
--- a/Peel/Model/CloudLibrary.swift
+++ b/Peel/Model/CloudLibrary.swift
@@ -25,6 +25,9 @@ final class CloudLibrary {
let scanRun = ScanRun()
var isScanning: Bool { scanRun.isRunning }
private(set) var isFreeing = false
+ /// The downloads the running Remove Downloads has removed so far.
+ let freeing = MoveProgress()
+ @ObservationIgnored private var freeingTask: Task<CloudFreeing, Never>?
private(set) var refusals: [CloudRefusal] = []
/// `wasCutShort`: there is more than the list shows. `couldNotRead`: iCloud Drive couldn't be read at all.
private(set) var wasCutShort = false
@@ -80,16 +83,25 @@ final class CloudLibrary {
selectedURLs.formIntersection(Set(scan.files.map(\.url)))
}
- /// Frees the local copies of the selected files and returns the bytes freed: the selection less what was
- /// refused. Nothing goes to the Trash, since each file stays in iCloud and there is nothing to put back.
+ /// Frees the local copies of the selected files and returns the bytes freed. Nothing goes to the Trash, since
+ /// each file stays in iCloud and there is nothing to put back.
func freeSelected() async -> Int64 {
isFreeing = true
defer { isFreeing = false }
let chosen = files?.filter { selectedURLs.contains($0.url) } ?? []
- let refused = await CloudStorage.free(chosen, exclusions: ExclusionsStore.shared.exclusions)
- let kept = Set(refused.map(\.url))
+ let exclusions = ExclusionsStore.shared.exclusions
+ let task = Task { [freeing] in
+ await freeing.run(toMove: chosen.count) { await CloudStorage.free(chosen, exclusions: exclusions) }
+ }
+ freeingTask = task
+ let done = await task.value
+ freeingTask = nil
await refresh()
- refusals = refused
- return chosen.filter { !kept.contains($0.url) }.map(\.size).cappedSum
+ refusals = done.refused
+ return done.freed.map(\.size).cappedSum
+ }
+
+ func stopFreeing() {
+ freeingTask?.cancel()
}
}
diff --git a/Peel/Views/Cloud/CloudView.swift b/Peel/Views/Cloud/CloudView.swift
index e1cb03cb..c490884a 100644
--- a/Peel/Views/Cloud/CloudView.swift
+++ b/Peel/Views/Cloud/CloudView.swift
@@ -136,17 +136,32 @@ struct CloudView: View {
}
.edgeBar(.bottom) {
if cloud.files?.isEmpty == false {
- RemovalBar(
- selectedSize: cloud.selectedSize,
- isScanning: cloud.isScanning,
- isWorking: cloud.isFreeing,
- scan: cloud.scanRun,
- isEnabled: cloud.selectedSize > 0 && !cloud.isFreeing,
- onRemove: { isConfirming = true },
- title: "Remove Downloads",
- systemImage: "icloud.and.arrow.down",
- notice: freed
- )
+ if #available(macOS 27, *) {
+ RemovalBar(
+ selectedSize: cloud.selectedSize,
+ isScanning: cloud.isScanning,
+ isWorking: cloud.isFreeing,
+ scan: cloud.scanRun,
+ isEnabled: cloud.selectedSize > 0 && !cloud.isFreeing,
+ onRemove: { isConfirming = true },
+ title: "Remove Downloads",
+ systemImage: "icloud.and.arrow.down",
+ notice: freed,
+ removal: (cloud.freeing, cloud.stopFreeing)
+ )
+ } else {
+ RemovalBar(
+ selectedSize: cloud.selectedSize,
+ isScanning: cloud.isScanning,
+ isWorking: cloud.isFreeing,
+ scan: cloud.scanRun,
+ isEnabled: cloud.selectedSize > 0 && !cloud.isFreeing,
+ onRemove: { isConfirming = true },
+ title: "Remove Downloads",
+ systemImage: "icloud.and.arrow.down",
+ notice: freed
+ )
+ }
}
}
.fadesInColumn(whenRowsChange: cloud.files?.map(\.id))
diff --git a/Peel/Views/RemovalBar.swift b/Peel/Views/RemovalBar.swift
index 7f98b54e..e72f1172 100644
--- a/Peel/Views/RemovalBar.swift
+++ b/Peel/Views/RemovalBar.swift
@@ -35,6 +35,8 @@ struct RemovalBar: View {
var progress: MoveProgress?
/// The apps the page's removal waits for to quit.
var waitingToQuit: String?
+ /// iCloud Drive's Remove Downloads: on macOS 27 the spinner says how far it got, and the button stops it.
+ var removal: (progress: MoveProgress, stop: () -> Void)?
private enum Purpose {
/// What the page itself selected, which `onRemove` acts on.
@@ -67,7 +69,8 @@ struct RemovalBar: View {
systemImage: String = "trash",
notice: BarNotice? = nil,
progress: MoveProgress? = nil,
- waitingToQuit: String? = nil
+ waitingToQuit: String? = nil,
+ removal: (progress: MoveProgress, stop: () -> Void)? = nil
) {
purpose = .own(
SizeTotal(known: selectedSize, isComplete: isSelectionMeasured),
@@ -82,6 +85,7 @@ struct RemovalBar: View {
self.notice = notice
self.progress = progress
self.waitingToQuit = waitingToQuit
+ self.removal = removal
}
/// A bar for a page of the storage tools, whose selection travels to the others' pages.
@@ -155,6 +159,10 @@ struct RemovalBar: View {
if isShowingScan {
ReadSoFar(scan: scan)
.foregroundStyle(.secondary)
+ } else if #available(macOS 27, *), let removal, removal.progress.isMoving {
+ Text("Removed \(removal.progress.moved, format: .number) of \(removal.progress.toMove, format: .number)")
+ .monospacedDigit()
+ .foregroundStyle(.secondary)
}
} else if isShowingMove, let moving {
ProgressView()
@@ -197,8 +205,13 @@ struct RemovalBar: View {
.foregroundStyle(.secondary)
}
} action: {
- Button(String(localized: title), systemImage: systemImage, action: reading.remove)
- .disabled(!isEnabled)
+ if #available(macOS 27, *), isShowingWork, let removal, removal.progress.isMoving {
+ Button("Stop", systemImage: "xmark", action: removal.stop)
+ .help(Text("Stop removing downloads"))
+ } else {
+ Button(String(localized: title), systemImage: systemImage, action: reading.remove)
+ .disabled(!isEnabled)
+ }
}
.motion(value: isShowingMove)
.motion(value: isShowingWait)