Skip to content

Symlinks are broken when extracting tar archive #19879

@mishal

Description

@mishal

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions