[Haskell-cafe] Re: File path programme
glynn at gclements.plus.com
Wed Feb 2 08:28:35 EST 2005
Peter Simons wrote:
> > Hmmm, I'm not really sure what "equivalence" for file
> > paths should mean in the presence of hard/symbolic links,
> > (NFS-)mounted file systems, etc.
> Well, there is a sort-of canonic version for every path; on
> most Unix systems the function realpath(3) will find it.
> My interpretation is that two paths are equivalent iff they
> point to the same target.
I think that any definition which includes an "iff" is likely to be
More likely, you will have to settle for a definition such that, if
two paths are considered equal, they refer to the same "file", but
without the converse (i.e. even if they aren't equal, they might still
refer to the same file).
Even so, you will need to make certain assumptions. E.g. older Unices
would allow root to replace the "." and ".." entries; you probably
want to assume that can't happen.
> You (and the others who pointed it out) are correct, though,
> that the current 'canon' function doesn't accomplish that. I
> guess, I'll have to move it into the IO monad to get it
> right. And I should probably rename it, too. ;-)
A version in the IO monad would allow for a "tighter" definition (i.e.
more likely to correctly identify that two different path values
actually refer to the same file).
[Certainly, you have to use the IO monad if you want to allow for case
sensitivity, as that depends upon which filesystems are mounted
Within the IO monad, the obvious approach is to stat() both pathnames
and check whether their targets have the same device/inode pairs.
That's reasonably simple, and probably about as good as you can get.
That still won't handle the case where you mount a single remote
filesystem via both NFS and SMB though. I doubt that anything can
There are also issues of definition, e.g. is "/dev/tty" considered
"equivalent" to the specific "/dev/ttyXX" device for the current
Glynn Clements <glynn at gclements.plus.com>
More information about the Haskell-Cafe