|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of Compressy. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Alchemy <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
namespace Gocobachi\Compressy\Parser; |
|
12
|
|
|
|
|
13
|
|
|
use Gocobachi\Compressy\Exception\RuntimeException; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* This class is responsible of parsing GNUTar command line output |
|
17
|
|
|
*/ |
|
18
|
|
|
class BSDTarOutputParser implements ParserInterface |
|
19
|
|
|
{ |
|
20
|
|
|
const PERMISSIONS = '([ldrwx-]+)'; |
|
21
|
|
|
const HARD_LINK = '(\d+)'; |
|
22
|
|
|
const OWNER = '([a-z][-a-z0-9]*)'; |
|
23
|
|
|
const GROUP = '([a-z][-a-z0-9]*)'; |
|
24
|
|
|
const FILESIZE = '(\d*)'; |
|
25
|
|
|
const DATE = '([a-zA-Z0-9]+\s+[a-z0-9]+\s+[a-z0-9:]+)'; |
|
26
|
|
|
const FILENAME = '(.*)'; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @inheritdoc |
|
30
|
|
|
*/ |
|
31
|
|
|
public function parseFileListing($output) |
|
32
|
|
|
{ |
|
33
|
|
|
$lines = array_values(array_filter(explode("\n", $output))); |
|
34
|
|
|
$members = array(); |
|
35
|
|
|
|
|
36
|
|
|
// BSDTar outputs two differents format of date according to the mtime |
|
37
|
|
|
// of the member. If the member is younger than six months the year is not shown. |
|
38
|
|
|
// On 4.5+ FreeBSD system the day is displayed first |
|
39
|
|
|
$dateFormats = array('M d Y', 'M d H:i', 'd M Y', 'd M H:i'); |
|
40
|
|
|
|
|
41
|
|
|
foreach ($lines as $line) { |
|
42
|
|
|
$matches = array(); |
|
43
|
|
|
|
|
44
|
|
|
// drw-rw-r-- 0 toto titi 0 Jan 3 1980 practice/ |
|
45
|
|
|
// -rw-rw-r-- 0 toto titi 10240 Jan 22 13:31 practice/records |
|
46
|
|
|
if (!preg_match_all("#" . |
|
47
|
|
|
self::PERMISSIONS . "\s+" . // match (drw-r--r--) |
|
48
|
|
|
self::HARD_LINK . "\s+" . // match (1) |
|
49
|
|
|
self::OWNER . "\s" . // match (toto) |
|
50
|
|
|
self::GROUP . "\s+" . // match (titi) |
|
51
|
|
|
self::FILESIZE . "\s+" . // match (0) |
|
52
|
|
|
self::DATE . "\s+" . // match (Jan 3 1980) |
|
53
|
|
|
self::FILENAME . // match (practice) |
|
54
|
|
|
"#", $line, $matches, PREG_SET_ORDER |
|
55
|
|
|
)) { |
|
56
|
|
|
continue; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$chunks = array_shift($matches); |
|
60
|
|
|
|
|
61
|
|
|
if (8 !== count($chunks)) { |
|
62
|
|
|
continue; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$date = null; |
|
66
|
|
|
|
|
67
|
|
|
foreach ($dateFormats as $format) { |
|
68
|
|
|
$date = \DateTime::createFromFormat($format, $chunks[6]); |
|
69
|
|
|
|
|
70
|
|
|
if (false === $date) { |
|
71
|
|
|
continue; |
|
72
|
|
|
} else { |
|
73
|
|
|
break; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
if (false === $date) { |
|
78
|
|
|
throw new RuntimeException(sprintf('Failed to parse mtime date from %s', $line)); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$members[] = array( |
|
82
|
|
|
'location' => $chunks[7], |
|
83
|
|
|
'size' => $chunks[5], |
|
84
|
|
|
'mtime' => $date, |
|
85
|
|
|
'is_dir' => 'd' === $chunks[1][0] |
|
86
|
|
|
); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return $members; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @inheritdoc |
|
94
|
|
|
*/ |
|
95
|
|
|
public function parseInflatorVersion($output) |
|
96
|
|
|
{ |
|
97
|
|
|
$chunks = explode(' ', $output, 3); |
|
98
|
|
|
|
|
99
|
|
|
if (2 > count($chunks)) { |
|
100
|
|
|
return null; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
list(, $version) = explode(' ', $output, 3); |
|
104
|
|
|
|
|
105
|
|
|
return $version; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @inheritdoc |
|
110
|
|
|
*/ |
|
111
|
|
|
public function parseDeflatorVersion($output) |
|
112
|
|
|
{ |
|
113
|
|
|
return $this->parseInflatorVersion($output); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|