fix for bug #64343
[pharext/pharext] / src / pharext / Task / PaxFixup.php
1 <?php
2
3 namespace pharext\Task;
4
5 use pharext\Exception;
6 use pharext\Task;
7 use pharext\Tempfile;
8
9 class PaxFixup implements Task
10 {
11 private $source;
12
13 public function __construct($source) {
14 $this->source = $source;
15 }
16
17 private function openArchive($source) {
18 $hdr = file_get_contents($source, false, null, 0, 3);
19 if ($hdr === "\x1f\x8b\x08") {
20 $fd = fopen("compress.zlib://$source", "r");
21 } elseif ($hdr === "BZh") {
22 $fd = fopen("compress.bzip2://$source", "r");
23 } else {
24 $fd = fopen($source, "r");
25 }
26 if (!is_resource($fd)) {
27 throw new Exception;
28 }
29 return $fd;
30 }
31
32 public function run($verbose = false) {
33 if ($verbose !== false) {
34 printf("Fixing up a tarball with global pax header ...\n");
35 }
36 $temp = new Tempfile("paxfix");
37 stream_copy_to_stream($this->openArchive($this->source),
38 $temp->getStream(), -1, 1024);
39 $temp->closeStream();
40 return (new Extract((string) $temp))->run($verbose);
41 }
42 }