[comp.unix.programmer] hard links: how to tell with system call under BSD4.3?

mlevin@jade.tufts.edu (05/10/91)

  Is there any way, from a system call on BSD4.3, to determine if a
file is a hard link?  I know lstat() will describe soft links, but
none of my Unix books seem to say how one can figure out if a file is
a hard link or not. Is this impossible? Please email me at
mlevin@jade.tufts.edu with any responses. Thanks in advance.

Mike Levin

phil@ux1.cso.uiuc.edu (Phil Howard KA9WGN) (05/11/91)

mlevin@jade.tufts.edu writes:


>  Is there any way, from a system call on BSD4.3, to determine if a
>file is a hard link?  I know lstat() will describe soft links, but
>none of my Unix books seem to say how one can figure out if a file is
>a hard link or not. Is this impossible? Please email me at
>mlevin@jade.tufts.edu with any responses. Thanks in advance.

If it is not a soft link then it is a hard link.

In fact, soft links themselves involve a hard link as well.

What a hard link really is, is a name in a directory that points to an
inode which describes a file.  Every file has that.

People often refer to a file as being hard linked only after it has TWO
OR MORE such names.  Keep in mind that EVERY name is equivalent.  There
is no "primary" name.

If you have a file called "abc" and you hard link "xyz" to it:

    ln abc xyz

then you now have TWO names pointing to the same file.  Both "abc" and "xyz"
are equal.  If you remove "abc":

    rm abc

Then "xyz" will still point to the file, and the file will still exist.
In fact you can even restore the name "abc" by doing:

    ln xyz abc

So this is why there is no indicator of hard links.  EVERY name is in fact
a hard link.  However you cannot do multiple hard links to the same inode
if that inode is a symlink (soft link) or a directory (unless your system
is broken as mine is).

Check for the link count.  That will tell you how many names the file has.
For a directory it should always be 2, one for the parent pointing to it and
one for it's own "." file.
-- 
 /***************************************************************************\
/ Phil Howard -- KA9WGN -- phil@ux1.cso.uiuc.edu   |  Guns don't aim guns at  \
\ Lietuva laisva -- Brivu Latviju -- Eesti vabaks  |  people; CRIMINALS do!!  /
 \***************************************************************************/

rearl@gnu.ai.mit.edu (Robert Earl) (05/11/91)

In article <1991May10.210452.28889@ux1.cso.uiuc.edu> phil@ux1.cso.uiuc.edu (Phil Howard KA9WGN) writes:

|   So this is why there is no indicator of hard links.  EVERY name is in fact
|   a hard link.  However you cannot do multiple hard links to the same inode
|   if that inode is a symlink (soft link) or a directory (unless your system
|   is broken as mine is).

The superuser can generally make hard links to directories, and a bug
involving rename() in sticky directories on some systems seems to
allow users to end up with hardlinked directories, whether they like it
or not...

|   Check for the link count.  That will tell you how many names the file has.
|   For a directory it should always be 2, one for the parent pointing to it and
|   one for it's own "." file.

Well, no, for each subdirectory made, a parent directory shows one
more link.  (From the ".." entry in the child).  For example, my home
directory has 11 links.


--robert

r3jjs@VAX1.CC.UAKRON.EDU (Jeremy J Starcher) (05/13/91)

In article <1991May10.210452.28889@ux1.cso.uiuc.edu> phil@ux1.cso.uiuc.edu (Phil Howard KA9WGN) writes:
>
>Check for the link count.  That will tell you how many names the file has.
>For a directory it should always be 2, one for the parent pointing to it and
>one for it's own "." file.

Hm.. not sure if our system is damaged or not, but all of a directories
children also have a link to the parent (..).  

I can tell how many branches there are by looking at the hardlink number
and subracting two.
-- 
--------------------------+---------------------------------------------------
Jeremy J Starcher         !  No programmer programs in LOGO after reaching
r3jjs@vax1.cc.uakron.edu  !  age 14...
r3jjs@akronvm.bitnet      !

bill@gothamcity.jsc.nasa.gov (Bill Shirley) (05/16/91)

In article <1344@VAX1.CC.UAKRON.EDU>, r3jjs@VAX1.CC.UAKRON.EDU (Jeremy J Starcher) writes:
|> In article <1991May10.210452.28889@ux1.cso.uiuc.edu> phil@ux1.cso.uiuc.edu (Phil Howard KA9WGN) writes:
|> >
|> >Check for the link count.  That will tell you how many names the file has.
|> >For a directory it should always be 2, one for the parent pointing to it an
                                       ^
					at least

|> >one for it's own "." file.
|> 
|> Hm.. not sure if our system is damaged or not, but all of a directories
|> children also have a link to the parent (..).  

this is *definately* how it should be !!

|> 
|> I can tell how many branches there are by looking at the hardlink number
|> and subracting two.