Everything2
Near Matches
Ignore Exact
Full Text
Everything2

hardlink

created by Saige

(idea) by ariels (1.9 d) (print)   ?   (I like it!) Sat Apr 01 2000 at 9:34:59

UN*X allows file access from directories, which attach filenames to inodes (or vnodes, if you've been spoiled by NFS and later weenie file systems). The connection filename ⇒ inode is the "hardlink". A "file" (really, an inode, as we've just seen!) can have filenames in multiple directories.

You create a hardlink either by creating a file, or by saying "ln a b", which creates "b" as a filename pointing to the same inode that "a" points to. You could now even say "rm a", "safe" in the knowledge that b still holds your file. You could even write "mv" in this way (apart from inode limits).

Clearly, hardlinks are UN*X's gift to the Klingon programmer.

You cannot have a hard link to a directory in most UNIXen today. This is widely considered a Good Thing: it could badly break directory-traversing programs, except those written by Klingon programmers for non-Klingons. Even if you could, these Klingon-written directory-traversing programs could detect this by examining the inode of .. in every directory they enter, refusing if that inode doesn't match the inode of the directory through which they're entering. Sure, they'd miss some directories (and consequently presumably have to kill themselves), but they wouldn't enter an infinite loop (and consequently have to kill -9 their programmer). Of course, such a shameful program could be fooled, creating directories it can never enter (except when started from), by saying

% mkdir a
% ln a b
% rmdir a

You can always put symbolic links to a directory, though. Symlinks are for weenies. Well, they would be, except that they're far more useful.


(thing) by rp (1.6 d) (print)   ?   (I like it!) 1 C! Thu May 02 2002 at 13:52:48

The main reason the Unix hard link is confusing is that it suggests a feature in Unix file systems that doesn't actually exist.

In Unix, a file is identified by an inode on a file system. Files have attributes, such as permissions, ownership and various timestamps.

A directory is a file that contains a mapping of file names to inodes. An inode can appear multiple times, in different directories, or even in the same (under different names). An occurrence of a file in a directory is called a hard link to the file.

This means that a filename is not a property of a file: it is a property of a hard link to the file, which is an entry in a directory in which the file appears. Creating a hardlink is not an operation on a file; it is an edit operation on a directory!

However, the constraint is maintained that every inode has at least one hard link, while every hard link (= directory entry) points to a valid file. To impose these constraints, a file is deleted automatically when the last hard link to it is deleted, while a file can only be created together with a hard link to it. (Exceptions are possible, when you know about file descriptors.)

A further constraint is that for every file there is a finite path of hardlinks to it from the root directory, /. This guarantees that every file has a full pathname, also called an absolute name or path to the file: it is formed by concatenating the names of the hard links preceded by /.

A file has a unique full pathname only when all files on the path have no more than one hard link. For directories, this constraint is usually maintained within a single file system, but it isn't across file systems: the same file system can be simultaneously mounted on different directories, and automounters and loopback file systems even allow it to appear in infinitely many different places. All this without using a single symlink.

So now that we know what a hard link is, where does the confusion come from?

It originates from the fact that most operations on files are by filename or full pathname, and practically none of them will increase the number of hard links to a file. Therefore it is natural for users to think of a filename as being a property of the file, indicating its unique point of appearance in the directory tree. Users may work with Unix for years without creating multiple hard links.

Users who do know about hard links tend to use the term for additional hard links to an already existing file, created, for instance, with the ln command. While this is a natural thought pattern, it leaves the incorrect impression that such additional hard links are in any way different from the first hard link to a file. They are not: once a file has multiple hard links, none of them can be distinguished as "the real filename" or "the original link" in any way. After doing ln a b, I do not have "the real file a" and "the hardlink b"; I have one file with two hardlinks, a and b.

Other systems, such as E2, use the term hard link in different ways.


printable version
chaos

soft link hard link brackets softlink
E2 Link and Logger Client E2 node autolinker in perl unixism Hit by the realization that they are all getting to know you nodes
Everything couples/Ex-couples Link and Link link inode
symlink Red interconnectedness of all things pipe link
HTML formatted hard links autonoding Mandelbrot set Top ten ways to fuck up your kids
E2 HTML tags Piping pipe Please help us recover your nodes by linking their titles below
Y'know, if you log in, you can write something here, or contact authors directly on the site. Create a New User if you don't already have an account.
  Epicenter
Login
Password

password reminder
register

Everything2 Help

Cool Staff Picks
Things you could have written:
Mind your Ps and Qs
lawnjarts
Robert Rodriguez
colcannon
Sandalwood
Bay leaf
Winnie Mandela
Benford's Law
Egyptian rat screw
A Christmas Carol
Ten reasons to believe in God
King of Birds
transitioning
New Writeups
locke baron
lynx(thing)
Simulacron3
Reality, Dimensions and the Natural Ontology(essay)
SubSane
Making Love to a 9-Foot Woman(person)
Ouzo
Thoughts(idea)
antigravpussy
I fall silent, listening. The breadcrumbs are talking about us(person)
calgon
Buffalo Bill by the pool(poetry)
gate
Anarchy is Order(idea)
ushdfgakjasgh
Scribeling(thing)
XWiz
Trism(review)
artman2003
Briefcase Full of Souls - Part I(fiction)
Dreamvirus
Alan Ladd(person)
waverider37
Harold Holt(person)
The Debutante
Until death do us part(fiction)
Ysardo
a brother to a sister(personal)
antigravpussy
your warm whispers(personal)
This affordable entertainment brought to you by The Everything Development Company