Problem with stat's mode field

J

J Krugman

It was my understanding that the mode "field" returned by stat()
(more precisely, the third element in the array returned by stat)
reflects whether a file is a link or not, but I find that this is
not the case, at least on Linux. For example:

$ mkdir test_stat
$ cd test_stat
$ touch file
$ ln file hard
$ ln -s file soft
$ /bin/ls -l
total 0
-rw-r--r-- 2 krugman krugman 0 Jan 12 11:37 file
-rw-r--r-- 2 krugman krugman 0 Jan 12 11:37 hard
lrwxrwxrwx 1 krugman krugman 4 Jan 12 11:37 soft -> file
$ perl -e 'printf "%o\n", (stat)[2] for @ARGV' *
100644
100644
100644

As this shows, a file and links, whether hard or soft, pointing to
it all have the same value for stat's mode "field". What's am I
missing?

How can I tell whether a file is a link or not?

Thanks,

jill
 
T

Tad McClellan

J Krugman said:
How can I tell whether a file is a link or not?


If you mean "a symbolic link or not", then:

perldoc -f readlink


If you mean "a hard link or not", then I don't understand the question.

Why do you think you need to know if it is a hard link or not?
 
B

Brian McCauley

J Krugman said:
It was my understanding that the mode "field" returned by stat()
(more precisely, the third element in the array returned by stat)
reflects whether a file is a link or not,

OK by "link" I shall assume you mean "symbolic link" since that's the
only thing that is meaningful.

The stat() function returns information about the file not the link.

To get the corresponding information about the link use the lstat()
function. If applied to a non-symlink then lstat() will return the
same as stat().
As this shows, a file and links, whether hard or soft, pointing to
it all have the same value for stat's mode "field". What's am I
missing?

Note that hard links are just directory entries. Every directory
entry for a file is equal - there is no concept of one being the "real
name" and the others being somehow inferior.
How can I tell whether a file is a link or not?

if ( -l $filename )

--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
 
J

Juha Laiho

J Krugman said:
It was my understanding that the mode "field" returned by stat()
(more precisely, the third element in the array returned by stat)
reflects whether a file is a link or not, but I find that this is
not the case, at least on Linux.

Please see documentation for function lstat.
$ /bin/ls -l
total 0
-rw-r--r-- 2 krugman krugman 0 Jan 12 11:37 file
-rw-r--r-- 2 krugman krugman 0 Jan 12 11:37 hard
lrwxrwxrwx 1 krugman krugman 4 Jan 12 11:37 soft -> file
$ perl -e 'printf "%o\n", (stat)[2] for @ARGV' *
100644
100644
100644

As this shows, a file and links, whether hard or soft, pointing to
it all have the same value for stat's mode "field". What's am I
missing?

For hard links, you can't tell which of the current links (if any)
is the original. By looking at the link count (field #3, nlink), you
can tell whether there are multiple names for the given FS object
(and also how many names the object has), but that's all - all the
names (hard links) are equal in functionality.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,060
Latest member
BuyKetozenseACV

Latest Threads

Top