Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

How do I portably determine if two filenames refer to the same file?

by jcb (Parson)
on Aug 16, 2019 at 03:31 UTC ( #11104545=perlquestion: print w/replies, xml ) Need Help??

jcb has asked for the wisdom of the Perl Monks concerning the following question: (files)

On POSIX systems, and possibly others, a file can have multiple names (known as hardlinks) or can itself contain the name of another file in such a way that the system substitutes that other file for most operations on a file (these are symlinks). For various platforms, are similar features available, and, if so, how best to determine if two seemingly different names are in fact the same file?

This question was also asked in Seekers of Perl Wisdom as Portable way to determine if two names refer to the same file?. The discussion there may also be of interest.

Originally posted as a Categorized Question.

  • Comment on How do I portably determine if two filenames refer to the same file?

Re: How do I portably determine if two filenames refer to the same file?
by jcb (Parson) on Aug 16, 2019 at 03:40 UTC

    For POSIX systems, the first two values in the list returned by Perl's stat builtin provide a solution. These are the "device" and "inode" numbers for the file and, taken as a pair, uniquely identify a file.

    # return true if both names are for the same file sub check_same_file_under_posix { my ($file_a, $file_b) = @_; my ($dev_a, $ino_a, @other_a) = stat($file_a); my ($dev_b, $ino_b, @other_b) = stat($file_b); if (($dev_a == $dev_b) && ($ino_a == $ino_b)) { return 1 } else { return 0 } }

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2021-04-21 11:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?