php / php/php-src

Symlinks are broken when extracting tar archive

Open
#19,879 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Extension: phar Feature Status: Verified
Dominant language
C
Stars
40.4k
Forks
8.1k
Avg merge
2d 13h
Merged PRs (30d)
96

Description

Description

I have a TAR archive that contains symlink(s).

mkdir test
cd test
echo "file1" > file1
ln -s file1 file2
mkdir a
cd a
echo "file3" > file3
cd ..
ln -s a b
cd ..
tar -cf test.tar test

Extracting using tar works fine:

rm -r test
tar -xvf test.tar
ls -lah test
total 24K
drwxr-xr-x   3 michal michal 4.0K Sep 19 09:36 .
drwxr-xr-x 104 michal users   12K Sep 19 09:37 ..
drwxr-xr-x   2 michal michal 4.0K Sep 19 09:36 a
lrwxrwxrwx   1 michal michal    1 Sep 19 09:36 b -> a
-rw-r--r--   1 michal michal    6 Sep 19 09:36 file1
lrwxrwxrwx   1 michal michal    5 Sep 19 09:36 file2 -> file1

The following code extracts the archive using php:

<?php

$tar = '/home/michal/test.tar';
$targetDir = '/tmp/phpissue';

$phar = new \PharData($tar);
$phar->extractTo($targetDir, null, true);

Resulted in this:

ls -lah /tmp/phpissue/test/
total 16K
drwxr-xr-x 3 michal michal 4.0K Sep 19 09:42 .
drwxr-xr-x 3 michal michal 4.0K Sep 19 09:42 ..
drwxr-xr-x 2 michal michal 4.0K Sep 19 09:42 a
-rwxrwxrwx 1 michal michal    0 Sep 19 09:42 b
-rw-r--r-- 1 michal michal    6 Sep 19 09:42 file1
-rwxrwxrwx 1 michal michal    0 Sep 19 09:42 file2

The directory "b" is just an empty file, also the file2 is just en empty file. Both were symlinks.

I expect that those symlinks are correctly extracted.

PHP Version
Tested on php8.2+ (all with the same result):

PHP 8.4.12 (cli) (built: Sep  7 2025 14:06:59) (NTS)
Copyright (c) The PHP Group
Built by Debian
Zend Engine v4.4.12, Copyright (c) Zend Technologies
    with Zend OPcache v8.4.12, Copyright (c), by Zend Technologies

PHP 8.3.25 (cli) (built: Sep  7 2025 14:03:54) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.3.25, Copyright (c) Zend Technologies
    with Zend OPcache v8.3.25, Copyright (c), by Zend Technologies

PHP 8.2.29 (cli) (built: Sep  7 2025 14:06:52) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.29, Copyright (c) Zend Technologies
    with Zend OPcache v8.2.29, Copyright (c), by Zend Technologies
Operating System

Debian Bookworm

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the PharData::extractTo entry point and trace how TAR symlink entries are handled during extraction. Reproduce the supplied archive case on PHP 8.2+ and verify that file2 and b are symbolic links targeting file1 and a rather than empty regular files.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.