How Unrar Handles Hard Links Extraction
When extracting archives that contain hard links, the
unrar utility recreates the original filesystem references
by leveraging operating system APIs, provided the target filesystem
supports them. This article explains how unrar detects hard
link records within RAR archives, the extraction order and system calls
it uses, and how it handles potential errors and unsupported
filesystems.
Archival Support and Metadata
The RAR archive format, specifically RAR 5.0 and later, includes native support for storing filesystem links, including hard links, symbolic links, and directory junctions. When an archive is created with hard link preservation enabled, the archiver stores the full data stream for only the first occurrence of the file. Any subsequent hard-linked instances are saved as lightweight metadata entries referencing the target file's path rather than duplicating the file contents.
The Extraction Process
During extraction, unrar processes the archive
sequentially:
- Primary File Extraction: When
unrarencounters the first instance of the linked file, it extracts the file data to disk as a standard file. - Link Resolution: When it encounters subsequent entries flagged as hard links, it reads the target path stored in the metadata.
- Link Creation: Instead of writing duplicate data
blocks to the disk,
unrarinvokes native system calls to link the new directory entry directly to the existing file's inode or file record.- On Unix-like systems (Linux, macOS, BSD), it uses the standard
link()orlinkat()POSIX functions. - On Windows (operating on NTFS volumes), it utilizes the Win32 API
function
CreateHardLink().
- On Unix-like systems (Linux, macOS, BSD), it uses the standard
Because hard links point to the same underlying data, the extracted links share the exact same disk space, permissions, and file attributes as the source file.
Extraction Dependencies and Order
Because a hard link requires an existing physical file to point to, extraction depends heavily on the presence of the primary target. If you extract the entire archive in its original order, the process completes seamlessly. However, issues can arise under specific conditions:
- Partial Extraction: If a user selectively extracts
only the hard link entry without extracting the primary target file,
unrarcannot establish the link. The operation typically results in an error stating that the target file does not exist. - Extraction Order: In cases where files are processed out of order, the link creation will fail unless the target file is created on the disk first.
Filesystem Limitations and Fallback Behavior
Hard links are strictly a filesystem-level feature and are not universally supported across all storage media:
- Supported Filesystems: Native hard linking functions correctly on NTFS (Windows), ext4, Btrfs, XFS (Linux), and APFS (macOS).
- Unsupported Filesystems: Filesystems such as FAT16, FAT32, and exFAT do not support hard links.
When extracting to a filesystem that does not support hard links,
unrar cannot invoke the respective system calls. Depending
on the version and command-line flags used, unrar either
fails the link creation step with an I/O error or, in some build
variants, falls back to copying the original file to satisfy the
destination path, sacrificing the space-saving benefit of the link.
Security and Privilege Considerations
Modern versions of unrar include security boundaries to
prevent directory traversal and link-based vulnerabilities. The utility
verifies that the hard link target resolves within the designated
extraction directory to avoid unauthorized modifications to system
files. Additionally, standard user permissions apply: unrar
can only create hard links within directories where the active user has
write permissions, and on Windows, creating cross-volume hard links is
fundamentally restricted by the OS architecture.