How detect cyclic symbolic links using NSFileManager?

My code is crashing Xcode (or even macOS kernel) during debugging - Xcode just vanishes from screen!

// pseudo code
public func hunt(in directory: URL) {
    let fileIterator = fileMan.enumerator(at: directory)
    // collect app packages into a list
    var packages = [URL]()
    for case let fileURL as URL in fileIterator {
        if fileURL.pathExtension == "app" { packages.append(fileURL) }
    }

    // FileWrappers
    var wrappers = [FileWrappers]()
    for packageURL in packages {
        //!!! The line below eventually crashes Xcode (or even macOS kernel once)!
        wrappers[packageURL] = try? FileWrapper(url: packageURL, options: .immediate)
        // NOTE: I need FileWrapper.matchesContents later in some code
    }
}

// unit test case
func test() {}
    myObj.hunt(in: URL(fileURLWithPath: "/Applications"))
}

I suspect that the FileWrapper constructor is traversing directories and encounter cyclic symbolic links and eventually it crashes; since it's running at system runtime level, most probably it also crashes macOS kernel!

So my question is that is there any way to detect cyclic symbolic links so that I can design my own logics similar to FileWrapper?

BTW, I have latest Xcode 15.1 and Sonoma 14.2.1.

It’s hard to say exactly what’s going on here without a crash report. However, it seems like your code is trying to create a file wrapper for every app in /Applications. That’s going to end badly because file wrappers are an in-memory representation of a file system hierarchy, and /Applications contains items, like Xcode, that are too big to load into memory.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

How detect cyclic symbolic links using NSFileManager?
 
 
Q