From c110bd16b29608783408cbd0aa1636ae7e9f90a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Boris=20B=C3=BCgling?= Date: Tue, 15 Aug 2023 16:12:49 -0700 Subject: [PATCH] Use `URL.resourceValues()` for symlink detection (#428) This should provide a significant performance improvement on macOS. See [this](https://github.com/apple/swift-tools-support-core/pull/400#issuecomment-1618933610]) for more discussion on non-macOS platforms. Co-authored-by: Pyry Jahkola --- Sources/TSCBasic/FileSystem.swift | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Sources/TSCBasic/FileSystem.swift b/Sources/TSCBasic/FileSystem.swift index 800387a5..b33b1c61 100644 --- a/Sources/TSCBasic/FileSystem.swift +++ b/Sources/TSCBasic/FileSystem.swift @@ -383,8 +383,10 @@ private struct LocalFileSystem: FileSystem { } func isSymlink(_ path: AbsolutePath) -> Bool { - let attrs = try? FileManager.default.attributesOfItem(atPath: path.pathString) - return attrs?[.type] as? FileAttributeType == .typeSymbolicLink + let url = NSURL(fileURLWithPath: path.pathString) + // We are intentionally using `NSURL.resourceValues(forKeys:)` here since it improves performance on Darwin platforms. + let result = try? url.resourceValues(forKeys: [.isSymbolicLinkKey]) + return (result?[.isSymbolicLinkKey] as? Bool) == true } func isReadable(_ path: AbsolutePath) -> Bool {