hard links: how to tell with system call under BSD4.3?

Phil Howard KA9WGN phil at ux1.cso.uiuc.edu
Sat May 11 07:04:52 AEST 1991


mlevin at 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 at 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 at ux1.cso.uiuc.edu   |  Guns don't aim guns at  \
\ Lietuva laisva -- Brivu Latviju -- Eesti vabaks  |  people; CRIMINALS do!!  /
 \***************************************************************************/



More information about the Comp.unix.programmer mailing list