How are symbolic links managed internally by UNIX/Linux systems. It is known that a symbolic link may exist even without an actual target file (Dangling link). So what is that which represents a symbolic link internally.
In Windows, the answer is a reparse point
.
Questions:
Is the answer an inode
in UNIX/Linux?
If yes, then will the inode number be same for target and links?
If yes, can the link inode can have permissions different from that of target's inode (if one exists)?
A symbolic link (or "symlink") is file system feature that can be used to create a link to a specific file or folder. It is similar to a Windows "shortcut" or Mac "alias," but is not an actual file. Instead, a symbolic link is a entry in a file system that points to a directory or file.
A symbolic link is a special type of file whose contents are a string that is the pathname of another file, the file to which the link refers. (The contents of a symbolic link can be read using readlink(2).) In other words, a symbolic link is a pointer to another name, and not to an underlying object.
Yes, a symbolic link is a pointer to another location. This means that any changes you make are in fact updating at the target location.
If you delete a file for which a symbolic link still exists, the rm will succeed but the symbolic link would remain and any attempt to reference it will return a 'file not found' error.
It is not about UNIX/Linux but about filesystem implementation - but yes, Unix/Linux uses inodes at kernel level and filesystem implementations have inodes (at least virtual ones).
In the general, symbolic links are simply files (btw, directories are also files), that have:
file-type
in the "inode" that tells to the system this file is a "symbolic link"Virtual filesystems can have symbolic links too, so, check FUSE or some other filesystem implementation sources. (ext2/ext3/ufs..etc)
So,
Is the answer an inode in UNIX/Linux?
depends on filesystem implementation, but yes, generally the inode contains a "file-type" (and owners, access rights, timestamps, size, pointers to data blocks). There are filesystems that don't have inode
s (in a physical implementation) but have only "virtual inodes" for maintaining compatibility with the kernel.
If yes, then will the inode number be same for target and links?
No. Usually, the symlink is a file with its own inode, (with file-type, own data blocks, etc.)
If yes, can the link inode can have permissions different from that of target's inode(if one exists)?
This is about how symlink files are handled. Usually, the kernel doesn't allow changes to the symlink permissions - and symlinks always have default permissions. You could write your own filesystem that would allow different permissions for symlinks, but you would get into trouble because common programs like chmod
don't change permissions on symlinks themselves, so making such a filesystem would be pointless anyway)
To understand the difference between hard links and symlinks, you should understand directories first.
Directories are files (with differentiated by a flag in the inode) that tell the kernel, "handle this file as a map of file-name
to inode_number
". Hard-links are simply file names that map to the same inode
. So if the directory-file contains:
file_a: 1000 file_b: 1001 file_c: 1000
the above means, in this directory, are 3 files:
This is the main difference to symlinks, where the inode of file_b
(inode 1001) could have content "file_a" and a flag meaning "this is a symlink". In this case, file_b
would be a symlink pointing to file_a
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With