System Calls for File Operations in Unix/Linux: Link Files

In Unix/Linux, every file has a pathname. However, Unix/Linux allows different pathnames to represent the same file. These are called LINK files. There are two kinds of links, HARD link and SOFT or symbolic link.

1. Hard Link Files

HARD Links: The command

ln oldpath newpath

creates a HARD link from newpath to oldpath. The corresponding syscall is

link(char *oldpath, char *newpath)

Hard linked files share the same file representation data structure (inode) in the file system. The file’s links count records the number of hard links to the same inode. Hard links can only be applied to non-directory files. Otherwise, it may create loops in the file system name space, which is not allowed. Conversely, the syscall

unlink(char *pathname)

decrements the links count of a file. The file is truly removed if the links count becomes 0. This is what the rm (file) command does. If a file contains very important information, it would be a good idea to create many hard links to the file to prevent it from being deleted accidentally.

2. Symbolic Link Files

SOFT Links: The command

ln -s oldpath newpath    # ln command with the -s flag

creates a SOFT or Symbolic link from newpath to oldpath. The corresponding syscall is

symlink(char *oldpath, char *newpath)

The newpath is a regular file of the LNK type containing the oldpath string. It acts as a detour sign, directing access to the linked target file. Unlike hard links, soft links can be applied to any file, including directories. Soft links are useful in the following situations.

(1). To access to a very long and often used pathname by a shorter name, e.g.

x -> aVeryLongPathnameFile

(2). Link standard dynamic library names to actual versions of dynamic libraries, e.g.

libc.so.6 -> libc.2.7.so

When changing the actual dynamic library to a different version, the library installing program only needs to change the (soft) link to point to the newly installed library.

One drawback of soft link is that the target file may no longer exist. If so, the detour sign might direct the poor driver to fall off a cliff. In Linux, such dangers are displayed in the appropriate color of dark RED by the ls command, alerting the user that the link is broken. Also, if foo -> /a/b/c is a soft link, the open(“foo”, 0) syscall will open the linked file /a/b/c, not the link file itself. So the open()/read() syscalls can not read soft link files. Instead, the readlink syscall must be used to read the contents of soft link files.

Source: Wang K.C. (2018), Systems Programming in Unix/Linux, Springer; 1st ed. 2018 edition.

Leave a Reply

Your email address will not be published. Required fields are marked *