Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Dereference a filesystem path with softlinks

by narse (Pilgrim)
on Apr 20, 2004 at 18:40 UTC ( [id://346747]=perlquestion: print w/replies, xml ) Need Help??

narse has asked for the wisdom of the Perl Monks concerning the following question:

This problem is hard for me to explain so let pose this example:

I have a directory /foo/ and a link to it names /bar/ . If I run a script from within bar that calls Cwd::cwd(), I get the path /foo/ instead of bar which is fine. The only problem then is I want to compare the cwd with other path variables that point to the soft link. String comparison on /foo/ and /bar/ is false, when in actuality they are the same directory.

In my specific code, I would like to use File::Spec::rel2abs() to complete path names from a relative point so the ideal would be to get /foo/ from /bar/, not simply getting the indirect pwd.

Is there a module that will do this?

Thanks in advance.
  • Comment on Dereference a filesystem path with softlinks

Replies are listed 'Best First'.
•Re: Dereference a filesystem path with softlinks
by merlyn (Sage) on Apr 20, 2004 at 19:09 UTC
Re: Dereference a filesystem path with softlinks
by sgifford (Prior) on Apr 20, 2004 at 18:54 UTC

    I'm not sure of a module to do this, but to remove all symlinks from a path, you'd look at each component in the path, check if it's a symlink (with -l), if so get the location of the link (with readlink) and replace that part of the path with the link. You'd have to do this recursively, since symlinks can point to symlinks, and you'd also have to have some kind of loop detection, since there can be circular references.

    I have some shell script code which does something similar at:

    http://www.suspectclass.com/~sgifford/permcheck/permcheck
    which should give a general idea of how you'd go about it.
Re: Dereference a filesystem path with softlinks
by matija (Priest) on Apr 20, 2004 at 19:16 UTC
    I think the only way to determine if two paths (that could include soft links) point to the same directory is to do a @arr=stat("$path/."), take the inode number ($arr[1] in this case), and compare those.

    If the paths point to the same inode number, it's the same directory. If they don't, it isn't.

      Unless of course it's two different directories on different devices which just happen to have the same inode number . . .

      $ df -k | head -3; perl -le 'print"$_:\t".(stat $_)[1]for@ARGV' / /dev + Filesystem 1K-blocks Used Avail Capacity Mounted +on /dev/disk0s9 58598824 39199392 19143432 67% / devfs 96 96 0 100% /dev /: 2 /dev: 2

      Update: To elaborate, you want to use the tuple (device number, inode number), not just inode number.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://346747]
Approved by kvale
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (8)
As of 2024-04-18 16:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found