What exactly is the GNU tar ././@LongLink "trick"?

Learn what exactly is the gnu tar ././@longlink "trick"? with practical examples, diagrams, and best practices. Covers interop, gnu, tar development techniques with visual explanations.

Unmasking the GNU tar '././@LongLink' Trick: A Deep Dive into Interoperability

Hero image for What exactly is the GNU tar ././@LongLink "trick"?

Explore the mysterious GNU tar '././@LongLink' entry, its purpose in handling long filenames, and the interoperability challenges it presents across different tar implementations.

When working with tar archives, especially those created by GNU tar, you might occasionally encounter a peculiar entry named ././@LongLink or ././@LongLink/ when listing the archive's contents. This isn't a regular file or directory you'd expect to find in your backup; rather, it's a special mechanism employed by GNU tar to handle filenames that exceed the standard length limits defined by various tar formats. Understanding this 'trick' is crucial for ensuring proper interoperability and avoiding unexpected issues when extracting archives on different systems or with different tar utilities.

The Problem: Tar Format Limitations

Historically, tar (tape archive) formats have had limitations on the length of filenames they can store. The original V7 tar format, for instance, allowed only 100 characters for a filename. Later standards like POSIX ustar extended this to 256 characters, often by splitting the path into a prefix and name. However, even 256 characters can be insufficient for deeply nested directory structures or very descriptive filenames common in modern file systems. When GNU tar encounters a filename longer than what the chosen tar format can natively support, it needs a way to store this information without corrupting the archive or truncating the filename.

flowchart TD
    A[File with Long Name] --> B{Filename Length > Tar Limit?}
    B -- Yes --> C[GNU tar creates '././@LongLink' entry]
    C --> D[Stores full long filename in @LongLink's data block]
    D --> E[Creates placeholder entry for actual file/symlink]
    E --> F[Placeholder points to @LongLink for name]
    B -- No --> G[Stores file normally]
    F --> H[Archive Created]
    G --> H

How GNU tar handles long filenames with @LongLink

To overcome filename length limitations, GNU tar introduced a proprietary extension. When it encounters a filename that exceeds the maximum length for the selected tar format, it performs the following steps:

  1. Creates a special entry: It writes a special entry into the tar archive with the name ././@LongLink (for regular files) or ././@LongLink/ (for directories, though less common for directories themselves to exceed limits, but their contents might). This entry is typically of type 'L' (for long link) or 'K' (for long name).
  2. Stores the full filename: The data block associated with this ././@LongLink entry contains the full, un-truncated long filename.
  3. Creates a placeholder entry: Immediately following the ././@LongLink entry, GNU tar writes another tar header for the actual file or symbolic link. This placeholder entry will have a truncated or generic filename (e.g., a hash or a short name) and a special flag indicating that its true name is stored in the preceding ././@LongLink entry.

During extraction, a GNU tar utility recognizes the ././@LongLink entry, reads the full filename from its data block, and then applies that name to the subsequent placeholder entry, effectively reconstructing the original long filename.

# Create a very long filename
mkdir -p very/deeply/nested/directory/structure/with/an/extremely/long/path/that/exceeds/common/tar/limits
touch very/deeply/nested/directory/structure/with/an/extremely/long/path/that/exceeds/common/tar/limits/this_is_a_very_long_filename_indeed_and_it_will_trigger_the_longlink_mechanism.txt

# Create a tar archive using GNU tar
gnu_tar -cvf longlink_example.tar very

# List the contents of the archive, observing the @LongLink entry
gnu_tar -tvf longlink_example.tar

Demonstrating the creation and listing of an archive with a long filename using GNU tar.

Interoperability Challenges

While ingenious, the ././@LongLink mechanism is a GNU tar extension. This leads to significant interoperability issues:

  • Non-GNU tar utilities: Other tar implementations (e.g., BSD tar on macOS, or tar on Solaris) do not understand the ././@LongLink special entry. When they encounter it, they will typically:
    • Extract ././@LongLink as a literal file or directory named @LongLink (often containing the long filename as its content).
    • Extract the subsequent placeholder file with its truncated or generic name, completely ignoring the intended long name.
    • In some cases, they might even abort with an error if they don't recognize the entry type.
  • Data Loss/Corruption (Perceived): Users extracting archives with non-GNU tar might find their long filenames truncated or replaced, leading to perceived data loss or a corrupted directory structure.
  • Portability: Archives created with GNU tar and containing long filenames are not truly portable to systems that rely on other tar implementations without specific handling or conversion.

Mitigation and Best Practices

To avoid issues related to ././@LongLink, consider these strategies:

  1. Use pax for maximum portability: The pax utility (Portable Archive eXchange) is designed for maximum interoperability across different systems and can often handle various tar and cpio formats gracefully. It's part of POSIX.
  2. Specify tar format: When creating archives with GNU tar, you can explicitly specify a standard format that supports longer filenames, such as ustar or posix (also known as pax format in GNU tar). The posix format (often referred to as pax format by GNU tar) uses extended headers to store metadata like long filenames, which is a more standardized approach than ././@LongLink.
  3. Avoid excessively long filenames: While not always feasible, keeping filenames and path lengths reasonable can prevent triggering the ././@LongLink mechanism altogether.
  4. Test extraction: If you're creating archives for distribution, test extraction on a system with a different tar implementation (e.g., macOS or a non-GNU/Linux system) to ensure compatibility.
  5. Use zip or other archive formats: For cross-platform distribution where tar interoperability is a major concern, zip archives are often a more universally understood format, though they have their own set of limitations and features.
# Create an archive using GNU tar, forcing the POSIX (pax) format
gnu_tar -cvf longlink_posix_example.tar --format=posix very

# List the contents - you should NOT see '././@LongLink'
gnu_tar -tvf longlink_posix_example.tar

# Create an archive using GNU tar, forcing the ustar format
gnu_tar -cvf longlink_ustar_example.tar --format=ustar very

# List the contents - ustar has a 256 char limit, so it might still use @LongLink if path is too long
gnu_tar -tvf longlink_ustar_example.tar

Using GNU tar's --format option to specify archive format and improve portability.