Oihana PHP

untarUniqueAlias.php

Table of Contents

Functions

untarUniqueAlias()  : string|null
Gives a compressed archive a second, uniquely named path, so decompressing it twice in one process does not collide.

Functions

untarUniqueAlias()

Gives a compressed archive a second, uniquely named path, so decompressing it twice in one process does not collide.

untarUniqueAlias(string $tarFile, string $compression) : string|null

The collision. PharData::decompress() writes its .tar next to the archive, named after it, and registers it in a list of open phars that removing the file does not clear. Extracting the same .tar.gz a second time therefore failed with "Unable to add newly converted phar ... a phar with that name already exists" — measured on 2026-10-24, and a caller restoring several sites in one run meets it on the second.

Phar::unlinkArchive() is the documented way to clear both, and calling it from untar() corrupted the Zend heap. So the collision is avoided rather than cleaned up: give the archive a unique name and the .tar inherits it.

A hard link first, which costs no bytes and no time whatever the archive weighs; copy() only if the filesystem refuses one. Beside the archive in both cases, because a hard link cannot cross a filesystem — and because that is already where decompress() writes.

Parameters
$tarFile : string

Path to the compressed archive.

$compression : string

CompressionType::GZIP or BZIP2, as reported by tarCompressionOfFile(). The alias ends in the matching extension, so decompress() strips it the same way.

Tags
throws
UnsupportedCompressionException

If the compression has no known extension.

example
use oihana\files\enums\CompressionType;
use function oihana\files\archive\tar\untarUniqueAlias;

$alias = untarUniqueAlias( '/backups/site.tar.gz' , CompressionType::GZIP ) ;
// → '/backups/untar-6710f3c8d1e29.tar.gz', the same inode
author

Marc Alcaraz (ekameleon)

since
1.5.0
Return values
string|null —

The alias, to be removed by the caller, or null when neither a link nor a copy could be made — in which case the caller is expected to carry on with the archive itself rather than fail an extraction that would otherwise work.

On this page

Search results