Friday, October 16, 2009

Hardlink

A hard link is essentially a label or name assigned to a file. Conventionally, we think of a file as consisting of a set of information that has a single name. However, it is possible to create a number of different names that all refer to the same contents. Commands executed upon any of these different names will then operate upon the same file contents. This will create a new item in your working directory, newlink, which is linked to the contents of oldfile. The new link will show up along with the rest of your filenames when you list them using the ls command. This new link is not a separate copy of the old file, but rather a different name for exactly the same file contents as the old file. Consequently, any changes you make to oldfile will be visible in newlink. You can use the standard Unix rm command to delete a link. After a link has been removed, the file contents will still exist as long as there is one name referencing the file.

Thus, if you use the rm command on a filename, and a separate link exists to the same file contents, you have not really deleted the file; you can still access it through the other link. Consequently, hard links can make it difficult to keep track of files. Furthermore, hard links cannot refer to files located on different computers linked by NFS, nor can they refer to directories. For all of these reasons, you should consider using a symbolic link, also known as a soft link, instead of a hard link.A hard link is a directory reference, or pointer, to a file on a storage volume. The name associated with the file is a label stored in a directory structure that refers the operating system to the file data. As such, more than one name can be associated with the same file. When accessed through different names, any changes made will affect the same file data. Hard links are supported by POSIX-compliant systems (such as Linux and Apple's OS X (see Time Machine), as well as Windows 2000 and later Windows NT operating systems.A hard link is the file system representation of a file by which more than one path references a single file in the same volume.

To create a hard link, use the Create Hard Link function. Any changes to that file are instantly visible to applications that access it through the hard links that reference it. However, the directory entry size and attribute information is updated only for the link through which the change was made. Note that the attributes on the file are reflected in every hard link to that file, and changes to that file's attributes propagate to all the hard links. For example if you reset the READONLY attribute on a hard link to delete that particular hard link, and there are multiple hard links to the actual file, then you will need to reset the READONLY bit on the file from one of the remaining hard links to bring the file and all remaining hard links back to the READONLY state.
On POSIX-compliant operating systems, such as all Unix-like systems, 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.

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 disassociates 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.

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

Hardlink

No comments:

Post a Comment