in reply to How does one compare the relationship between two file system paths in a portable way?

If the only systems running Perl were Unix, Dos, or Mac this would be easy: all I would need to do is canonize the two paths and then compare them as strings.
Considering that Unix allows symbolic links, and mounts anywhere on the current filesystem (not to mention the fact that things can be mounted more than once), this is not as easy as you may think.
For example, if A is a substring of B, then A is the parent of B.
Yes, but the reverse isn't true. If A isn't a substring of B, then that doesn't mean A isn't the parent of B.
  • Comment on Re: How does compare the relationship between two file system paths in a portable way?

Replies are listed 'Best First'.
Re^2: How does compare the relationship between two file system paths in a portable way?
by ELISHEVA (Prior) on Aug 17, 2009 at 23:14 UTC

    Agreed. I should have been more precise in my scoping of the problem (and have updated my original post accordingly). My particular use cases for this routine involves paths that can be presumed to be real file names. But others interested in a "comparePath" function might have different goals. Even in cases where that assumption can't be made, Perl offers, what I presume is, a portable way to identify symbolic links (-l, for example) so those could easily be filtered out and removed from consideration. Or alternatively one could use a function like readlink to convert the link to its real name. Do you have experiences where readlink and -l have caused problems?

    Come to think of it, I don't know how to detect mounts. Is -l also used?

    Best, beth

      As long as you remember to do a readlink on all segments of the path, then it may work. As long as there aren't any loops. Loops can make two paths be parents of each other. Note however that it's not forbidden for a Unix filesystem to allow directory *hard*links. Not sure if Perl runs on any system that has a filesystem where that is allowed.

      As for detecting mounts, stat tells you the device number of a file or directory. I don't know how to get, from Perl and in a portable way, a list of mount points.

        Note however that it's not forbidden for a Unix filesystem to allow directory *hard*links. Not sure if Perl runs on any system that has a filesystem where that is allowed.

        You can run Perl on Cygwin. A standard Cygwin installation will always have the equivalent of at least one hardlink.

        Usually the Windows directory that stores the Cygwin root directory will be in the default location C:\cygwin. Under Cygwin, that is mounted as /, but it can also be accessed as /cygdrive/c/cygwin. Both the drive leter (c) and directory name (cygwin) can be changed during installation.