[ Previous | Next | Contents | Glossary | Home | Search ]
AIX Version 4.3 General Programming Concepts: Writing and Debugging Programs

Linking for Programmers

A link is a connection between a file name and an i-node (hard link) or between file names (symbolic link). Linking allows access to an i-node from multiple file names. Directory entries pair file names with i-nodes. File names are easy for users to identify, and i-nodes contain the real disk addresses of the file's data. A reference count of all links into an i-node is maintained in the i_nlink field of the i-node. Subroutines that create and destroy links use file names, not file descriptors. Therefore, it is not necessary to open files when creating a link.

Processes can access and change the contents of the i-node by any of the linked file names. Two kinds of links exist in this operating system:

hard Created by the link subroutine. The presence of a hard link guarantees the existence of a file because a hard link increments the link count in the i_nlink field of the i-node. The unlink subroutine releases links. When all hard links to an i-node are released, the file is no longer accessible.

The user ID that created the original file owns the file and retains access mode authority over the file. Otherwise, all hard links are treated equally by the operating system. Hard links must link file names and i-nodes within the same file system since the i-node number is relative to a single file system.

Hard links always refer to a specific file because the directory entry created by the hard link pairs the new file name to an i-node. For example, if the /u/tom/bob file is linked to the /u/jack/foo file, the link count in the i_nlink field of the foo file is 2 . Both hard links are equal. If /u/jack/foo is removed, the file continues to exist by the name /u/tom/bob and can be accessed by users with access to the tom directory. However, the owner of the file is jack even though /u/jack/foo was removed. The space occupied by the file is charged to jack's quota account. File ownership is changed using the chown subroutine.

symbolic Created by the symlink subroutine. Symbolic links are implemented as a file that contains a path name. When a process encounters a symbolic link, the path contained in the symbolic link is prepended to the path the process was searching. If the path name in the symbolic link is an absolute path name, the process searches from the root directory for the named file. If the path name in the symbolic link does not begin with a / (slash), the process interprets the rest of the path relative to the position of the symbolic link. The unlink subroutine also removes symbolic links.

Symbolic links can traverse file systems because they are treated as regular files by the operating system rather than as part of the file system structure. The presence of a symbolic link does not guarantee the existence of the target file because a symbolic link has no effect on the i_nlink field of the i-node.

The readlink subroutine reads the contents of a symbolic link. Many subroutines (including the open and stat subroutines) follow symbolic paths. The lstat subroutine was created to report on the status of the file containing the symbolic link and does not follow the link. See the symlink subroutine for a list of subroutines that traverse symbolic links.

Symbolic links are also called soft links because they link to a file by path name. If the target file is renamed or removed, the symbolic link cannot resolve. For example, the symbolic link to /u/joe/foo is a file that contains the literal data /u/joe/foo . When the owner of the foo file removes this file, subroutine calls made to the symbolic link cannot succeed. If the file owner then creates a new file named foo in the same directory, the symbolic link leads to the new file. Therefore, the link is considered soft because it is linked to interchangeable i-nodes.

In the ls -l command listing, an l in the first position indicates a linked file. In the final column of that listing, the links between files are represented as Path2 -> Path1 (or Newname -> Oldname ).

The unlink subroutine removes a directory entry. The Path parameter in the subroutine identifies the file to be disconnected. At the completion of the unlink call, the link count of the i-node is reduced by the value of 1. The remove subroutine also removes a file name by calling either the unlink or rmdir subroutine.

Directory Links

The mkdir subroutine creates directory entries for new directories, thus creating hard links to the i-node representing the directory. Symbolic links are recommended for creating additional links to a directory. Symbolic links do not interfere with the . and .. directory entries and will maintain the empty, well-formed directory status. See the Understanding Directory Links figure, for a graphic example of the empty, well-formed directory /u/joe/foo.

The rmdir and remove subroutines remove links to directories.

Related Information

Files, Directories, and File Systems for Programmers contains links to other file system information.

Directories introduces the format of directory entries and the subroutines that manipulate directories.

Working with File I/O describes the internal procedure of reading data from files.

The ls command.

The chown subroutine, link subroutine, statx, lstat, stat, fstatx, fstat, fullstat, or ffullstat subroutine, open, openx, or creat subroutine, symlink subroutine, remove subroutine, rmdir subroutine, unlink subroutine.


[ Previous | Next | Contents | Glossary | Home | Search ]