cocoamacruby

NSURL.URLByResolvingSymlinksInPath does not work for /tmp, /var. Is there a workaround?


(code examples in macruby syntax)

If I create an url with:

u1 = NSURL.fileURLWithPath('/tmp')

and then try to create an url with symlinks resolved:

u2=u1.URLByResolvingSymlinksInPath

I get back /tmp:

irb(main):010:0> u2.pathComponents
=> ["/", "tmp"]

This is a problem because if I create an NSDirectoryEnumerator with this NSURL (or /var or /etc), and prefetch the NSURLParentDirectoryURLKey, the NSURLs returned will have path /private/tmp/...

I need to get the components of the directory paths that are relative to the root of the NSDirectoryEnumerator. Is there a robust way to do this, other than hardcoded detection of the special cases I know about (which so far are /tmp, /var, and /etc)?

Is anyone aware of other "special" directories on which URLByResolvingSymlinksInPath doesn't work?


Solution

  • I was never able to find a complete answer to this. For my case, I added a method to NSURL that handles /tmp, /var & /etc:

    class NSURL
      def URLByResolvingSymlinksInPathIncludingPrivate
        url = self.URLByResolvingSymlinksInPath
        parts = url.pathComponents
        if (parts.first == '/') && ['tmp', 'var', 'etc'].include?(parts[1])
          url = NSURL.fileURLWithPathComponents(['/', 'private'] + parts[1..-1])
        end
        url
      end
    end
    

    I decided it was best to add an explicit method and not monkeypatch URLByResolvingSymlinksInPath.