title: Determine a file's path(s) relative to a directory, including symlinks
body: <p>I have a directory with thousands of descendants (at least 1,000, probably no more than 20,000). Given a file path (which is guaranteed to exist), I want to know where that file can be found inside that directory -- including via symlinks.</p>
  
  <p>For example, given:</p>
  
  <ul>
  <li>The directory path is <code>/base</code>.</li>
  <li>The real file path is <code>/elsewhere/myfile</code>.</li>
  <li><code>/base</code> is a symlink to <code>/realbase</code></li>
  <li><code>/realbase/foo</code> is a symlink to <code>/elsewhere</code>.</li>
  <li><code>/realbase/bar/baz</code> is a symlink to <code>/elsewhere/myfile</code>.</li>
  </ul>
  
  <p>I want to find the paths <code>/base/foo/myfile</code> and <code>/base/bar/baz</code>.</p>
  
  <p>I could do this by recursively checking every symlink in <code>/base</code>, but this would be very slow. I'm hoping that there's a more graceful solution.</p>
  
  <hr>
  
  <h3>Motivation</h3>
  
  <p>This is for a Sublime Text plugin. When the user saves a file, we want to detect whether it is in the Sublime configuration directory. In particular, we want to do so even if the file is symlinked from inside the config directory and the user is editing the file at its physical path (e.g. inside their Dropbox directory). There may be other applications as well.</p>
  
  <p>Sublime runs on Linux, Windows, and the Mac OS, and so ideally should the solution.</p>