I have the following pseudo code:
func load(at packageURL: URL) {
let realPackageURL = packageURL.resolvingSymlinksInPath()
guard let it = fileMan.enumerator(at: realPackageURL)
for case let fileURL as URL in it {
print(fileURL)
// Get filename relative to package root directory
let relativeFileName = String(filePath.suffix(filePath.count - packagePath.count))
}
}
When called with "file:///tmp/some.app"
, the enumerated fileURL is actually
file:///private/tmp/GIMP.app/Contents/
packageURL.resolvingSymlinksInPath()
actually does nothing, I assume /tmp
is a hard link.
This makes it impossible to get a correct relative path. Is there any remedy for this?
Unfortunately, contentsOfDirectory(at:) suffers the same problem as the enumerator.
But anyway, I devised a workaround that leads to a little more memory footprint.
var fileList = [URL]()
for case let fileURL as URL in it {
fileList.append(fileURL)
}
// Sort by length so that the shortest comes first
fileList.sort { $0.relativePath.count < $1.relativePath.count }
// The shortest path without last component is the root path.
let packageRoot = fileList[0].url.deletingLastPathComponent().path