The Full Wiki

Hard link: Wikis

Advertisements
  

Note: Many of our articles have direct quotes from sources you can cite, within the Wikipedia article! This article doesn't yet, but we're working on it! See more info or our list of citable articles.

Encyclopedia

From Wikipedia, the free encyclopedia

In computing, a hard link is a directory entry that associates a name with a file on a file system. A directory is itself a special kind of file that contains a list of such entries. The term is particular to file systems which allow multiple hard links to be created for the same file. This has the effect of creating multiple names for the same file, causing an aliasing effect: if the file is opened by one of its names, and changes are made to its content, then these changes will also be visible when the file is opened by an alternative name. By contrast, a soft link on such filesystems is not a link to a file itself, but to a file name; this also creates aliasing, but in a different way.

Directories are files, so multiple hard links to directories are possible; however, their unrestricted creation is usually avoided, because of the cyclic structures this may create.

Hard links -- that is, multiple hard links to the same file -- are supported by POSIX-compliant operating systems, such as Linux, Apple's OS X (see Time Machine), Windows NT4[1] and later Windows NT operating systems.

Support also depends on the type of file system being used. For instance, the NTFS file system supports it, while FAT does not.

Contents

Usage

On POSIX-compliant operating systems, such as all Unix-like systems, additional hard links to existing files are created with the link() system call, or the ln and link command-line utilities. The stat command can reveal how many hardlinks point to a given file. The link count is also included in the output of ls -l.

On Microsoft Windows, hard links can be created using the mklink /H command on Windows NT 6.0 and later systems (such as Windows Vista), and in earlier systems (Windows 2000, XP) using fsutil hardlink create. The Windows API from Windows 2000 onwards includes a CreateHardLink() call to create hard links, DeleteFile() is used to remove them, and GetFileInformationByHandle() can be used to determine the number of hard links associated with a file.[2] Hard links require an NTFS partition. Unix-like emulation or compatibility software running on Windows, such as Cygwin and Subsystem for UNIX-based Applications, allow the use of POSIX interfaces under Windows.

The process of unlinking dissociates a name from the data on the volume without destroying the associated data. The data is still accessible as long as at least one link that points to it still exists. When the last link is removed, the space is considered free. A process ambiguously called undeleting allows the recreation of links to data that is no longer associated with a name. However, this process is not available on all systems and is often not reliable.

Link counter

Most file systems that support hard links use reference counting. An integer value is stored with each physical data section. This integer represents the total number of links that have been created to point to the data. When a new link is created, this value is increased by one. When a link is removed, the value is decreased by one. If the link count becomes zero, the operating system usually automatically deallocates the data space of the file if no process has the file opened for access. The maintenance of this value assists users in preventing data loss. This is a simple method for the file system to track the use of a given area of storage, as zero values indicate free space and nonzero values indicate used space.

On POSIX-compliant operating systems, such as many Unix-variants, the reference count for a file or directory is returned by the stat() or fstat() system calls in the st_nlink field of struct stat. In contrast, programming language implementations that use reference counting rarely expose the reference count to the program being executed, since this information is just an implementation detail.

Example

An illustration of the concept of hard linking.

In the figure to the right, two hard links, named "LINK A.TXT" and "LINK B.TXT", point to the same physical data.

If the filename "LINK A.TXT" is opened in an editor, modified and saved, then those changes will be visible even if the filename "LINK B.TXT" is opened for viewing since both filenames point to the same data. The same is true if the file were opened as "LINK B.TXT" - or any other name associated with the data.

Some Editors however break the hard link concept, e.g. emacs renames the file to be opened to a backup file name (LINK B.TXT~). Emacs itself works with a local copy of the file it has generated and given the original file name. So the backup file (LINK B.TXT~) and the other hard link (LINK A.TXT) still share the same inode, which is not altered by the editor. The changes made by emacs only affects the copy of the hard link (LINK B.TXT).

Any number of hard links to the physical data may be created. To access the data, a user only needs to specify the name of any existing link; the operating system will resolve the location of the actual data.

If one of the links is removed with the POSIX unlink function (for example, with the UNIX 'rm' command), then the data is still accessible through any other link that remains. If all of the links are removed and no process has the file open, then the space occupied by the data is freed, allowing it to be reused in the future. This semantic allows for deleting open files without affecting the process that uses them. This technique is commonly used to ensure that temporary files are deleted automatically on program termination, including the case of abnormal termination.

Limitations of hard links

Most modern operating systems don't allow hard links on directories to prevent endless recursion. A notable exception to this is Mac OS X v10.5 (Leopard).[3] Symbolic links and NTFS junction points are generally used instead for this purpose.

Hard links can only be created to files on the same file system. If a link to a file on a different file system is needed, it may be created with a symbolic link.

The maximum number of hard links to a single file is limited by the size of the reference counter: with NTFS this is limited to 1023 because a 10 bit field is used for this purpose. On Unix-like systems the counter is usually machine word-sized (32 or 64-bit: 4294967296 or 18446744073709551616 links, respectively).

See also

Notes

  1. ^ "Link Shell Extension". http://schinagl.priv.at/nt/hardlinkshellext/hardlinkshellext.html.  
  2. ^ "NTFS Hard Links, Directory Junctions, and Windows Shortcuts". flexhex.com. http://www.flexhex.com/docs/articles/hard-links.phtml.  
  3. ^ Arstechnica article about Mac OS X hard links to directories.
Advertisements

Advertisements






Got something to say? Make a comment.
Your name
Your email address
Message