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.

A file of Peel's it cannot reach is never read as empty

Țugui Dragoș77582c686e94
diff --git a/Packages/PeelCore/Sources/PeelCore/Apps/AppMemory.swift b/Packages/PeelCore/Sources/PeelCore/Apps/AppMemory.swift
index c966c7a9..31145c0d 100644
--- a/Packages/PeelCore/Sources/PeelCore/Apps/AppMemory.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Apps/AppMemory.swift
@@ -40,7 +40,7 @@ public struct AppMemory: Sendable {
FileLock.whileHeld(beside: url) { remember(apps, now: now, onTopOf: read()) }
}
- /// `known` is nil when the file exists but cannot be read. Its contents are then unknown, so it is never
+ /// `known` is nil when the file cannot be read or reached. Its contents are then unknown, so it is never
/// written over, and the apps currently installed are still returned for this run.
private func remember(_ apps: [InstalledApp], now: Date, onTopOf known: [RememberedApp]?) -> [RememberedApp] {
var byIdentifier = Dictionary((known ?? []).map { ($0.bundleIdentifier, $0) }, uniquingKeysWith: { first, _ in first })
@@ -62,11 +62,11 @@ public struct AppMemory: Sendable {
return kept
}
- /// Returns nil when the file exists but cannot be read. The file is the only record of apps that are gone,
+ /// Returns nil when the file cannot be read or reached. The file is the only record of apps that are gone,
/// so a row that cannot be decoded costs only that row. The damaged file is first kept under another name,
/// and when that fails, this returns nil so the file is left alone.
private func read() -> [RememberedApp]? {
- guard FileManager.default.fileExists(atPath: url.path(percentEncoded: false)) else { return [] }
+ guard !url.isMissing else { return [] }
guard let data = BoundedRead.data(at: url) else { return nil }
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .iso8601
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/RefusalLog.swift b/Packages/PeelCore/Sources/PeelCore/Removal/RefusalLog.swift
index 1c71f83c..431f8f3b 100644
--- a/Packages/PeelCore/Sources/PeelCore/Removal/RefusalLog.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/RefusalLog.swift
@@ -58,7 +58,7 @@ public actor RefusalLog {
public func clear() -> Bool {
FileLock.whileHeld(beside: url) {
- guard url.isThere else { return true }
+ guard !url.isMissing else { return true }
return (try? FileManager.default.removeItem(at: url)) != nil
}
}
@@ -66,7 +66,7 @@ public actor RefusalLog {
/// The records in the file. A file that fails to decode is set aside and its readable rows kept. Nil when the
/// file could not be read or set aside, so it must not be written over.
private func current() -> [RefusalRecord]? {
- guard url.isThere else { return [] }
+ guard !url.isMissing else { return [] }
guard let data = BoundedRead.data(at: url, maximum: 16 * 1_024 * 1_024) else { return nil }
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = LogTime.decoding
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/RemovalLog.swift b/Packages/PeelCore/Sources/PeelCore/Removal/RemovalLog.swift
index 8d87d747..f4b7f748 100644
--- a/Packages/PeelCore/Sources/PeelCore/Removal/RemovalLog.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/RemovalLog.swift
@@ -82,7 +82,7 @@ public actor RemovalLog {
/// The records on disk, or nil when the file must not be replaced because it could not be read.
private func current() -> [RemovalRecord]? {
- guard FileManager.default.fileExists(atPath: url.path(percentEncoded: false)) else {
+ guard !url.isMissing else {
note(nil)
return []
}
diff --git a/Packages/PeelCore/Sources/PeelCore/Support/Exclusions.swift b/Packages/PeelCore/Sources/PeelCore/Support/Exclusions.swift
index 5006c179..fdda4345 100644
--- a/Packages/PeelCore/Sources/PeelCore/Support/Exclusions.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Support/Exclusions.swift
@@ -184,8 +184,8 @@ public struct ExclusionStore: Sendable {
case notSaved(Exclusions)
}
- /// Loads the saved list. No file means an empty list. A file that is there but cannot be read gives
- /// `.unreadable`, never an empty list, so what the user excluded stays protected.
+ /// Loads the saved list. No file means an empty list. A file that cannot be read or reached gives `.unreadable`,
+ /// never an empty list, so what the user excluded stays protected.
@concurrent
public func load() async -> Exclusions {
read()
@@ -220,7 +220,7 @@ public struct ExclusionStore: Sendable {
}
private func read() -> Exclusions {
- guard FileManager.default.fileExists(atPath: url.path(percentEncoded: false)) else { return .none }
+ guard !url.isMissing else { return .none }
guard let data = BoundedRead.data(at: url), let saved = try? JSONDecoder().decode(Exclusions.self, from: data) else {
return .unreadable
}
diff --git a/Packages/PeelCore/Sources/PeelCore/Support/RealFolder.swift b/Packages/PeelCore/Sources/PeelCore/Support/RealFolder.swift
index 2bbdf640..fa5aec49 100644
--- a/Packages/PeelCore/Sources/PeelCore/Support/RealFolder.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Support/RealFolder.swift
@@ -8,6 +8,13 @@ extension URL {
return lstat(path(percentEncoded: false), &info) == 0
}
+ /// True only when nothing is there under this name. A name macOS cannot look up, as in a folder that cannot be
+ /// searched, is not missing, so one of Peel's files it cannot reach is never read as one that is not there.
+ var isMissing: Bool {
+ var info = stat()
+ return lstat(path(percentEncoded: false), &info) != 0 && errno == ENOENT
+ }
+
/// True for a folder that is there in its own right: a symbolic link to one does not count.
var isRealFolder: Bool {
let values = try? resourceValues(forKeys: [.isDirectoryKey, .isSymbolicLinkKey])
diff --git a/Packages/PeelCore/Sources/PeelCore/Updates/TeamRegistry.swift b/Packages/PeelCore/Sources/PeelCore/Updates/TeamRegistry.swift
index 5fe2b6de..1bb77635 100644
--- a/Packages/PeelCore/Sources/PeelCore/Updates/TeamRegistry.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Updates/TeamRegistry.swift
@@ -85,10 +85,10 @@ public struct TeamRegistry: Sendable {
}
}
- /// Returns nil for a file that exists but cannot be read or decoded. Rebuilt from the apps as they are, the
+ /// Returns nil for a file that cannot be read, reached or decoded. Rebuilt from the apps as they are, the
/// registry would silently accept any change of team made since it was last read.
private func load() -> [String: Entry]? {
- guard FileManager.default.fileExists(atPath: url.path(percentEncoded: false)) else { return [:] }
+ guard !url.isMissing else { return [:] }
guard let data = BoundedRead.data(at: url) else { return nil }
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .iso8601
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/AppMemoryTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/AppMemoryTests.swift
index 25fc3c3e..c0b5843b 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/AppMemoryTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/AppMemoryTests.swift
@@ -25,6 +25,19 @@ struct AppMemoryTests {
#expect(Set(second.map(\.bundleIdentifier)) == ["net.shinyfrog.bear", "dev.zed.Zed"], "an app it saw before was forgotten")
}
+ /// A memory that leads nowhere cannot be read, so it is left as it is rather than replaced by a new file.
+ @Test func leavesAMemoryItCannotReachAsItIs() async throws {
+ let directory = try TemporaryDirectory()
+ let url = directory.url.appending(path: "apps.json")
+ let path = url.path(percentEncoded: false)
+ try FileManager.default.createSymbolicLink(atPath: path, withDestinationPath: "/nowhere/apps.json")
+
+ _ = await AppMemory(url: url).remember([app("Bear", "net.shinyfrog.bear")])
+
+ let kind = try FileManager.default.attributesOfItem(atPath: path)[.type] as? FileAttributeType
+ #expect(kind == .typeSymbolicLink, "the memory was written over")
+ }
+
/// macOS's own apps come and go with the system, so remembering them says nothing useful.
@Test func leavesTheSystemsOwnAppsOut() async throws {
let directory = try TemporaryDirectory()
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/ExclusionsTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/ExclusionsTests.swift
index 5b1b63b3..c469878d 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/ExclusionsTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/ExclusionsTests.swift
@@ -237,6 +237,21 @@ struct ExclusionsTests {
#expect(result.failures.map(\.reason) == [.protectedLocation])
}
+ /// A list Peel cannot reach is not known to be empty: a folder on the way that cannot be searched, or a link
+ /// that leads nowhere, reads as a list that cannot be read, so nothing moves.
+ @Test func aListItCannotReachIsUnreadableNotEmpty() async throws {
+ let directory = try TemporaryDirectory()
+ let saved = Data(#"{"paths":[],"bundleIdentifiers":["com.example.app"]}"#.utf8)
+ let file = try directory.file("Peel/exclusions.json", contents: saved)
+ let link = directory.url.appending(path: "linked.json")
+ try FileManager.default.createSymbolicLink(at: link, withDestinationURL: URL(filePath: "/nowhere/list.json"))
+ try directory.setPermissions(0o600, of: "Peel")
+ defer { try? directory.setPermissions(0o755, of: "Peel") }
+
+ #expect(await ExclusionStore(url: file).load().isUnreadable)
+ #expect(await ExclusionStore(url: link).load().isUnreadable)
+ }
+
/// Until the app has read the saved list, what the user excluded is not known, so nothing moves, as for a list
/// that cannot be read.
@Test func movesNothingBeforeTheListIsRead() async throws {
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/RefusalLogTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/RefusalLogTests.swift
index 71dc274e..f8333bfa 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/RefusalLogTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/RefusalLogTests.swift
@@ -93,6 +93,17 @@ struct RefusalLogTests {
#expect(try Data(contentsOf: url) == Data("[]".utf8), "an unreadable record was written over")
}
+ /// A record in a folder that cannot be searched may be there, so it is not called cleared.
+ @Test func aRecordItCannotReachIsNotClearedAway() async throws {
+ let directory = try TemporaryDirectory()
+ let url = try directory.directory("Peel").appending(path: "refusals.json")
+ try Data("[]".utf8).write(to: url)
+ try directory.setPermissions(0o600, of: "Peel")
+ defer { try? directory.setPermissions(0o755, of: "Peel") }
+
+ #expect(await RefusalLog(url: url).clear() == false)
+ }
+
@Test func clearForgetsEverything() async throws {
let directory = try TemporaryDirectory()
let log = RefusalLog(url: directory.url.appending(path: "Peel/refusals.json"))
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/RemovalHistoryTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/RemovalHistoryTests.swift
index e65671a5..faf1f4fc 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/RemovalHistoryTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/RemovalHistoryTests.swift
@@ -272,6 +272,19 @@ struct RemovalHistoryTests {
#expect(outcome.problem == .unreadable)
}
+ /// A log in a folder that cannot be searched is not known to be empty, so it is unreadable, never replaced.
+ @Test func aLogItCannotReachIsUnreadableNotEmpty() async throws {
+ let directory = try TemporaryDirectory()
+ let url = try directory.file("Peel/removals.json", contents: Data("[]".utf8))
+ try directory.setPermissions(0o600, of: "Peel")
+ defer { try? directory.setPermissions(0o755, of: "Peel") }
+
+ let outcome = await RemovalLog(url: url).add([Self.record(batch: UUID())])
+
+ #expect(outcome.records == nil)
+ #expect(outcome.problem == .unreadable)
+ }
+
/// History showing "3 items" for a removal that touched twelve is worse than showing nothing: the user
/// cannot tell which nine are missing. Whole batches are dropped, never part of one.
@Test func recordsAWholeBatchEvenWhenItIsLargerThanTheCap() async throws {
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/UpdateTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/UpdateTests.swift
index 2e9d6bb1..8865006d 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/UpdateTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/UpdateTests.swift
@@ -250,6 +250,19 @@ struct TeamRegistryTests {
InstalledApp(url: URL(filePath: "/Applications/\(identifier).app"), bundleIdentifier: identifier, name: identifier, teamIdentifier: team)
}
+ /// A registry that leads nowhere cannot be read, so it reports nothing and is left as it is.
+ @Test func leavesARegistryItCannotReachAsItIs() async throws {
+ let directory = try TemporaryDirectory()
+ let url = directory.url.appending(path: "teams.json")
+ let path = url.path(percentEncoded: false)
+ try FileManager.default.createSymbolicLink(atPath: path, withDestinationPath: "/nowhere/teams.json")
+
+ #expect(await TeamRegistry(url: url).check([app("com.example.app", team: "AAAA111111")]).isEmpty)
+
+ let kind = try FileManager.default.attributesOfItem(atPath: path)[.type] as? FileAttributeType
+ #expect(kind == .typeSymbolicLink, "the registry was written over")
+ }
+
/// A change of signer is the one security warning the Applications tool gives. It is kept on disk and
/// reported at every launch until the user acknowledges it.
@Test func keepsSayingAnAppChangedHandsUntilItIsAcknowledged() async throws {