So I learned that hardlink on folders are forbidden to avoid infinite loops in the file system. Seems a good idea! But why is it not a problem to create an infinite loop with simlinks on folders? Why does the system can handle simlink loops but not hardlink loops?
1 Answer
A hard link is a second copy of a pointer to a hard piece of filesystem data, the inode, while a symbolic link (symlink) is simply a special file containing an instruction to (akin to a web URL) to go and look at a particular location.
It is impossible to tell the difference between a new hard-link to a file/folder without scanning the entire filesystem metadata and looking for objects that point to the same underlying data blocks.
A symlink on the other hand is a special file that has a flag saying "I am a link to a filesystem location". The operating system knows how to follow these files, but a program can simply say "just give me the file that points at the folder". A hardlink does not give that option, it is the folder except for having a different name.
As a result there are a few differences between them.
If you rename one of the hard links to a file then the other hard link will still be pointing to the inode and therefore the file/folder data.
A symlink on the other hand, because it is little more than an instruction saying "here is an address, go look there", can be completely invalidated by renaming the original file or directory.
The difference could be seen when copying directory structures.
Lets say you have a hard link that points to it's own parent:
Folder A
|__Folder B
and Folder B
is a hard link to Folder A
. If you follow that hard link then you see Folder B
within itself forever. It is impossible to ever copy the entire contents of the folder because you will recurse forever into it.
Folder A
|__Folder B
|__Folder B
|__Folder B
|__Folder B
|__Folder B
Tools could scan the entire filesystem metadata to see if a file is a duplicate hard link, but this is not something a user really wants the tools to do. The filesystem metadata could be millions of file pointers spread across a large disk, and you don't really want a simple folder copy program to take several minutes scanning the disk just to figure out if it can copy 5 folders in a directory.
A symlink on the other hand is "just" a file containing a pointer.
Folder A
|__(File pointer to) Folder B
So if you try to copy Folder A then you get Folder A
and a file that says Go to the location of Folder A
. The copy routine can see immediately that the symbolic link is a special file pointing somewhere. All it has to do is copy Folder A
and the file Folder B
which is a symlink to Folder A