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.

Read an app folder that is a link

Țugui Dragoșea4479db8319
diff --git a/Packages/PeelCore/Sources/PeelCore/Apps/AppCatalog.swift b/Packages/PeelCore/Sources/PeelCore/Apps/AppCatalog.swift
index deb2b3c8..dfb22f3c 100644
--- a/Packages/PeelCore/Sources/PeelCore/Apps/AppCatalog.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Apps/AppCatalog.swift
@@ -140,7 +140,7 @@ public enum AppCatalog {
}
guard !Task.isCancelled else { return AppScan(apps: [], unreadable: [], needsFullDiskAccess: false) }
- Reading.forget(inside: answered.map(\.0), except: bundles)
+ Reading.forget(inside: answered.map { followingALink($0.0) }, except: bundles)
let closedApps = read.filter { $0.app == nil && AppInspector.isClosed($0.bundle) }.map(\.bundle)
let refused = answered.flatMap(\.1.unreadable) + closedApps
return AppScan(
@@ -240,14 +240,20 @@ public enum AppCatalog {
return values?.isHidden != true && values?.isPackage != true
}
+ /// What is read for `directory`: the enumerator doesn't follow a link it starts from, answering "Not a directory".
+ private static func followingALink(_ directory: URL) -> URL {
+ directory.isSymbolicLink ? PathPattern.canonical(directory) : directory
+ }
+
/// The apps in `directory`, `depth` levels deep, and the folders in it that are there and could not be read. A
/// folder that is not there, such as an Applications folder nobody made, holds no app.
static func appBundles(
in directory: URL, unless isStopped: () -> Bool = { false }
) -> (bundles: [URL], unreadable: [URL]) {
var unreadable: [URL] = []
+ guard !directory.isALinkThatLeadsNowhere else { return ([], []) }
guard let enumerator = FileManager.default.enumerator(
- at: directory,
+ at: followingALink(directory),
includingPropertiesForKeys: [.isDirectoryKey],
options: [.skipsHiddenFiles, .skipsPackageDescendants],
errorHandler: { url, error in
diff --git a/Packages/PeelCore/Sources/PeelCore/Apps/AppFolders.swift b/Packages/PeelCore/Sources/PeelCore/Apps/AppFolders.swift
index 237b59f0..db9dd6f8 100644
--- a/Packages/PeelCore/Sources/PeelCore/Apps/AppFolders.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Apps/AppFolders.swift
@@ -75,6 +75,11 @@ public struct AppFolders: Sendable {
}
static func refusal(of folder: URL, home: URL, isInTheCloud: (URL) -> Bool) -> Refusal? {
+ // A link is read as where it leads, so that is what it is judged as too.
+ if folder.isSymbolicLink,
+ let refusal = refusal(of: PathPattern.canonical(folder), home: home, isInTheCloud: isInTheCloud) {
+ return refusal
+ }
let path = folded(folder)
let library = folded(home) + "/library"
let broad = [
diff --git a/Packages/PeelCore/Sources/PeelCore/Support/RealFolder.swift b/Packages/PeelCore/Sources/PeelCore/Support/RealFolder.swift
index 58d001a8..010870bc 100644
--- a/Packages/PeelCore/Sources/PeelCore/Support/RealFolder.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Support/RealFolder.swift
@@ -9,6 +9,18 @@ extension URL {
return lstat(path(percentEncoded: false), &info) == 0
}
+ /// `lstat` follows a link named with a slash after it, as a URL marked as a directory is.
+ var isSymbolicLink: Bool {
+ var info = stat()
+ return lstat(PathPattern.comparablePath(of: self), &info) == 0 && info.st_mode & S_IFMT == S_IFLNK
+ }
+
+ /// True for a link whose destination isn't there, as on a disk that isn't connected.
+ var isALinkThatLeadsNowhere: Bool {
+ var info = stat()
+ return isSymbolicLink && stat(path(percentEncoded: false), &info) != 0 && errno == ENOENT
+ }
+
/// 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.
public var isMissing: Bool {
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/AppCatalogTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/AppCatalogTests.swift
index 4c3ef278..c295483c 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/AppCatalogTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/AppCatalogTests.swift
@@ -4,6 +4,39 @@ import Synchronization
import Testing
struct AppCatalogTests {
+ @Test func readsAFolderGivenAsALink() throws {
+ let directory = try TemporaryDirectory()
+ try directory.directory("elsewhere/Example.app")
+ try directory.directory("elsewhere/Tools/Other.app")
+ let target = directory.url.appending(path: "elsewhere", directoryHint: .isDirectory)
+ let link = directory.url.appending(path: "Applications", directoryHint: .isDirectory)
+ try FileManager.default.createSymbolicLink(
+ atPath: directory.url.appending(path: "Applications").path(percentEncoded: false),
+ withDestinationPath: "elsewhere"
+ )
+
+ let found = AppCatalog.appBundles(in: link)
+
+ #expect(found.unreadable.isEmpty)
+ #expect(Set(found.bundles.map(\.lastPathComponent)) == ["Example.app", "Other.app"])
+ let place = PathPattern.comparablePath(of: target)
+ #expect(found.bundles.allSatisfy { $0.path(percentEncoded: false).hasPrefix(place) })
+ }
+
+ @Test func aLinkThatLeadsNowhereHoldsNoApp() throws {
+ let directory = try TemporaryDirectory()
+ let link = directory.url.appending(path: "Applications", directoryHint: .isDirectory)
+ try FileManager.default.createSymbolicLink(
+ atPath: directory.url.appending(path: "Applications").path(percentEncoded: false),
+ withDestinationPath: "/Volumes/org-example-not-connected/Applications"
+ )
+
+ let found = AppCatalog.appBundles(in: link)
+
+ #expect(found.unreadable.isEmpty)
+ #expect(found.bundles.isEmpty)
+ }
+
private let yesterday = Date(timeIntervalSince1970: 1_800_000_000)
private let today = Date(timeIntervalSince1970: 1_800_086_400)
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/AppFoldersTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/AppFoldersTests.swift
index 6f42c0c2..8bc36526 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/AppFoldersTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/AppFoldersTests.swift
@@ -3,6 +3,25 @@ import Foundation
import Testing
struct AppFoldersTests {
+ @Test func aLinkIsJudgedByWhereItLeads() throws {
+ let directory = try TemporaryDirectory()
+ let home = try directory.directory("home")
+ try directory.directory("home/Applications")
+ try directory.directory("home/.Trash/Old")
+ try directory.directory("elsewhere")
+ func link(_ name: String, to destination: String) throws -> URL {
+ try FileManager.default.createSymbolicLink(
+ atPath: directory.url.appending(path: name).path(percentEncoded: false),
+ withDestinationPath: destination
+ )
+ return directory.url.appending(path: name, directoryHint: .isDirectory)
+ }
+
+ #expect(AppFolders.refusal(of: try link("one", to: "home/Applications"), home: home) == .alreadyLookedIn)
+ #expect(AppFolders.refusal(of: try link("two", to: "home/.Trash/Old"), home: home) == .inATrash)
+ #expect(AppFolders.refusal(of: try link("three", to: "elsewhere"), home: home) == nil)
+ }
+
/// A folder chosen for apps is looked through three levels deep. The whole disk, the home folder or a folder
/// Peel already looks in would only repeat or slow every reading of the apps.
@Test func refusesAFolderTooBroadOrAlreadyLookedIn() {
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/FolderWatchTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/FolderWatchTests.swift
index 7bf981cf..c41127ac 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/FolderWatchTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/FolderWatchTests.swift
@@ -33,6 +33,21 @@ struct FolderWatchTests {
#expect(await firstChange(of: changes, within: Self.patience), "no change arrived")
}
+ @Test func hearsWhatChangesWhereALinkLeads() async throws {
+ let directory = try TemporaryDirectory()
+ let target = try directory.directory("elsewhere")
+ try FileManager.default.createSymbolicLink(
+ atPath: directory.url.appending(path: "Applications").path(percentEncoded: false),
+ withDestinationPath: "elsewhere"
+ )
+ let link = directory.url.appending(path: "Applications", directoryHint: .isDirectory)
+ let changes = FolderWatch.changes(in: [link])
+
+ try FileManager.default.createDirectory(at: target.appending(path: "New.app"), withIntermediateDirectories: false)
+
+ #expect(await firstChange(of: changes, within: Self.patience), "no change arrived")
+ }
+
/// The control: without this, a watcher that reported constantly would pass the tests above.
@Test func staysSilentWhileNothingHappens() async throws {
let directory = try TemporaryDirectory()