> 
> I have another question.  Haing but a small background in programming, I was 
>wondering what the difference is between a symbolic link and a hard link...  SOmthing 
>link a pointer in C??
> 


First off, The HARD-LINK:

        When you create a file in unix, you simply create a file
somewhere on the disk.  The file has something called an "i-node",
which is more-or-less an id for the file (*).
        Now, directories are really just files.  And, since
they are files, they too have i-nodes.  These "directory-files"
contain a list of i-nodes of other files (**).  Some of these i-nodes
in a directory-file may belong to other "directory-files".  This
is how one gets sub-directories.  Let me illustrate:


Let's say I have three files:  (i-node 01), (i-node 02), (i-node 03)
I have a root directory:  (i-node 00)  (root will always have i-node zero)
and another directory:    (i-node 04)

Note that there are not names associated with the files.  They are simply
files on disk that are designated by i-node number, not by name.
Names are defined in the directory-files!


Let's assume i-node 00 (always the root directory) looks like this:

i-node | name
----------------
01     | 1.txt
02     | 2.txt
04     | foo


And let's assume i-inode 04 (our other directory-file) looks like this:

i-node | name
----------------
03     | 3.txt


The resulting directory structure looks something like:

/
|
|-1.txt
|
|-2.txt
|
|-/foo
   |
   |-3.txt


Because of this setup, we can see that the files can be moved around
the directory structure by simply moving the i-node entry from one
directory file to another.  Let's move the file /2.txt to /foo/2.txt:

i-node 00:                   i-node 04:

i-node | name               i-node | name
----------------          ------------------
01     | 1.txt               03    |  3.txt
04     | foo                 02    |  2.txt


now we have:

/
|
|-1.txt
|
|-/foo
   |
   |-3.txt
   |
   |-2.txt 



but what if we did this:

  i-node 00:                                 i-node 04:

i-node | name                              i-node | name
---------------                         ------------------
01     | 1.txt                             03    |  3.txt
02     | 2.txt  <- same inode in both! ->  02    |  99.txt
04     | foo

now we've got:

/
|
|-1.txt
|
|-2.txt
|
|-/foo
   |
   |-3.txt
   |
   |-99.txt 

This is hard-linking

Both of those files (2.txt and 99.txt) are really the SAME FILE.
The new link is indistiguishable from the original.
If you edit one, you edit the other.
But what about deleting?  simple: the file system keeps track of
how many "hard-links" there are for each file.  If you delete the file
in directory /foo, the link-counter is decremented by one, and the file
is removed from the table in i-node 04.  If the counter ever reaches
zero, (the last hard-link is removed) the file will actually be deleted.

Keep in mind, the two links to the file may have the same names.


SOFT-LINKS:

I've forgotten how soft-links are implemented.
However, in a soft-link, you have what is, more or less, an alias to a file.
If the original file is deleted, your alias points to nothing.
If you soft-link to a file, and the owner changes the permissions so you
can't read it, tough!   

Typically, one will use soft-links unless they have a specific reason
to do otherwise.

Also, one cannot hard-link files across file systems.  So if a file is on
one disk, you can't create a hard-link to it from another disk.  Soft-links
must be used in this case.

I hope this helps,

        Bryan Scaringe


* This is an oversimplification.
  I suggest you do a search on "i-node" for a more detailed discription.
  "i-node" is a GOOD term to familiarize your self with.

** they also contain some administrative stuff, but for our purposes,
   we can safely ignore it. 

Reply via email to