Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is there behind a symbolic link?

Tags:

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)?

like image 688
Aravind Avatar asked Jun 04 '13 08:06

Aravind


People also ask

What is the point of a symbolic link?

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.

What are the contents of a symlink 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.

Do symbolic links work both ways?

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.

What happens to symbolic link when file is deleted?

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.


1 Answers

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:

  • the flag file-type in the "inode" that tells to the system this file is a "symbolic link"
  • file-content: path to the target - in other words: a symbolic link is simply a file which contains a filename with a flag in the inode.

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 inodes (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:

  • file_a described by inode 1000
  • file_b described by inode 1001 and
  • file_c again described by inode 1000 (so it is a hard link with file_a, not hardlink to file_a - because it is impossible to tell which filename came first - they are identical).

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.

like image 156
jm666 Avatar answered Oct 25 '22 17:10

jm666