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 why a launch agent's file couldn't be opened to edit

Țugui Dragoș4e587f97cd3b
diff --git a/Localization/Peel/Localizable.xcstrings b/Localization/Peel/Localizable.xcstrings
index 1f556628..3c6ea730 100644
--- a/Localization/Peel/Localizable.xcstrings
+++ b/Localization/Peel/Localizable.xcstrings
@@ -141544,6 +141544,112 @@
}
}
},
+ "Peel couldn’t read this file: %@" : {
+ "localizations" : {
+ "cs" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Peel nemohl přečíst tento soubor: %@"
+ }
+ },
+ "de" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Peel konnte diese Datei nicht lesen: %@"
+ }
+ },
+ "es" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Peel no ha podido leer este archivo: %@"
+ }
+ },
+ "fr" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Peel n’a pas pu lire ce fichier : %@"
+ }
+ },
+ "it" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Peel non è riuscito a leggere questo file: %@"
+ }
+ },
+ "ja" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Peelはこのファイルを読み取れませんでした: %@"
+ }
+ },
+ "ko" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Peel이 이 파일을 읽을 수 없습니다: %@"
+ }
+ },
+ "nl" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Peel kon dit bestand niet lezen: %@"
+ }
+ },
+ "pl" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Peel nie mógł odczytać tego pliku: %@"
+ }
+ },
+ "pt" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "O Peel não conseguiu ler este arquivo: %@"
+ }
+ },
+ "ro" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Peel nu a putut citi acest fișier: %@"
+ }
+ },
+ "ru" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Peel не удалось прочитать этот файл: %@"
+ }
+ },
+ "sv" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Peel kunde inte läsa den här filen: %@"
+ }
+ },
+ "tr" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Peel bu dosyayı okuyamadı: %@"
+ }
+ },
+ "uk" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Peel не вдалося прочитати цей файл: %@"
+ }
+ },
+ "zh-Hans" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Peel未能读取此文件:%@"
+ }
+ },
+ "zh-Hant" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Peel無法讀取此檔案:%@"
+ }
+ }
+ }
+ },
"Peel couldn’t read your exclusions" : {
"localizations" : {
"cs" : {
diff --git a/Packages/PeelCore/Sources/PeelCore/BackgroundItems/LaunchAgentFile.swift b/Packages/PeelCore/Sources/PeelCore/BackgroundItems/LaunchAgentFile.swift
index 02e5a0a4..0db3e859 100644
--- a/Packages/PeelCore/Sources/PeelCore/BackgroundItems/LaunchAgentFile.swift
+++ b/Packages/PeelCore/Sources/PeelCore/BackgroundItems/LaunchAgentFile.swift
@@ -13,7 +13,8 @@ public struct LaunchAgentFile: Sendable {
case notAPlainFile
case notYours
case tooLarge
- case unreadable
+ /// macOS wouldn't let Peel read it, and said why when it did.
+ case unreadable(SystemError?)
/// The file changed since it was opened.
case changed
case notAPropertyList(line: Int?)
@@ -84,9 +85,10 @@ public struct LaunchAgentFile: Sendable {
}
guard info.st_uid == owner else { return .failure(.notYours) }
guard info.st_size <= maximumBytes else { return .failure(.tooLarge) }
- guard let (data, identity) = BoundedRead.contents(at: url, maximum: maximumBytes),
- identity.holdsTheSameContents(as: FileIdentity(info))
- else { return .failure(.unreadable) }
+ guard let (data, identity) = BoundedRead.contents(at: url, maximum: maximumBytes) else {
+ return .failure(.unreadable(BoundedRead.openError(of: url)))
+ }
+ guard identity.holdsTheSameContents(as: FileIdentity(info)) else { return .failure(.changed) }
var format = PropertyListSerialization.PropertyListFormat.xml
guard let plist = try? PropertyListSerialization.propertyList(from: data, options: [], format: &format),
(plist as? [String: Any])?["Label"] as? String == item.label
@@ -94,10 +96,13 @@ public struct LaunchAgentFile: Sendable {
let text: String
if format == .xml, let written = String(data: data, encoding: .utf8) {
text = written
- } else if let xml = try? PropertyListSerialization.data(fromPropertyList: plist, format: .xml, options: 0) {
- text = String(decoding: xml, as: UTF8.self)
} else {
- return .failure(.unreadable)
+ do {
+ let xml = try PropertyListSerialization.data(fromPropertyList: plist, format: .xml, options: 0)
+ text = String(decoding: xml, as: UTF8.self)
+ } catch {
+ return .failure(.unreadable(SystemError(error)))
+ }
}
return .success(
LaunchAgentFile(url: url, label: item.label, text: text, opened: identity, mode: info.st_mode & 0o7777)
diff --git a/Packages/PeelCore/Sources/PeelCore/Support/BoundedRead.swift b/Packages/PeelCore/Sources/PeelCore/Support/BoundedRead.swift
index dfa2e14f..bf7fef5e 100644
--- a/Packages/PeelCore/Sources/PeelCore/Support/BoundedRead.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Support/BoundedRead.swift
@@ -64,6 +64,16 @@ enum BoundedRead {
return Data(buffer[0..<filled])
}
+ /// What macOS answers when `url` is opened to be read, or nil when it opens: why a read found nothing.
+ static func openError(of url: URL) -> SystemError? {
+ let descriptor = open(url.path(percentEncoded: false), O_RDONLY | O_NONBLOCK | O_CLOEXEC)
+ guard descriptor < 0 else {
+ close(descriptor)
+ return nil
+ }
+ return SystemError(domain: NSPOSIXErrorDomain, code: Int(errno))
+ }
+
/// Whether `url` opens as a regular file of at most `maximum` bytes, which is what `data(at:maximum:)` asks
/// before it reads.
static func opens(_ url: URL, maximum: Int = maximumBytes) -> Bool {
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/LaunchAgentFileTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/LaunchAgentFileTests.swift
index 119dba78..4408bcf0 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/LaunchAgentFileTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/LaunchAgentFileTests.swift
@@ -128,6 +128,17 @@ struct LaunchAgentFileTests {
#expect(await LaunchAgentFile.open(agent(home.plist, source: .systemLibrary)).failure == .cannotBeEdited)
}
+ @Test func saysWhatMacOSAnsweredWhenTheFileCantBeRead() async throws {
+ let home = try Home()
+ chmod(home.plist.path(percentEncoded: false), 0)
+ defer { chmod(home.plist.path(percentEncoded: false), 0o644) }
+
+ #expect(
+ await LaunchAgentFile.open(agent(home.plist)).failure
+ == .unreadable(SystemError(domain: NSPOSIXErrorDomain, code: Int(EACCES)))
+ )
+ }
+
@Test func saysWhatKeepsAnEditFromBeingSaved() async throws {
let home = try Home()
let file = try await home.open(agent(home.plist))
diff --git a/Peel/Views/BackgroundItems/LaunchAgentEditor.swift b/Peel/Views/BackgroundItems/LaunchAgentEditor.swift
index 0b5b73d5..97f19921 100644
--- a/Peel/Views/BackgroundItems/LaunchAgentEditor.swift
+++ b/Peel/Views/BackgroundItems/LaunchAgentEditor.swift
@@ -133,7 +133,8 @@ extension LaunchAgentFile.Problem {
case .notYours: String(localized: "Another account owns this file, so Peel doesn’t edit it.")
case .tooLarge:
String(localized: "This file is larger than 1 MB, more than a job’s file holds, so Peel doesn’t open it.")
- case .unreadable: String(localized: "Peel couldn’t read this file.")
+ case .unreadable(let error?): String(localized: "Peel couldn’t read this file: \(error.localizedDescription)")
+ case .unreadable(nil): String(localized: "Peel couldn’t read this file.")
case .changed: String(localized: "The file changed since Peel read it. Close this and open it again.")
case .notAPropertyList(let line?): String(localized: "This isn’t a valid property list. Look at line \(line).")
case .notAPropertyList(nil): String(localized: "This isn’t a valid property list.")