in reply to Simple Path Cleanup

Using cd/cwd will follow soft links in the path and yield a path without soft links. As you say, there are other ways to deterime such path without changing the current working directory, but it is not clear that they will be any faster as the same underlying operations will be required. You may find that the system functions are as fast as any available.

Hard links are not so easily dealt with. Any path through hard links is equally valid and there will be no easy way to distinguish between a seemingly innocuous path (e.g. /some/irrelevant/file ) and the path of a critical file (e.g. /etc/passwd or / ). With hard links, you do have the certainty that they do not traverse file systems, so you can at least reliably determine what file system a path refers to. This might be adequate if, for example, all the paths you are checking should be on a file system that contains non-critical data only. Otherwise, it is hard to imagine any certain solution other than comparing the inode number of your subject path with the inode numbers of all critical files and directories on the same file system.

Replies are listed 'Best First'.
Re^2: Simple Path Cleanup
by almut (Canon) on May 10, 2009 at 09:41 UTC
    Hard links are not so easily dealt with...

    Luckily, most OSes don't allow hard links to directories.  I've heard Mac OS X (at least "Leopard") is an exception, though...

      It is a long time since I last deluded myself that a hard link to a directory would be a good thing. Even then it was discouraged due to concerns with infinite loops in directory traversal. IMHO it is a good thing that it is more strongly prohibited in most systems these days.

      I did come across a simple algorithm for resolving paths to a "canonical" path even if hard links to directories are allowed: follow all the links to the ultimate directory, then follow the chain of ".." back to the root directory. This assumes only that ".." in every directory is set to the "canonical" parent of that directory, despite any other links that might exist.

      If you are a bit paranoid (or work with people like I was in years gone by, who like to do things like making hard links to directories and other twisted manipulations of the file system) you might also be concerned about multiple mounts of file systems and directories that are possible on some systems, particularly with some loopback file systems. Usually one would only have one read/write mount at a time, but I understand multiple read/write mounts are possible in some cases.