I was just reading this post here:
What are circular symlinks in Unix-like systems used for?
And the answers were quite interesting. They seem to say conclusively that there is no reason one would ever create such a circular symlink, and therefore it must have been created in error. If this is true, then why on earth are they allowed? Is it because the mechanics of disallowing them are prohibitively complicated or computationally intensive?
I don't see why this would be the case: can't we just compare the address in memory to see if it is the same as the target address, and then if they are the same, throw an error?
EDIT: perhaps in certain languages there will be an error unless you use some sort of forcing option. In those cases, then my question simply becomes: why would you allow a force option?
EDIT: upon some further research with the help of @Wumpus Q Wembley, it appears that this is indeed disallowed in unix and results in the following error:
ln: ‘/usr/bin/apt-config’ and ‘/usr/bin/apt-config’ are the same file
but that this can indeed happen when the files that are being symlinked to themselves are already symlinks from some other file. I'm not sure why that behavior is desirable?
-Paul
I know of (and use) one case where circular symlink is useful. It's a corner case, but nonetheless.
The symlink in question is located in the /boot
directory and (circularly) points to itself as in:
boot -> ./
It was created with the following command:
ln -s . boot
The reason it is necessary is as follows:
When I set up various Linux systems, in some cases I use separate /boot
partition and in some - I don't.
I have a cookie-cutter grub.cfg
file, which reads something like:
menuentry "Gentoo GNU/Linux" {
echo "Loading Linux kernel"
linux /boot/vmlinuz root=...
echo "Loading initial ramdisk"
initrd /boot/initramfs
}
If I didn't have the symlink, the above would not work for the systems with separate /boot
partition. For those systems I would need to have:
menuentry "Gentoo GNU/Linux" {
echo "Loading Linux kernel"
linux /vmlinuz root=...
echo "Loading initial ramdisk"
initrd /initramfs
}
Note the absence of /boot
in front of /vmlinuz
and /initramfs
.
The reason for this is that the /boot
partition is the root partition for the GRUB and vmlinuz
and initramfs
are located in the root /
directory.
Whereas, for systems without separate /boot
partition, the system root /
partition is also the root partition for the GRUB, and vmlinuz
and the initramfs
are located in the /boot
directory.
With the symlink in place, I can use the same grub.cfg
for both types systems.
It wouldn't be especially difficult to detect that a symlink would point to itself when creating it, but that wouldn't prevent them from being created in other ways. Consider:
mkdir foo
cd foo
ln -s ../bar/example example
cd ..
mv foo bar
Preventing that would require that before renaming the directory, the OS would scan through its contents (including subdirectories) checking the target of every link. Much too costly.
Even checking upon creation can be costly, if the symlink's target path contains lots of other symlinks or some slow network filesystems.
Basically, it's just not worth the trouble. Self-referential symlinks aren't useful, but they aren't really harmful either.